Browse Source

Merge pull request #10819 from cakephp/version-shell

Add version shell and --version
Mark Story 8 years ago
parent
commit
d2a00a3c07

+ 41 - 1
src/Console/CommandRunner.php

@@ -19,6 +19,7 @@ use Cake\Console\ConsoleIo;
 use Cake\Console\Exception\StopException;
 use Cake\Console\Shell;
 use Cake\Http\BaseApplication;
+use Cake\Shell\VersionShell;
 use RuntimeException;
 
 /**
@@ -41,6 +42,13 @@ class CommandRunner
     protected $root;
 
     /**
+     * Alias mappings.
+     *
+     * @var array
+     */
+    protected $aliases = [];
+
+    /**
      * Constructor
      *
      * @param \Cake\Http\BaseApplication $app The application to run CLI commands for.
@@ -50,6 +58,32 @@ class CommandRunner
     {
         $this->app = $app;
         $this->root = $root;
+        $this->aliases = [
+            '--version' => 'version'
+        ];
+    }
+
+    /**
+     * Replace the entire alias map for a runner.
+     *
+     * Aliases allow you to define alternate names for commands
+     * in the collection. This can be useful to add top level switches
+     * like `--version` or `-h`
+     *
+     * ### Usage
+     *
+     * ```
+     * $runner->setAliases(['--version' => 'version']);
+     * ```
+     *
+     * @param array $aliases The map of aliases to replace.
+     * @return $this
+     */
+    public function setAliases(array $aliases)
+    {
+        $this->aliases = $aliases;
+
+        return $this;
     }
 
     /**
@@ -64,7 +98,10 @@ class CommandRunner
     {
         $this->app->bootstrap();
 
-        $commands = $this->app->console(new CommandCollection());
+        $commands = new CommandCollection([
+            'version' => VersionShell::class,
+        ]);
+        $commands = $this->app->console($commands);
         if (!($commands instanceof CommandCollection)) {
             $type = is_object($commands) ? get_class($commands) : gettype($commands);
             throw new RuntimeException(
@@ -111,6 +148,9 @@ class CommandRunner
      */
     protected function getShell(ConsoleIo $io, CommandCollection $commands, $name)
     {
+        if (isset($this->aliases[$name])) {
+            $name = $this->aliases[$name];
+        }
         if (!$commands->has($name)) {
             throw new RuntimeException(
                 "Unknown command `{$this->root} {$name}`." .

+ 35 - 0
src/Shell/VersionShell.php

@@ -0,0 +1,35 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.5.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+
+namespace Cake\Shell;
+
+use Cake\Console\Shell;
+use Cake\Core\Configure;
+
+/**
+ * Print out the version of CakePHP in use.
+ */
+class VersionShell extends Shell
+{
+    /**
+     * Print out the version of CakePHP in use.
+     *
+     * @return void
+     */
+    public function main()
+    {
+        $this->out(Configure::version());
+    }
+}

+ 9 - 11
tests/TestCase/Console/CommandRunnerTest.php

@@ -138,19 +138,17 @@ class CommandRunnerTest extends TestCase
      *
      * @return void
      */
-    public function testRunVersionLongOption()
+    public function testRunVersionAlias()
     {
-        $this->markTestIncomplete();
-    }
+        $app = $this->getMockBuilder(BaseApplication::class)
+            ->setMethods(['middleware', 'bootstrap'])
+            ->setConstructorArgs([$this->config])
+            ->getMock();
 
-    /**
-     * Test using `cake -v` invokes the version command
-     *
-     * @return void
-     */
-    public function testRunVersionShortOption()
-    {
-        $this->markTestIncomplete();
+        $output = new ConsoleOutput();
+        $runner = new CommandRunner($app, 'cake');
+        $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
+        $this->assertContains(Configure::version(), $output->messages()[0]);
     }
 
     /**

+ 1 - 1
tests/TestCase/Shell/CompletionShellTest.php

@@ -116,7 +116,7 @@ class CompletionShellTest extends TestCase
         $output = $this->out->output;
 
         $expected = 'TestPlugin.example TestPlugin.sample TestPluginTwo.example unique welcome ' .
-            "cache i18n orm_cache plugin routes server i18m sample testing_dispatch\n";
+            "cache i18n orm_cache plugin routes server version i18m sample testing_dispatch\n";
         $this->assertTextEquals($expected, $output);
     }