Skip to content

Commit

Permalink
Added getChangelog method with option to set a diff starting point ve…
Browse files Browse the repository at this point in the history
…rsion

Selfupgrade command is now going to prompt you before continuing upgrading and optionally can show the change log
Added -y|--all-yes option in grav self upgrade command to skip any prompt and just upgrade
  • Loading branch information
w00fz committed Oct 10, 2014
1 parent 5ca9180 commit 92ce0aa
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
34 changes: 30 additions & 4 deletions system/src/Grav/Common/GPM/Remote/Grav.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class Grav extends Collection
private $version;
private $date;

/**
* @param bool $refresh
* @param null $callback
*/
public function __construct($refresh = false, $callback = null)
{
parent::__construct($this->repository);
Expand All @@ -19,9 +23,7 @@ public function __construct($refresh = false, $callback = null)
$this->version = @$this->data->version ?: '-';
$this->date = @$this->data->date ?: '-';

$this->data = $this->data->assets;

foreach ($this->data as $slug => $data) {
foreach ($this->data->assets as $slug => $data) {
$this->items[$slug] = new Package($data);
}
}
Expand All @@ -32,7 +34,31 @@ public function __construct($refresh = false, $callback = null)
*/
public function getAssets()
{
return $this->data;
return $this->data->assets;
}

/**
* Returns the changelog list for each version of Grav
* @param string $diff the version number to start the diff from
*
* @return array changelog list for each version
*/
public function getChangelog($diff = null)
{
if (!$diff) {
return $this->data->changelog;
}

$diffLog = [];
foreach ($this->data->changelog as $version => $changelog) {
preg_match("/[\d\.]+/", $version, $cleanVersion);

if (!$cleanVersion || version_compare($diff, $cleanVersion[0], ">=")) { continue; }

$diffLog[$version] = $changelog;
}

return $diffLog;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions system/src/Grav/Common/GPM/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public function getAssets()
return $this->remote->getAssets();
}

/**
* Returns the changelog list for each version of Grav
* @param string $diff the version number to start the diff from
*
* @return array return the chagenlog list for each version
*/
public function getChangelog($diff = null)
{
return $this->remote->getChangelog($diff);
}

/**
* Checks if the currently installed Grav is upgradable to a newer version
* @return boolean True if it's upgradable, False otherwise.
Expand Down
51 changes: 50 additions & 1 deletion system/src/Grav/Console/Gpm/SelfupgradeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

class SelfupgradeCommand extends Command
{
Expand All @@ -32,6 +33,12 @@ protected function configure()
InputOption::VALUE_NONE,
'Force re-fetching the data from remote'
)
->addOption(
'all-yes',
'y',
InputOption::VALUE_NONE,
'Assumes yes (or best approach) instead of prompting'
)
->setDescription("Detects and performs an update of plugins and themes when available")
->setHelp('The <info>update</info> command updates plugins and themes when a new version is available');
}
Expand All @@ -51,7 +58,49 @@ protected function execute(InputInterface $input, OutputInterface $output)
exit;
}

$this->output->writeln("Preparing to upgrade Grav to v<cyan>" . $remote . "</cyan> [release date: " . $release . "]");
$questionHelper = $this->getHelper('question');
$skipPrompt = $this->input->getOption('all-yes');

$this->output->writeln("Grav v<cyan>$remote</cyan> is now available [release date: $release].");
$this->output->writeln("You are currently using v<cyan>".GRAV_VERSION."</cyan>.");

if (!$skipPrompt) {
$question = new ConfirmationQuestion("Would you like to upgrade now? [y|N] ", false);
$answer = $questionHelper->ask($this->input, $this->output, $question);

if (!$answer) {
$this->output->writeln("Aborting...");

exit;
}

$question = new ConfirmationQuestion("Would you like to read the changelog before proceeding? [y|N] ", false);
$answer = $questionHelper->ask($this->input, $this->output, $question);

if ($answer) {
$changelog = $this->upgrader->getChangelog(GRAV_VERSION);

$this->output->writeln("");
foreach ($changelog as $version => $log) {
$title = $version . ' [' . $log->date . ']';
$content = preg_replace_callback("/\d\.\s\[\]\(#(.*)\)/", function($match){
return "\n".ucfirst($match[1]).":";
}, $log->content);

$this->output->writeln($title);
$this->output->writeln(str_repeat('-', strlen($title)));
$this->output->writeln($content);
$this->output->writeln("");
}

$question = new ConfirmationQuestion("Press [ENTER] to continue.", true);
$questionHelper->ask($this->input, $this->output, $question);
}

}

$this->output->writeln("");
$this->output->writeln("Preparing to upgrade to v<cyan>$remote</cyan>..");

$this->output->write(" |- Downloading upgrade [" . $this->formatBytes($update->size) . "]... 0%");
$this->file = $this->download($update);
Expand Down

0 comments on commit 92ce0aa

Please sign in to comment.