Skip to content

Commit

Permalink
consolidation#497 Fix: Add Basic Self Update Functionality: Inject Ve…
Browse files Browse the repository at this point in the history
…rsion + Repo from Application Class
  • Loading branch information
amenk committed Aug 30, 2017
1 parent 10f40ab commit 63816c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function __construct($name, $version)
new InputOption('--define', '-D', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Define a configuration item value.', [])
);

$selfUpdateCommand = new SelfUpdateCommand('self:update');
$this->add($selfUpdateCommand);
$this->addSelfUpdateCommand();
}

/**
Expand Down Expand Up @@ -59,4 +58,9 @@ public function addInitRoboFileCommand($roboFile, $roboClass)
});
$this->add($createRoboFile);
}

protected function addSelfUpdateCommand() {
$selfUpdateCommand = new SelfUpdateCommand( 'self:update', Robo::VERSION, 'consolidation/robo' );
$this->add( $selfUpdateCommand );
}
}
21 changes: 16 additions & 5 deletions src/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ class SelfUpdateCommand extends Command
{
private $command;

protected $gitHubRepository;

protected $currentVersion;

public function __construct( $name = null, $currentVersion = null, $gitHubRepository = null) {
parent::__construct( $name );
$this->currentVersion = $currentVersion;
$this->gitHubRepository = $gitHubRepository;
}


/**
* {@inheritdoc}
*/
Expand All @@ -42,7 +53,7 @@ protected function configure()
);
}

protected function getLatestReleaseFromGithub($repository)
protected function getLatestReleaseFromGithub()
{
$opts = [
'http' => [
Expand All @@ -55,11 +66,11 @@ protected function getLatestReleaseFromGithub($repository)

$context = stream_context_create($opts);

$releases = file_get_contents('https://api.github.com/repos/' . $repository . '/releases', false, $context);
$releases = file_get_contents('https://api.github.com/repos/' . $this->gitHubRepository . '/releases', false, $context);
$releases = json_decode($releases);

if (! isset($releases[0])) {
throw new \Exception('API error - no release found at GitHub repository ' . $repository);
throw new \Exception('API error - no release found at GitHub repository ' . $this->gitHubRepository);
}

$version = $releases[0]->tag_name;
Expand Down Expand Up @@ -91,10 +102,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
}

list( $latest, $downloadUrl ) = $this->getLatestReleaseFromGithub('consolidation/robo');
list( $latest, $downloadUrl ) = $this->getLatestReleaseFromGithub();


if (Robo::VERSION == $latest) {
if ($this->currentVersion == $latest) {
$output->writeln('No update available');
return;
}
Expand Down

0 comments on commit 63816c4

Please sign in to comment.