Commit 6a9b3c77 by Qiang Xue

Finished console Gii.

parent bfb9aa84
...@@ -9,9 +9,11 @@ $params = array_merge( ...@@ -9,9 +9,11 @@ $params = array_merge(
return [ return [
'id' => 'app-console', 'id' => 'app-console',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'bootstrap' => ['log'], 'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'console\controllers', 'controllerNamespace' => 'console\controllers',
'modules' => [], 'modules' => [
'gii' => 'yii\gii\Module',
],
'components' => [ 'components' => [
'log' => [ 'log' => [
'targets' => [ 'targets' => [
......
...@@ -8,8 +8,11 @@ $db = require(__DIR__ . '/db.php'); ...@@ -8,8 +8,11 @@ $db = require(__DIR__ . '/db.php');
return [ return [
'id' => 'basic-console', 'id' => 'basic-console',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'bootstrap' => ['log'], 'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'app\commands', 'controllerNamespace' => 'app\commands',
'modules' => [
'gii' => 'yii\gii\Module',
],
'components' => [ 'components' => [
'cache' => [ 'cache' => [
'class' => 'yii\caching\FileCache', 'class' => 'yii\caching\FileCache',
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\gii\console;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Action extends \yii\base\Action
{
/**
* @var \yii\gii\Generator
*/
public $generator;
/**
* @inheritdoc
*/
public function run()
{
echo "Loading generator '$this->id'...\n\n";
if ($this->generator->validate()) {
$files = $this->generator->generate();
$answers = [];
foreach ($files AS $file) {
$answers[$file->id] = true;
}
$params['hasError'] = $this->generator->save($files, (array)$answers, $results);
$params['results'] = $results;
echo $params['hasError'];
echo "\n";
echo $results;
} else {
echo "Attribute Errors\n";
echo "----------------\n";
foreach ($this->generator->errors AS $attribute => $errors) {
echo "$attribute: " . implode('; ', $errors) . "\n";
}
echo "\n";
}
}
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\gii\console;
use yii\helpers\Console;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class GenerateAction extends \yii\base\Action
{
/**
* @var \yii\gii\Generator
*/
public $generator;
/**
* @var GenerateController
*/
public $controller;
/**
* @inheritdoc
*/
public function run()
{
echo "Running '{$this->generator->getName()}'...\n\n";
if ($this->generator->validate()) {
$this->generateCode();
} else {
$this->displayValidationErrors();
}
}
protected function displayValidationErrors()
{
$this->controller->stdout("Code not generated. Please fix the following errors:\n\n", Console::FG_RED);
foreach ($this->generator->errors as $attribute => $errors) {
echo ' - ' . $this->controller->ansiFormat($attribute, Console::FG_CYAN) . ': ' . implode('; ', $errors) . "\n";
}
echo "\n";
}
protected function generateCode()
{
$files = $this->generator->generate();
$n = count($files);
if ($n === 0) {
echo "No code to be generated.\n";
return;
}
echo "The following files will be generated:\n";
$skipAll = $this->controller->interactive ? null : true;
$answers = [];
foreach ($files as $file) {
$path = $file->getRelativePath();
if (is_file($file->path)) {
if (file_get_contents($file->path) === $file->content) {
echo ' ' . $this->controller->ansiFormat('[unchanged]', Console::FG_GREY);
echo $this->controller->ansiFormat(" $path\n", Console::FG_CYAN);
$answers[$file->id] = false;
} else {
echo ' ' . $this->controller->ansiFormat('[changed]', Console::FG_RED);
echo $this->controller->ansiFormat(" $path\n", Console::FG_CYAN);
if ($skipAll !== null) {
$answers[$file->id] = !$skipAll;
} else {
$answer = $this->controller->select("Do you want to overwrite this file?", [
'y' => 'Overwrite this file.',
'n' => 'Skip this file.',
'ya' => 'Overwrite this and the rest of the changed files.',
'na' => 'Skip this and the rest of the changed files.',
]);
$answers[$file->id] = $answer === 'y' || $answer === 'ya';
if ($answer === 'ya') {
$skipAll = false;
} elseif ($answer === 'na') {
$skipAll = true;
}
}
}
} else {
echo ' ' . $this->controller->ansiFormat('[new]', Console::FG_GREEN);
echo $this->controller->ansiFormat(" $path\n", Console::FG_CYAN);
$answers[$file->id] = true;
}
}
if (!array_sum($answers)) {
$this->controller->stdout("\nNo files were chosen to be generated.\n", Console::FG_CYAN);
return;
}
if (!$this->controller->confirm("\nReady to generate the selected files?", true)) {
$this->controller->stdout("\nNo file was generated.\n", Console::FG_CYAN);
return;
}
if ($this->generator->save($files, (array)$answers, $results)) {
$this->controller->stdout("\nFiles were generated successfully!\n", Console::FG_GREEN);
} else {
$this->controller->stdout("\nSome errors occurred while generating the files.", Console::FG_RED);
}
echo preg_replace('%<span class="error">(.*?)</span>%', '\1', $results) . "\n";
}
}
...@@ -8,17 +8,21 @@ ...@@ -8,17 +8,21 @@
namespace yii\gii\console; namespace yii\gii\console;
use Yii; use Yii;
use yii\base\InlineAction;
use yii\console\Controller; use yii\console\Controller;
/** /**
* Allows you to run Gii from the command line. * This is the command line version of Gii - a code generator.
* Example command: *
* You can use this command to generate models, controllers, etc. For example,
* to generate an ActiveRecord model based on a DB table, you can run:
* *
* ``` * ```
* $ ./yii gii/<generator> --property1=foo --property2=bar --generate=true * $ ./yii gii/model --tableName=city --modelClass=City
* ``` * ```
* *
* @author Tobias Munk <schmunk@usrbin.de> * @author Tobias Munk <schmunk@usrbin.de>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class GenerateController extends Controller class GenerateController extends Controller
...@@ -31,6 +35,9 @@ class GenerateController extends Controller ...@@ -31,6 +35,9 @@ class GenerateController extends Controller
* @var boolean whether to generate all files and overwrite existing files * @var boolean whether to generate all files and overwrite existing files
*/ */
public $generate = false; public $generate = false;
/**
* @var array a list of the available code generators
*/
public $generators = []; public $generators = [];
/** /**
...@@ -45,19 +52,6 @@ class GenerateController extends Controller ...@@ -45,19 +52,6 @@ class GenerateController extends Controller
public function __get($name) public function __get($name)
{ {
return isset($this->_options[$name]) ? $this->_options[$name] : null; return isset($this->_options[$name]) ? $this->_options[$name] : null;
// todo: should determine which options are valid
if ($this->action) {
$options = $this->options($this->action->id);
if (in_array($name, $options)) {
return isset($this->_options[$name]) ? $this->_options[$name] : null;
} else {
return parent::__get($name);
}
} elseif (array_key_exists($name, $this->_options)) {
return $this->_options[$name];
} else {
return parent::__get($name);
}
} }
/** /**
...@@ -66,20 +60,11 @@ class GenerateController extends Controller ...@@ -66,20 +60,11 @@ class GenerateController extends Controller
public function __set($name, $value) public function __set($name, $value)
{ {
$this->_options[$name] = $value; $this->_options[$name] = $value;
return;
// todo: should determine which options are valid
if ($this->action) {
$options = $this->options($this->action->id);
if (in_array($name, $options)) {
$this->_options[$name] = $value;
} else {
parent::__set($name, $value);
}
} else {
$this->_options[$name] = $value;
}
} }
/**
* @inheritdoc
*/
public function init() public function init()
{ {
parent::init(); parent::init();
...@@ -88,8 +73,12 @@ class GenerateController extends Controller ...@@ -88,8 +73,12 @@ class GenerateController extends Controller
} }
} }
/**
* @inheritdoc
*/
public function createAction($id) public function createAction($id)
{ {
/** @var $action GenerateAction */
$action = parent::createAction($id); $action = parent::createAction($id);
foreach ($this->_options as $name => $value) { foreach ($this->_options as $name => $value) {
$action->generator->$name = $value; $action->generator->$name = $value;
...@@ -105,13 +94,21 @@ class GenerateController extends Controller ...@@ -105,13 +94,21 @@ class GenerateController extends Controller
$actions = []; $actions = [];
foreach ($this->generators as $name => $generator) { foreach ($this->generators as $name => $generator) {
$actions[$name] = [ $actions[$name] = [
'class' => 'yii\gii\console\Action', 'class' => 'yii\gii\console\GenerateAction',
'generator' => $generator, 'generator' => $generator,
]; ];
} }
return $actions; return $actions;
} }
public function actionIndex()
{
$this->run('/help', ['gii']);
}
/**
* @inheritdoc
*/
public function getUniqueID() public function getUniqueID()
{ {
return $this->id; return $this->id;
...@@ -139,8 +136,12 @@ class GenerateController extends Controller ...@@ -139,8 +136,12 @@ class GenerateController extends Controller
*/ */
public function getActionHelpSummary($action) public function getActionHelpSummary($action)
{ {
/** @var $action Action */ if ($action instanceof InlineAction) {
return $action->generator->getName(); return parent::getActionHelpSummary($action);
} else {
/** @var $action GenerateAction */
return $action->generator->getName();
}
} }
/** /**
...@@ -148,9 +149,13 @@ class GenerateController extends Controller ...@@ -148,9 +149,13 @@ class GenerateController extends Controller
*/ */
public function getActionHelp($action) public function getActionHelp($action)
{ {
/** @var $action Action */ if ($action instanceof InlineAction) {
$description = $action->generator->getDescription(); return parent::getActionHelp($action);
return wordwrap(preg_replace('/\s+/', ' ', $description)); } else {
/** @var $action GenerateAction */
$description = $action->generator->getDescription();
return wordwrap(preg_replace('/\s+/', ' ', $description));
}
} }
/** /**
...@@ -166,7 +171,10 @@ class GenerateController extends Controller ...@@ -166,7 +171,10 @@ class GenerateController extends Controller
*/ */
public function getActionOptionsHelp($action) public function getActionOptionsHelp($action)
{ {
/** @var $action Action */ if ($action instanceof InlineAction) {
return parent::getActionOptionsHelp($action);
}
/** @var $action GenerateAction */
$attributes = $action->generator->attributes; $attributes = $action->generator->attributes;
unset($attributes['templates']); unset($attributes['templates']);
$hints = $action->generator->hints(); $hints = $action->generator->hints();
...@@ -176,7 +184,7 @@ class GenerateController extends Controller ...@@ -176,7 +184,7 @@ class GenerateController extends Controller
$type = gettype($value); $type = gettype($value);
$options[$name] = [ $options[$name] = [
'type' => $type === 'NULL' ? 'string' : $type, 'type' => $type === 'NULL' ? 'string' : $type,
'required' => $action->generator->isAttributeRequired($name), 'required' => $value === null && $action->generator->isAttributeRequired($name),
'default' => $value, 'default' => $value,
'comment' => isset($hints[$name]) ? $this->formatHint($hints[$name]) : '', 'comment' => isset($hints[$name]) ? $this->formatHint($hints[$name]) : '',
]; ];
......
...@@ -294,14 +294,24 @@ class HelpController extends Controller ...@@ -294,14 +294,24 @@ class HelpController extends Controller
if (!empty($args)) { if (!empty($args)) {
foreach ($args as $name => $arg) { foreach ($args as $name => $arg) {
echo $this->formatOptionHelp('- ' . $this->ansiFormat($name, Console::FG_CYAN), $arg['required'], $arg['type'], $arg['default'], $arg['comment']) . "\n\n"; echo $this->formatOptionHelp(
'- ' . $this->ansiFormat($name, Console::FG_CYAN),
$arg['required'],
$arg['type'],
$arg['default'],
$arg['comment']) . "\n\n";
} }
} }
if (!empty($options)) { if (!empty($options)) {
$this->stdout("\nOPTIONS\n\n", Console::BOLD); $this->stdout("\nOPTIONS\n\n", Console::BOLD);
foreach ($options as $name => $option) { foreach ($options as $name => $option) {
echo $this->formatOptionHelp($this->ansiFormat('--' . $name, Console::FG_RED), !empty($option['required']), $option['type'], $option['default'], $option['comment']) . "\n\n"; echo $this->formatOptionHelp(
$this->ansiFormat('--' . $name, Console::FG_RED, empty($option['required']) ? Console::FG_RED : Console::BOLD),
!empty($option['required']),
$option['type'],
$option['default'],
$option['comment']) . "\n\n";
} }
} }
} }
......
...@@ -737,8 +737,8 @@ class BaseConsole ...@@ -737,8 +737,8 @@ class BaseConsole
top: top:
$input = $options['default'] $input = $options['default']
? static::input("$text [" . $options['default'] . ']: ') ? static::input("$text [" . $options['default'] . '] ')
: static::input("$text: "); : static::input("$text ");
if (!strlen($input)) { if (!strlen($input)) {
if (isset($options['default'])) { if (isset($options['default'])) {
...@@ -767,7 +767,7 @@ class BaseConsole ...@@ -767,7 +767,7 @@ class BaseConsole
* @param boolean $default this value is returned if no selection is made. * @param boolean $default this value is returned if no selection is made.
* @return boolean whether user confirmed * @return boolean whether user confirmed
*/ */
public static function confirm($message, $default = true) public static function confirm($message, $default = false)
{ {
echo $message . ' (yes|no) [' . ($default ? 'yes' : 'no') . ']:'; echo $message . ' (yes|no) [' . ($default ? 'yes' : 'no') . ']:';
$input = trim(static::stdin()); $input = trim(static::stdin());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment