InitCommandFile.php

Namespace

Drush\CommandFiles\core

Classes

Namesort ascending Description
InitCommandFile

File

lib/Drush/CommandFiles/core/InitCommandFile.php
View source
  1. <?php
  2. namespace Drush\CommandFiles\core;
  3. use Drush\Log\LogLevel;
  4. class InitCommandFile extends \Robo\Tasks
  5. {
  6. /**
  7. * Initialize local Drush configuration
  8. *
  9. * @command core-init
  10. *
  11. * @todo: @global-options
  12. *
  13. * @description Enrich the bash startup file with completion and aliases.
  14. * @option $edit Open the new config file in an editor.
  15. * @option $add-path Always add Drush to the \$PATH in the user's .bashrc
  16. * file, even if it is already in the \$PATH. Use --no-add-path to skip
  17. * updating .bashrc with the Drush \$PATH. Default is to update .bashrc
  18. * only if Drush is not already in the \$PATH.
  19. * @aliases init
  20. * @usage core-init --edit
  21. * Enrich Bash and open drush config file in editor.
  22. * @usage core-init --edit --bg
  23. * Return to shell prompt as soon as the editor window opens
  24. */
  25. public function initializeDrush($options = [
  26. 'edit' => '',
  27. 'add-path' => '',
  28. ]) {
  29. $home = drush_server_home();
  30. $drush_config_dir = $home . "/.drush";
  31. $drush_config_file = $drush_config_dir . "/drushrc.php";
  32. $drush_bashrc = $drush_config_dir . "/drush.bashrc";
  33. $drush_prompt = $drush_config_dir . "/drush.prompt.sh";
  34. $drush_complete = $drush_config_dir . "/drush.complete.sh";
  35. $examples_dir = DRUSH_BASE_PATH . "/examples";
  36. $example_configuration = $examples_dir . "/example.drushrc.php";
  37. $example_bashrc = $examples_dir . "/example.bashrc";
  38. $example_prompt = $examples_dir . "/example.prompt.sh";
  39. $example_complete = DRUSH_BASE_PATH . "/drush.complete.sh";
  40. $collection = $this->collectionBuilder();
  41. // Create a ~/.drush directory if it does not yet exist
  42. $collection->taskFilesystemStack()
  43. ->mkdir($drush_config_dir);
  44. // If there is no ~/.drush/drushrc.php, then copy the
  45. // example Drush configuration file here
  46. if (!is_file($drush_config_file)) {
  47. $collection->taskWriteToFile($drush_config_file)
  48. ->textFromFile($example_configuration);
  49. }
  50. // Decide whether we want to add our Bash commands to
  51. // ~/.bashrc or ~/.bash_profile, and create a task to
  52. // update it with includes of the various files we write,
  53. // as needed. If it is, then we will add it to the collection.
  54. $bashrc = $this->findBashrc($home);
  55. $taskUpdateBashrc = $this->taskWriteToFile($bashrc)
  56. ->append();
  57. // List of Drush bash configuration files, and
  58. // their source templates.
  59. $drushBashFiles = [
  60. $drush_bashrc => $example_bashrc,
  61. $drush_complete => $example_complete,
  62. $drush_prompt => $example_prompt,
  63. ];
  64. // Mapping from Drush bash configuration files
  65. // to a description of what each one is.
  66. $drushBashFileDescriptions = [
  67. $drush_bashrc => 'Drush bash customizations',
  68. $drush_complete => 'Drush completion',
  69. $drush_prompt => 'Drush prompt customizations',
  70. ];
  71. foreach ($drushBashFiles as $destFile => $sourceFile) {
  72. // If the destination file does not exist, then
  73. // copy the example file there.
  74. if (!is_file($destFile)) {
  75. $collection->taskWriteToFile($destFile)
  76. ->textFromFile($sourceFile);
  77. $description = $drushBashFileDescriptions[$destFile];
  78. $collection->progressMessage('Copied {description} to {path}', ['description' => $description, 'path' => $destFile], LogLevel::OK);
  79. $pattern = basename($destFile);
  80. $taskUpdateBashrc->appendUnlessMatches("#$pattern#", "# Include $description.\n". $this->bashAddition($destFile));
  81. }
  82. }
  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 = $options['add-path'];
  86. if ((!drush_which("drush") || $add_path) && ($add_path !== FALSE)) {
  87. $drush_path = $this->findPathToDrush();
  88. $drush_path = preg_replace("%^" . preg_quote($home) . "/%", '$HOME/', $drush_path);
  89. $pattern = "$drush_path";
  90. $taskUpdateBashrc->appendUnlessMatches("#$pattern#", "# Path to Drush, added by 'drush init'.\nexport PATH=\"\$PATH:$drush_path\"\n\n");
  91. }
  92. $openEditor = FALSE;
  93. if ($taskUpdateBashrc->wouldChange()) {
  94. if (drush_confirm(dt("Modify !file to include Drush configuration files?", array('!file' => $bashrc)))) {
  95. $collection->addTask($taskUpdateBashrc);
  96. $collection->progressMessage('Updated bash configuration file {path}', ['path' => $bashrc], LogLevel::OK);
  97. $collection->progressMessage('Start a new shell in order to experience the improvements (e.g. `{shell}`).', ['shell' => 'bash'], LogLevel::OK);
  98. $openEditor = $options['edit'];
  99. }
  100. else {
  101. return drush_user_abort();
  102. }
  103. }
  104. else {
  105. $collection->progressMessage('No code added to {path}', ['path' => $bashrc]);
  106. }
  107. $result = $collection->run();
  108. if ($result->wasSuccessful() && $openEditor) {
  109. $exec = drush_get_editor();
  110. drush_shell_exec_interactive($exec, $drush_config_file, $drush_config_file);
  111. }
  112. return $result;
  113. }
  114. /**
  115. * Determine which .bashrc file is best to use on this platform.
  116. */
  117. protected function findBashrc($home) {
  118. return $home . "/.bashrc";
  119. }
  120. /**
  121. * Determine where Drush is located, so that we can add
  122. * that location to the $PATH
  123. */
  124. protected function findPathToDrush() {
  125. // First test: is Drush inside a vendor directory?
  126. // Does vendor/bin exist? If so, use that. We do
  127. // not have a good way to locate the 'bin' directory
  128. // if it has been relocated in the composer.json config
  129. // section.
  130. if ($vendor_pos = strpos(DRUSH_BASE_PATH, "/vendor/")) {
  131. $vendor_dir = substr(DRUSH_BASE_PATH, 0, $vendor_pos + 7);
  132. $vendor_bin = $vendor_dir . '/bin';
  133. if (is_dir($vendor_bin)) {
  134. return $vendor_bin;
  135. }
  136. }
  137. // Fallback is to use the directory that Drush is in.
  138. return DRUSH_BASE_PATH;
  139. }
  140. protected function bashAddition($file) {
  141. return <<<EOD
  142. if [ -f "$file" ] ; then
  143. source $file
  144. fi
  145. EOD;
  146. }
  147. }