2024-02-21 12:48:33 +00:00
< ? php
namespace App\Console\Command ;
2024-02-26 07:17:04 +00:00
use App\Action\DeterminePackageManager ;
2024-02-22 21:04:40 +00:00
use App\Action\DetermineProjectLanguage ;
2024-02-26 07:17:04 +00:00
use App\Enum\PackageManager ;
2024-02-21 23:32:59 +00:00
use App\Enum\ProjectLanguage ;
2024-02-21 12:48:33 +00:00
use App\Process\Process ;
2024-02-26 07:17:04 +00:00
use RuntimeException ;
2024-02-26 20:40:58 +00:00
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 ;
2024-02-26 20:40:58 +00:00
#[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' );
2024-02-21 13:22:17 +00:00
$workingDir = $input -> getOption ( 'working-dir' );
2024-02-21 12:48:33 +00:00
2024-02-22 21:04:40 +00:00
$language = $input -> getOption ( 'language' ) ? ? ( new DetermineProjectLanguage (
filesystem : $this -> filesystem ,
workingDir : $workingDir ,
)) -> getLanguage ();
2024-02-21 23:32:59 +00:00
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 )),
2024-02-26 07:17:04 +00:00
command : $this -> getCommand ( language : $language , workingDir : $workingDir ),
2024-02-21 13:22:17 +00:00
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 ;
}
2024-02-21 23:32:59 +00:00
/**
* @ param non - empty - string $language
* @ param non - empty - string $workingDir
* @ return non - empty - array < int , non - empty - string >
2024-02-26 07:17:04 +00:00
* @ throws RuntimeException If the lanuage cannot be determined .
2024-02-21 23:32:59 +00:00
*/
2024-02-26 07:17:04 +00:00
private function getCommand ( string $language , string $workingDir ) : array
2024-02-21 23:32:59 +00:00
{
if ( $language === ProjectLanguage :: JavaScript -> value ) {
2024-02-26 07:17:04 +00:00
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' ];
2024-02-21 23:32:59 +00:00
}
}
2024-02-26 07:17:04 +00:00
// TODO: add a test to ensure the exception is thrown?
throw new RuntimeException ( 'Project language cannot be determined.' );
2024-02-21 23:32:59 +00:00
}
2024-02-21 12:48:33 +00:00
}