@@ -26,19 +26,16 @@ class AutocompleteCommand extends TerminusCommand
2626 public function install ()
2727 {
2828 $ this ->checkRequirements ();
29- $ message = "To complete the installation, paste in the terminal the following: \n\n" ;
30- $ message .= "terminus autocomplete:update \n" ;
31- if (TERMINUS_OS == 'DAR ' ) {
32- $ message .= "echo 'source \$(brew --prefix)/etc/bash_completion' >> ~/.bash_profile \n" ;
33- $ message .= "echo 'source ~/.terminus-autocomplete' >> ~/.bash_profile \n" ;
34- $ message .= "source ~/.bash_profile \n" ;
35- } else {
36- $ message .= "echo 'source /etc/bash_completion' >> ~/.bashrc \n" ;
37- $ message .= "echo 'source ~/.terminus-autocomplete' >> ~/.bashrc \n" ;
38- $ message .= "source ~/.bashrc \n" ;
29+ $ prefix = (TERMINUS_OS == 'DAR ' ) ? '\$(brew --prefix) ' : '' ;
30+ $ bashrc = (TERMINUS_OS == 'DAR ' ) ? '.bash_profile ' : '.bashrc ' ;
31+ $ message = "To complete the installation, paste in the terminal the following: " . PHP_EOL ;
32+ $ message .= PHP_EOL . "echo 'source $ {prefix}/etc/bash_completion' >> ~/ $ {bashrc}" . PHP_EOL ;
33+ $ terminus_autocomplete = getenv ('HOME ' ) . '/.terminus-autocomplete ' ;
34+ if (file_exists ($ terminus_autocomplete )) {
35+ $ message .= "echo 'source ~/.terminus-autocomplete' >> ~/ $ {bashrc}" . PHP_EOL ;
3936 }
40- $ message .= "terminus autocomplete:test " ;
4137 $ this ->log ()->notice ($ message );
38+ $ this ->update ();
4239 }
4340
4441 /**
@@ -51,7 +48,7 @@ public function install()
5148 public function check ()
5249 {
5350 $ this ->checkRequirements ();
54- $ message = "All requirements are installed. Type 'terminus autocomplete:install' to complete. " ;
51+ $ message = PHP_EOL . "All requirements are installed. Type 'terminus autocomplete:install' to complete. " . PHP_EOL ;
5552 $ this ->log ()->notice ($ message );
5653 }
5754
@@ -64,7 +61,7 @@ public function check()
6461 */
6562 public function test ()
6663 {
67- $ message = "To test if autocomplete is working, type 'terminus auto<TAB>' and it should expand to 'terminus autocomplete:'. " ;
64+ $ message = PHP_EOL . "To test if autocomplete is working, type 'terminus auto<TAB>' and it should expand to 'terminus autocomplete:'. " . PHP_EOL ;
6865 $ this ->log ()->notice ($ message );
6966 }
7067
@@ -82,6 +79,47 @@ public function update()
8279 $ this ->execute ($ command );
8380 $ message = 'Terminus autocomplete commands have been updated. ' ;
8481 $ this ->log ()->notice ($ message );
82+ $ this ->pantheon_site_environments ();
83+ $ bashrc = (TERMINUS_OS == 'DAR ' ) ? '.bash_profile ' : '.bashrc ' ;
84+ $ message = "To complete the update, execute the following: " . PHP_EOL ;
85+ $ message .= PHP_EOL . "source ~/ $ {bashrc}" . PHP_EOL ;
86+ $ this ->log ()->notice ($ message );
87+ $ this ->test ();
88+ }
89+
90+ /**
91+ * Add Pantheon site environments to autocomplete.
92+ */
93+ protected function pantheon_site_environments ()
94+ {
95+ $ pantheon_aliases = getenv ('HOME ' ) . '/.drush/site-aliases/pantheon.aliases.drushrc.php ' ;
96+ if (!file_exists ($ pantheon_aliases )) {
97+ $ message = PHP_EOL . "Optional: Install Drush site aliases to add Pantheon site environments to autocomplete. " . PHP_EOL ;
98+ $ message .= "See https://pantheon.io/docs/drush/#download-drush-aliases-locally for more information. " . PHP_EOL ;
99+ $ this ->log ()->notice ($ message );
100+ } else {
101+ if (!$ this ->commandExists ('drush ' )) {
102+ $ message = "Please install Drush to add Pantheon site environments to autocomplete. Run 'cgr drush/drush' to install. " . PHP_EOL ;
103+ $ message .= "See http://docs.drush.org/en/master/install/ for alternative ways to install. " ;
104+ throw new TerminusNotFoundException ($ message );
105+ }
106+ $ sites = shell_exec ("drush sa | grep @pantheon. | cut -d'.' -f2,3 | xargs " );
107+ $ terminus_autocomplete = getenv ('HOME ' ) . '/.terminus-autocomplete ' ;
108+ $ lines = file ($ terminus_autocomplete , FILE_IGNORE_NEW_LINES );
109+ $ line = shell_exec ("rgrep -n '^}' $ {terminus_autocomplete} | cut -d':' -f1 " );
110+ $ line = trim (preg_replace ('/[\n|\r]/ ' , '' , $ line )) - 1 ;
111+ $ lines [$ line ] = " prev= \${COMP_WORDS[COMP_CWORD-1]} " ;
112+ $ lines [$ line + 1 ] = " if [[ \$prev == \"drush \" ]]; then " ;
113+ $ lines [$ line + 2 ] = " sites= \"$ {sites}\"" ;
114+ $ lines [$ line + 3 ] = " COMPREPLY=( \$(compgen -W \"\${sites} \" -- \${cur})) " ;
115+ $ lines [$ line + 4 ] = " return 0 " ;
116+ $ lines [$ line + 5 ] = " fi " ;
117+ $ lines [$ line + 6 ] = "} " ;
118+ $ lines [$ line + 7 ] = "complete -o default -F _terminus terminus " ;
119+ file_put_contents ($ terminus_autocomplete , implode (PHP_EOL , $ lines ));
120+ $ message = "Site environments have been added to autocomplete. " ;
121+ $ this ->log ()->notice ($ message );
122+ }
85123 }
86124
87125 /**
@@ -104,7 +142,7 @@ protected function execute($cmd)
104142 /**
105143 * Platform independent check whether a command exists.
106144 *
107- * @param string $command Command to check
145+ * @param string $command Command to check
108146 * @return bool True if exists, false otherwise
109147 */
110148 protected function commandExists ($ command )
@@ -127,14 +165,15 @@ protected function checkRequirements()
127165 $ message = 'Please install brew to continue. See https://brew.sh. ' ;
128166 throw new TerminusNotFoundException ($ message );
129167 }
130- $ prefix = $ this ->execute ("$(brew --prefix) " );
168+ $ prefix = shell_exec ("$(brew --prefix) " );
169+ $ prefix = trim (preg_replace ('/[\n|\r]/ ' , '' , $ prefix ));
131170 }
132171 $ bash_completion = "$ {prefix}/etc/bash_completion " ;
133172 if (!file_exists ("$ bash_completion " )) {
134- $ message = "Please install bash-completion to continue. \n" ;
135- $ message .= " macOS : brew install bash-completion \n" ;
136- $ message .= " Arch-based: sudo pacman -S bash-completion \n" ;
137- $ message .= "Debian-based: sudo apt install bash-completion \n" ;
173+ $ message = "Please install bash-completion to continue. " . PHP_EOL ;
174+ $ message .= " MacOS : brew install bash-completion " . PHP_EOL ;
175+ $ message .= " Arch-based: sudo pacman -S bash-completion " . PHP_EOL ;
176+ $ message .= "Debian-based: sudo apt install bash-completion " . PHP_EOL ;
138177 $ message .= "Redhat-based: sudo yum install bash-completion " ;
139178 throw new TerminusNotFoundException ($ message );
140179 }
@@ -147,8 +186,8 @@ protected function checkRequirements()
147186 throw new TerminusNotFoundException ($ message );
148187 }
149188 if (!$ this ->commandExists ('symfony-autocomplete ' )) {
150- $ message = "Please install symfony-autocomplete to continue. \n" ;
151- $ message .= "Install with 'cgr bamarni/symfony-console-autocomplete'. \n" ;
189+ $ message = "Please install symfony-autocomplete to continue. " . PHP_EOL ;
190+ $ message .= "Install with 'cgr bamarni/symfony-console-autocomplete'. " . PHP_EOL ;
152191 $ message .= "Make sure ' \$HOME/.config/composer/vendor/bin' is in your PATH. " ;
153192 throw new TerminusNotFoundException ($ message );
154193 }
0 commit comments