Create Drush aliases for the provided project and environments.
public createAliases ( Platformsh\Client\Model\Project $project, string $projectRoot, Platformsh\Client\Model\Environment[] $environments, string $original = null, boolean $merge = true ) : boolean | ||
$project | Platformsh\Client\Model\Project | The project |
$projectRoot | string | The project root |
$environments | Platformsh\Client\Model\Environment[] | The environments |
$original | string | The original group name |
$merge | boolean | Whether to merge existing alias settings |
return | boolean | Whether any aliases have been created. |
protected function execute(InputInterface $input, OutputInterface $output) { $projectRoot = $this->getProjectRoot(); if (!$projectRoot) { throw new RootNotFoundException(); } $projectConfig = LocalProject::getProjectConfig($projectRoot); $current_group = isset($projectConfig['alias-group']) ? $projectConfig['alias-group'] : $projectConfig['id']; if ($input->getOption('pipe')) { $output->writeln($current_group); return 0; } $project = $this->getCurrentProject(); $new_group = ltrim($input->getOption('group'), '@'); $homeDir = $this->getHomeDir(); $drushHelper = new DrushHelper($output); $drushHelper->ensureInstalled(); $drushHelper->setHomeDir($homeDir); $aliases = $drushHelper->getAliases($current_group); if ($new_group && $new_group != $current_group || !$aliases || $input->getOption('recreate')) { $new_group = $new_group ?: $current_group; $this->stdErr->writeln("Creating Drush aliases in the group <info>@{$new_group}</info>"); $questionHelper = $this->getHelper('question'); if ($new_group != $current_group) { $existing = $drushHelper->getAliases($new_group); if ($existing && $new_group != $current_group) { $question = "The Drush alias group <info>@{$new_group}</info> already exists. Overwrite?"; if (!$questionHelper->confirm($question, $input, $output, false)) { return 1; } } LocalProject::writeCurrentProjectConfig('alias-group', $new_group, $projectRoot); } $environments = $this->getEnvironments($project, true, false); $drushHelper->createAliases($project, $projectRoot, $environments, $current_group); if ($new_group != $current_group) { $drushDir = $homeDir . '/.drush'; $oldFile = $drushDir . '/' . $current_group . '.aliases.drushrc.php'; if (file_exists($oldFile)) { if ($questionHelper->confirm("Delete old Drush alias group <info>@{$current_group}</info>?", $input, $this->stdErr)) { unlink($oldFile); } } } // Clear the Drush cache now that the aliases have been updated. $drushHelper->clearCache(); // Read the new aliases. $aliases = $drushHelper->getAliases($new_group); } if ($aliases) { $this->stdErr->writeln("Drush aliases for <info>{$project->title}</info> ({$project->id}):"); foreach (explode("\n", $aliases) as $alias) { $output->writeln(' @' . $alias); } } return 0; }
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); }
public function testCreateAliases() { // Set up a dummy project with a remote environment. $project = new Project(array('id' => 'test', 'title' => 'Test project title')); $environments = array(); $environments[] = new Environment(array('id' => 'master', '_links' => array('public-url' => array('href' => 'http://example.com'), 'ssh' => array('href' => 'ssh://user@example.com')))); // Set up file structure. $testDir = tempnam($this->root, ''); unlink($testDir); mkdir($testDir); $projectRoot = "{$testDir}/project"; $homeDir = "{$testDir}/home"; mkdir($projectRoot); mkdir($homeDir); // Check that aliases are created. $this->drushHelper->setHomeDir($homeDir); $this->drushHelper->createAliases($project, $projectRoot, $environments); $this->assertFileExists("{$homeDir}/.drush/test.aliases.drushrc.php"); // Check that aliases exist for the 'master' and local environments. $aliases = array(); include_once "{$homeDir}/.drush/test.aliases.drushrc.php"; $this->assertArrayHasKey('master', $aliases); $this->assertArrayHasKey('_local', $aliases); }
protected function execute(InputInterface $input, OutputInterface $output) { $project = $this->getCurrentProject(); if (!$project) { throw new \Exception('This can only be run from inside a project directory'); } $projectRoot = $this->getProjectRoot(); $projectConfig = LocalProject::getProjectConfig($projectRoot); $current_group = isset($projectConfig['alias-group']) ? $projectConfig['alias-group'] : $project['id']; if ($input->getOption('pipe') || !$this->isTerminal($output)) { $output->writeln($current_group); return 0; } $new_group = ltrim($input->getOption('group'), '@'); $homeDir = $this->getHelper('fs')->getHomeDirectory(); $drushHelper = new DrushHelper($output); $drushHelper->ensureInstalled(); $drushHelper->setHomeDir($homeDir); if ($new_group && $new_group != $current_group) { $questionHelper = $this->getHelper('question'); $existing = $drushHelper->getAliases($new_group); if ($existing) { if (!$questionHelper->confirm("The alias group <info>@{$new_group}</info> already exists. Overwrite?", $input, $output, false)) { return 1; } } $project['alias-group'] = $new_group; LocalProject::writeCurrentProjectConfig('alias-group', $new_group, $projectRoot); $output->write("Creating Drush aliases in the group <info>@{$new_group}</info>..."); $environments = $this->getEnvironments($project, true, false); $drushHelper->createAliases($project, $projectRoot, $environments); $output->writeln(" done"); $drushDir = $homeDir . '/.drush'; $oldFile = $drushDir . '/' . $current_group . '.aliases.drushrc.php'; if (file_exists($oldFile)) { if ($questionHelper->confirm("Delete old alias group <info>@{$current_group}</info>?", $input, $output)) { unlink($oldFile); } } // Clear the Drush cache now that the aliases have been updated. $drushHelper->clearCache(); $current_group = $new_group; } elseif ($input->getOption('recreate')) { $output->write("Recreating Drush aliases..."); $environments = $this->getEnvironments($project, true, false); $drushHelper->createAliases($project, $projectRoot, $environments); $drushHelper->clearCache(); $output->writeln(' done'); } // Don't run expensive drush calls if they are not needed. if ($input->getOption('quiet')) { return 0; } $aliases = $drushHelper->getAliases($current_group); if ($aliases) { $output->writeln("Aliases for <info>{$project['name']}</info> ({$project['id']}):"); foreach (explode("\n", $aliases) as $alias) { $output->writeln(' @' . $alias); } } return 0; }