Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/App/Command/ExecCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\Process;
use Yiisoft\YiiDevTool\App\Component\Console\PackageCommand;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Package\Package;

#[AsCommand(
Expand Down Expand Up @@ -66,17 +67,7 @@ protected function processPackage(Package $package): void
* We want to receive the output of the program in real time,
* so we pass a callback that will read the data as it comes in.
*/
->run(function ($type, $data) use ($io) {
/**
* We do not split data into output streams by data type,
* because many programs write non-error messages to the error stream.
*
* We write everything to one regular output stream.
*/
$io
->important()
->write($data);
});
->run(ProcessOutput::callback($io));

/**
* End each program block with a new line so that
Expand Down
6 changes: 4 additions & 2 deletions src/App/Command/Git/CheckoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Yiisoft\YiiDevTool\App\Component\Console\PackageCommand;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Package\Package;

#[AsCommand(
Expand Down Expand Up @@ -48,11 +49,12 @@ protected function processPackage(Package $package): void
->getBranches()
->all();
$branchExists = in_array($this->branch, $branches, true);
$callback = ProcessOutput::callback($io);

if ($branchExists) {
$gitWorkingCopy->checkout($this->branch);
$gitWorkingCopy->runWithOutput('checkout', [$this->branch], $callback);
} else {
$gitWorkingCopy->checkoutNewBranch($this->branch);
$gitWorkingCopy->runWithOutput('checkout', [['b' => true], $this->branch], $callback);
}

$io->done();
Expand Down
9 changes: 2 additions & 7 deletions src/App/Command/Git/CommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\Process;
use Yiisoft\YiiDevTool\App\Component\Console\PackageCommand;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Package\Package;

#[AsCommand(
Expand Down Expand Up @@ -60,19 +61,13 @@ protected function processPackage(Package $package): void
}

$process = new Process(['git', 'commit', '-m', $this->message], $package->getPath());
$process->run();
ProcessOutput::run($process, $io);

if ($process->isSuccessful()) {
$io
->important()
->info($process->getOutput() . $process->getErrorOutput());
$io->done();
} else {
$output = $process->getErrorOutput();

$io
->important()
->info($output);
$io->error([
"An error occurred during committing package <package>{$package->getId()}</package> repository.",
'Package committing aborted.',
Expand Down
14 changes: 3 additions & 11 deletions src/App/Command/Git/PullCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\Process;
use Yiisoft\YiiDevTool\App\Component\Console\PackageCommand;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Package\Package;

#[AsCommand(
Expand All @@ -33,23 +34,14 @@ protected function processPackage(Package $package): void
$io->preparePackageHeader($package, 'Pulling package {package}');

$process = new Process(['git', 'pull'], $package->getPath());
$process
->setTimeout(null)
->run();
$process->setTimeout(null);
ProcessOutput::run($process, $io);

if ($process->isSuccessful()) {
$output = $process->getOutput() . $process->getErrorOutput();

$io
->important(trim($output) !== 'Already up to date.')
->info($output);
$io->done();
} else {
$output = $process->getErrorOutput();

$io
->important()
->info($output);
$io->error([
"An error occurred during pulling package <package>{$package->getId()}</package> repository.",
'Package pull aborted.',
Expand Down
6 changes: 4 additions & 2 deletions src/App/Command/Git/PushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Symfony\Component\Console\Attribute\AsCommand;
use Yiisoft\YiiDevTool\App\Component\Console\PackageCommand;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Git\GitException;
use Yiisoft\YiiDevTool\App\Component\Package\Package;

Expand Down Expand Up @@ -38,11 +39,12 @@ protected function processPackage(Package $package): void
$currentBranch = $gitWorkingCopy
->getBranches()
->head();
$callback = ProcessOutput::callback($io);

if ($currentBranch === 'master') {
$gitWorkingCopy->push();
$gitWorkingCopy->runWithOutput('push', [], $callback);
} else {
$gitWorkingCopy->push('origin', $currentBranch);
$gitWorkingCopy->runWithOutput('push', ['origin', $currentBranch], $callback);
}

$io->done();
Expand Down
9 changes: 2 additions & 7 deletions src/App/Command/Git/RequestPullCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;
use Yiisoft\YiiDevTool\App\Component\Console\PackageCommand;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Package\Package;

#[AsCommand(
Expand Down Expand Up @@ -71,12 +72,9 @@ protected function processPackage(Package $package): void
}

$process = new Process($processParameters, $package->getPath());
$process->run();
ProcessOutput::run($process, $io);

if ($process->isSuccessful()) {
$io
->important()
->info($process->getOutput() . $process->getErrorOutput());
$io->done();

return;
Expand All @@ -93,9 +91,6 @@ protected function processPackage(Package $package): void

$output = $process->getErrorOutput();

$io
->important()
->info($output);
$io->error([
"An error occurred during creating PR for package <package>{$package->getId()}</package> repository.",
'Creating PR aborted.',
Expand Down
9 changes: 3 additions & 6 deletions src/App/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Symfony\Component\Process\Process;
use Yiisoft\YiiDevTool\App\Component\Console\PackageCommand;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Package\Package;

final class LintCommand extends PackageCommand
Expand Down Expand Up @@ -42,13 +43,9 @@ protected function processPackage(Package $package): void
'--ignore=*/vendor/*,*/docs/*',
], $this->getAppRootDir());

$process->run();
ProcessOutput::run($process, $io);

if ($process->getExitCode() > 0) {
$io
->important()
->info($process->getOutput() . $process->getErrorOutput());
} else {
if ($process->getExitCode() === 0) {
$io->success('✔ No problems found.');
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/App/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Console\PackageCommand;
use Yiisoft\YiiDevTool\App\Component\Package\Package;

Expand Down Expand Up @@ -44,7 +45,7 @@ protected function processPackage(Package $package): void
'test',
], $package->getPath());
$process->setTimeout(20);
$process->run();
ProcessOutput::run($process, $io);

if (!$process->isSuccessful() && $this->isComposerTestNotImplemented($process)) {
$command = [
Expand All @@ -60,7 +61,7 @@ protected function processPackage(Package $package): void

$process = new Process($command, $package->getPath());
$process->setTimeout(20);
$process->run();
ProcessOutput::run($process, $io);
}

if ($process->getExitCode() === 0) {
Expand All @@ -77,9 +78,6 @@ protected function processPackage(Package $package): void

$output = $process->getErrorOutput();
$this->registerPackageError($package, $output, 'testing package');
$io
->important()
->info($process->getOutput() . $output);
}

private function isComposerTestNotImplemented(Process $process): bool
Expand Down
11 changes: 4 additions & 7 deletions src/App/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Process\Process;
use Yiisoft\YiiDevTool\App\Component\Console\OutputManager;
use Yiisoft\YiiDevTool\App\Component\Console\PackageCommand;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Package\Package;
use Yiisoft\YiiDevTool\App\PackageService;

Expand Down Expand Up @@ -111,18 +112,14 @@ private function gitPull(Package $package, OutputManager $io): void
$process = new Process(['git', 'pull']);
$process->setWorkingDirectory($package->getPath());

$process
->setTimeout(null)
->run();
$process->setTimeout(null);
ProcessOutput::run($process, $io);

if ($process->isSuccessful()) {
$io->info($process->getOutput() . $process->getErrorOutput());
$io->done();
} else {
$output = $process->getErrorOutput();

$io
->important()
->info($output);
$io->error([
'An error occurred during running `git pull`.',
]);
Expand Down
25 changes: 25 additions & 0 deletions src/App/Component/Console/ProcessOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\YiiDevTool\App\Component\Console;

use Closure;
use Symfony\Component\Process\Process;

final class ProcessOutput
{
public static function callback(OutputManager $io): Closure
{
return static function (string $type, string $data) use ($io): void {
$io
->important()
->write($data);
};
}

public static function run(Process $process, OutputManager $io): int
{
return $process->run(self::callback($io));
}
}
18 changes: 17 additions & 1 deletion src/App/Component/Git/GitWorkingCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,29 @@ public function reset(mixed ...$argsOrOptions): string
* @param mixed[] $argsOrOptions
*/
public function run(string $command, array $argsOrOptions = []): string
{
return $this->runCommand($command, $argsOrOptions);
}

/**
* @param mixed[] $argsOrOptions
*/
public function runWithOutput(string $command, array $argsOrOptions, callable $callback): string
{
return $this->runCommand($command, $argsOrOptions, $callback);
}

/**
* @param mixed[] $argsOrOptions
*/
private function runCommand(string $command, array $argsOrOptions = [], ?callable $callback = null): string
{
$process = new Process(
array_merge([$this->gitBinary, $command], $this->buildArguments($argsOrOptions)),
$this->directory
);
$process->setTimeout(null);
$process->run();
$process->run($callback);

if (!$process->isSuccessful()) {
$message = trim($process->getErrorOutput() . $process->getOutput());
Expand Down
21 changes: 7 additions & 14 deletions src/App/PackageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Process\Process;
use Yiisoft\Files\FileHelper;
use Yiisoft\YiiDevTool\App\Component\Console\OutputManager;
use Yiisoft\YiiDevTool\App\Component\Console\ProcessOutput;
use Yiisoft\YiiDevTool\App\Component\Package\Package;
use Yiisoft\YiiDevTool\App\Component\Package\PackageErrorList;
use Yiisoft\YiiDevTool\App\Component\Package\PackageList;
Expand Down Expand Up @@ -66,20 +67,16 @@ public function gitClone(
$io->info("Repository url: <file>{$package->getConfiguredRepositoryUrl()}</file>");

$process = new Process(['git', 'clone', $package->getConfiguredRepositoryUrl(), $package->getPath()]);
$process
->setTimeout(null)
->run();
$process->setTimeout(null);

ProcessOutput::run($process, $io);

if ($process->isSuccessful()) {
$io->info($process->getOutput() . $process->getErrorOutput());
$io->done();
return;
}

$output = $process->getErrorOutput();
$io
->important()
->info($output);

$io->error([
"An error occurred during cloning package <package>{$package->getName()}</package> repository.",
Expand Down Expand Up @@ -192,19 +189,15 @@ private function composerInstallOrUpdate(

$process = new Process($params);

$process
->setTimeout(null)
->run();
$process->setTimeout(null);

ProcessOutput::run($process, $io);

if ($process->isSuccessful()) {
$io->info($process->getOutput() . $process->getErrorOutput());
$io->done();
} else {
$output = $process->getErrorOutput();

$io
->important()
->info($output);
$io->error([
"An error occurred during running `composer $command`.",
"Package $command aborted.",
Expand Down
Loading
Loading