8
8
9
9
namespace Inhere \Console \BuiltIn ;
10
10
11
- use BadMethodCallException ;
12
11
use Closure ;
13
12
use Exception ;
14
13
use Inhere \Console \Component \PharCompiler ;
18
17
use Inhere \Console \Util \Helper ;
19
18
use Inhere \Console \Util \Show ;
20
19
use RuntimeException ;
21
- use UnexpectedValueException ;
20
+ use Toolkit \ Stdlib \ Str ;
22
21
use function basename ;
23
22
use function file_exists ;
24
23
use function is_dir ;
@@ -58,70 +57,78 @@ protected function init(): void
58
57
* default is current work-dir.(<cyan>{workDir}</cyan>)
59
58
* -c, --config STRING Use the custom config file for build phar(<cyan>./phar.build.inc</cyan>)
60
59
* -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
63
64
*
64
- * @param Input $in
65
- * @param Output $out
65
+ * @param Input $input
66
+ * @param Output $output
66
67
*
67
68
* @return int
68
- * @throws UnexpectedValueException
69
- * @throws RuntimeException
70
- * @throws BadMethodCallException
71
69
* @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
72
79
*/
73
- public function packCommand ($ in , $ out ): int
80
+ public function packCommand ($ input , $ output ): int
74
81
{
75
- $ time = microtime (1 );
76
- $ workDir = $ in ->getPwd ();
82
+ $ startAt = microtime (1 );
83
+ $ workDir = $ input ->getPwd ();
77
84
78
- $ dir = $ in ->getOpt ('dir ' ) ?: $ workDir ;
85
+ $ dir = $ input ->getOpt ('dir ' ) ?: $ workDir ;
79
86
$ cpr = $ this ->configCompiler ($ dir );
80
87
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 ' );
84
97
85
98
// use fast build
86
99
if ($ this ->input ->boolOpt ('fast ' )) {
87
100
$ cpr ->setModifies ($ cpr ->findChangedByGit ());
88
101
$ this ->output ->liteNote ('Use fast build, will only pack changed or new files(from git status) ' );
89
102
}
90
103
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
+ }
92
109
93
- $ out ->info ('Pack file to Phar: ' );
94
110
$ 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 );
96
115
});
97
116
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 );
108
119
109
120
// packing ...
110
121
$ cpr ->pack ($ pharFile , $ refresh );
111
122
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> ' ,
122
125
' - 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 );
125
132
126
133
return 0 ;
127
134
}
@@ -147,14 +154,48 @@ protected function configCompiler(string $dir): PharCompiler
147
154
$ configFile = $ this ->input ->getSameOpt (['c ' , 'config ' ]) ?: $ dir . '/phar.build.inc ' ;
148
155
149
156
if ($ configFile && is_file ($ configFile )) {
157
+ /** @noinspection PhpIncludeInspection */
150
158
require $ configFile ;
151
-
152
159
return $ compiler ->in ($ dir );
153
160
}
154
161
155
162
throw new RuntimeException ("The phar build config file not exists! File: $ configFile " );
156
163
}
157
164
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
+
158
199
/**
159
200
* @param Closure $compilerConfiger
160
201
*/
0 commit comments