Automatically select the PHP project type

This commit is contained in:
Oliver Davies 2024-02-21 00:08:14 +00:00
parent 20048ad8f4
commit 48540cdab1
2 changed files with 29 additions and 4 deletions

View file

@ -4,6 +4,7 @@
### Added ### 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 `versa build` to build a Sculpin project.
* Add `--extra-args` to pass extra arguments to the underlying command. * Add `--extra-args` to pass extra arguments to the underlying command.

32
versa
View file

@ -43,17 +43,41 @@ $application->addOption(
); );
$application->setCode(function (InputInterface $input): int { $application->setCode(function (InputInterface $input): int {
$projectType = null;
$extraArgs = $input->getOption('extra-args'); $extraArgs = $input->getOption('extra-args');
$workingDir = $input->getOption('working-dir'); $workingDir = $input->getOption('working-dir');
$filesystem = new Filesystem(); $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. // TODO: only allow defined commands - build, install, test, run.
switch ($input->getArgument('command')) { switch ($input->getArgument('command')) {
case 'build': case 'build':
switch ($input->getOption('type')) { switch ($projectType) {
case ProjectType::Drupal->value: case ProjectType::Drupal->value:
if ($isDockerCompose) { if ($isDockerCompose) {
$process = Process::create( $process = Process::create(
@ -65,7 +89,7 @@ $application->setCode(function (InputInterface $input): int {
} }
break; break;
} }
switch ($input->getOption('type')) { switch ($projectType) {
case ProjectType::Sculpin->value: case ProjectType::Sculpin->value:
$process = Process::create( $process = Process::create(
command: ['./vendor/bin/sculpin', 'generate'], command: ['./vendor/bin/sculpin', 'generate'],
@ -97,7 +121,7 @@ $application->setCode(function (InputInterface $input): int {
$process->setTimeout(null); $process->setTimeout(null);
$process->run(); $process->run();
} else { } else {
switch ($input->getOption('type')) { switch ($projectType) {
case ProjectType::Sculpin->value: case ProjectType::Sculpin->value:
$process = Process::create( $process = Process::create(
command: ['./vendor/bin/sculpin', 'generate', '--server', '--watch'], command: ['./vendor/bin/sculpin', 'generate', '--server', '--watch'],