diff --git a/CHANGELOG.md b/CHANGELOG.md index 166a0d2..d445d21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added +* Automatically find the PHP project type (i.e. Drupal or Sculpin) based on its `composer.json` dependencies. * Add `versa build` to build a Sculpin project. * Add `--extra-args` to pass extra arguments to the underlying command. diff --git a/versa b/versa index d358d5f..eb327f1 100755 --- a/versa +++ b/versa @@ -43,17 +43,41 @@ $application->addOption( ); $application->setCode(function (InputInterface $input): int { + $projectType = null; + $extraArgs = $input->getOption('extra-args'); $workingDir = $input->getOption('working-dir'); $filesystem = new Filesystem(); - $isDockerCompose = $filesystem->exists($workingDir.'/docker-compose.yaml'); + // 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')) { + $json = json_decode( + json: strval(file_get_contents($workingDir.'/composer.json')), + associative: true, + ); + + $dependencies = array_keys($json['require']); + + if (in_array(needle: 'drupal/core', haystack: $dependencies, strict: true) || in_array(needle: 'drupal/core-recommended', haystack: $dependencies, strict: true)) { + $projectType = ProjectType::Drupal->value; + } elseif (in_array(needle: 'sculpin/sculpin', haystack: $dependencies, strict: true)) { + $projectType = ProjectType::Sculpin->value; + } + } + + // Even if the project type is found automatically, still override it with + // the option value if there is one. + $projectType = $input->getOption('type') ?? $projectType; + + $isDockerCompose = $filesystem->exists($workingDir . '/docker-compose.yaml'); // TODO: only allow defined commands - build, install, test, run. switch ($input->getArgument('command')) { case 'build': - switch ($input->getOption('type')) { + switch ($projectType) { case ProjectType::Drupal->value: if ($isDockerCompose) { $process = Process::create( @@ -65,7 +89,7 @@ $application->setCode(function (InputInterface $input): int { } break; } - switch ($input->getOption('type')) { + switch ($projectType) { case ProjectType::Sculpin->value: $process = Process::create( command: ['./vendor/bin/sculpin', 'generate'], @@ -97,7 +121,7 @@ $application->setCode(function (InputInterface $input): int { $process->setTimeout(null); $process->run(); } else { - switch ($input->getOption('type')) { + switch ($projectType) { case ProjectType::Sculpin->value: $process = Process::create( command: ['./vendor/bin/sculpin', 'generate', '--server', '--watch'],