From cf89ca76a07d6e2986bad396761f60b9a38d30ab Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 20 Feb 2024 08:45:00 +0000 Subject: [PATCH] Extract a Process class that wraps Symfony's --- src/Process/Process.php | 26 ++++++++++++++++++++++++++ versa | 29 ++++++++--------------------- 2 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 src/Process/Process.php diff --git a/src/Process/Process.php b/src/Process/Process.php new file mode 100644 index 0000000..195d2c8 --- /dev/null +++ b/src/Process/Process.php @@ -0,0 +1,26 @@ + $command + * @param string $workingDir + * @param ?string $extraArgs + */ +final class Process +{ + public static function create(array $command, string $workingDir, ?string $extraArgs = null): SymfonyProcess + { + $process = new SymfonyProcess(command: array_filter([...$command, $extraArgs])); + $process->setTty(true); + $process->setWorkingDirectory($workingDir); + + return $process; + } +} diff --git a/versa b/versa index 7ce555b..d358d5f 100755 --- a/versa +++ b/versa @@ -4,11 +4,12 @@ require __DIR__.'/vendor/autoload.php'; use App\Enum\ProjectType; +use App\Process\Process; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\SingleCommandApplication; use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Process\Process; +use Symfony\Component\Process\Process as SymfonyProcess; $application = new SingleCommandApplication(); @@ -41,20 +42,6 @@ $application->addOption( default: '.', ); -/** - * @param non-empty-array $command - * @param string $workingDir - * @param ?string $extraArgs - */ -function buildProcess(array $command, string $workingDir, ?string $extraArgs = null): Process -{ - $process = new Process(command: array_filter([...$command, $extraArgs])); - $process->setTty(true); - $process->setWorkingDirectory($workingDir); - - return $process; -} - $application->setCode(function (InputInterface $input): int { $extraArgs = $input->getOption('extra-args'); $workingDir = $input->getOption('working-dir'); @@ -69,7 +56,7 @@ $application->setCode(function (InputInterface $input): int { switch ($input->getOption('type')) { case ProjectType::Drupal->value: if ($isDockerCompose) { - $process = buildProcess( + $process = Process::create( command: ['docker', 'compose', 'build'], extraArgs: $extraArgs, workingDir: $workingDir, @@ -80,7 +67,7 @@ $application->setCode(function (InputInterface $input): int { } switch ($input->getOption('type')) { case ProjectType::Sculpin->value: - $process = buildProcess( + $process = Process::create( command: ['./vendor/bin/sculpin', 'generate'], extraArgs: $extraArgs, workingDir: $workingDir, @@ -92,7 +79,7 @@ $application->setCode(function (InputInterface $input): int { case 'install': // TODO: Composer in Docker Compose? - $process = buildProcess( + $process = Process::create( command: ['composer', 'install'], extraArgs: $extraArgs, workingDir: $workingDir, @@ -102,7 +89,7 @@ $application->setCode(function (InputInterface $input): int { case 'run': if ($isDockerCompose) { - $process = buildProcess( + $process = Process::create( command: ['docker', 'compose', 'up'], extraArgs: $extraArgs, workingDir: $workingDir, @@ -112,7 +99,7 @@ $application->setCode(function (InputInterface $input): int { } else { switch ($input->getOption('type')) { case ProjectType::Sculpin->value: - $process = buildProcess( + $process = Process::create( command: ['./vendor/bin/sculpin', 'generate', '--server', '--watch'], extraArgs: $extraArgs, workingDir: $workingDir, @@ -127,7 +114,7 @@ $application->setCode(function (InputInterface $input): int { case 'test': // TODO: PHPUnit, Pest or ParaTest. // TODO: commands in Docker Compose? - $process = buildProcess( + $process = Process::create( command: ['./vendor/bin/phpunit'], extraArgs: $extraArgs, workingDir: $workingDir,