Skip to content

Commit 943efa5

Browse files
committed
chore: migrate install command to sprout
1 parent c93fc46 commit 943efa5

File tree

1 file changed

+17
-87
lines changed

1 file changed

+17
-87
lines changed

src/InstallCommand.php

Lines changed: 17 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,107 +4,37 @@
44

55
namespace Leaf\Console;
66

7-
use Symfony\Component\Console\Command\Command;
8-
use Symfony\Component\Console\Input\InputArgument;
9-
use Symfony\Component\Console\Input\InputInterface;
10-
use Symfony\Component\Console\Input\InputOption;
11-
use Symfony\Component\Console\Output\OutputInterface;
12-
use Symfony\Component\Process\Process;
7+
use Leaf\Sprout\Command;
138

149
class InstallCommand extends Command
1510
{
16-
protected static $defaultName = 'install';
11+
protected $signature = 'install {packages*} {--d|dev}';
1712

18-
protected function configure()
19-
{
20-
$this
21-
->setHelp('Install a new package')
22-
->setDescription('Add a new package to your leaf app')
23-
->addArgument('packages', InputArgument::IS_ARRAY, 'package(s) to install. Can also include a version constraint, e.g. foo/bar or foo/[email protected]')
24-
->addOption('dev', 'd', InputOption::VALUE_NONE, 'Install package as a dev dependency');
25-
}
13+
protected $description = 'Install a new package';
2614

27-
protected function execute(InputInterface $input, OutputInterface $output): int
15+
protected function execute(): int
2816
{
29-
$packages = $input->getArgument('packages');
17+
$packages = $this->argument('packages');
3018

3119
if (count($packages)) {
32-
return $this->install($packages, $input, $output);
33-
}
34-
35-
return $this->installDependencies($output);
36-
}
37-
38-
protected function installDependencies($output)
39-
{
40-
$composerJsonPath = getcwd() . '/composer.json';
41-
$composerLockPath = getcwd() . '/composer.lock';
42-
43-
if (!file_exists($composerJsonPath)) {
44-
$output->writeln('<error>No composer.json found in the current directory. Pass in a package to add if you meant to install something.</error>');
45-
46-
return 1;
47-
}
48-
49-
$composer = Utils\Core::findComposer();
50-
$process = Process::fromShellCommandline(
51-
file_exists($composerLockPath) ? "$composer install --ansi" : "$composer update --ansi",
52-
null,
53-
null,
54-
null,
55-
null
56-
);
20+
foreach ($packages as $package) {
21+
if (strpos($package, '/') == false) {
22+
$package = "leafs/$package";
23+
}
5724

58-
$process->run(function ($type, $line) use ($output) {
59-
$output->write($line);
60-
});
25+
$package = str_replace('@', ':', $package);
26+
$package = $this->option('dev') ? "$package --dev" : $package;
6127

62-
if (!$process->isSuccessful()) {
63-
return 1;
64-
}
28+
$this->writeln("<info>Installing $package...</info>");
6529

66-
$output->writeln('<comment>packages installed successfully!</comment>');
30+
if (!sprout()->composer()->install($package)->isSuccessful()) {
31+
return 1;
32+
}
6733

68-
return 0;
69-
}
70-
71-
/**
72-
* Install packages
73-
*/
74-
protected function install($packages, $input, $output)
75-
{
76-
foreach ($packages as $package) {
77-
if (strpos($package, '/') == false) {
78-
$package = "leafs/$package";
34+
$this->writeln("<comment>$package installed successfully!</comment>");
7935
}
80-
81-
$package = str_replace('@', ':', $package);
82-
83-
$output->writeln("<info>Installing $package...</info>");
84-
$composer = Utils\Core::findComposer();
85-
$process = Process::fromShellCommandline(
86-
"$composer require $package" . ($input->getOption('dev') ? ' --dev' : '') . ' --ansi',
87-
null,
88-
null,
89-
null,
90-
null
91-
);
92-
93-
$process->run(function ($type, $line) use ($output) {
94-
$output->write($line);
95-
});
96-
97-
if (!$process->isSuccessful()) {
98-
return 1;
99-
}
100-
101-
$output->writeln("<comment>$package installed successfully!</comment>");
102-
}
103-
104-
if (count($packages) > 1) {
105-
$output->writeln('<info>All packages installed</info>');
10636
}
10737

108-
return 0;
38+
return (int) sprout()->composer()->install()->isSuccessful();
10939
}
11040
}

0 commit comments

Comments
 (0)