This repository has been archived on 2025-08-17. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
versa/src/Console/Command/InstallCommand.php

65 lines
1.9 KiB
PHP
Raw Normal View History

2024-02-21 12:48:33 +00:00
<?php
namespace App\Console\Command;
use App\Action\DetermineProjectLanguage;
use App\Enum\ProjectLanguage;
2024-02-21 12:48:33 +00:00
use App\Process\Process;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
2024-02-21 12:48:33 +00:00
final class InstallCommand extends AbstractCommand
{
public function execute(InputInterface $input, OutputInterface $output): int
{
2024-02-25 13:54:13 +00:00
$args = $input->getOption('args');
$workingDir = $input->getOption('working-dir');
2024-02-21 12:48:33 +00:00
$language = $input->getOption('language') ?? (new DetermineProjectLanguage(
filesystem: $this->filesystem,
workingDir: $workingDir,
))->getLanguage();
$filesystem = new Filesystem();
2024-02-21 12:48:33 +00:00
// TODO: Composer in Docker Compose?
$process = Process::create(
2024-02-25 13:54:13 +00:00
args: explode(separator: ' ', string: strval($args)),
command: $this->getCommand(
filesystem: $filesystem,
language: $language,
workingDir: $workingDir,
),
workingDir: $workingDir,
2024-02-21 12:48:33 +00:00
);
2024-02-21 13:20:41 +00:00
$process->setTimeout(null);
2024-02-21 12:48:33 +00:00
$process->run();
return Command::SUCCESS;
}
/**
* @param Filesystem $filesystem
* @param non-empty-string $language
* @param non-empty-string $workingDir
* @return non-empty-array<int, non-empty-string>
*/
private function getCommand(Filesystem $filesystem, string $language, string $workingDir): array
{
if ($language === ProjectLanguage::JavaScript->value) {
if ($filesystem->exists($workingDir.'/yarn.lock')) {
return ['yarn'];
} elseif ($filesystem->exists($workingDir.'/pnpm-lock.yaml')) {
return ['pnpm', 'install'];
} else {
return ['npm', 'install'];
}
}
return ['composer', 'install'];
}
2024-02-21 12:48:33 +00:00
}