Skip to content

Commit 4eb424f

Browse files
committed
Reduce lines
1 parent 3f172d5 commit 4eb424f

File tree

4 files changed

+181
-296
lines changed

4 files changed

+181
-296
lines changed

README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Single file console framework to help you write scripts quickly, **v2.0 requires
4343
* [Run Sub-Process](#run-sub-process)
4444
* [Hide Command Name](#hide-command-name)
4545
* [Custom Output](#custom-output)
46+
* [Disable the Output](#disable-the-output)
47+
* [Override `exec()`](#override-exec)
4648
* [Delegating Multiple Tasks](#delegating-multiple-tasks)
4749
* [Contributing and PR is Welcome](#contributing-and-pr-is-welcome)
4850
<!-- TOC -->
@@ -806,11 +808,16 @@ Use `exec()` to run a sub-process, it will instantly print the output and return
806808
$app->exec('ls');
807809
$app->exec('git status');
808810
$app->exec('git commit ...');
809-
$code = $app->exec('git push');
811+
$result = $app->exec('git push');
810812

811-
if ($code !== 0) {
813+
// All output will instantly print to STDOUT
814+
815+
if ($result->code !== 0) {
812816
// Failure
813817
}
818+
819+
$result->code; // 0 is success
820+
$result->success; // BOOL
814821
```
815822

816823
Use `mustExec()` to make sure a sub-process should run success, otherwise it will throw an exception.
@@ -853,23 +860,50 @@ $log = '';
853860

854861
$app->exec(
855862
'cmd ...',
856-
function (string $data, bool $err) use ($app, &$log) {
863+
output: function (string $data, bool $err) use ($app, &$log) {
857864
$app->write($data, $err);
858865

859866
$log .= $data;
860867
}
861868
);
862869
```
863870

871+
### Disable the Output
872+
873+
Use `false` to disable the output, you can get full output from result object after sub-process finished.
874+
875+
Note, the output will only write to result object if `output` set to `false`. If you set `output` as closure or
876+
keep default `NULL`, the output will be empty in result object.
877+
878+
```php
879+
$result = $app->exec('cmd ...', output: false);
880+
881+
$result->output; // StdOutput of sub-process
882+
$result->errOutput; // StdErr Output of sub-process
883+
884+
885+
// Below will not write to the result object
886+
$result = $app->exec('cmd ...');
887+
// OR
888+
$result = $app->exec('cmd ...', output: function () { ... });
889+
890+
$result->output; // Empty
891+
$result->errOutput; // Empty
892+
```
893+
894+
### Override `exec()`
895+
864896
By now, running sub-process by `prop_open()` is in BETA, if `prop_open()` not work for your environment, simply override
865897
`exec()` to use PHP `system()` instead.
866898

867899
```php
868-
public function exec(string $cmd, \Closure|null $output = null, bool $showCmd = true): int
900+
public function exec(string $cmd, \Closure|null $output = null, bool $showCmd = true): ExecResult
869901
{
870-
$this->writeln('>> ' . $cmd);
902+
!$showCmd || $this->writeln('>> ' . $cmd);
903+
904+
$returnLine = system($cmd, $code);
871905

872-
return system($cmd);
906+
return new \Asika\SimpleConsole\ExecResult($code, $returnLine, $returnLine);
873907
}
874908
```
875909

bin/release.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App;
66

77
use Asika\SimpleConsole\Console;
8+
use Asika\SimpleConsole\ExecResult;
89

910
require __DIR__ . '/../vendor/autoload.php';
1011

@@ -162,15 +163,15 @@ public static function parseVersion(string $currentVersion): array
162163
return [$major, $minor, $patch, $prereleaseType, $prereleaseVersion];
163164
}
164165

165-
public function exec(string $cmd, ?\Closure $output = null, bool $showCmd = true): int
166+
public function exec(string $cmd, \Closure|null|false $output = null, bool $showCmd = true): ExecResult
166167
{
167168
$this->writeln('>> ' . ($this->isDryRun ? '(Dry Run) ' : '') . $cmd);
168169

169170
if (!$this->isDryRun) {
170171
return parent::exec($cmd, $output, false);
171172
}
172173

173-
return 0;
174+
return new ExecResult();
174175
}
175176

176177
public function addScript(string $script): static

0 commit comments

Comments
 (0)