From 4ab2e986c6ccf6f1d89db1971e353b0c32d8df6d Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 28 Sep 2013 01:10:38 +0400 Subject: [PATCH] Advanced application: init script can now be executed in non-interactive input mode Usage: init --env=Development init --env=Production --- apps/advanced/README.md | 3 ++- apps/advanced/init | 72 ++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/apps/advanced/README.md b/apps/advanced/README.md index 6860c11..cdc10eb 100644 --- a/apps/advanced/README.md +++ b/apps/advanced/README.md @@ -103,7 +103,8 @@ GETTING STARTED After you install the application, you have to conduct the following steps to initialize the installed application. You only need to do these once for all. -1. Execute the `init` command and select `dev` as environment. +1. Execute the `init` command and select `dev` as environment. Alternatively you can execute it as `init --env=Development` +or `init --env=Production`. 2. Create a new database. It is assumed that MySQL InnoDB is used. If not, adjust `console/migrations/m130524_201442_init.php`. 3. In `common/config/params.php` set your database details in `components.db` values. diff --git a/apps/advanced/init b/apps/advanced/init index 17ed854..3a8f6a6 100755 --- a/apps/advanced/init +++ b/apps/advanced/init @@ -1,27 +1,49 @@ #!/usr/bin/env php $name) { - echo " [$i] $name\n"; +echo "Yii Application Initialization Tool v1.0\n\n"; + +$envName = null; +if (empty($params['env'])) { + echo "Which environment do you want the application to be initialized in?\n\n"; + foreach ($envNames as $i => $name) { + echo " [$i] $name\n"; + } + echo "\n Your choice [0-" . (count($envs) - 1) . ', or "q" to quit] '; + $answer = trim(fgets(STDIN)); + + if (!ctype_digit($answer) || !in_array($answer, range(0, count($envs) - 1))) { + echo "\n Quit initialization.\n"; + exit(1); + } + + if(isset($envNames[$answer])) { + $envName = $envNames[$answer]; + } +} +else { + $envName = $params['env']; } -echo "\n Your choice [0-" . (count($envs) - 1) . ', or "q" to quit] '; -$answer = trim(fgets(STDIN)); -if (!ctype_digit($answer) || !isset($envNames[$answer])) { - echo "\n Quit initialization.\n"; - return; + +if (!in_array($envName, $envNames)) { + $envsList = implode(', ', $envNames); + echo "\n $envName is not a valid environment. Try one of the following: $envsList. \n"; + exit(2); } -$env = $envs[$envNames[$answer]]; -echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] "; -$answer = trim(fgets(STDIN)); -if (strncasecmp($answer, 'y', 1)) { - echo "\n Quit initialization.\n"; - return; +$env = $envs[$envName]; + +if (empty($params['env'])) { + echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] "; + $answer = trim(fgets(STDIN)); + if (strncasecmp($answer, 'y', 1)) { + echo "\n Quit initialization.\n"; + exit(1); + } } echo "\n Start initialization ...\n\n"; @@ -110,3 +132,23 @@ function copyFile($root, $source, $target, &$all) file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source)); return true; } + +function getParams() +{ + $rawParams = array(); + if (isset($_SERVER['argv'])) { + $rawParams = $_SERVER['argv']; + array_shift($rawParams); + } + + $params = array(); + foreach ($rawParams as $param) { + if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) { + $name = $matches[1]; + $params[$name] = isset($matches[3]) ? $matches[3] : true; + } else { + $params[] = $param; + } + } + return $params; +}