function drush_init_core_init

8.0.x init.drush.inc drush_init_core_init()

Initialize local Drush configuration

File

commands/core/init.drush.inc, line 38
Set up local Drush configuration.

Code

function drush_init_core_init() {
  $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";
  $bashrc_additions = array();

  // Create a ~/.drush directory if it does not yet exist
  if (!is_dir($drush_config_dir)) {
    drush_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)) {
    @copy($example_configuration, $drush_config_file);
    drush_log(dt("Copied example Drush configuration file to !path.", array('!path' => $drush_config_file)), LogLevel::OK);
  }

  // If there is no ~/.drush/drush.bashrc file, then copy
  // the example bashrc file here
  if (!is_file($drush_bashrc)) {
    @copy($example_bashrc, $drush_bashrc);
    $pattern = basename($drush_bashrc);
    $bashrc_additions["%$pattern%"] = "# Include Drush bash customizations.\n. $drush_bashrc\n\n";
    drush_log(dt("Copied example Drush bash configuration file to !path.", array('!path' => $drush_bashrc)), LogLevel::OK);
  }

  // If there is no ~/.drush/drush.complete.sh file, then copy it there
  if (!is_file($drush_complete)) {
    copy($example_complete, $drush_complete);
    $pattern = basename($drush_complete);
    $bashrc_additions["%$pattern%"] = "# Include Drush completion.\n. $drush_complete\n\n";
    drush_log(dt("Copied example Drush prompt file to !path.", array('!path' => $drush_complete)), LogLevel::OK);
  }

  // If there is no ~/.drush/drush.prompt.sh file, then copy
  // the example prompt.sh file here
  if (!is_file($drush_prompt)) {
    @copy($example_prompt, $drush_prompt);
    $pattern = basename($drush_prompt);
    $bashrc_additions["%$pattern%"] = "# Include Drush prompt customizations.\n. $drush_prompt\n\n";
    drush_log(dt("Copied example Drush prompt file to !path.", array('!path' => $drush_prompt)), LogLevel::OK);
  }

  // Decide whether we want to add our Bash commands to
  // ~/.bashrc or ~/.bash_profile
  $bashrc = drush_init_find_bashrc($home);

  // If Drush is not in the $PATH, then figure out which
  // path to add so that Drush can be found globally.
  $add_path = drush_get_option('add-path', NULL);
  if ((!drush_which("drush") || $add_path) && ($add_path !== FALSE)) {
    $drush_path = drush_find_path_to_drush($home);
    $drush_path = preg_replace("%^" . preg_quote($home) . "/%", '$HOME/', $drush_path);

    $bashrc_additions["%$drush_path%"] = "# Path to Drush, added by 'drush init'.\nexport \$PATH=\"\$PATH:$drush_path\"\n\n";
  }

  // Modify the user's bashrc file, adding our customizations.
  $bashrc_contents = "";
  if (file_exists($bashrc)) {
    $bashrc_contents = file_get_contents($bashrc);
  }
  $new_bashrc_contents = $bashrc_contents;
  foreach ($bashrc_additions as $pattern => $addition) {
    // Only put in the addition if the pattern does not already
    // exist in the bashrc file.
    if (!preg_match($pattern, $new_bashrc_contents)) {
      $new_bashrc_contents = $addition . $new_bashrc_contents;
    }
  }
  if ($new_bashrc_contents != $bashrc_contents) {
    file_put_contents($bashrc, $new_bashrc_contents);
    drush_log(dt("Updated bash configuration file !path", array('!path' => $bashrc)), LogLevel::OK);
  }

  if (drush_get_option('edit')) {
    $exec = drush_get_editor();
    drush_shell_exec_interactive($exec, $drush_config_file, $drush_config_file);
  }
}