-
Notifications
You must be signed in to change notification settings - Fork 46
[WIP] Add a self-update command to update local phpmnd.phar from Github releases
#44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 8 commits
36fbe57
5eaced4
c9f9bf4
28640d4
cdf04ed
ab1ec61
52f0c62
4030f8b
4c29e14
b2571b1
e22bea4
734ed1b
39a2be4
6bc8e1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| <?php | ||
|
|
||
| namespace Povils\PHPMND\Console\Command; | ||
|
|
||
| use Povils\PHPMND\Console\Application; | ||
| use Symfony\Component\Console\Command\Command as BaseCommand; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Humbug\SelfUpdate\Updater; | ||
| use Humbug\SelfUpdate\Strategy\GithubStrategy; | ||
|
|
||
| class SelfUpdate extends BaseCommand | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make protected methods,properties to private :) |
||
| { | ||
|
|
||
| /** | ||
| * Packagist package name | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PACKAGIST_PACKAGE_NAME and you dont need comment |
||
| */ | ||
| const PACKAGE_NAME = 'povils/phpmnd'; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this belongs to Application i think so |
||
|
|
||
| /** | ||
| * This is the remote file name, not local name. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comment and name constant REMOTE_FILENAME :) |
||
| */ | ||
| const FILE_NAME = 'phpmnd.phar'; | ||
|
|
||
| /** | ||
| * @var OutputInterface | ||
| */ | ||
| protected $output; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $version; | ||
|
|
||
| /** | ||
| * Setup command and arguments. | ||
| */ | ||
| protected function configure() | ||
| { | ||
| $this | ||
| ->setName('self-update') | ||
| ->setDescription('Update phpmnd.phar to most recent stable build.') | ||
| ->addOption( | ||
| 'stable', | ||
| 's', | ||
| InputOption::VALUE_NONE, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can probably be deleted. If support is needed for non-stable releases (alphas, betas, RCs) alternative stability flags can be passed into phar-updater. |
||
| 'Update to most recent stable version of PHPMND tagged on Github.' | ||
| ) | ||
| ->addOption( | ||
| 'rollback', | ||
| 'r', | ||
| InputOption::VALUE_NONE, | ||
| 'Rollback to previous version of PHPMND if available on filesystem.' | ||
| ) | ||
| ->addOption( | ||
| 'check', | ||
| 'c', | ||
| InputOption::VALUE_NONE, | ||
| 'Checks what updates are available.' | ||
| ) | ||
| ; | ||
| } | ||
|
|
||
| /** | ||
| * Execute the command. | ||
| * | ||
| * @param InputInterface $input | ||
| * @param OutputInterface $output | ||
| */ | ||
| protected function execute(InputInterface $input, OutputInterface $output) | ||
| { | ||
| $this->output = $output; | ||
| $this->version = $this->getApplication()->getVersion(); | ||
|
|
||
| /** | ||
| * Check for ancilliary options | ||
| */ | ||
| if ($input->getOption('rollback')) { | ||
| $this->rollback(); | ||
| return; | ||
| } | ||
|
|
||
| if ($input->getOption('check')) { | ||
| $this->printAvailableUpdates(); | ||
| return; | ||
| } | ||
|
|
||
| $this->updateToStableBuild(); | ||
| } | ||
|
|
||
| /** | ||
| * Perform update using phar-updater configured for stable versions. | ||
| */ | ||
| protected function updateToStableBuild() | ||
| { | ||
| $this->update($this->getStableUpdater()); | ||
| } | ||
|
|
||
| /** | ||
| * Get phar-updater instance. | ||
| */ | ||
| protected function getStableUpdater() | ||
| { | ||
| $updater = new Updater(null, false); | ||
| $updater->setStrategy(Updater::STRATEGY_GITHUB); | ||
| return $this->getGithubReleasesUpdater($updater); | ||
| } | ||
|
|
||
| /** | ||
| * Perform in-place update of phar. | ||
| */ | ||
| protected function update(Updater $updater) | ||
| { | ||
| $this->output->writeln('Updating...'.PHP_EOL); | ||
| try { | ||
| $result = $updater->update(); | ||
|
|
||
| $newVersion = $updater->getNewVersion(); | ||
| $oldVersion = $updater->getOldVersion(); | ||
|
|
||
| if ($result) { | ||
| $this->output->writeln('<fg=green>PHPMND has been updated.</fg=green>'); | ||
| $this->output->writeln(sprintf( | ||
| '<fg=green>Current version is:</fg=green> <options=bold>%s</options=bold>.', | ||
| $newVersion | ||
| )); | ||
| $this->output->writeln(sprintf( | ||
| '<fg=green>Previous version was:</fg=green> <options=bold>%s</options=bold>.', | ||
| $oldVersion | ||
| )); | ||
| } else { | ||
| $this->output->writeln('<fg=green>PHPMND is currently up to date.</fg=green>'); | ||
| $this->output->writeln(sprintf( | ||
| '<fg=green>Current version is:</fg=green> <options=bold>%s</options=bold>.', | ||
| $oldVersion | ||
| )); | ||
| } | ||
| } catch (\Exception $e) { | ||
| $this->output->writeln(sprintf('Error: <fg=yellow>%s</fg=yellow>', $e->getMessage())); | ||
| } | ||
| $this->output->write(PHP_EOL); | ||
| } | ||
|
|
||
| /** | ||
| * Attempt to rollback to the previous phar version. | ||
| */ | ||
| protected function rollback() | ||
| { | ||
| $updater = new Updater(null, false); | ||
| try { | ||
| $result = $updater->rollback(); | ||
| if ($result) { | ||
| $this->output->writeln('<fg=green>PHPMND has been rolled back to prior version.</fg=green>'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is different from <fg=green> ? I would prefer use , , and so on
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your preference didn't show up in comment (probably github markdown parser rendering as HTML tag).
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. damn :D <info>, <danger>, <warning>
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :D Updated. Check if those are in line with what you would prefer.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, seem to be getting ignored for some reason in actual output (at least here). May need to check that tomorrow. |
||
| } else { | ||
| $this->output->writeln('<fg=red>Rollback failed for reasons unknown.</fg=red>'); | ||
| } | ||
| } catch (\Exception $e) { | ||
| $this->output->writeln(sprintf('Error: <fg=yellow>%s</fg=yellow>', $e->getMessage())); | ||
| } | ||
| } | ||
|
|
||
| protected function printAvailableUpdates() | ||
| { | ||
| $this->printCurrentLocalVersion(); | ||
| $this->printCurrentStableVersion(); | ||
| } | ||
|
|
||
| /** | ||
| * Print the current version of the phar in use. | ||
| */ | ||
| protected function printCurrentLocalVersion() | ||
| { | ||
| $this->output->writeln(sprintf( | ||
| 'Your current local build version is: <options=bold>%s</options=bold>', | ||
| $this->version | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Send updater to version printer. | ||
| */ | ||
| protected function printCurrentStableVersion() | ||
| { | ||
| $this->printVersion($this->getStableUpdater()); | ||
| } | ||
|
|
||
| /** | ||
| * Print a remotely available version. | ||
| * @param Updater $updater | ||
| */ | ||
| protected function printVersion(Updater $updater) | ||
| { | ||
| $stability = 'stable'; | ||
| try { | ||
| if ($updater->hasUpdate()) { | ||
| $this->output->writeln(sprintf( | ||
| 'The current %s build available remotely is: <options=bold>%s</options=bold>', | ||
| $stability, | ||
| $updater->getNewVersion() | ||
| )); | ||
| } elseif (false == $updater->getNewVersion()) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getNewVersion returns bool?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, it does. There's an implementation shortfall in the phar-updater library, where this avoids calling Packagist twice. Something to be fixed there in a future version. |
||
| $this->output->writeln(sprintf('There are no new %s builds available.', $stability)); | ||
| } else { | ||
| $this->output->writeln(sprintf('You have the current %s build installed.', $stability)); | ||
| } | ||
| } catch (\Exception $e) { | ||
| $this->output->writeln(sprintf('Error: <fg=yellow>%s</fg=yellow>', $e->getMessage())); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Configure phar-updater with local phar details. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment doesnt match with method name. In other words get rid of comment :) |
||
| * @param Updater $updater | ||
| * @return Updater | ||
| */ | ||
| protected function getGithubReleasesUpdater(Updater $updater) | ||
| { | ||
| $updater->getStrategy()->setPackageName(self::PACKAGE_NAME); | ||
| $updater->getStrategy()->setPharName(self::FILE_NAME); | ||
| $updater->getStrategy()->setCurrentLocalVersion($this->version); | ||
| return $updater; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one file is the one i need to take a better look :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it doesn't sit right here, but can't think of a better way via Symfony/Console. We're basically checking if no command is sent, and prefixing
run. We have aruncommand since adding multiple commands (even two for self-update) seems to require abandoning a default command. Also, the usingsetDefaultCommandmethod for Application doesn't accept arguments so that's not a solution.Not sure if these are specific design decisions (or just limitations) by the Symfony team, or I'm just missing something obvious that would make that
prepareArgvfunction go away.