2024-02-25 16:30:23 +00:00
< ? php
namespace App\Console\Command ;
2024-02-26 07:17:04 +00:00
use App\Action\DeterminePackageManager ;
2024-02-25 16:30:23 +00:00
use App\Action\DetermineProjectLanguage ;
2024-02-26 07:17:04 +00:00
use App\Enum\PackageManager ;
2024-02-25 16:30:23 +00:00
use App\Enum\ProjectLanguage ;
use App\Process\Process ;
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Output\OutputInterface ;
final class PackageInstallCommand extends AbstractCommand
{
2024-02-25 16:44:48 +00:00
public static string $description = 'Install a new package' ;
2024-02-25 16:30:23 +00:00
public function configure () : void
{
parent :: configure ();
$this -> addArgument (
name : 'package-name' ,
mode : InputArgument :: REQUIRED ,
);
}
public function execute ( InputInterface $input , OutputInterface $output ) : int
{
$args = $input -> getOption ( 'args' );
$workingDir = $input -> getOption ( 'working-dir' );
$language = $input -> getOption ( 'language' ) ? ? ( new DetermineProjectLanguage (
filesystem : $this -> filesystem ,
workingDir : $workingDir ,
)) -> getLanguage ();
switch ( $language ) {
case ProjectLanguage :: PHP -> value :
$process = Process :: create (
args : explode ( separator : ' ' , string : $args ? ? '' ),
command : [ 'composer' , 'require' , $input -> getArgument ( 'package-name' )],
workingDir : '.' ,
);
$process -> setTimeout ( null );
$process -> run ();
break ;
case ProjectLanguage :: JavaScript -> value :
2024-02-26 07:17:04 +00:00
$packageManager = new DeterminePackageManager (
filesystem : $this -> filesystem ,
projectLanguage : $language ,
workingDir : $workingDir ,
);
switch ( $packageManager -> getPackageManager ()) {
case PackageManager :: pnpm -> value :
$command = [ 'pnpm' , 'install' ];
break ;
case PackageManager :: yarn -> value :
$command = [ 'yarn' , 'add' ];
break ;
default :
$command = [ 'npm' , 'install' ];
break ;
2024-02-25 16:30:23 +00:00
}
$process = Process :: create (
args : explode ( separator : ' ' , string : $args ? ? '' ),
command : $command ,
workingDir : $workingDir ,
);
$process -> setTimeout ( null );
$process -> run ();
break ;
}
return Command :: SUCCESS ;
}
}