88
99namespace Inhere \Console \BuiltIn ;
1010
11- use BadMethodCallException ;
1211use Closure ;
1312use Exception ;
1413use Inhere \Console \Component \PharCompiler ;
1817use Inhere \Console \Util \Helper ;
1918use Inhere \Console \Util \Show ;
2019use RuntimeException ;
21- use UnexpectedValueException ;
20+ use Toolkit \ Stdlib \ Str ;
2221use function basename ;
2322use function file_exists ;
2423use function is_dir ;
@@ -58,70 +57,78 @@ protected function init(): void
5857 * default is current work-dir.(<cyan>{workDir}</cyan>)
5958 * -c, --config STRING Use the custom config file for build phar(<cyan>./phar.build.inc</cyan>)
6059 * -o, --output STRING Setting the output file name(<cyan>{defaultPkgName}.phar</cyan>)
61- * --fast BOOL Fast build. only add modified files by <cyan>git status -s</cyan>
62- * --refresh BOOL Whether build vendor folder files on phar file exists(<cyan>False</cyan>)
60+ * --fast Fast build. only add modified files by <cyan>git status -s</cyan>
61+ * --refresh Whether build vendor folder files on phar file exists(<cyan>False</cyan>)
62+ * --files STRING Only pack the list files to the exist phar, multi use ',' split
63+ * --no-progress Disable output progress on the runtime
6364 *
64- * @param Input $in
65- * @param Output $out
65+ * @param Input $input
66+ * @param Output $output
6667 *
6768 * @return int
68- * @throws UnexpectedValueException
69- * @throws RuntimeException
70- * @throws BadMethodCallException
7169 * @throws Exception
70+ * @example
71+ * {fullCommand} Pack current dir to a phar file.
72+ * {fullCommand} --dir vendor/swoft/devtool Pack the specified dir to a phar file.
73+ *
74+ * custom output phar file name
75+ * php -d phar.readonly=0 {binFile} phar:pack -o=mycli.phar
76+ *
77+ * only update the input files:
78+ * php -d phar.readonly=0 {binFile} phar:pack -o=mycli.phar --debug --files app/Command/ServeCommand.php
7279 */
73- public function packCommand ($ in , $ out ): int
80+ public function packCommand ($ input , $ output ): int
7481 {
75- $ time = microtime (1 );
76- $ workDir = $ in ->getPwd ();
82+ $ startAt = microtime (1 );
83+ $ workDir = $ input ->getPwd ();
7784
78- $ dir = $ in ->getOpt ('dir ' ) ?: $ workDir ;
85+ $ dir = $ input ->getOpt ('dir ' ) ?: $ workDir ;
7986 $ cpr = $ this ->configCompiler ($ dir );
8087
81- $ counter = null ;
82- $ refresh = $ in ->boolOpt ('refresh ' );
83- $ pharFile = $ workDir . '/ ' . $ in ->sameOpt (['o ' , 'output ' ], basename ($ workDir ) . '.phar ' );
88+ $ refresh = $ input ->boolOpt ('refresh ' );
89+ $ outFile = $ input ->sameOpt (['o ' , 'output ' ], basename ($ workDir ) . '.phar ' );
90+ $ pharFile = $ workDir . '/ ' . $ outFile ;
91+
92+ Show::aList ([
93+ 'work dir ' => $ workDir ,
94+ 'project ' => $ dir ,
95+ 'phar file ' => $ pharFile ,
96+ ], 'Building Information ' );
8497
8598 // use fast build
8699 if ($ this ->input ->boolOpt ('fast ' )) {
87100 $ cpr ->setModifies ($ cpr ->findChangedByGit ());
88101 $ this ->output ->liteNote ('Use fast build, will only pack changed or new files(from git status) ' );
89102 }
90103
91- $ out ->liteInfo ("Now, will begin building phar package. \n from path: <comment> $ workDir</comment> \n" . " phar file: <info> $ pharFile</info> " );
104+ // Manual append some files
105+ if ($ files = $ input ->getStringOpt ('files ' )) {
106+ $ cpr ->setModifies (Str::explode ($ files ));
107+ $ output ->liteInfo ("will only pack input files to the exists phar: $ outFile " );
108+ }
92109
93- $ out ->info ('Pack file to Phar: ' );
94110 $ cpr ->onError (function ($ error ) {
95- $ this ->output ->warning ($ error );
111+ $ this ->writeln ("<warning> $ error</warning> " );
112+ });
113+ $ cpr ->on (PharCompiler::ON_MESSAGE , function ($ msg ) {
114+ $ this ->output ->colored ('> ' . $ msg );
96115 });
97116
98- if ($ in ->getOpt ('debug ' )) {
99- $ cpr ->onAdd (function ($ path ) {
100- $ this ->output ->write (" <comment>+</comment> $ path " );
101- });
102- } else {
103- $ counter = Show::counterTxt ('Handling ... ' , 'Done. ' );
104- $ cpr ->onAdd (function () use ($ counter ) {
105- $ counter ->send (1 );
106- });
107- }
117+ $ output ->colored ('Collect Pack files ' , 'comment ' );
118+ $ this ->outputProgress ($ cpr , $ input );
108119
109120 // packing ...
110121 $ cpr ->pack ($ pharFile , $ refresh );
111122
112- // end
113- if ($ counter ) {
114- $ counter ->send (-1 );
115- }
116-
117- $ out ->write ([
118- PHP_EOL . '<success>Phar build completed!</success> ' ,
119- " - Phar file: $ pharFile " ,
120- ' - Phar size: ' . round (filesize ($ pharFile ) / 1024 / 1024 , 2 ) . ' Mb ' ,
121- ' - Pack Time: ' . round (microtime (1 ) - $ time , 3 ) . ' s ' ,
123+ $ info = [
124+ PHP_EOL . '<success>Phar Build Completed!</success> ' ,
122125 ' - Pack File: ' . $ cpr ->getCounter (),
123- ' - Commit ID: ' . $ cpr ->getVersion (),
124- ]);
126+ ' - Pack Time: ' . round (microtime (true ) - $ startAt , 3 ) . ' s ' ,
127+ ' - Phar Size: ' . round (filesize ($ pharFile ) / 1024 / 1024 , 2 ) . ' Mb ' ,
128+ " - Phar File: $ pharFile " ,
129+ ' - Commit ID: ' . $ cpr ->getLastCommit (),
130+ ];
131+ $ output ->writeln ($ info );
125132
126133 return 0 ;
127134 }
@@ -147,14 +154,48 @@ protected function configCompiler(string $dir): PharCompiler
147154 $ configFile = $ this ->input ->getSameOpt (['c ' , 'config ' ]) ?: $ dir . '/phar.build.inc ' ;
148155
149156 if ($ configFile && is_file ($ configFile )) {
157+ /** @noinspection PhpIncludeInspection */
150158 require $ configFile ;
151-
152159 return $ compiler ->in ($ dir );
153160 }
154161
155162 throw new RuntimeException ("The phar build config file not exists! File: $ configFile " );
156163 }
157164
165+ /**
166+ * @param PharCompiler $cpr
167+ * @param Input $input
168+ *
169+ * @return void
170+ */
171+ private function outputProgress (PharCompiler $ cpr ,Input $ input ): void
172+ {
173+ if ($ input ->getBoolOpt ('no-progress ' )) {
174+ return ;
175+ }
176+
177+ if ($ input ->getOpt ('debug ' )) {
178+ // $output->info('Pack file to Phar ... ...');
179+ $ cpr ->onAdd (function (string $ path ) {
180+ $ this ->writeln (" <info>+</info> $ path " );
181+ });
182+
183+ $ cpr ->on ('skip ' , function (string $ path , bool $ isFile ) {
184+ $ mark = $ isFile ? '[F] ' : '[D] ' ;
185+ $ this ->writeln (" <red>-</red> $ path <info> $ mark</info> " );
186+ });
187+ } else {
188+ $ counter = Show::counterTxt ('Collecting ... ' , 'Done. ' );
189+ $ cpr ->onAdd (function () use ($ counter ) {
190+ $ counter ->send (1 );
191+ });
192+ $ cpr ->on (PharCompiler::ON_COLLECTED , function () use ($ counter ) {
193+ $ counter ->send (-1 );
194+ $ this ->writeln ('' );
195+ });
196+ }
197+ }
198+
158199 /**
159200 * @param Closure $compilerConfiger
160201 */
0 commit comments