Skip to content

Commit dd5c1ee

Browse files
committed
✨ added update command
1 parent 04bcc89 commit dd5c1ee

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-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", "2.0.0");
1010

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

src/UpdateCommand.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 UpdateCommand extends Command
12+
{
13+
protected static $defaultName = 'update';
14+
15+
protected function configure()
16+
{
17+
$this
18+
->setHelp("Update leaf cli")
19+
->setDescription("Update leaf cli to the latest version");
20+
}
21+
22+
protected function execute(InputInterface $input, OutputInterface $output)
23+
{
24+
$composer = $this->findComposer();
25+
$uninstall = Process::fromShellCommandline("$composer global remove leafs/cli --no-update --no-install");
26+
$install = Process::fromShellCommandline("$composer global require leafs/cli", null, null, null, null);
27+
28+
$uninstall->run(function ($type, $line) use ($output) {
29+
$output->write($line);
30+
});
31+
32+
if ($uninstall->isSuccessful()) {
33+
sleep(1);
34+
35+
$install->run(function ($type, $line) use ($output) {
36+
$output->write($line);
37+
});
38+
39+
if ($install->isSuccessful()) {
40+
$output->writeln("<info>Leaf CLI installed successfully!</info>");
41+
return 0;
42+
}
43+
}
44+
45+
return 1;
46+
}
47+
48+
/**
49+
* Get the composer command for the environment.
50+
*
51+
* @return string
52+
*/
53+
protected function findComposer()
54+
{
55+
$composerPath = getcwd() . '/composer.phar';
56+
57+
if (file_exists($composerPath)) {
58+
return '"' . PHP_BINARY . '" ' . $composerPath;
59+
}
60+
61+
return 'composer';
62+
}
63+
}

0 commit comments

Comments
 (0)