/** * Process the defined special destinations. */ protected function symLinkSpecialDestinations() { foreach ($this->specialDestinations as $sourcePattern => $relDestination) { $matched = glob($this->appRoot . '/' . $sourcePattern, GLOB_NOSORT); if (!$matched) { continue; } // On Platform these replacements would be a bit different. $absDestination = str_replace(array('{webroot}', '{approot}'), $this->buildDir, $relDestination); foreach ($matched as $source) { // Ignore the source if it's in ignoredFiles. $relSource = str_replace($this->appRoot . '/', '', $source); if (in_array($relSource, $this->ignoredFiles)) { continue; } $this->output->writeln("Symlinking {$relSource} to {$relDestination}"); $destination = $absDestination; // Do not overwrite directories with files. if (!is_dir($source) && is_dir($destination)) { $destination = $destination . '/' . basename($source); } // Delete existing files, emitting a warning. if (file_exists($destination)) { $this->output->writeln(sprintf("Overriding existing path '%s' in destination", str_replace($this->buildDir . '/', '', $destination))); $this->fsHelper->remove($destination); } $this->fsHelper->symlink($source, $destination); } } }
/** * Test FilesystemHelper::symlinkAll(). */ public function testSymlinkAll() { $testSource = $this->tempDir(true); $testDestination = $this->tempDir(); // Test plain symlinking. $this->filesystemHelper->symlinkAll($testSource, $testDestination); $this->assertFileExists($testDestination . '/test-file'); $this->assertFileExists($testDestination . '/test-dir/test-file'); $this->assertFileExists($testDestination . '/test-nesting/1/2/3/test-file'); // Test with skipping an existing file. $testDestination = $this->tempDir(); touch($testDestination . '/test-file'); $this->filesystemHelper->symlinkAll($testSource, $testDestination); $this->assertFileExists($testDestination . '/test-file'); $this->assertFileExists($testDestination . '/test-dir/test-file'); $this->assertFileExists($testDestination . '/test-nesting/1/2/3/test-file'); // Test with relative links. This has no effect on Windows. $testDestination = $this->tempDir(); $this->filesystemHelper->setRelativeLinks(true); $this->filesystemHelper->symlinkAll($testSource, $testDestination); $this->filesystemHelper->setRelativeLinks(false); $this->assertFileExists($testDestination . '/test-file'); $this->assertFileExists($testDestination . '/test-dir/test-file'); $this->assertFileExists($testDestination . '/test-nesting/1/2/3/test-file'); // Test with a blacklist. $testDestination = $this->tempDir(); touch($testSource . '/test-file2'); $this->filesystemHelper->symlinkAll($testSource, $testDestination, true, false, ['test-file']); $this->assertFileNotExists($testDestination . '/test-file'); $this->assertFileExists($testDestination . '/test-dir/test-file'); $this->assertFileExists($testDestination . '/test-nesting/1/2/3/test-file'); }
public function testDoNotSymlinkBuildsIntoSitesDefault() { $tempDir = self::$root->getName(); $repository = tempnam($tempDir, ''); unlink($repository); mkdir($repository); $fsHelper = new FilesystemHelper(); $sourceDir = 'tests/data/apps/drupal/project'; $fsHelper->copyAll($sourceDir, $repository); $wwwDir = $repository . '/www'; // Run these tests twice to check that a previous build does not affect // the next one. for ($i = 1; $i <= 2; $i++) { $this->assertTrue($this->builder->build($repository, $wwwDir)); $this->assertFileExists($wwwDir . '/sites/default/settings.php'); $this->assertFileNotExists($wwwDir . '/sites/default/builds'); $this->assertFileNotExists($wwwDir . '/sites/default/www'); } }
/** * @param string $sourceDir * * @return string */ protected function createDummyProject($sourceDir) { if (!is_dir($sourceDir)) { throw new \InvalidArgumentException("Not a directory: {$sourceDir}"); } $projectRoot = $this->createTempSubDir('project'); // Set up the project. $fsHelper = new FilesystemHelper(); $fsHelper->copyAll($sourceDir, $projectRoot); // @todo perhaps make some of these steps unnecessary $local = new LocalProject(); $cwd = getcwd(); chdir($projectRoot); exec('git init'); chdir($cwd); $local->ensureGitRemote($projectRoot, 'testProjectId'); $local->writeCurrentProjectConfig(['id' => 'testProjectId'], $projectRoot); return $projectRoot; }
/** * @param string $sourceDir * * @return string */ protected function createDummyProject($sourceDir) { if (!is_dir($sourceDir)) { throw new \InvalidArgumentException("Not a directory: {$sourceDir}"); } $tempDir = self::$root->getName(); $projectRoot = tempnam($tempDir, ''); unlink($projectRoot); mkdir($projectRoot); // Set up the project files. $local = new LocalProject(); $local->createProjectFiles($projectRoot, 'testProjectId'); // Make a dummy repository. $repositoryDir = $projectRoot . '/' . LocalProject::REPOSITORY_DIR; mkdir($repositoryDir); $fsHelper = new FilesystemHelper(); $fsHelper->copyAll($sourceDir, $repositoryDir); return $projectRoot; }
/** * Test with a custom source and destination. */ public function testBuildCustomSourceDestination() { // Copy the 'vanilla' app to a temporary directory. $sourceDir = $this->createTempSubDir(); $fsHelper = new FilesystemHelper(); $fsHelper->copyAll('tests/data/apps/vanilla', $sourceDir); // Create another temporary directory. $destination = $this->createTempSubDir(); // Test with symlinking. $builder = new LocalBuild(['abslinks' => true], null, self::$output); $builder->build($sourceDir, $destination); $this->assertFileExists($destination . '/index.html'); // Test with copying. $builder = new LocalBuild(['copy' => true, 'abslinks' => true], null, self::$output); $builder->build($sourceDir, $destination); $this->assertFileExists($destination . '/index.html'); // Remove the temporary files. exec('rm -R ' . escapeshellarg($destination) . ' ' . escapeshellarg($sourceDir)); }
public function testCreateAliasesMultiDrupal() { // Set up file structure. $testDir = $this->createTempSubDir(); $fsHelper = new FilesystemHelper(); $fsHelper->copyAll(__DIR__ . '/../data/repositories/multi-drupal', $testDir . '/project/repository'); $projectRoot = $testDir . '/project'; $homeDir = "{$testDir}/home"; mkdir($homeDir); // Check that aliases are created. $this->drushHelper->setHomeDir($homeDir); $this->drushHelper->createAliases($this->project, $projectRoot, $this->environments); $this->assertFileExists("{$homeDir}/.drush/test.aliases.drushrc.php"); // Check that aliases exist for the 'master' and local environments. $aliases = []; include_once "{$homeDir}/.drush/test.aliases.drushrc.php"; $this->assertArrayHasKey('master--drupal1', $aliases); $this->assertArrayHasKey('_local--drupal1', $aliases); $this->assertArrayHasKey('master--drupal2', $aliases); $this->assertArrayHasKey('_local--drupal2', $aliases); }
/** * Create a settings.local.php for a Drupal site. * * This helps with database setup, etc. */ protected function installDrupalSettingsLocal() { $sitesDefault = $this->getWebRoot() . '/sites/default'; $shared = $this->getSharedDir(); $settingsLocal = $sitesDefault . '/settings.local.php'; if ($shared !== false && is_dir($sitesDefault) && !file_exists($settingsLocal)) { $sharedSettingsLocal = $shared . '/settings.local.php'; $relative = $this->config->get('local.shared_dir') . '/settings.local.php'; if (!file_exists($sharedSettingsLocal)) { $this->output->writeln("Creating file: <info>{$relative}</info>"); $this->fsHelper->copy(CLI_ROOT . '/resources/drupal/settings.local.php.dist', $sharedSettingsLocal); $this->output->writeln('Edit this file to add your database credentials and other Drupal configuration.'); } else { $this->output->writeln("Symlinking <info>{$relative}</info> into sites/default"); } $this->fsHelper->symlink($sharedSettingsLocal, $settingsLocal); } }
/** * Remove old files from a directory. * * @param string $directory * @param int $maxAge * @param int $keepMax * @param array $blacklist * @param bool $quiet * * @return int[] */ protected function cleanDirectory($directory, $maxAge = null, $keepMax = 5, array $blacklist = [], $quiet = true) { if (!is_dir($directory)) { return [0, 0]; } $files = glob($directory . '/*'); if (!$files) { return [0, 0]; } // Sort files by modified time (descending). usort($files, function ($a, $b) { return filemtime($a) < filemtime($b); }); $now = time(); $numDeleted = 0; $numKept = 0; foreach ($files as $filename) { if (in_array($filename, $blacklist)) { $numKept++; continue; } if ($keepMax !== null && $numKept >= $keepMax || $maxAge !== null && $now - filemtime($filename) > $maxAge) { if (!$quiet) { $this->output->writeln("Deleting: " . basename($filename)); } if ($this->fsHelper->remove($filename)) { $numDeleted++; } elseif (!$quiet) { $this->output->writeln("Failed to delete: <error>" . basename($filename) . "</error>"); } } else { $numKept++; } } return [$numDeleted, $numKept]; }
/** * @return string */ protected function getHomeDir() { if (!isset(self::$homeDir)) { $fs = new FilesystemHelper(); self::$homeDir = $fs->getHomeDirectory(); } return self::$homeDir; }
/** * @return string */ protected function getCacheDir() { $sessionId = 'cli-' . preg_replace('/[\\W]+/', '-', self::$sessionId); $fs = new FilesystemHelper(); return $fs->getHomeDirectory() . '/.platformsh/.session/sess-' . $sessionId; }
/** * @return string */ protected function getHomeDir() { if (!isset(self::$homeDir)) { self::$homeDir = FilesystemHelper::getHomeDirectory(); } return self::$homeDir; }
/** * @return string */ public function getUserConfigDir() { return FilesystemHelper::getHomeDirectory() . '/' . $this->get('application.user_config_dir'); }