diff --git a/src/Console/Command/AbstractCommand.php b/src/Console/Command/AbstractCommand.php index 44d56da..9784672 100644 --- a/src/Console/Command/AbstractCommand.php +++ b/src/Console/Command/AbstractCommand.php @@ -4,9 +4,15 @@ namespace App\Console\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; abstract class AbstractCommand extends Command { + protected ?string $extraArgs; + + protected string $workingDir; + protected function configure(): void { $this->addOption( @@ -32,4 +38,12 @@ abstract class AbstractCommand extends Command default: '.', ); } + + public function execute(InputInterface $input, OutputInterface $output): int + { + $this->extraArgs = $input->getOption('extra-args'); + $this->workingDir = $input->getOption('working-dir'); + + return Command::SUCCESS; + } } diff --git a/src/Console/Command/BuildCommand.php b/src/Console/Command/BuildCommand.php index 3b29b8c..2d5c4b8 100644 --- a/src/Console/Command/BuildCommand.php +++ b/src/Console/Command/BuildCommand.php @@ -16,17 +16,14 @@ final class BuildCommand extends AbstractCommand { $projectType = null; - $extraArgs = $input->getOption('extra-args'); - $workingDir = $input->getOption('working-dir'); - $filesystem = new Filesystem(); // Attempt to prepopulate some of the options, such as the project type // based on its dependencies. // TODO: move this logic to a service so it can be tested. - if ($filesystem->exists($workingDir.'/composer.json')) { + if ($filesystem->exists($this->workingDir.'/composer.json')) { $json = json_decode( - json: strval(file_get_contents($workingDir.'/composer.json')), + json: strval(file_get_contents($this->workingDir.'/composer.json')), associative: true, ); @@ -45,15 +42,15 @@ final class BuildCommand extends AbstractCommand // the option value if there is one. $projectType = $input->getOption('type') ?? $projectType; - $isDockerCompose = $filesystem->exists($workingDir . '/docker-compose.yaml'); + $isDockerCompose = $filesystem->exists($this->workingDir . '/docker-compose.yaml'); switch ($projectType) { case ProjectType::Drupal->value: if ($isDockerCompose) { $process = Process::create( command: ['docker', 'compose', 'build'], - extraArgs: $extraArgs, - workingDir: $workingDir, + extraArgs: $this->extraArgs, + workingDir: $this->workingDir, ); $process->run(); @@ -67,8 +64,8 @@ final class BuildCommand extends AbstractCommand case ProjectType::Sculpin->value: $process = Process::create( command: ['./vendor/bin/sculpin', 'generate'], - extraArgs: $extraArgs, - workingDir: $workingDir, + extraArgs: $this->extraArgs, + workingDir: $this->workingDir, ); $process->run(); diff --git a/src/Console/Command/InstallCommand.php b/src/Console/Command/InstallCommand.php index 683db48..94606d5 100644 --- a/src/Console/Command/InstallCommand.php +++ b/src/Console/Command/InstallCommand.php @@ -11,14 +11,13 @@ final class InstallCommand extends AbstractCommand { public function execute(InputInterface $input, OutputInterface $output): int { - $extraArgs = $input->getOption('extra-args'); - $workingDir = $input->getOption('working-dir'); + parent::execute($input, $output); // TODO: Composer in Docker Compose? $process = Process::create( command: ['composer', 'install'], - extraArgs: $extraArgs, - workingDir: $workingDir, + extraArgs: $this->extraArgs, + workingDir: $this->workingDir, ); $process->run(); diff --git a/src/Console/Command/RunCommand.php b/src/Console/Command/RunCommand.php index 637e6bd..8faee1c 100644 --- a/src/Console/Command/RunCommand.php +++ b/src/Console/Command/RunCommand.php @@ -13,16 +13,15 @@ final class RunCommand extends AbstractCommand { public function execute(InputInterface $input, OutputInterface $output): int { - $projectType = null; + parent::execute($input, $output); - $extraArgs = $input->getOption('extra-args'); - $workingDir = $input->getOption('working-dir'); + $projectType = null; // Attempt to prepopulate some of the options, such as the project type // based on its dependencies. // TODO: move this logic to a service so it can be tested. $json = json_decode( - json: strval(file_get_contents($workingDir.'/composer.json')), + json: strval(file_get_contents($this->workingDir.'/composer.json')), associative: true, ); @@ -41,13 +40,13 @@ final class RunCommand extends AbstractCommand $projectType = $input->getOption('type') ?? $projectType; $filesystem = new Filesystem(); - $isDockerCompose = $filesystem->exists($workingDir . '/docker-compose.yaml'); + $isDockerCompose = $filesystem->exists($this->workingDir . '/docker-compose.yaml'); if ($isDockerCompose) { $process = Process::create( command: ['docker', 'compose', 'up'], - extraArgs: $extraArgs, - workingDir: $workingDir, + extraArgs: $this->extraArgs, + workingDir: $this->workingDir, ); $process->setTimeout(null); @@ -57,8 +56,8 @@ final class RunCommand extends AbstractCommand case ProjectType::Sculpin->value: $process = Process::create( command: ['./vendor/bin/sculpin', 'generate', '--server', '--watch'], - extraArgs: $extraArgs, - workingDir: $workingDir, + extraArgs: $this->extraArgs, + workingDir: $this->workingDir, ); $process->setTimeout(null); diff --git a/src/Console/Command/TestCommand.php b/src/Console/Command/TestCommand.php index a3f9045..61a3267 100644 --- a/src/Console/Command/TestCommand.php +++ b/src/Console/Command/TestCommand.php @@ -11,12 +11,11 @@ final class TestCommand extends AbstractCommand { public function execute(InputInterface $input, OutputInterface $output): int { - $extraArgs = $input->getOption('extra-args'); - $workingDir = $input->getOption('working-dir'); + parent::execute($input, $output); // TODO: move this logic to a service so it can be tested. $json = json_decode( - json: strval(file_get_contents($workingDir.'/composer.json')), + json: strval(file_get_contents($this->workingDir.'/composer.json')), associative: true, ); @@ -33,8 +32,8 @@ final class TestCommand extends AbstractCommand // TODO: commands in Docker Compose? $process = Process::create( command: $command, - extraArgs: $extraArgs, - workingDir: $workingDir, + extraArgs: $this->extraArgs, + workingDir: $this->workingDir, ); $process->run();