|
4 | 4 |
|
5 | 5 | namespace Leaf\Console; |
6 | 6 |
|
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; |
13 | 8 |
|
14 | 9 | class InstallCommand extends Command |
15 | 10 | { |
16 | | - protected static $defaultName = 'install'; |
| 11 | + protected $signature = 'install {packages*} {--d|dev}'; |
17 | 12 |
|
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'; |
26 | 14 |
|
27 | | - protected function execute(InputInterface $input, OutputInterface $output): int |
| 15 | + protected function execute(): int |
28 | 16 | { |
29 | | - $packages = $input->getArgument('packages'); |
| 17 | + $packages = $this->argument('packages'); |
30 | 18 |
|
31 | 19 | 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 | + } |
57 | 24 |
|
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; |
61 | 27 |
|
62 | | - if (!$process->isSuccessful()) { |
63 | | - return 1; |
64 | | - } |
| 28 | + $this->writeln("<info>Installing $package...</info>"); |
65 | 29 |
|
66 | | - $output->writeln('<comment>packages installed successfully!</comment>'); |
| 30 | + if (!sprout()->composer()->install($package)->isSuccessful()) { |
| 31 | + return 1; |
| 32 | + } |
67 | 33 |
|
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>"); |
79 | 35 | } |
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>'); |
106 | 36 | } |
107 | 37 |
|
108 | | - return 0; |
| 38 | + return (int) sprout()->composer()->install()->isSuccessful(); |
109 | 39 | } |
110 | 40 | } |
0 commit comments