Skip to content

Commit 9c368d5

Browse files
committed
updated install action
1 parent fc3bd8e commit 9c368d5

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

bin/leaf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ if (file_exists(__DIR__.'/../../../autoload.php')) {
99
$app = new Symfony\Component\Console\Application('Leaf CLI', '1.0.0');
1010

1111
$app->add(new Leaf\Console\CreateCommand);
12+
$app->add(new Leaf\Console\InstallCommand);
1213
$app->add(new Leaf\Console\App\ServeCommand);
1314
$app->add(new Leaf\Console\App\InteractCommand);
1415

src/InstallCommand.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Leaf\Console;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputArgument;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\Process\Process;
10+
11+
class InstallCommand extends Command
12+
{
13+
protected static $defaultName = 'install';
14+
15+
protected function configure()
16+
{
17+
$this
18+
->setHelp("Install a new package")
19+
->setDescription("Add a new package to your leaf app")
20+
->addArgument("package", InputArgument::REQUIRED, "package to install")
21+
->addArgument("version", InputArgument::OPTIONAL, "version to install");
22+
}
23+
24+
protected function execute(InputInterface $input, OutputInterface $output)
25+
{
26+
$package = $input->getArgument("package");
27+
$version = $input->getArgument("version") ?? null;
28+
29+
$output->writeln("<info>Installing $package...</info>");
30+
// $output->write(shell_exec("composer require $package"));
31+
$composer = $this->findComposer();
32+
$process = Process::fromShellCommandline("$composer require $package $version", null, null, null, null);
33+
34+
$process->run(function ($type, $line) use ($output) {
35+
$output->write($line);
36+
});
37+
38+
if ($process->isSuccessful()) {
39+
$output->writeln("<comment>$package installed successfully!</comment>");
40+
}
41+
}
42+
43+
/**
44+
* Get the composer command for the environment.
45+
*
46+
* @return string
47+
*/
48+
protected function findComposer()
49+
{
50+
$composerPath = getcwd() . '/composer.phar';
51+
52+
if (file_exists($composerPath)) {
53+
return '"' . PHP_BINARY . '" ' . $composerPath;
54+
}
55+
56+
return 'composer';
57+
}
58+
}

0 commit comments

Comments
 (0)