|
| 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