Skip to content

Commit ab619ac

Browse files
committed
some update for process util, phar compiler can only pack changed files
1 parent 220cb12 commit ab619ac

File tree

4 files changed

+107
-24
lines changed

4 files changed

+107
-24
lines changed

src/Components/PharCompiler.php

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ class PharCompiler
103103
*/
104104
private $directories = [];
105105

106+
/**
107+
* @var array|\Iterator The modifies files list. if not empty, will skip find dirs.
108+
*/
109+
private $modifies;
110+
106111
/**
107112
* @var \Closure[] Some events. if you want to get some info on packing.
108113
*/
@@ -278,6 +283,17 @@ public function in($dirs): self
278283
return $this;
279284
}
280285

286+
/**
287+
* @param array|\Iterator $modifies
288+
* @return PharCompiler
289+
*/
290+
public function setModifies($modifies): self
291+
{
292+
$this->modifies = $modifies;
293+
294+
return $this;
295+
}
296+
281297
/**
282298
* Compiles composer into a single phar file
283299
* @param string $pharFile The full path to the file to create
@@ -290,7 +306,9 @@ public function pack(string $pharFile, $refresh = true): string
290306
throw new \RuntimeException("Please setting the 'directories' want building directories by 'in()'");
291307
}
292308

293-
if ($refresh && file_exists($pharFile)) {
309+
$exists = file_exists($pharFile);
310+
311+
if ($refresh && $exists) {
294312
unlink($pharFile);
295313
}
296314

@@ -315,10 +333,19 @@ public function pack(string $pharFile, $refresh = true): string
315333
$basePath = $this->basePath;
316334
$phar->startBuffering();
317335

318-
// collect files
319-
foreach ($this->directories as $directory) {
320-
foreach ($this->findFiles($directory) as $file) {
321-
$this->packFile($phar, $file);
336+
// only build modifies
337+
if (!$refresh && $exists && $this->modifies) {
338+
foreach ($this->modifies as $file) {
339+
if ('/' === $file[0] || is_file($file = $basePath . '/' . $file)) {
340+
$this->packFile($phar, new \SplFileInfo($file));
341+
}
342+
}
343+
} else {
344+
// collect files in there are dirs.
345+
foreach ($this->directories as $directory) {
346+
foreach ($this->findFiles($directory) as $file) {
347+
$this->packFile($phar, $file);
348+
}
322349
}
323350
}
324351

@@ -353,6 +380,36 @@ public function pack(string $pharFile, $refresh = true): string
353380
return $pharFile;
354381
}
355382

383+
/**
384+
* find changed or new created files by git status.
385+
* @return \Generator
386+
*/
387+
public function findChangedByGit()
388+
{
389+
list(, $output, ) = ProcessUtil::run('git status -s', $this->basePath);
390+
391+
// 'D some.file' deleted
392+
// ' M some.file' modified
393+
// '?? some.file' new file
394+
foreach (explode("\n", trim($output)) as $file) {
395+
$file = trim($file);
396+
397+
// only php file.
398+
if (!strpos($file, '.php')) {
399+
continue;
400+
}
401+
402+
// modified files
403+
if (strpos($file, 'M ') === 0) {
404+
yield substr($file, 2);
405+
406+
// new files
407+
} elseif (strpos($file,'?? ') === 0) {
408+
yield substr($file, 3);
409+
}
410+
}
411+
}
412+
356413
/**
357414
* @param string $directory
358415
* @return \Iterator|\SplFileInfo[]
@@ -819,5 +876,4 @@ public function getPharFile(): string
819876
{
820877
return $this->pharFile;
821878
}
822-
823879
}

src/Components/Style/Style.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ protected function loadDefaultStyles()
142142
->add('blue', ['fg' => 'blue'])
143143
->add('cyan', ['fg' => 'cyan'])
144144
->add('magenta', ['fg' => 'magenta'])
145+
->add('mga', ['fg' => 'magenta'])
145146
->add('red', ['fg' => 'red'])
146147
->add('darkGray', ['fg' => 'black', 'extra' => true])
147148
->add('yellow', ['fg' => 'yellow']);

src/Utils/CliUtil.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ public static function getNullDevice(): string
5252
return 'NUL';
5353
}
5454

55+
/**
56+
* @return string
57+
*/
58+
public static function getOutsideIP(): string
59+
{
60+
list($code, $output) = ProcessUtil::run('ip addr | grep eth0');
61+
62+
if ($code === 0 && $output && preg_match('#inet (.*)\/#', $output, $ms)) {
63+
return $ms[1];
64+
}
65+
66+
return 'unknown';
67+
}
68+
5569
/**
5670
* @param string $command
5771
* @param null|string $logfile

src/Utils/ProcessUtil.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ class ProcessUtil
2626
/**
2727
* @return bool
2828
*/
29-
public static function isSupported(): bool
29+
public static function pcntlIsEnabled(): bool
3030
{
3131
return !Helper::isWindows() && \function_exists('pcntl_fork');
3232
}
3333

3434
/**
3535
* @return bool
3636
*/
37-
public static function signalIsEnabled(): bool
37+
public static function posixIsEnabled(): bool
3838
{
39-
return self::isSupported();
39+
return !Helper::isWindows() && \function_exists('posix_kill');
4040
}
4141

4242
/**
@@ -72,7 +72,7 @@ public static function run(string $command, string $cwd = null): array
7272
// TODO: Write passphrase in pipes[3].
7373
fclose($pipes[3]);
7474

75-
// Close all pipes before proc_close!
75+
// Close all pipes before proc_close! $code === 0 is success.
7676
$code = proc_close($process);
7777

7878
return [$code, $output, $error];
@@ -85,7 +85,7 @@ public static function run(string $command, string $cwd = null): array
8585
*/
8686
public static function daemonRun(\Closure $beforeQuit = null): int
8787
{
88-
if (!self::isSupported()) {
88+
if (!self::pcntlIsEnabled()) {
8989
return 0;
9090
}
9191

@@ -160,7 +160,7 @@ public static function forks(int $number, callable $onStart = null, callable $on
160160
return false;
161161
}
162162

163-
if (!self::isSupported()) {
163+
if (!self::pcntlIsEnabled()) {
164164
return false;
165165
}
166166

@@ -195,7 +195,7 @@ public static function create(callable $onStart = null, callable $onError = null
195195
*/
196196
public static function fork(callable $onStart = null, callable $onError = null, $id = 0)
197197
{
198-
if (!self::isSupported()) {
198+
if (!self::pcntlIsEnabled()) {
199199
return false;
200200
}
201201

@@ -233,7 +233,7 @@ public static function fork(callable $onStart = null, callable $onError = null,
233233
*/
234234
public static function wait(callable $onExit): bool
235235
{
236-
if (!self::isSupported()) {
236+
if (!self::pcntlIsEnabled()) {
237237
return false;
238238
}
239239

@@ -281,7 +281,7 @@ public static function stopWorkers(array $children, int $signal = SIGTERM, array
281281
return false;
282282
}
283283

284-
if (!self::isSupported()) {
284+
if (!self::pcntlIsEnabled()) {
285285
return false;
286286
}
287287

@@ -317,27 +317,26 @@ public static function stopWorkers(array $children, int $signal = SIGTERM, array
317317
* @param int $timeout
318318
* @return bool
319319
*/
320-
public static function kill(int $pid, $force = false, int $timeout = 3): bool
320+
public static function kill(int $pid, bool $force = false, int $timeout = 3): bool
321321
{
322322
return self::sendSignal($pid, $force ? SIGKILL : SIGTERM, $timeout);
323323
}
324324

325325
/**
326326
* Do shutdown process and wait it exit.
327-
* @param int $pid Master Pid
328-
* @param int $signal
327+
* @param int $pid Master Pid
328+
* @param bool $force
329329
* @param int $waitTime
330330
* @param null $error
331331
* @param string $name
332332
* @return bool
333333
*/
334334
public static function killAndWait(
335-
int $pid, int $signal = SIGTERM, int $waitTime = 10, &$error = null, $name = 'process'
335+
int $pid, &$error = null, $name = 'process', bool $force = false, int $waitTime = 10
336336
): bool
337337
{
338-
// $opts = array_merge([], $opts);
339338
// do stop
340-
if (!self::kill($signal)) {
339+
if (!self::kill($pid, $force)) {
341340
$error = "Send stop signal to the $name(PID:$pid) failed!";
342341

343342
return false;
@@ -372,7 +371,7 @@ public static function killAndWait(
372371
return false;
373372
}
374373

375-
Show::write(' OK');
374+
Show::color(' OK');
376375
return true;
377376
}
378377

@@ -421,7 +420,7 @@ public static function quit($code = 0)
421420
*/
422421
public static function sendSignal(int $pid, int $signal, int $timeout = 0): bool
423422
{
424-
if ($pid <= 0 || !self::isSupported()) {
423+
if ($pid <= 0 || !self::posixIsEnabled()) {
425424
return false;
426425
}
427426

@@ -467,6 +466,10 @@ public static function sendSignal(int $pid, int $signal, int $timeout = 0): bool
467466
*/
468467
public static function installSignal($signal, callable $handler): bool
469468
{
469+
if (!self::pcntlIsEnabled()) {
470+
return false;
471+
}
472+
470473
return pcntl_signal($signal, $handler, false);
471474
}
472475

@@ -476,6 +479,10 @@ public static function installSignal($signal, callable $handler): bool
476479
*/
477480
public static function dispatchSignal(): bool
478481
{
482+
if (!self::pcntlIsEnabled()) {
483+
return false;
484+
}
485+
479486
// receive and dispatch sig
480487
return pcntl_signal_dispatch();
481488
}
@@ -527,9 +534,14 @@ public static function getCurrentUser(): array
527534
/**
528535
* @param int $seconds
529536
* @param callable $handler
537+
* @return bool|int
530538
*/
531539
public static function afterDo(int $seconds, callable $handler)
532540
{
541+
if (!self::pcntlIsEnabled()) {
542+
return false;
543+
}
544+
533545
/* self::signal(SIGALRM, function () {
534546
static $i = 0;
535547
echo "#{$i}\talarm\n";
@@ -541,7 +553,7 @@ public static function afterDo(int $seconds, callable $handler)
541553
self::installSignal(SIGALRM, $handler);
542554

543555
// self::alarm($seconds);
544-
pcntl_alarm($seconds);
556+
return pcntl_alarm($seconds);
545557
}
546558

547559
/**

0 commit comments

Comments
 (0)