diff --git a/bin/gpm b/bin/gpm index 0a0e1941c..adf80b481 100755 --- a/bin/gpm +++ b/bin/gpm @@ -23,5 +23,6 @@ $grav = Grav::instance(['loader' => $autoload]); $app = new Application('Grav Package Manager', '0.1.0'); $app->addCommands(array( new \Grav\Console\Gpm\FetchCommand($grav), + new \Grav\Console\Gpm\IndexCommand($grav), )); $app->run(); diff --git a/system/src/Grav/Console/Gpm/IndexCommand.php b/system/src/Grav/Console/Gpm/IndexCommand.php new file mode 100644 index 000000000..98cbccbd6 --- /dev/null +++ b/system/src/Grav/Console/Gpm/IndexCommand.php @@ -0,0 +1,94 @@ +grav = $grav; + + // just for the gpm cli we force the filesystem driver cache + $this->grav['config']->set('system.cache.driver', 'default'); + $this->argv = $_SERVER['argv'][0]; + + parent::__construct(); + } + + protected function configure() { + $this + ->setName("index") + ->addOption( + 'force', + 'f', + InputOption::VALUE_NONE, + 'Force re-fetching the data from remote' + ) + ->setDescription("Lists the plugins and themes available for installation") + ->setHelp('The index command lists the plugins and themes available for installation'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->input = $input; + $this->output = $output; + + $this->setColors(); + + $fetchCommand = $this->getApplication()->find('fetch'); + $args = new ArrayInput(array('command' => 'fetch', '-f' => $input->getOption('force'))); + $commandExec = $fetchCommand->run($args, $output); + + if ($commandExec != 0){ + $output->writeln("Error: An error occured while trying to fetch data from getgrav.org"); + exit; + } + + $this->data = $this->grav['cache']->fetch(md5('cli:gpm')); + + $this->output->writeln(''); + + foreach ($this->data as $type => $result) { + $result = json_decode($result)->results; + $name = $result->name; + + $this->output->writeln("$name [ ".count($result->data)." ]"); + + foreach ($result->data as $index => $package) { + $this->output->writeln(str_pad($index + 1, 2, '0', STR_PAD_LEFT).". ".str_pad($package->name, 15)." [".str_pad($package->slug, 15, ' ', STR_PAD_BOTH)."]"); + } + + $this->output->writeln(''); + } + + $this->output->writeln('You can either get more informations about a package by typing:'); + $this->output->writeln(' '.$this->argv.' info '); + $this->output->writeln(''); + $this->output->writeln('Or you can install a package by typing:'); + $this->output->writeln(' '.$this->argv.' install '); + $this->output->writeln(''); + } + + private function setColors() + { + $this->output->getFormatter()->setStyle('normal', new OutputFormatterStyle('white')); + $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'))); + } +}