Browse Source

初始化脚本

wechat_public_accounts
blobt 5 years ago
parent
commit
13bf9fb642
  1. 23
      environments/skeleton/common/config/main-local.php
  2. 3
      environments/skeleton/common/config/params-local.php
  3. 7
      environments/skeleton/console/config/main-local.php
  4. 3
      environments/skeleton/console/config/params-local.php
  5. 3
      environments/skeleton/console/config/test-local.php
  6. 121
      init.php

23
environments/skeleton/common/config/main-local.php

@ -0,0 +1,23 @@
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=',
'username' => '',
'password' => '',
'charset' => 'utf8',
'enableSchemaCache' => true,
'schemaCacheDuration' => 86400, // time in seconds
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
],
];

3
environments/skeleton/common/config/params-local.php

@ -0,0 +1,3 @@
<?php
return [
];

7
environments/skeleton/console/config/main-local.php

@ -0,0 +1,7 @@
<?php
return [
'bootstrap' => ['gii'],
'modules' => [
'gii' => 'yii\gii\Module',
],
];

3
environments/skeleton/console/config/params-local.php

@ -0,0 +1,3 @@
<?php
return [
];

3
environments/skeleton/console/config/test-local.php

@ -0,0 +1,3 @@
<?php
return [
];

121
init.php

@ -10,7 +10,26 @@ if (!extension_loaded('openssl')) {
die('The OpenSSL PHP extension is required by Yii2.');
}
$params = getParams();
$root = str_replace('\\', '/', __DIR__);
$skeletonPath = "environments/skeleton";
/* 初始化基础骨架 */
echo "\nDo you want to init the skeleton? [Yes|No]\n";
$answer = trim(fgets(STDIN));
if ($answer == 'yes') {
echo "\n Skeleton initialization ...\n\n";
$files = getFileList($skeletonPath);
foreach ($files as $file) {
$all = false;
if (!copyFile($root, "$skeletonPath/$file", $file, $all, $params)) {
break;
}
}
echo "\n ... skeleton initialization completed.\n\n";
exit;
}
/* 输入app名称 */
echo "Please enter the app name:";
@ -44,8 +63,6 @@ $type = $answer;
//TODO 后面希望通过这个脚本 可以创建web后台 H5前台 API等程序的基础模板,这些模板保存在environment目录里面
/**
* Prints error message.
* @param string $message message
@ -107,4 +124,104 @@ function formatMessage($message, $styles) {
return sprintf("\x1b[%sm", implode(';', array_map('getStyleCode', $styles))) . $message . "\x1b[0m";
}
/**
* 获取给定目录底下的所有文件
* @param string $root
* @param string $basePath
* @return array
*/
function getFileList($root, $basePath = '') {
$files = [];
$handle = opendir($root);
while (($path = readdir($handle)) !== false) {
if ($path === '.git' || $path === '.svn' || $path === '.' || $path === '..') {
continue;
}
$fullPath = "$root/$path";
$relativePath = $basePath === '' ? $path : "$basePath/$path";
if (is_dir($fullPath)) {
$files = array_merge($files, getFileList($fullPath, $relativePath));
} else {
$files[] = $relativePath;
}
}
closedir($handle);
return $files;
}
/**
* 文件复制
* @param string $root
* @param string $source
* @param string $target
* @param boolean $all
* @param array $params
* @return boolean
*/
function copyFile($root, $source, $target, &$all, $params) {
if (!is_file($root . '/' . $source)) {
echo " skip $target ($source not exist)\n";
return true;
}
if (is_file($root . '/' . $target)) {
if (file_get_contents($root . '/' . $source) === file_get_contents($root . '/' . $target)) {
echo " unchanged $target\n";
return true;
}
if ($all) {
echo " overwrite $target\n";
} else {
echo " exist $target\n";
echo " ...overwrite? [Yes|No|All|Quit] ";
$answer = !empty($params['overwrite']) ? $params['overwrite'] : trim(fgets(STDIN));
if (!strncasecmp($answer, 'q', 1)) {
return false;
} else {
if (!strncasecmp($answer, 'y', 1)) {
echo " overwrite $target\n";
} else {
if (!strncasecmp($answer, 'a', 1)) {
echo " overwrite $target\n";
$all = true;
} else {
echo " skip $target\n";
return true;
}
}
}
}
file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
return true;
}
echo " generate $target\n";
@mkdir(dirname($root . '/' . $target), 0777, true);
file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
return true;
}
/**
* 获取输出参数
* @return array
*/
function getParams() {
$rawParams = [];
if (isset($_SERVER['argv'])) {
$rawParams = $_SERVER['argv'];
array_shift($rawParams);
}
$params = [];
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;
}
?>
Loading…
Cancel
Save