public function InitCommandFile::initializeDrush

master InitCommandFile.php public InitCommandFile::initializeDrush($options =['edit' '' 'add-path' ''])

Initialize local Drush configuration

@command core-init

@todo: @global-options

@description Enrich the bash startup file with completion and aliases. @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 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

File

lib/Drush/CommandFiles/core/InitCommandFile.php, line 27

Namespace

Drush\CommandFiles\core

Class

InitCommandFile

Code

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;
}