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

78 lines
2.5 KiB
PHP
Raw Normal View History

2024-02-21 12:48:33 +00:00
<?php
namespace App\Console\Command;
use App\Action\DeterminePackageManager;
use App\Action\DetermineProjectLanguage;
use App\Enum\PackageManager;
use App\Enum\ProjectLanguage;
2024-02-21 12:48:33 +00:00
use App\Process\Process;
use RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;
2024-02-21 12:48:33 +00:00
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
description: 'Install the project\'s dependencies',
name: 'install',
)]
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();
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(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 non-empty-string $language
* @param non-empty-string $workingDir
* @return non-empty-array<int, non-empty-string>
* @throws RuntimeException If the lanuage cannot be determined.
*/
private function getCommand(string $language, string $workingDir): array
{
if ($language === ProjectLanguage::JavaScript->value) {
return ['composer', 'install'];
} elseif ($language === ProjectLanguage::JavaScript->value) {
$packageManager = new DeterminePackageManager(
filesystem: $this->filesystem,
projectLanguage: $language,
workingDir: $workingDir,
);
switch ($packageManager->getPackageManager()) {
case PackageManager::pnpm->value:
return ['pnpm', 'install'];
case PackageManager::yarn->value:
return ['yarn'];
default:
return ['npm', 'install'];
}
}
// TODO: add a test to ensure the exception is thrown?
throw new RuntimeException('Project language cannot be determined.');
}
2024-02-21 12:48:33 +00:00
}