private function removeDirectory($directory) { $result = $this->file_system->checkDir($directory); if ($result) { $remove = drush_confirm(dt('Do you want to delete directory %directory?', array('%directory' => $directory))); if ($remove) { $result = $this->file_system->removeDir($directory); } else { $result = drush_user_abort('Aborting...'); } } return $result; }
/** * Load the desired environments. * * @param string $env * The environment argument. May be composite (env1+env2). * * @return bool * Whether the environments were loaded. */ public function setEnvironments($env) { $envs = explode('+', $env); while (!empty($envs)) { $_env = array_shift($envs); if (!$this->setEnvironment($_env)) { $msg = dt("Unable to locate an environment definition for '@env'.", ['@env' => $_env]); if (empty($envs)) { return drush_set_error('DRUSH_DRUPAL_ERROR_MESSAGE', $msg); } else { drush_log($msg, 'warning'); if (!drush_confirm(dt("Do you want to process the other environments?"))) { return drush_user_abort(dt("Aborting.")); } } } } return TRUE; }
/** * Select the most appropriate release for a project, based on a strategy. * * @param Array &$request * A request array. * The array will be expanded with the project type. * @param String $restrict_to * One of: * 'dev': Forces choosing a -dev release. * 'version': Forces choosing a point release. * '': No restriction. * Default is ''. * @param String $select * Strategy for selecting a release, should be one of: * - auto: Try to select the latest release, if none found allow the user * to choose. * - always: Force the user to choose a release. * - never: Try to select the latest release, if none found then fail. * - ignore: Ignore and return NULL. * If no supported release is found, allow to ask the user to choose one. * @param Boolean $all * In case $select = TRUE this indicates that all available releases will be * offered the user to choose. * * @return array * The selected release. */ public function selectReleaseBasedOnStrategy($request, $restrict_to = '', $select = 'never', $all = FALSE, $version = NULL) { if (!in_array($select, array('auto', 'never', 'always', 'ignore'))) { return drush_set_error('DRUSH_PM_UNKNOWN_SELECT_STRATEGY', dt("Error: select strategy must be one of: auto, never, always, ignore", array())); } $project_release_info = $this->get($request); if (!$project_release_info) { return FALSE; } if ($select != 'always') { if (isset($request['version'])) { $release = $project_release_info->getSpecificRelease($request['version']); if ($release === FALSE) { return drush_set_error('DRUSH_PM_COULD_NOT_FIND_VERSION', dt("Could not locate !project version !version.", array('!project' => $request['name'], '!version' => $request['version']))); } } if ($restrict_to == 'dev') { // If you specified a specific release AND --dev, that is either // redundant (okay), or contradictory (error). if (!empty($release)) { if ($release['version_extra'] != 'dev') { return drush_set_error('DRUSH_PM_COULD_NOT_FIND_VERSION', dt("You requested both --dev and !project version !version, which is not a '-dev' release.", array('!project' => $request['name'], '!version' => $request['version']))); } } else { $release = $project_release_info->getDevRelease(); if ($release === FALSE) { return drush_set_error('DRUSH_PM_NO_DEV_RELEASE', dt('There is no development release for project !project.', array('!project' => $request['name']))); } } } // If there was no specific release requested, try to identify the most appropriate release. if (empty($release)) { $release = $project_release_info->getRecommendedOrSupportedRelease(); } if ($release) { return $release; } else { $message = dt('There are no stable releases for project !project.', array('!project' => $request['name'])); if ($select == 'never') { return drush_set_error('DRUSH_PM_NO_STABLE_RELEASE', $message); } drush_log($message, 'warning'); if ($select == 'ignore') { return NULL; } } } // At this point the only chance is to ask the user to choose a release. if ($restrict_to == 'dev') { $filter = 'dev'; } elseif ($all) { $filter = 'all'; } else { $filter = ''; } $releases = $project_release_info->filterReleases($filter, $version); $options = array(); foreach ($releases as $release) { $options[$release['version']] = array($release['version'], '-', gmdate('Y-M-d', $release['date']), '-', implode(', ', $release['release_status'])); } $choice = drush_choice($options, dt('Choose one of the available releases for !project:', array('!project' => $request['name']))); if (!$choice) { return drush_user_abort(); } return $releases[$choice]; }
/** * Initialize local Drush configuration * * Enrich the bash startup file with completion and aliases. * Copy .drushrc file to ~/.drush * * @todo: '@package core', @global-options, replacement mechanism for 'bootstrap' * * @option $edit Open the new config file in an editor. * @option $add-path Always add Drush to the \$PATH in the user's .bashrc * file, even if it is already in the \$PATH. Use --no-add-path to skip * updating .bashrc with the Drush \$PATH. Default is to update .bashrc * only if Drush is not already in the \$PATH. * @aliases core-init, init * @usage core-init --edit * Enrich Bash and open drush config file in editor. * @usage core-init --edit --bg * Return to shell prompt as soon as the editor window opens */ public function initializeDrush($options = ['edit' => '', 'add-path' => '']) { $home = drush_server_home(); $drush_config_dir = $home . "/.drush"; $drush_config_file = $drush_config_dir . "/drushrc.php"; $drush_bashrc = $drush_config_dir . "/drush.bashrc"; $drush_prompt = $drush_config_dir . "/drush.prompt.sh"; $drush_complete = $drush_config_dir . "/drush.complete.sh"; $examples_dir = DRUSH_BASE_PATH . "/examples"; $example_configuration = $examples_dir . "/example.drushrc.php"; $example_bashrc = $examples_dir . "/example.bashrc"; $example_prompt = $examples_dir . "/example.prompt.sh"; $example_complete = DRUSH_BASE_PATH . "/drush.complete.sh"; $collection = $this->collectionBuilder(); // Create a ~/.drush directory if it does not yet exist $collection->taskFilesystemStack()->mkdir($drush_config_dir); // If there is no ~/.drush/drushrc.php, then copy the // example Drush configuration file here if (!is_file($drush_config_file)) { $collection->taskWriteToFile($drush_config_file)->textFromFile($example_configuration); } // Decide whether we want to add our Bash commands to // ~/.bashrc or ~/.bash_profile, and create a task to // update it with includes of the various files we write, // as needed. If it is, then we will add it to the collection. $bashrc = $this->findBashrc($home); $taskUpdateBashrc = $this->taskWriteToFile($bashrc)->append(); // List of Drush bash configuration files, and // their source templates. $drushBashFiles = [$drush_bashrc => $example_bashrc, $drush_complete => $example_complete, $drush_prompt => $example_prompt]; // Mapping from Drush bash configuration files // to a description of what each one is. $drushBashFileDescriptions = [$drush_bashrc => 'Drush bash customizations', $drush_complete => 'Drush completion', $drush_prompt => 'Drush prompt customizations']; foreach ($drushBashFiles as $destFile => $sourceFile) { // If the destination file does not exist, then // copy the example file there. if (!is_file($destFile)) { $collection->taskWriteToFile($destFile)->textFromFile($sourceFile); $description = $drushBashFileDescriptions[$destFile]; $collection->progressMessage('Copied {description} to {path}', ['description' => $description, 'path' => $destFile], LogLevel::OK); $pattern = basename($destFile); $taskUpdateBashrc->appendUnlessMatches("#{$pattern}#", "# Include {$description}.\n" . $this->bashAddition($destFile)); } } // If Drush is not in the $PATH, then figure out which // path to add so that Drush can be found globally. $add_path = $options['add-path']; if ((!drush_which("drush") || $add_path) && $add_path !== FALSE) { $drush_path = $this->findPathToDrush(); $drush_path = preg_replace("%^" . preg_quote($home) . "/%", '$HOME/', $drush_path); $pattern = "{$drush_path}"; $taskUpdateBashrc->appendUnlessMatches("#{$pattern}#", "# Path to Drush, added by 'drush init'.\nexport PATH=\"\$PATH:{$drush_path}\"\n\n"); } $openEditor = FALSE; if ($taskUpdateBashrc->wouldChange()) { if (drush_confirm(dt("Modify !file to include Drush configuration files?", array('!file' => $bashrc)))) { $collection->addTask($taskUpdateBashrc); $collection->progressMessage('Updated bash configuration file {path}', ['path' => $bashrc], LogLevel::OK); $collection->progressMessage('Start a new shell in order to experience the improvements (e.g. `{shell}`).', ['shell' => 'bash'], LogLevel::OK); $openEditor = $options['edit']; } else { return drush_user_abort(); } } else { $collection->progressMessage('No code added to {path}', ['path' => $bashrc]); } $result = $collection->run(); if ($result->wasSuccessful() && $openEditor) { $exec = drush_get_editor(); drush_shell_exec_interactive($exec, $drush_config_file, $drush_config_file); } return $result; }