Added destination option to allow using the IndexCommand on a different grav instance

IndexCommand displays the local and remote versions. It also detects if the package is already installed
This commit is contained in:
Djamil Legato
2014-09-01 22:12:55 -07:00
parent 1ac4014d64
commit 0d0c22c940

View File

@@ -17,6 +17,7 @@ class IndexCommand extends Command {
protected $ouput;
protected $data;
protected $argv;
protected $destination;
public function __construct(Grav $grav){
$this->grav = $grav;
@@ -37,6 +38,13 @@ class IndexCommand extends Command {
InputOption::VALUE_NONE,
'Force re-fetching the data from remote'
)
->addOption(
'destination',
'd',
InputOption::VALUE_OPTIONAL,
'The destination where the packages check should be looked at. By default this would be where the grav instance has been launched from',
GRAV_ROOT
)
->setDescription("Lists the plugins and themes available for installation")
->setHelp('The <info>index</info> command lists the plugins and themes available for installation');
}
@@ -45,6 +53,7 @@ class IndexCommand extends Command {
{
$this->input = $input;
$this->output = $output;
$this->destination = realpath($this->input->getOption('destination'));
$this->setColors();
@@ -68,7 +77,7 @@ class IndexCommand extends Command {
$this->output->writeln("<green>$name</green> [ ".count($result->data)." ]");
foreach ($result->data as $index => $package) {
$this->output->writeln(str_pad($index + 1, 2, '0', STR_PAD_LEFT).". <cyan>".str_pad($package->name, 15)."</cyan> [".str_pad($package->slug, 15, ' ', STR_PAD_BOTH)."]");
$this->output->writeln(str_pad($index + 1, 2, '0', STR_PAD_LEFT).". <cyan>".str_pad($package->name, 15)."</cyan> [".str_pad($package->slug, 15, ' ', STR_PAD_BOTH)."] ". $this->versionDetails($package));
}
$this->output->writeln('');
@@ -82,13 +91,37 @@ class IndexCommand extends Command {
$this->output->writeln('');
}
private function setColors()
private function setColors()
{
$this->output->getFormatter()->setStyle('normal', new OutputFormatterStyle('white'));
$this->output->getFormatter()->setStyle('yellow', new OutputFormatterStyle('yellow', null, array('bold')));
$this->output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
$this->output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan', null, array('bold')));
$this->output->getFormatter()->setStyle('green', new OutputFormatterStyle('green', null, array('bold')));
$this->output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta', null, array('bold')));
$this->output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
}
private function versionDetails($package)
{
$localVersion = @file_get_contents($this->destination.DS.$package->install_path.DS."VERSION");
$remoteVersion = $package->version;
if ($localVersion) $localVersion = str_replace(array("\r", "\n"), '', $localVersion);
$compare = version_compare($localVersion, $remoteVersion);
if (!$localVersion || !$compare){
$installed = !$localVersion ? ' (<magenta>not installed</magenta>)' : ' (<cyan>installed</cyan>)';
return " [v<green>$remoteVersion</green>]$installed";
}
if ($compare < 0){
return " [v<red>$localVersion</red> <cyan>-></cyan> v<green>$remoteVersion</green>]";
}
if ($compare > 0){
return " [Great Scott! -> local: v<yellow>$localVersion</yellow> | remote: v<yellow>$remoteVersion</yellow>]";
}
var_dump($localVersion, $remoteVersion, version_compare($localVersion, $remoteVersion));
}
}