5 * Set up local Drush configuration.
8 use Drush\Log\LogLevel;
11 * Implementation of hook_drush_command().
14 * An associative array describing your command(s).
16 function init_drush_command() {
17 $items['core-init'] = array(
18 'description' => 'Enrich the bash startup file with completion and aliases. Copy .drushrc file to ~/.drush',
19 'aliases' => array('init'),
20 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
22 'global-options' => array('editor', 'bg'),
24 'edit' => 'Open the new config file in an editor.',
25 '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.",
28 'drush core-init --edit' => 'Enrich Bash and open drush config file in editor.',
29 'drush core-init --edit --bg' => 'Return to shell prompt as soon as the editor window opens.',
36 * Initialize local Drush configuration
38 function drush_init_core_init() {
39 $home = drush_server_home();
40 $drush_config_dir = $home . "/.drush";
41 $drush_config_file = $drush_config_dir . "/drushrc.php";
42 $drush_bashrc = $drush_config_dir . "/drush.bashrc";
43 $drush_prompt = $drush_config_dir . "/drush.prompt.sh";
44 $drush_complete = $drush_config_dir . "/drush.complete.sh";
45 $examples_dir = DRUSH_BASE_PATH . "/examples";
46 $example_configuration = $examples_dir . "/example.drushrc.php";
47 $example_bashrc = $examples_dir . "/example.bashrc";
48 $example_prompt = $examples_dir . "/example.prompt.sh";
49 $example_complete = DRUSH_BASE_PATH . "/drush.complete.sh";
50 $bashrc_additions = array();
52 // Create a ~/.drush directory if it does not yet exist
53 if (!is_dir($drush_config_dir)) {
54 drush_mkdir($drush_config_dir);
57 // If there is no ~/.drush/drushrc.php, then copy the
58 // example Drush configuration file here
59 if (!is_file($drush_config_file)) {
60 copy($example_configuration, $drush_config_file);
61 drush_log(dt("Copied example Drush configuration file to !path", array('!path' => $drush_config_file)), LogLevel::OK);
64 // If there is no ~/.drush/drush.bashrc file, then copy
65 // the example bashrc file there
66 if (!is_file($drush_bashrc)) {
67 copy($example_bashrc, $drush_bashrc);
68 $pattern = basename($drush_bashrc);
69 $bashrc_additions["%$pattern%"] = "# Include Drush bash customizations.\n". drush_bash_addition($drush_bashrc);
70 drush_log(dt("Copied example Drush bash configuration file to !path", array('!path' => $drush_bashrc)), LogLevel::OK);
73 // If there is no ~/.drush/drush.complete.sh file, then copy it there
74 if (!is_file($drush_complete)) {
75 copy($example_complete, $drush_complete);
76 $pattern = basename($drush_complete);
77 $bashrc_additions["%$pattern%"] = "# Include Drush completion.\n". drush_bash_addition($drush_complete);
78 drush_log(dt("Copied Drush completion file to !path", array('!path' => $drush_complete)), LogLevel::OK);
81 // If there is no ~/.drush/drush.prompt.sh file, then copy
82 // the example prompt.sh file here
83 if (!is_file($drush_prompt)) {
84 copy($example_prompt, $drush_prompt);
85 $pattern = basename($drush_prompt);
86 $bashrc_additions["%$pattern%"] = "# Include Drush prompt customizations.\n". drush_bash_addition($drush_prompt);
87 drush_log(dt("Copied example Drush prompt file to !path", array('!path' => $drush_prompt)), LogLevel::OK);
90 // Decide whether we want to add our Bash commands to
91 // ~/.bashrc or ~/.bash_profile
92 $bashrc = drush_init_find_bashrc($home);
94 // If Drush is not in the $PATH, then figure out which
95 // path to add so that Drush can be found globally.
96 $add_path = drush_get_option('add-path', NULL);
97 if ((!drush_which("drush") || $add_path) && ($add_path !== FALSE)) {
98 $drush_path = drush_find_path_to_drush($home);
99 $drush_path = preg_replace("%^" . preg_quote($home) . "/%", '$HOME/', $drush_path);
101 $bashrc_additions["%$drush_path%"] = "# Path to Drush, added by 'drush init'.\nexport PATH=\"\$PATH:$drush_path\"\n\n";
104 // Modify the user's bashrc file, adding our customizations.
105 $bashrc_contents = "";
106 if (file_exists($bashrc)) {
107 $bashrc_contents = file_get_contents($bashrc);
109 $new_bashrc_contents = $bashrc_contents;
110 foreach ($bashrc_additions as $pattern => $addition) {
111 // Only put in the addition if the pattern does not already
112 // exist in the bashrc file.
113 if (!preg_match($pattern, $new_bashrc_contents)) {
114 $new_bashrc_contents = $new_bashrc_contents . $addition;
117 if ($new_bashrc_contents != $bashrc_contents) {
118 if (drush_confirm(dt(implode('', $bashrc_additions) . "Append the above code to !file?", array('!file' => $bashrc)))) {
119 file_put_contents($bashrc, "\n\n". $new_bashrc_contents);
120 drush_log(dt("Updated bash configuration file !path", array('!path' => $bashrc)), LogLevel::OK);
121 drush_log(dt("Start a new shell in order to experience the improvements (e.g. `bash`)."), LogLevel::OK);
122 if (drush_get_option('edit')) {
123 $exec = drush_get_editor();
124 drush_shell_exec_interactive($exec, $drush_config_file, $drush_config_file);
128 return drush_user_abort();
132 drush_log(dt('No code added to !path', array('!path' => $bashrc)), LogLevel::OK);
137 * Determine which .bashrc file is best to use on this platform.
139 function drush_init_find_bashrc($home) {
140 return $home . "/.bashrc";
144 * Determine where Drush is located, so that we can add
145 * that location to the $PATH
147 function drush_find_path_to_drush($home) {
148 // First test: is Drush inside a vendor directory?
149 // Does vendor/bin exist? If so, use that. We do
150 // not have a good way to locate the 'bin' directory
151 // if it has been relocated in the composer.json config
153 if ($vendor_pos = strpos(DRUSH_BASE_PATH, "/vendor/")) {
154 $vendor_dir = substr(DRUSH_BASE_PATH, 0, $vendor_pos + 7);
155 $vendor_bin = $vendor_dir . '/bin';
156 if (is_dir($vendor_bin)) {
161 // Fallback is to use the directory that Drush is in.
162 return DRUSH_BASE_PATH;
165 function drush_bash_addition($file) {
167 if [ -f "$file" ] ; then