Inheritance: extends ToolstackBase
 public function isEnabled()
 {
     $projectRoot = $this->getProjectRoot();
     if ($projectRoot) {
         return Drupal::isDrupal($projectRoot . '/' . LocalProject::REPOSITORY_DIR);
     }
     return true;
 }
Esempio n. 2
0
 public function install()
 {
     parent::install();
     $this->copyGitIgnore('gitignore-composer');
     if (Drupal::isDrupal($this->getWebRoot())) {
         $this->installDrupalSettingsLocal();
     }
 }
 protected function configure()
 {
     $this->setName('project:build')->setAliases(array('build'))->addArgument('app', InputArgument::IS_ARRAY, 'Specify application(s) to build')->setDescription('Build the current project locally')->addOption('abslinks', 'a', InputOption::VALUE_NONE, 'Use absolute links')->addOption('no-clean', null, InputOption::VALUE_NONE, 'Do not remove old builds')->addOption('no-archive', null, InputOption::VALUE_NONE, 'Do not create or use a build archive')->addOption('no-cache', null, InputOption::VALUE_NONE, 'Disable caching.');
     $projectRoot = $this->getProjectRoot();
     if (!$projectRoot || Drupal::isDrupal($projectRoot . '/' . LocalProject::REPOSITORY_DIR)) {
         $this->addOption('working-copy', null, InputOption::VALUE_NONE, 'Drush: use git to clone a repository of each Drupal module rather than simply downloading a version.')->addOption('concurrency', null, InputOption::VALUE_OPTIONAL, 'Drush: set the number of concurrent projects that will be processed at the same time.', 8);
     }
 }
 public function isHiddenInList()
 {
     // Hide this command in the list if the project is not Drupal.
     $projectRoot = $this->getProjectRoot();
     if ($projectRoot && !Drupal::isDrupal($projectRoot)) {
         return true;
     }
     return parent::isHiddenInList();
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->validateInput($input);
     $drushCommand = $input->getArgument('cmd');
     $sshOptions = '';
     // Pass through options that the CLI shares with Drush and SSH.
     foreach (['yes', 'no', 'quiet'] as $option) {
         if ($input->getOption($option)) {
             $drushCommand .= " --{$option}";
         }
     }
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
         $drushCommand .= " --debug";
         $sshOptions .= ' -vv';
     } elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
         $drushCommand .= " --verbose";
         $sshOptions .= ' -v';
     } elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         $drushCommand .= " --verbose";
     } elseif ($output->getVerbosity() == OutputInterface::VERBOSITY_QUIET) {
         $drushCommand .= " --quiet";
         $sshOptions .= ' -q';
     }
     $appName = $this->selectApp($input, function (LocalApplication $app) {
         return Drupal::isDrupal($app->getRoot());
     });
     $selectedEnvironment = $this->getSelectedEnvironment();
     $sshUrl = $selectedEnvironment->getSshUrl($appName);
     // Get the LocalApplication object for the specified application, if
     // available.
     $projectRoot = $this->getProjectRoot();
     if ($projectRoot && $this->selectedProjectIsCurrent()) {
         $app = LocalApplication::getApplication($appName, $projectRoot, self::$config);
     }
     // Use the local application configuration (if available) to determine
     // the correct Drupal root.
     if (isset($app)) {
         $drupalRoot = '/app/' . $app->getDocumentRoot();
     } else {
         // Fall back to the PLATFORM_DOCUMENT_ROOT environment variable,
         // which is usually correct, except where the document_root was
         // specified as '/'.
         $documentRootEnvVar = self::$config->get('service.env_prefix') . 'DOCUMENT_ROOT';
         $drupalRoot = '${' . $documentRootEnvVar . ':-/app/public}';
         $this->debug('<comment>Warning:</comment> using $' . $documentRootEnvVar . ' for the Drupal root. This fails in cases where the document_root is /.');
     }
     $dimensions = $this->getApplication()->getTerminalDimensions();
     $columns = $dimensions[0] ?: 80;
     $sshDrushCommand = "COLUMNS={$columns} drush --root=\"{$drupalRoot}\"";
     if ($environmentUrl = $selectedEnvironment->getLink('public-url')) {
         $sshDrushCommand .= " --uri=" . escapeshellarg($environmentUrl);
     }
     $sshDrushCommand .= ' ' . $drushCommand . ' 2>&1';
     $command = 'ssh' . $sshOptions . ' ' . escapeshellarg($sshUrl) . ' ' . escapeshellarg($sshDrushCommand);
     return $this->getHelper('shell')->executeSimple($command);
 }
 public function hideInList()
 {
     // Hide this command in the list if the project is not Drupal.
     $projectRoot = $this->getProjectRoot();
     if ($projectRoot) {
         if (!Drupal::isDrupal($projectRoot . '/' . LocalProject::REPOSITORY_DIR)) {
             return true;
         }
     }
     return parent::hideInList();
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $projectId = $input->getArgument('id');
     $environmentOption = $input->getOption('environment');
     $hostOption = $input->getOption('host');
     if (empty($projectId)) {
         if ($input->isInteractive() && ($projects = $this->getProjects(true))) {
             $projectId = $this->offerProjectChoice($projects, $input);
         } else {
             $this->stdErr->writeln("<error>You must specify a project.</error>");
             return 1;
         }
     } else {
         $result = $this->parseProjectId($projectId);
         $projectId = $result['projectId'];
         $hostOption = $hostOption ?: $result['host'];
         $environmentOption = $environmentOption ?: $result['environmentId'];
     }
     $project = $this->getProject($projectId, $hostOption, true);
     if (!$project) {
         $this->stdErr->writeln("<error>Project not found: {$projectId}</error>");
         return 1;
     }
     $environments = $this->getEnvironments($project);
     if ($environmentOption) {
         if (!isset($environments[$environmentOption])) {
             // Reload the environments list.
             $environments = $this->getEnvironments($project, true);
             if (!isset($environments[$environmentOption])) {
                 $this->stdErr->writeln("Environment not found: <error>{$environmentOption}</error>");
             }
             return 1;
         }
         $environment = $environmentOption;
     } elseif (count($environments) === 1) {
         $environment = key($environments);
     } else {
         $environment = 'master';
     }
     /** @var \Platformsh\Cli\Helper\PlatformQuestionHelper $questionHelper */
     $questionHelper = $this->getHelper('question');
     $directory = $input->getArgument('directory');
     if (empty($directory)) {
         $slugify = new Slugify();
         $directory = $project->title ? $slugify->slugify($project->title) : $project->id;
         $directory = $questionHelper->askInput('Directory', $input, $this->stdErr, $directory);
     }
     if ($projectRoot = $this->getProjectRoot()) {
         if (strpos(realpath(dirname($directory)), $projectRoot) === 0) {
             $this->stdErr->writeln("<error>A project cannot be cloned inside another project.</error>");
             return 1;
         }
     }
     /** @var \Platformsh\Cli\Helper\FilesystemHelper $fsHelper */
     $fsHelper = $this->getHelper('fs');
     // Create the directory structure.
     $existed = false;
     if (file_exists($directory)) {
         $existed = true;
         $this->stdErr->writeln("The directory <error>{$directory}</error> already exists");
         if (file_exists($directory . '/' . LocalProject::PROJECT_CONFIG) && $questionHelper->confirm("Overwrite?", $input, $this->stdErr, false)) {
             $fsHelper->remove($directory);
         } else {
             return 1;
         }
     }
     mkdir($directory);
     $projectRoot = realpath($directory);
     if (!$projectRoot) {
         throw new \Exception("Failed to create project directory: {$directory}");
     }
     if ($existed) {
         $this->stdErr->writeln("Re-created project directory: {$directory}");
     } else {
         $this->stdErr->writeln("Created new project directory: {$directory}");
     }
     $cleanUp = function () use($projectRoot, $fsHelper) {
         $fsHelper->remove($projectRoot);
     };
     $local = new LocalProject();
     $hostname = parse_url($project->getUri(), PHP_URL_HOST) ?: null;
     $local->createProjectFiles($projectRoot, $project->id, $hostname);
     // Prepare to talk to the Platform.sh repository.
     $gitUrl = $project->getGitUrl();
     $repositoryDir = $projectRoot . '/' . LocalProject::REPOSITORY_DIR;
     $gitHelper = new GitHelper(new ShellHelper($this->stdErr));
     $gitHelper->ensureInstalled();
     // First check if the repo actually exists.
     $repoHead = $gitHelper->execute(['ls-remote', $gitUrl, 'HEAD'], false);
     if ($repoHead === false) {
         // The ls-remote command failed.
         $cleanUp();
         $this->stdErr->writeln('<error>Failed to connect to the Platform.sh Git server</error>');
         // Suggest SSH key commands.
         $sshKeys = [];
         try {
             $sshKeys = $this->getClient(false)->getSshKeys();
         } catch (\Exception $e) {
             // Ignore exceptions.
         }
         if (!empty($sshKeys)) {
             $this->stdErr->writeln('');
             $this->stdErr->writeln('Please check your SSH credentials');
             $this->stdErr->writeln('You can list your keys with: <comment>platform ssh-keys</comment>');
         } else {
             $this->stdErr->writeln('You probably need to add an SSH key, with: <comment>platform ssh-key:add</comment>');
         }
         return 1;
     } elseif (is_bool($repoHead)) {
         // The repository doesn't have a HEAD, which means it is empty.
         // We need to create the folder, run git init, and attach the remote.
         mkdir($repositoryDir);
         // Initialize the repo and attach our remotes.
         $this->stdErr->writeln("Initializing empty project repository");
         $gitHelper->execute(['init'], $repositoryDir, true);
         $this->stdErr->writeln("Adding Platform.sh Git remote");
         $local->ensureGitRemote($repositoryDir, $gitUrl);
         $this->stdErr->writeln("Your repository has been initialized and connected to <info>Platform.sh</info>!");
         $this->stdErr->writeln("Commit and push to the <info>{$environment}</info> branch and Platform.sh will build your project automatically");
         return 0;
     }
     // We have a repo! Yay. Clone it.
     $cloneArgs = ['--branch', $environment, '--origin', 'platform'];
     $cloned = $gitHelper->cloneRepo($gitUrl, $repositoryDir, $cloneArgs);
     if (!$cloned) {
         // The clone wasn't successful. Clean up the folders we created
         // and then bow out with a message.
         $cleanUp();
         $this->stdErr->writeln('<error>Failed to clone Git repository</error>');
         $this->stdErr->writeln('Please check your SSH credentials or contact Platform.sh support');
         return 1;
     }
     $gitHelper->updateSubmodules(true, $repositoryDir);
     $local->ensureGitRemote($repositoryDir, $gitUrl);
     $this->setProjectRoot($projectRoot);
     $this->stdErr->writeln("\nThe project <info>{$project->title}</info> was successfully downloaded to: <info>{$directory}</info>");
     // Return early if there is no code in the repository.
     if (!glob($repositoryDir . '/*')) {
         return 0;
     }
     // Ensure that Drush aliases are created.
     if (Drupal::isDrupal($projectRoot . '/' . LocalProject::REPOSITORY_DIR)) {
         $this->stdErr->writeln('');
         $this->runOtherCommand('local:drush-aliases', ['--group' => basename($directory)]);
     }
     // Launch the first build.
     $success = true;
     if ($input->getOption('build')) {
         // Launch the first build.
         $this->stdErr->writeln('');
         $this->stdErr->writeln('Building the project locally for the first time. Run <info>platform build</info> to repeat this.');
         $options = ['environmentId' => $environment, 'noClean' => true];
         $builder = new LocalBuild($options, $output);
         $success = $builder->buildProject($projectRoot);
     } else {
         $this->stdErr->writeln("\nYou can build the project with: " . "\n    cd {$directory}" . "\n    platform build");
     }
     return $success ? 0 : 1;
 }
Esempio n. 8
0
 /**
  * @param Project       $project
  * @param Environment[] $environments
  */
 protected function updateDrushAliases(Project $project, array $environments)
 {
     $projectRoot = $this->getProjectRoot();
     if (!$projectRoot) {
         return;
     }
     // Double-check that the passed project is the current one.
     $currentProject = $this->getCurrentProject();
     if (!$currentProject || $currentProject->id != $project->id) {
         return;
     }
     // Ignore the project if it doesn't contain a Drupal application.
     if (!Drupal::isDrupal($projectRoot . '/' . LocalProject::REPOSITORY_DIR)) {
         return;
     }
     $this->debug('Updating Drush aliases');
     /** @var \Platformsh\Cli\Helper\DrushHelper $drushHelper */
     $drushHelper = $this->getHelper('drush');
     $drushHelper->setHomeDir($this->getHomeDir());
     $drushHelper->createAliases($project, $projectRoot, $environments);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $projectId = $input->getArgument('id');
     $environmentOption = $input->getOption('environment');
     $hostOption = $input->getOption('host');
     if (empty($projectId)) {
         if ($input->isInteractive() && ($projects = $this->api()->getProjects(true))) {
             $projectId = $this->offerProjectChoice($projects, $input);
         } else {
             $this->stdErr->writeln("<error>You must specify a project.</error>");
             return 1;
         }
     } else {
         $result = $this->parseProjectId($projectId);
         $projectId = $result['projectId'];
         $hostOption = $hostOption ?: $result['host'];
         $environmentOption = $environmentOption ?: $result['environmentId'];
     }
     $project = $this->api()->getProject($projectId, $hostOption, true);
     if (!$project) {
         $this->stdErr->writeln("<error>Project not found: {$projectId}</error>");
         return 1;
     }
     $environments = $this->api()->getEnvironments($project);
     if ($environmentOption) {
         if (!isset($environments[$environmentOption])) {
             // Reload the environments list.
             $environments = $this->api()->getEnvironments($project, true);
             if (!isset($environments[$environmentOption])) {
                 $this->stdErr->writeln("Environment not found: <error>{$environmentOption}</error>");
             }
             return 1;
         }
         $environmentId = $environmentOption;
     } elseif (count($environments) === 1) {
         $environmentId = key($environments);
     } else {
         $environmentId = 'master';
     }
     $directory = $input->getArgument('directory');
     if (empty($directory)) {
         $slugify = new Slugify();
         $directory = $project->title ? $slugify->slugify($project->title) : $project->id;
         /** @var \Platformsh\Cli\Helper\QuestionHelper $questionHelper */
         $questionHelper = $this->getHelper('question');
         $directory = $questionHelper->askInput('Directory', $directory);
     }
     if ($projectRoot = $this->getProjectRoot()) {
         if (strpos(realpath(dirname($directory)), $projectRoot) === 0) {
             $this->stdErr->writeln("<error>A project cannot be cloned inside another project.</error>");
             return 1;
         }
     }
     // Create the directory structure.
     if (file_exists($directory)) {
         $this->stdErr->writeln("The directory <error>{$directory}</error> already exists");
         return 1;
     }
     if (!($parent = realpath(dirname($directory)))) {
         throw new \Exception("Not a directory: " . dirname($directory));
     }
     $projectRoot = $parent . '/' . basename($directory);
     // Prepare to talk to the remote repository.
     $gitUrl = $project->getGitUrl();
     $gitHelper = new GitHelper(new ShellHelper($this->stdErr));
     $gitHelper->ensureInstalled();
     // First check if the repo actually exists.
     try {
         $exists = $gitHelper->remoteRepoExists($gitUrl);
     } catch (\Exception $e) {
         // The ls-remote command failed.
         $this->stdErr->writeln('<error>Failed to connect to the ' . self::$config->get('service.name') . ' Git server</error>');
         // Suggest SSH key commands.
         $sshKeys = [];
         try {
             $sshKeys = $this->api()->getClient(false)->getSshKeys();
         } catch (\Exception $e) {
             // Ignore exceptions.
         }
         if (!empty($sshKeys)) {
             $this->stdErr->writeln('');
             $this->stdErr->writeln('Please check your SSH credentials');
             $this->stdErr->writeln('You can list your keys with: <comment>' . self::$config->get('application.executable') . ' ssh-keys</comment>');
         } else {
             $this->stdErr->writeln('You probably need to add an SSH key, with: <comment>' . self::$config->get('application.executable') . ' ssh-key:add</comment>');
         }
         return 1;
     }
     $projectConfig = ['id' => $projectId];
     $host = parse_url($project->getUri(), PHP_URL_HOST);
     if ($host) {
         $projectConfig['host'] = $host;
     }
     // If the remote repository exists, then locally we need to create the
     // folder, run git init, and attach the remote.
     if (!$exists) {
         $this->stdErr->writeln('Creating project directory: <info>' . $directory . '</info>');
         if (mkdir($projectRoot) === false) {
             $this->stdErr->writeln('Failed to create the project directory.');
             return 1;
         }
         // Initialize the repo and attach our remotes.
         $this->debug('Initializing the repository');
         $gitHelper->init($projectRoot, true);
         // As soon as there is a Git repo present, add the project config file.
         $this->localProject->writeCurrentProjectConfig($projectConfig, $projectRoot);
         $this->debug('Adding Git remote(s)');
         $this->localProject->ensureGitRemote($projectRoot, $gitUrl);
         $this->stdErr->writeln('');
         $this->stdErr->writeln('Your project has been initialized and connected to <info>' . self::$config->get('service.name') . '</info>!');
         $this->stdErr->writeln('');
         $this->stdErr->writeln('Commit and push to the <info>master</info> branch of the <info>' . self::$config->get('detection.git_remote_name') . '</info> Git remote, and ' . self::$config->get('service.name') . ' will build your project automatically.');
         return 0;
     }
     // We have a repo! Yay. Clone it.
     $projectLabel = $this->api()->getProjectLabel($project);
     $this->stdErr->writeln('Downloading project ' . $projectLabel);
     $cloneArgs = ['--branch', $environmentId, '--origin', self::$config->get('detection.git_remote_name')];
     if ($output->isDecorated()) {
         $cloneArgs[] = '--progress';
     }
     $cloned = $gitHelper->cloneRepo($gitUrl, $projectRoot, $cloneArgs);
     if ($cloned === false) {
         // The clone wasn't successful. Clean up the folders we created
         // and then bow out with a message.
         $this->stdErr->writeln('<error>Failed to clone Git repository</error>');
         $this->stdErr->writeln('Please check your SSH credentials or contact ' . self::$config->get('service.name') . ' support');
         return 1;
     }
     $this->setProjectRoot($projectRoot);
     $this->localProject->writeCurrentProjectConfig($projectConfig, $projectRoot);
     $this->localProject->ensureGitRemote($projectRoot, $gitUrl);
     $gitHelper->updateSubmodules(true, $projectRoot);
     $this->stdErr->writeln("\nThe project <info>{$projectLabel}</info> was successfully downloaded to: <info>{$directory}</info>");
     // Return early if there is no code in the repository.
     if (!glob($projectRoot . '/*', GLOB_NOSORT)) {
         return 0;
     }
     // Ensure that Drush aliases are created.
     if (Drupal::isDrupal($projectRoot)) {
         $this->stdErr->writeln('');
         $this->runOtherCommand('local:drush-aliases', ['--group' => basename($directory)]);
     }
     // Launch the first build.
     $success = true;
     if ($input->getOption('build')) {
         // Launch the first build.
         $this->stdErr->writeln('');
         $this->stdErr->writeln('Building the project locally for the first time. Run <info>' . self::$config->get('application.executable') . ' build</info> to repeat this.');
         $options = ['no-clean' => true];
         $builder = new LocalBuild($options, self::$config, $output);
         $success = $builder->build($projectRoot);
     } else {
         $this->stdErr->writeln("\nYou can build the project with: " . "\n    cd {$directory}" . "\n    " . self::$config->get('application.executable') . " build");
     }
     return $success ? 0 : 1;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->validateInput($input);
     if ($input instanceof ArgvInput) {
         $helper = new ArgvHelper();
         $drushCommand = $helper->getPassedCommand($this, $input);
     }
     if (empty($drushCommand)) {
         $drushCommand = $input->getArgument('cmd');
     }
     $sshOptions = '';
     // Pass through options that the CLI shares with Drush and SSH.
     foreach (['yes', 'no', 'quiet'] as $option) {
         if ($input->getOption($option)) {
             $drushCommand .= " --{$option}";
         }
     }
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
         $drushCommand .= " --debug";
         $sshOptions .= ' -vv';
     } elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
         $drushCommand .= " --verbose";
         $sshOptions .= ' -v';
     } elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         $drushCommand .= " --verbose";
     } elseif ($output->getVerbosity() == OutputInterface::VERBOSITY_QUIET) {
         $drushCommand .= " --quiet";
         $sshOptions .= ' -q';
     }
     $appName = $this->selectApp($input, function (LocalApplication $app) {
         return Drupal::isDrupal($app->getRoot());
     });
     $selectedEnvironment = $this->getSelectedEnvironment();
     $sshUrl = $selectedEnvironment->getSshUrl($appName);
     // Get the LocalApplication object for the specified application, if
     // available.
     $projectRoot = $this->getProjectRoot();
     if ($projectRoot && $this->selectedProjectIsCurrent() && is_dir($projectRoot . '/' . LocalProject::REPOSITORY_DIR)) {
         $apps = LocalApplication::getApplications($projectRoot . '/' . LocalProject::REPOSITORY_DIR);
         if (count($apps) === 1 && $appName === null) {
             $app = reset($apps);
         } else {
             foreach ($apps as $possibleApp) {
                 if ($possibleApp->getName() === $appName) {
                     $app = $possibleApp;
                     break;
                 }
             }
         }
     }
     // Use the local application configuration (if available) to determine
     // the correct Drupal root.
     if (isset($app) && isset($app->getConfig()['web']['document_root'])) {
         $documentRoot = trim($app->getConfig()['web']['document_root'], '/') ?: 'public';
         $drupalRoot = '/app/' . $documentRoot;
     } else {
         // Fall back to the PLATFORM_DOCUMENT_ROOT environment variable,
         // which is usually correct, except where the document_root was
         // specified as '/'.
         $drupalRoot = '${PLATFORM_DOCUMENT_ROOT:-/app/public}';
         $this->debug('<comment>Warning:</comment> using $PLATFORM_DOCUMENT_ROOT for the Drupal root. This fails in cases where the document_root is /.');
     }
     $dimensions = $this->getApplication()->getTerminalDimensions();
     $columns = $dimensions[0] ?: 80;
     $sshDrushCommand = "COLUMNS={$columns} drush --root=\"{$drupalRoot}\"";
     if ($environmentUrl = $selectedEnvironment->getLink('public-url')) {
         $sshDrushCommand .= " --uri=" . escapeshellarg($environmentUrl);
     }
     $sshDrushCommand .= ' ' . $drushCommand . ' 2>&1';
     $command = 'ssh' . $sshOptions . ' ' . escapeshellarg($sshUrl) . ' ' . escapeshellarg($sshDrushCommand);
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         $this->stdErr->writeln("Running command: <info>{$command}</info>");
     }
     passthru($command, $return_var);
     return $return_var;
 }
Esempio n. 11
0
 /**
  * Update the user's local Drush aliases.
  *
  * This is called via the 'environments_changed' event.
  *
  * @see Api::getEnvironments()
  *
  * @param EnvironmentsChangedEvent $event
  */
 public function updateDrushAliases(EnvironmentsChangedEvent $event)
 {
     $projectRoot = $this->getProjectRoot();
     if (!$projectRoot) {
         return;
     }
     // Double-check that the passed project is the current one.
     $currentProject = $this->getCurrentProject();
     if (!$currentProject || $currentProject->id != $event->getProject()->id) {
         return;
     }
     // Ignore the project if it doesn't contain a Drupal application.
     if (!Drupal::isDrupal($projectRoot)) {
         return;
     }
     $this->debug('Updating Drush aliases');
     /** @var \Platformsh\Cli\Helper\DrushHelper $drushHelper */
     $drushHelper = $this->getHelper('drush');
     $drushHelper->setHomeDir($this->getHomeDir());
     $drushHelper->createAliases($event->getProject(), $projectRoot, $event->getEnvironments());
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $projectId = $input->getArgument('id');
     if (empty($projectId)) {
         if ($input->isInteractive() && ($projects = $this->getProjects(true))) {
             $projectId = $this->offerProjectChoice($projects, $input);
         } else {
             $this->stdErr->writeln("<error>You must specify a project.</error>");
             return 1;
         }
     }
     $project = $this->getProject($projectId, $input->getOption('host'));
     if (!$project) {
         $this->stdErr->writeln("<error>Project not found: {$projectId}</error>");
         return 1;
     }
     /** @var \Platformsh\Cli\Helper\PlatformQuestionHelper $questionHelper */
     $questionHelper = $this->getHelper('question');
     $directoryName = $input->getArgument('directory-name');
     if (empty($directoryName)) {
         $slugify = new Slugify();
         $directoryName = $project->title ? $slugify->slugify($project->title) : $project->id;
         $directoryName = $questionHelper->askInput('Directory name', $input, $this->stdErr, $directoryName);
     }
     if ($projectRoot = $this->getProjectRoot()) {
         if (strpos(realpath(dirname($directoryName)), $projectRoot) === 0) {
             $this->stdErr->writeln("<error>A project cannot be cloned inside another project.</error>");
             return 1;
         }
     }
     /** @var \Platformsh\Cli\Helper\FilesystemHelper $fsHelper */
     $fsHelper = $this->getHelper('fs');
     // Create the directory structure.
     $existed = false;
     if (file_exists($directoryName)) {
         $existed = true;
         $this->stdErr->writeln("The directory <error>{$directoryName}</error> already exists");
         if ($questionHelper->confirm("Overwrite?", $input, $this->stdErr, false)) {
             $fsHelper->remove($directoryName);
         } else {
             return 1;
         }
     }
     mkdir($directoryName);
     $projectRoot = realpath($directoryName);
     if (!$projectRoot) {
         throw new \Exception("Failed to create project directory: {$directoryName}");
     }
     if ($existed) {
         $this->stdErr->writeln("Re-created project directory: <info>{$directoryName}</info>");
     } else {
         $this->stdErr->writeln("Created new project directory: <info>{$directoryName}</info>");
     }
     $local = new LocalProject();
     $hostname = parse_url($project->getUri(), PHP_URL_HOST) ?: null;
     $local->createProjectFiles($projectRoot, $project->id, $hostname);
     $environments = $this->getEnvironments($project, true);
     $environmentOption = $input->getOption('environment');
     if ($environmentOption) {
         if (!isset($environments[$environmentOption])) {
             $this->stdErr->writeln("Environment not found: <error>{$environmentOption}</error>");
             return 1;
         }
         $environment = $environmentOption;
     } elseif (count($environments) === 1) {
         $environment = key($environments);
     } elseif ($environments && $input->isInteractive()) {
         $environment = $this->offerEnvironmentChoice($environments, $input);
     } else {
         $environment = 'master';
     }
     // Prepare to talk to the Platform.sh repository.
     $gitUrl = $project->getGitUrl();
     $repositoryDir = $directoryName . '/' . LocalProject::REPOSITORY_DIR;
     $gitHelper = new GitHelper(new ShellHelper($this->stdErr));
     $gitHelper->ensureInstalled();
     // First check if the repo actually exists.
     $repoHead = $gitHelper->execute(array('ls-remote', $gitUrl, 'HEAD'), false);
     if ($repoHead === false) {
         // The ls-remote command failed.
         $fsHelper->rmdir($projectRoot);
         $this->stdErr->writeln('<error>Failed to connect to the Platform.sh Git server</error>');
         $this->stdErr->writeln('Please check your SSH credentials or contact Platform.sh support');
         return 1;
     } elseif (is_bool($repoHead)) {
         // The repository doesn't have a HEAD, which means it is empty.
         // We need to create the folder, run git init, and attach the remote.
         mkdir($repositoryDir);
         // Initialize the repo and attach our remotes.
         $this->stdErr->writeln("Initializing empty project repository");
         $gitHelper->execute(array('init'), $repositoryDir, true);
         $this->stdErr->writeln("Adding Platform.sh Git remote");
         $local->ensureGitRemote($repositoryDir, $gitUrl);
         $this->stdErr->writeln("Your repository has been initialized and connected to <info>Platform.sh</info>!");
         $this->stdErr->writeln("Commit and push to the <info>{$environment}</info> branch and Platform.sh will build your project automatically");
         return 0;
     }
     // We have a repo! Yay. Clone it.
     $cloneArgs = array('--branch', $environment, '--origin', 'platform');
     $cloned = $gitHelper->cloneRepo($gitUrl, $repositoryDir, $cloneArgs);
     if (!$cloned) {
         // The clone wasn't successful. Clean up the folders we created
         // and then bow out with a message.
         $fsHelper->rmdir($projectRoot);
         $this->stdErr->writeln('<error>Failed to clone Git repository</error>');
         $this->stdErr->writeln('Please check your SSH credentials or contact Platform.sh support');
         return 1;
     }
     $local->ensureGitRemote($repositoryDir, $gitUrl);
     $this->setProjectRoot($projectRoot);
     $this->stdErr->writeln('');
     $this->stdErr->writeln("The project <info>{$project->title}</info> was successfully downloaded to: <info>{$directoryName}</info>");
     // Ensure that Drush aliases are created.
     if (Drupal::isDrupal($projectRoot . '/' . LocalProject::REPOSITORY_DIR)) {
         $this->stdErr->writeln('');
         $this->runOtherCommand('local:drush-aliases', array('--group' => $directoryName), $input);
     }
     // Allow the build to be skipped.
     if ($input->getOption('no-build')) {
         return 0;
     }
     // Always skip the build if the cloned repository is empty ('.', '..',
     // '.git' being the only found items)
     if (count(scandir($repositoryDir)) <= 3) {
         return 0;
     }
     // Launch the first build.
     $this->stdErr->writeln('');
     $this->stdErr->writeln('Building the project locally for the first time. Run <info>platform build</info> to repeat this.');
     $builder = new LocalBuild(array('environmentId' => $environment), $output);
     $success = $builder->buildProject($projectRoot);
     return $success ? 0 : 1;
 }
Esempio n. 13
0
 /**
  * Create Drush aliases for the provided project and environments.
  *
  * @param Project       $project      The project
  * @param string        $projectRoot  The project root
  * @param Environment[] $environments The environments
  * @param bool          $merge        Whether to merge existing alias settings.
  *
  * @throws \Exception
  *
  * @return bool Whether any aliases have been created.
  */
 public function createAliases(Project $project, $projectRoot, $environments, $merge = true)
 {
     // Ignore the project if it doesn't contain a Drupal application.
     $repository = $projectRoot . '/' . LocalProject::REPOSITORY_DIR;
     if (!is_dir($repository) || !Drupal::isDrupal($repository)) {
         return false;
     }
     $group = !empty($project['alias-group']) ? $project['alias-group'] : $project['id'];
     // Ensure the existence of the .drush directory.
     $drushDir = $this->homeDir . '/.drush';
     if (!is_dir($drushDir)) {
         mkdir($drushDir);
     }
     $filename = $drushDir . '/' . $group . '.aliases.drushrc.php';
     if (!is_writable($drushDir) || file_exists($filename) && !is_writable($filename)) {
         throw new \Exception("Drush alias file not writable: {$filename}");
     }
     // Include the alias file so that the user's own modifications can be
     // merged.
     $aliases = array();
     if (file_exists($filename) && $merge) {
         // This may create a PHP parse error for invalid syntax, but in
         // that case the user could not run Drush anyway.
         include $filename;
     }
     // Generate aliases for the remote environments.
     $numValidEnvironments = 0;
     $autoGenerated = '';
     foreach ($environments as $environment) {
         $newAlias = $this->generateRemoteAlias($environment);
         if (!$newAlias) {
             continue;
         }
         // If the alias already exists, recursively replace existing
         // settings with new ones.
         if (isset($aliases[$environment['id']])) {
             $newAlias = array_replace_recursive($aliases[$environment['id']], $newAlias);
             unset($aliases[$environment['id']]);
         }
         $autoGenerated .= "\n// Automatically generated alias for the environment \"" . $environment['title'] . "\".\n";
         $autoGenerated .= $this->exportAlias($environment['id'], $newAlias);
         $numValidEnvironments++;
     }
     // Generate an alias for the local environment.
     $localAliasName = '_local';
     $local = array('root' => $projectRoot . '/' . LocalProject::WEB_ROOT, self::AUTO_REMOVE_KEY => true);
     if (isset($aliases[$localAliasName])) {
         $local = array_replace_recursive($aliases[$localAliasName], $local);
         unset($aliases[$localAliasName]);
     }
     $localAlias = "\n// Automatically generated alias for the local environment.\n" . $this->exportAlias($localAliasName, $local);
     // Add any user-defined (pre-existing) aliases.
     $userDefined = '';
     foreach ($aliases as $name => $alias) {
         if (!empty($alias[self::AUTO_REMOVE_KEY])) {
             // This is probably for a deleted Platform.sh environment.
             continue;
         }
         $userDefined .= $this->exportAlias($name, $alias) . "\n";
     }
     if ($userDefined) {
         $userDefined = "\n// User-defined aliases.\n" . $userDefined;
     }
     $header = "<?php\n" . "/**\n * @file" . "\n * Drush aliases for the Platform.sh project \"{$project['title']}\"." . "\n *" . "\n * This file is auto-generated by the Platform.sh CLI." . "\n *" . "\n * WARNING" . "\n * This file may be regenerated at any time." . "\n * - User-defined aliases will be preserved." . "\n * - Aliases for active environments (including any custom additions) will be preserved." . "\n * - Aliases for deleted or inactive environments will be deleted." . "\n * - All other information will be deleted." . "\n */\n\n";
     $export = $header . $userDefined . $localAlias . $autoGenerated;
     $this->writeAliasFile($filename, $export);
     return true;
 }
Esempio n. 14
0
 /**
  * Create Drush aliases for the provided project and environments.
  *
  * @param Project       $project      The project
  * @param string        $projectRoot  The project root
  * @param Environment[] $environments The environments
  * @param string        $original     The original group name
  * @param bool          $merge        Whether to merge existing alias settings
  *
  * @throws \Exception
  *
  * @return bool Whether any aliases have been created.
  */
 public function createAliases(Project $project, $projectRoot, $environments, $original = null, $merge = true)
 {
     $config = LocalProject::getProjectConfig($projectRoot);
     $group = !empty($config['alias-group']) ? $config['alias-group'] : $project['id'];
     // Ensure the existence of the .drush directory.
     $drushDir = $this->homeDir . '/.drush';
     if (!is_dir($drushDir)) {
         mkdir($drushDir);
     }
     $filename = $drushDir . '/' . $group . '.aliases.drushrc.php';
     if (!is_writable($drushDir) || file_exists($filename) && !is_writable($filename)) {
         throw new \Exception("Drush alias file not writable: {$filename}");
     }
     // Include the previous alias file(s) so that the user's own
     // modifications can be merged. This may create a PHP parse error for
     // invalid syntax, but in that case the user could not run Drush anyway.
     $aliases = [];
     $originalFiles = [$filename];
     if ($original) {
         array_unshift($originalFiles, $drushDir . '/' . $original . '.aliases.drushrc.php');
     }
     if ($merge) {
         foreach ($originalFiles as $originalFile) {
             if (file_exists($originalFile)) {
                 include $originalFile;
             }
         }
     }
     // Gather applications.
     $apps = LocalApplication::getApplications($projectRoot . '/' . LocalProject::REPOSITORY_DIR);
     $drupalApps = $apps;
     $multiApp = false;
     if (count($apps) > 1) {
         $multiApp = true;
         // Remove non-Drupal applications.
         foreach ($drupalApps as $key => $app) {
             if (!Drupal::isDrupal($app->getRoot())) {
                 unset($drupalApps[$key]);
             }
         }
     }
     // Generate aliases for the remote environments and applications.
     $autoGenerated = '';
     foreach ($environments as $environment) {
         foreach ($drupalApps as $app) {
             $newAlias = $this->generateRemoteAlias($environment, $app, $multiApp);
             if (!$newAlias) {
                 continue;
             }
             $aliasName = $environment->id;
             if (count($drupalApps) > 1) {
                 $aliasName .= '--' . $app->getId();
             }
             // If the alias already exists, recursively replace existing
             // settings with new ones.
             if (isset($aliases[$aliasName])) {
                 $newAlias = array_replace_recursive($aliases[$aliasName], $newAlias);
                 unset($aliases[$aliasName]);
             }
             $autoGenerated .= sprintf("\n// Automatically generated alias for the environment \"%s\", application \"%s\".\n", $environment->title, $app->getId());
             $autoGenerated .= $this->exportAlias($aliasName, $newAlias);
         }
     }
     // Generate an alias for the local environment, for each app.
     $localAlias = '';
     foreach ($drupalApps as $app) {
         $appId = $app->getId();
         $localAliasName = '_local';
         $webRoot = $projectRoot . '/' . LocalProject::WEB_ROOT;
         if (count($drupalApps) > 1) {
             $localAliasName .= '--' . $appId;
         }
         if ($multiApp) {
             $webRoot .= '/' . $appId;
         }
         $local = ['root' => $webRoot, self::AUTO_REMOVE_KEY => true];
         if (isset($aliases[$localAliasName])) {
             $local = array_replace_recursive($aliases[$localAliasName], $local);
             unset($aliases[$localAliasName]);
         }
         $localAlias .= sprintf("\n// Automatically generated alias for the local environment, application \"%s\".\n", $appId) . $this->exportAlias($localAliasName, $local);
     }
     // Add any user-defined (pre-existing) aliases.
     $userDefined = '';
     foreach ($aliases as $name => $alias) {
         if (!empty($alias[self::AUTO_REMOVE_KEY])) {
             // This is probably for a deleted Platform.sh environment.
             continue;
         }
         $userDefined .= $this->exportAlias($name, $alias) . "\n";
     }
     if ($userDefined) {
         $userDefined = "\n// User-defined aliases.\n" . $userDefined;
     }
     $header = "<?php\n" . "/**\n * @file" . "\n * Drush aliases for the Platform.sh project \"{$project->title}\"." . "\n *" . "\n * This file is auto-generated by the Platform.sh CLI." . "\n *" . "\n * WARNING" . "\n * This file may be regenerated at any time." . "\n * - User-defined aliases will be preserved." . "\n * - Aliases for active environments (including any custom additions) will be preserved." . "\n * - Aliases for deleted or inactive environments will be deleted." . "\n * - All other information will be deleted." . "\n */\n\n";
     $export = $header . $userDefined . $localAlias . $autoGenerated;
     $this->writeAliasFile($filename, $export);
     return true;
 }