Skip to content

Commit 53fd8d7

Browse files
committed
✨ added dependency install & updates
1 parent dbb66f9 commit 53fd8d7

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ composer.lock
33
phpunit.xml
44
.phpunit.result.cache
55
.idea
6+
test

src/InstallCommand.php

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,87 @@ class InstallCommand extends Command
1515
protected function configure()
1616
{
1717
$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");
18+
->setHelp('Install a new package')
19+
->setDescription('Add a new package to your leaf app')
20+
->addArgument('packages', InputArgument::IS_ARRAY, 'package(s) to install. Can also include a version constraint, e.g. foo/bar or foo/[email protected]');
2221
}
2322

2423
protected function execute(InputInterface $input, OutputInterface $output): int
2524
{
26-
$package = $input->getArgument("package");
27-
if (strpos($package, '/') == false) $package = "leafs/$package";
25+
$packages = $input->getArgument('packages');
2826

29-
$version = $input->getArgument("version") ?? null;
27+
if (count($packages)) {
28+
return $this->install($packages, $output);
29+
}
30+
31+
return $this->installDependencies($output);
32+
}
33+
34+
protected function installDependencies($output)
35+
{
36+
$composerJsonPath = getcwd() . '/composer.json';
37+
$composerLockPath = getcwd() . '/composer.lock';
38+
39+
if (!file_exists($composerJsonPath)) {
40+
$output->writeln('<error>No composer.json found in the current directory. Pass in a package to add if you meant to install something.</error>');
41+
return 1;
42+
}
3043

31-
$output->writeln("<info>Installing $package...</info>");
3244
$composer = $this->findComposer();
3345
$process = Process::fromShellCommandline(
34-
"$composer require $package $version", null, null, null, null
46+
file_exists($composerLockPath) ? "$composer install" : "$composer update",
47+
null,
48+
null,
49+
null,
50+
null
3551
);
3652

3753
$process->run(function ($type, $line) use ($output) {
3854
$output->write($line);
3955
});
4056

41-
if ($process->isSuccessful()) {
57+
if (!$process->isSuccessful()) return 1;
58+
59+
$output->writeln('<comment>packages installed successfully!</comment>');
60+
61+
return 0;
62+
}
63+
64+
/**
65+
* Install packages
66+
*/
67+
protected function install($packages, $output)
68+
{
69+
foreach ($packages as $package) {
70+
if (strpos($package, '/') == false || strpos($package, '.') == false) {
71+
$package = "leafs/$package";
72+
}
73+
$package = str_replace('@', ':', $package);
74+
75+
$output->writeln("<info>Installing $package...</info>");
76+
$composer = $this->findComposer();
77+
$process = Process::fromShellCommandline(
78+
"$composer require $package",
79+
null,
80+
null,
81+
null,
82+
null
83+
);
84+
85+
$process->run(function ($type, $line) use ($output) {
86+
$output->write($line);
87+
});
88+
89+
if (!$process->isSuccessful()) return 1;
90+
4291
$output->writeln("<comment>$package installed successfully!</comment>");
43-
return 0;
4492
}
4593

46-
return 1;
94+
if (count($packages) > 1) {
95+
$output->writeln('<info>All packages installed</info>');
96+
}
97+
98+
return 0;
4799
}
48100

49101
/**

0 commit comments

Comments
 (0)