-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbuild-fancy.php
More file actions
333 lines (315 loc) · 12.5 KB
/
Copy pathbuild-fancy.php
File metadata and controls
333 lines (315 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/php
<?php
if (php_sapi_name() !== 'cli') {
exit(0);
}
// --- Terminal styling helpers ---
class TerminalStyle
{
public const RESET = "\033[0m";
public const BOLD = "\033[1m";
public const UNDERLINE = "\033[4m";
public const GREEN = "\033[32m";
public const RED = "\033[31m";
public const YELLOW = "\033[33m";
public const CYAN = "\033[36m";
public const GRAY = "\033[90m";
/**
* Apply color to text
* @param string $text
* @param string $color
* @return string
*/
public static function color(string|BuildCommand $text, string $color): string
{
if (is_a($text, BuildCommand::class)) {
$text = $text->value;
}
return $color . $text . self::RESET;
}
/**
* Apply bold style to text
*
* @param string $text
* @return string
*/
public static function bold(string|BuildCommand $text): string
{
if (is_a($text, BuildCommand::class)) {
$text = $text->value;
}
return self::BOLD . $text . self::RESET;
}
/**
* Apply underline style to text
*
* @param string $text
* @return string
*/
public static function underline(string|BuildCommand $text): string
{
if (is_a($text, BuildCommand::class)) {
$text = $text->value;
}
return self::UNDERLINE . $text . self::RESET;
}
/**
* Generate a separator line
*
* @return string
*/
public static function sep(): string
{
return self::color(str_repeat('─', 100), self::GRAY);
}
}
// --- Enums for flags and build commands ---
enum Flag: string
{
case NoComposer = '--no-composer';
case Cleanup = '--cleanup';
case InstallNpm = '--install-npm';
case Release = '--release';
case DryRun = '--dry-run';
}
enum BuildCommand: string
{
case ComposerInstall = 'composer install --prefer-dist --no-progress --no-dev';
case ComposerDumpAutoload = 'composer dump-autoload';
case NpmCi = 'npm ci --no-progress --no-audit';
case NpmInstall = 'npm install --no-progress --no-audit';
case NpmRunBuild = 'npm run build';
case RemoveDist = 'rm -rf ./dist';
}
// --- Argument parser ---
class ArgvParser {
public array $flags = [];
public function __construct(private array $argv) {
foreach ($argv as $arg) {
foreach (Flag::cases() as $flag) {
if ($arg === $flag->value) {
$this->flags[$flag->name] = $flag;
}
}
}
}
public function has(Flag $flag): bool {
return isset($this->flags[$flag->name]);
}
}
// --- Build step ---
class BuildStep {
public function __construct(
public string $command,
public string $description,
public ?string $meta = null
) {}
public function run(string $dirName): int {
print TerminalStyle::sep() . PHP_EOL;
print TerminalStyle::bold(TerminalStyle::color("➤ Running: '{$this->command}'", TerminalStyle::CYAN)) . " for $dirName\n";
$timeStart = microtime(true);
$exitCode = ShellExecutor::run($this->command);
$buildTime = round(microtime(true) - $timeStart);
if ($exitCode === 0) {
print TerminalStyle::color("✔ Success", TerminalStyle::GREEN) . " ";
} else {
print TerminalStyle::color("✖ Failed", TerminalStyle::RED) . " ";
}
print TerminalStyle::color("({$buildTime}s)", TerminalStyle::YELLOW) . PHP_EOL;
print TerminalStyle::sep() . PHP_EOL . PHP_EOL;
return $exitCode;
}
}
// --- Shell executor ---
class ShellExecutor {
public static function run(string $command): int {
$fullCommand = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
? "cmd /v:on /c \"$command 2>&1 & echo Exit status : !ErrorLevel!\""
: "$command 2>&1 ; echo Exit status : $?";
$proc = popen($fullCommand, 'r');
$completeOutput = '';
while (!feof($proc)) {
$liveOutput = fread($proc, 4096);
$completeOutput .= $liveOutput;
print $liveOutput;
@flush();
}
pclose($proc);
preg_match('/[0-9]+$/', $completeOutput, $matches);
return intval($matches[0]);
}
}
// --- Cleaner ---
class Cleaner {
public function __construct(private string $dirName) {}
public function preview(): void {
$distignorePath = './.distignore';
$removables = [];
if (file_exists($distignorePath)) {
$removables = array_filter(array_map('trim', file($distignorePath)));
}
print TerminalStyle::sep() . PHP_EOL;
print TerminalStyle::bold(TerminalStyle::color("Planned files to remove:", TerminalStyle::YELLOW)) . PHP_EOL;
foreach ($removables as $removable) {
print TerminalStyle::color(" $removable", TerminalStyle::GRAY) . PHP_EOL;
}
print TerminalStyle::sep() . PHP_EOL;
}
public function clean(): void {
$distignorePath = './.distignore';
$removables = [];
if (file_exists($distignorePath)) {
$removables = array_filter(array_map('trim', file($distignorePath)));
}
print TerminalStyle::sep() . PHP_EOL;
print TerminalStyle::bold(TerminalStyle::color("🧹 Cleanup started...", TerminalStyle::YELLOW)) . PHP_EOL;
foreach ($removables as $removable) {
if (file_exists($removable)) {
print TerminalStyle::color("Removing $removable from {$this->dirName}", TerminalStyle::GRAY) . PHP_EOL;
shell_exec("rm -rf $removable");
}
}
print TerminalStyle::bold(TerminalStyle::color("🧹 Cleanup finished.", TerminalStyle::GREEN)) . PHP_EOL;
print TerminalStyle::sep() . PHP_EOL;
}
}
// --- Build runner ---
class BuildRunner {
private array $steps = [];
public function __construct(private ArgvParser $args) {}
private function maybeConvertEnum($value): string {
// PHP 8.1+ enum detection
if (is_object($value) && enum_exists(get_class($value)) && property_exists($value, 'value')) {
return $value->value;
}
return (string)$value;
}
/**
* Prepare build steps
*/
public function prepareSteps(): void {
// Composer
if (file_exists('composer.json')) {
if (!$this->args->has(Flag::NoComposer)) {
$this->steps[] = new BuildStep(
is_object(BuildCommand::ComposerInstall) && enum_exists(get_class(BuildCommand::ComposerInstall)) ? BuildCommand::ComposerInstall->value : (string)BuildCommand::ComposerInstall,
'Composer install'
);
}
$this->steps[] = new BuildStep(
is_object(BuildCommand::ComposerDumpAutoload) && enum_exists(get_class(BuildCommand::ComposerDumpAutoload)) ? BuildCommand::ComposerDumpAutoload->value : (string)BuildCommand::ComposerDumpAutoload,
'Composer dump-autoload'
);
}
// NPM
if (file_exists('package.json')) {
$npmPackage = json_decode(file_get_contents('package.json'));
if (file_exists('package-lock.json')) {
if (!$this->args->has(Flag::InstallNpm)) {
$this->steps[] = new BuildStep(
is_object(BuildCommand::NpmCi) && enum_exists(get_class(BuildCommand::NpmCi)) ? BuildCommand::NpmCi->value : (string)BuildCommand::NpmCi,
'Install NPM packages (ci)'
);
$this->steps[] = new BuildStep(
is_object(BuildCommand::NpmRunBuild) && enum_exists(get_class(BuildCommand::NpmRunBuild)) ? BuildCommand::NpmRunBuild->value : (string)BuildCommand::NpmRunBuild,
'Build NPM packages'
);
} else {
$this->steps[] = new BuildStep("npm install $npmPackage->name", 'NPM install package: ' . $npmPackage->name);
$this->steps[] = new BuildStep(
is_object(BuildCommand::RemoveDist) && enum_exists(get_class(BuildCommand::RemoveDist)) ? BuildCommand::RemoveDist->value : (string)BuildCommand::RemoveDist,
'Remove dist folder'
);
$this->steps[] = new BuildStep("mv node_modules/$npmPackage->name/dist ./", 'Move dist folder');
}
} else {
if (!$this->args->has(Flag::InstallNpm)) {
$this->steps[] = new BuildStep(
is_object(BuildCommand::NpmInstall) && enum_exists(get_class(BuildCommand::NpmInstall)) ? BuildCommand::NpmInstall->value : (string)BuildCommand::NpmInstall,
'Install NPM packages'
);
$this->steps[] = new BuildStep(
is_object(BuildCommand::NpmRunBuild) && enum_exists(get_class(BuildCommand::NpmRunBuild)) ? BuildCommand::NpmRunBuild->value : (string)BuildCommand::NpmRunBuild,
'Build NPM packages'
);
} else {
$this->steps[] = new BuildStep("npm install $npmPackage->name", 'NPM install package: ' . $npmPackage->name);
$this->steps[] = new BuildStep(
is_object(BuildCommand::RemoveDist) && enum_exists(get_class(BuildCommand::RemoveDist)) ? BuildCommand::RemoveDist->value : (string)BuildCommand::RemoveDist,
'Remove dist folder'
);
$this->steps[] = new BuildStep("mv node_modules/$npmPackage->name/dist ./", 'Move dist folder');
}
}
}
// Cleanup step
if ($this->args->has(Flag::Cleanup)) {
$distignorePath = './.distignore';
$removables = [];
if (file_exists($distignorePath)) {
$removables = array_filter(array_map('trim', file($distignorePath)));
}
$desc = $removables ? "Remove files" : "Remove files (none listed)";
$meta = $removables ? ("\n" . implode(", ", array_map(fn($f) => "$f", $removables))) : null;
$this->steps[] = new BuildStep('cleanup', $desc, $meta);
}
}
/**
* Print planned build steps as a table, including meta info for each step.
*/
public function printSteps(): void {
print TerminalStyle::sep() . PHP_EOL;
print TerminalStyle::bold(TerminalStyle::color("PLANNED BUILD STEPS:", TerminalStyle::UNDERLINE)) . PHP_EOL;
print TerminalStyle::sep() . PHP_EOL;
$numColWidth = 4;
$cmdColWidth = 18;
$descColWidth = 32;
printf(
"%s %s %s\n",
TerminalStyle::bold(str_pad("#", $numColWidth)),
TerminalStyle::bold(str_pad("Command", $cmdColWidth)),
TerminalStyle::bold(str_pad("Description", $descColWidth))
);
print TerminalStyle::sep() . PHP_EOL;
foreach ($this->steps as $i => $step) {
printf(
"%s %s %s\n",
TerminalStyle::color(str_pad(($i + 1) . ".", $numColWidth), TerminalStyle::CYAN),
TerminalStyle::bold(str_pad($step->command, $cmdColWidth)),
$step->description
);
if ($step->meta) {
echo str_pad("", $numColWidth);
print TerminalStyle::color(" Files:", TerminalStyle::YELLOW) . PHP_EOL;
print TerminalStyle::color($step->meta, TerminalStyle::GRAY) . PHP_EOL;
}
}
print TerminalStyle::sep() . PHP_EOL;
}
public function run(): void {
$dirName = basename(dirname(__FILE__));
foreach ($this->steps as $step) {
$exitCode = $step->run($dirName);
if ($exitCode > 0) {
exit($exitCode);
}
}
}
}
// --- Main Entrypoint ---
function main(array $argv) {
$args = new ArgvParser($argv);
$runner = new BuildRunner($args);
$runner->prepareSteps();
$runner->printSteps();
$dirName = basename(dirname(__FILE__));
if ($args->has(Flag::Cleanup) && !$args->has(Flag::DryRun)) {
(new Cleaner($dirName))->clean();
}
if ($args->has(Flag::DryRun)) {
print TerminalStyle::bold(TerminalStyle::color("Dry run: No commands will be executed.", TerminalStyle::YELLOW)) . PHP_EOL;
exit(0);
}
$runner->run();
}
main($argv);