init.drush.inc

Set up local Drush configuration.

Functions

Namesort descending Description
drush_find_path_to_drush Determine where Drush is located, so that we can add that location to the $PATH
drush_init_core_init Initialize local Drush configuration
drush_init_find_bashrc Determine which .bashrc file is best to use on this platform.
init_drush_command Implementation of hook_drush_command().

File

commands/core/init.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Set up local Drush configuration.
  5. */
  6. use Drush\Log\LogLevel;
  7. /**
  8. * Implementation of hook_drush_command().
  9. *
  10. * @return
  11. * An associative array describing your command(s).
  12. */
  13. function init_drush_command() {
  14. $items['core-init'] = array(
  15. 'description' => 'Enrich the bash startup file with completion and aliases. Copy .drushrc file to ~/.drush',
  16. 'aliases' => array('init'),
  17. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  18. 'package' => 'core',
  19. 'global-options' => array('editor', 'bg'),
  20. 'options' => array(
  21. 'edit' => 'Open the new config file in an editor.',
  22. '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.",
  23. ),
  24. 'examples' => array(
  25. 'drush core-init --edit' => 'Enrich Bash and open drush config file in editor.',
  26. 'drush core-init --edit --bg' => 'Return to shell prompt as soon as the editor window opens.',
  27. ),
  28. );
  29. return $items;
  30. }
  31. /**
  32. * Initialize local Drush configuration
  33. */
  34. function drush_init_core_init() {
  35. $home = drush_server_home();
  36. $drush_config_dir = $home . "/.drush";
  37. $drush_config_file = $drush_config_dir . "/drushrc.php";
  38. $drush_bashrc = $drush_config_dir . "/drush.bashrc";
  39. $drush_prompt = $drush_config_dir . "/drush.prompt.sh";
  40. $drush_complete = $drush_config_dir . "/drush.complete.sh";
  41. $examples_dir = DRUSH_BASE_PATH . "/examples";
  42. $example_configuration = $examples_dir . "/example.drushrc.php";
  43. $example_bashrc = $examples_dir . "/example.bashrc";
  44. $example_prompt = $examples_dir . "/example.prompt.sh";
  45. $example_complete = DRUSH_BASE_PATH . "/drush.complete.sh";
  46. $bashrc_additions = array();
  47. // Create a ~/.drush directory if it does not yet exist
  48. if (!is_dir($drush_config_dir)) {
  49. drush_mkdir($drush_config_dir);
  50. }
  51. // If there is no ~/.drush/drushrc.php, then copy the
  52. // example Drush configuration file here
  53. if (!is_file($drush_config_file)) {
  54. @copy($example_configuration, $drush_config_file);
  55. drush_log(dt("Copied example Drush configuration file to !path.", array('!path' => $drush_config_file)), LogLevel::OK);
  56. }
  57. // If there is no ~/.drush/drush.bashrc file, then copy
  58. // the example bashrc file here
  59. if (!is_file($drush_bashrc)) {
  60. @copy($example_bashrc, $drush_bashrc);
  61. $pattern = basename($drush_bashrc);
  62. $bashrc_additions["%$pattern%"] = "# Include Drush bash customizations.\n. $drush_bashrc\n\n";
  63. drush_log(dt("Copied example Drush bash configuration file to !path.", array('!path' => $drush_bashrc)), LogLevel::OK);
  64. }
  65. // If there is no ~/.drush/drush.complete.sh file, then copy it there
  66. if (!is_file($drush_complete)) {
  67. copy($example_complete, $drush_complete);
  68. $pattern = basename($drush_complete);
  69. $bashrc_additions["%$pattern%"] = "# Include Drush completion.\n. $drush_complete\n\n";
  70. drush_log(dt("Copied example Drush prompt file to !path.", array('!path' => $drush_complete)), LogLevel::OK);
  71. }
  72. // If there is no ~/.drush/drush.prompt.sh file, then copy
  73. // the example prompt.sh file here
  74. if (!is_file($drush_prompt)) {
  75. @copy($example_prompt, $drush_prompt);
  76. $pattern = basename($drush_prompt);
  77. $bashrc_additions["%$pattern%"] = "# Include Drush prompt customizations.\n. $drush_prompt\n\n";
  78. drush_log(dt("Copied example Drush prompt file to !path.", array('!path' => $drush_prompt)), LogLevel::OK);
  79. }
  80. // Decide whether we want to add our Bash commands to
  81. // ~/.bashrc or ~/.bash_profile
  82. $bashrc = drush_init_find_bashrc($home);
  83. // If Drush is not in the $PATH, then figure out which
  84. // path to add so that Drush can be found globally.
  85. $add_path = drush_get_option('add-path', NULL);
  86. if ((!drush_which("drush") || $add_path) && ($add_path !== FALSE)) {
  87. $drush_path = drush_find_path_to_drush($home);
  88. $drush_path = preg_replace("%^" . preg_quote($home) . "/%", '$HOME/', $drush_path);
  89. $bashrc_additions["%$drush_path%"] = "# Path to Drush, added by 'drush init'.\nexport \$PATH=\"\$PATH:$drush_path\"\n\n";
  90. }
  91. // Modify the user's bashrc file, adding our customizations.
  92. $bashrc_contents = "";
  93. if (file_exists($bashrc)) {
  94. $bashrc_contents = file_get_contents($bashrc);
  95. }
  96. $new_bashrc_contents = $bashrc_contents;
  97. foreach ($bashrc_additions as $pattern => $addition) {
  98. // Only put in the addition if the pattern does not already
  99. // exist in the bashrc file.
  100. if (!preg_match($pattern, $new_bashrc_contents)) {
  101. $new_bashrc_contents = $addition . $new_bashrc_contents;
  102. }
  103. }
  104. if ($new_bashrc_contents != $bashrc_contents) {
  105. file_put_contents($bashrc, $new_bashrc_contents);
  106. drush_log(dt("Updated bash configuration file !path", array('!path' => $bashrc)), LogLevel::OK);
  107. }
  108. if (drush_get_option('edit')) {
  109. $exec = drush_get_editor();
  110. drush_shell_exec_interactive($exec, $drush_config_file, $drush_config_file);
  111. }
  112. }
  113. /**
  114. * Determine which .bashrc file is best to use on this platform.
  115. */
  116. function drush_init_find_bashrc($home) {
  117. return $home . "/.bashrc";
  118. }
  119. /**
  120. * Determine where Drush is located, so that we can add
  121. * that location to the $PATH
  122. */
  123. function drush_find_path_to_drush($home) {
  124. // First test: is Drush inside a vendor directory?
  125. // Does vendor/bin exist? If so, use that. We do
  126. // not have a good way to locate the 'bin' directory
  127. // if it has been relocated in the composer.json config
  128. // section.
  129. if ($vendor_pos = strpos(DRUSH_BASE_PATH, "/vendor/")) {
  130. $vendor_dir = substr(DRUSH_BASE_PATH, 0, $vendor_pos + 7);
  131. $vendor_bin = $vendor_dir . '/bin';
  132. if (is_dir($vendor_bin)) {
  133. return $vendor_bin;
  134. }
  135. }
  136. // Fallback is to use the directory that Drush is in.
  137. return DRUSH_BASE_PATH;
  138. }