Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / commands / core / init.drush.inc
1 <?php
2
3 /**
4  * @file
5  *   Set up local Drush configuration.
6  */
7
8 use Drush\Log\LogLevel;
9
10 /**
11  * Implementation of hook_drush_command().
12  *
13  * @return
14  *   An associative array describing your command(s).
15  */
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,
21     'package' => 'core',
22     'global-options' => array('editor', 'bg'),
23     'options' => array(
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.",
26     ),
27     'examples' => array(
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.',
30     ),
31   );
32   return $items;
33 }
34
35 /**
36  * Initialize local Drush configuration
37  */
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();
51
52   // Create a ~/.drush directory if it does not yet exist
53   if (!is_dir($drush_config_dir)) {
54     drush_mkdir($drush_config_dir);
55   }
56
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);
62   }
63
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%"] = "\n# Include Drush bash customizations.". drush_bash_addition($drush_bashrc);
70     drush_log(dt("Copied example Drush bash configuration file to !path", array('!path' => $drush_bashrc)), LogLevel::OK);
71   }
72
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%"] = "\n# 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);
79   }
80
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%"] = "\n# 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);
88   }
89
90   // Decide whether we want to add our Bash commands to
91   // ~/.bashrc or ~/.bash_profile
92   $bashrc = drush_init_find_bashrc($home);
93
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);
100
101     $bashrc_additions["%$drush_path%"] = "\n# Path to Drush, added by 'drush init'.\nexport PATH=\"\$PATH:$drush_path\"\n\n";
102   }
103
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);
108   }
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;
115     }
116   }
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);
125       }
126     }
127     else {
128       return drush_user_abort();
129     }
130   }
131   else {
132     drush_log(dt('No code added to !path', array('!path' => $bashrc)), LogLevel::OK);
133   }
134 }
135
136 /**
137  * Determine which .bashrc file is best to use on this platform.
138  */
139 function drush_init_find_bashrc($home) {
140   return $home . "/.bashrc";
141 }
142
143 /**
144  * Determine where Drush is located, so that we can add
145  * that location to the $PATH
146  */
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
152   // section.
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)) {
157       return $vendor_bin;
158     }
159   }
160
161   // Fallback is to use the directory that Drush is in.
162   return DRUSH_BASE_PATH;
163 }
164
165 function drush_bash_addition($file) {
166   return <<<EOD
167
168 if [ -f "$file" ] ; then
169   source $file
170 fi
171
172
173 EOD;
174 }