88
99namespace Inhere \Console \Traits ;
1010
11+ use Inhere \Console \Component \Style \Style ;
1112use Inhere \Console \Face \CommandInterface ;
1213use Inhere \Console \Util \FormatUtil ;
1314use Inhere \Console \Util \Helper ;
@@ -197,10 +198,14 @@ public function showCommandList()
197198 }
198199
199200 /**
200- * for zsh:
201- * php bin/app --auto-completion --shell-env zsh
202- * for bash:
203- * php bin/app --auto-completion --shell-env bash
201+ * zsh:
202+ * php examples/app --auto-completion --shell-env zsh
203+ * php examples/app --auto-completion --shell-env zsh --gen-file
204+ * php examples/app --auto-completion --shell-env zsh --gen-file stdout
205+ * bash:
206+ * php examples/app --auto-completion --shell-env bash
207+ * php examples/app --auto-completion --shell-env bash --gen-file
208+ * php examples/app --auto-completion --shell-env bash --gen-file stdout
204209 * @param string $shellEnv
205210 * @param array $data
206211 */
@@ -213,21 +218,21 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
213218
214219 // info
215220 $ glue = ' ' ;
216- $ genFile = $ input ->getLongOpt ('gen-file ' );
221+ $ genFile = ( string ) $ input ->getLongOpt ('gen-file ' );
217222 $ filename = 'auto-completion. ' . $ shellEnv ;
218- $ tplDir = \dirname (__DIR__ , 2 ) . '/templates ' ;
223+ $ tplDir = \dirname (__DIR__ , 2 ) . '/res/ templates ' ;
219224
220225 if ($ shellEnv === 'bash ' ) {
221- $ tplFile = $ tplDir . '/auto -completion.bash .tpl ' ;
222- $ list = \array_merge (
226+ $ tplFile = $ tplDir . '/bash -completion.tpl ' ;
227+ $ list = \array_merge (
223228 $ this ->getCommandNames (),
224229 $ this ->getControllerNames (),
225230 $ this ->getInternalCommands ()
226231 );
227232 } else {
228- $ glue = \PHP_EOL ;
229- $ list = [];
230- $ tplFile = $ tplDir . '/auto -completion.zsh .tpl ' ;
233+ $ glue = \PHP_EOL ;
234+ $ list = [];
235+ $ tplFile = $ tplDir . '/zsh -completion.tpl ' ;
231236 foreach ($ data as $ name => $ desc ) {
232237 $ list [] = $ name . ': ' . \str_replace (': ' , '\: ' , $ desc );
233238 }
@@ -241,15 +246,36 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
241246 return ;
242247 }
243248
249+ if ($ shellEnv === 'zsh ' ) {
250+ $ commands = "' " . \implode ("' \n' " , $ list ) . "' " ;
251+ $ commands = Style::stripColor ($ commands );
252+ }
253+
244254 // dump at script file
255+ $ binName = $ input ->getBinName ();
245256 $ tplText = \file_get_contents ($ tplFile );
246257 $ content = \strtr ($ tplText , [
247- '{{filename}} ' => $ filename ,
248- '{{commands}} ' => $ commands ,
249- '{{binName}} ' => $ input ->getBinName (),
250- '{{datetime}} ' => \date ('Y-m-d H:i:s ' ),
258+ '{{version}} ' => $ this ->getVersion (),
259+ '{{filename}} ' => $ filename ,
260+ '{{commands}} ' => $ commands ,
261+ '{{binName}} ' => $ binName ,
262+ '{{datetime}} ' => \date ('Y-m-d H:i:s ' ),
263+ '{{fmtBinName}} ' => \str_replace ('/ ' , '_ ' , $ binName ),
251264 ]);
252265
253- \file_put_contents ($ input ->getPwd () . '/ ' . $ filename , $ content );
266+ // dump to stdout
267+ if ($ genFile === 'stdout ' ) {
268+ \file_put_contents ('php://stdout ' , $ content );
269+ return ;
270+ }
271+
272+ $ targetFile = $ input ->getPwd () . '/ ' . $ filename ;
273+ $ output ->write (['Target File: ' , $ targetFile , '' ]);
274+
275+ if (\file_put_contents ($ targetFile , $ content ) > 10 ) {
276+ $ output ->success ("O_O! Generate $ filename successful! " );
277+ } else {
278+ $ output ->error ("O^O! Generate $ filename failure! " );
279+ }
254280 }
255281}
0 commit comments