You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * kcbasic初始化工具
  5. */
  6. echo "KcDev Initialization Tool v1.0\n\n";
  7. /* 检查openssl模块是否已经安装并正确加载 */
  8. if (!extension_loaded('openssl')) {
  9. die('The OpenSSL PHP extension is required by Yii2.');
  10. }
  11. $params = getParams();
  12. $root = str_replace('\\', '/', __DIR__);
  13. $skeletonPath = "environments/skeleton";
  14. /* 初始化基础骨架 */
  15. echo "\nDo you want to init the skeleton? [Yes|No]\n";
  16. $answer = trim(fgets(STDIN));
  17. if ($answer == 'yes') {
  18. echo "\n Skeleton initialization ...\n\n";
  19. $files = getFileList($skeletonPath);
  20. foreach ($files as $file) {
  21. $all = false;
  22. if (!copyFile($root, "$skeletonPath/$file", $file, $all, $params)) {
  23. break;
  24. }
  25. }
  26. echo "\n ... skeleton initialization completed.\n\n";
  27. exit;
  28. }
  29. /* 输入app名称 */
  30. echo "Please enter the app name:";
  31. $appName = trim(strtolower(fgets(STDIN)));
  32. if (!preg_match('/^[_a-z]{3,12}$/i', $appName)) {
  33. printError("App name must be 3 to 12 letters or underline");
  34. exit();
  35. }
  36. if (is_dir($root . '/' . $appName)) {
  37. printError("App {$appName} is exists");
  38. exit();
  39. }
  40. /* 输入app类型 */
  41. $types = ["web application", "wechat application", "restfull api"];
  42. echo "\n\nPlease select the type of your app.\n";
  43. foreach ($type as $i => $name) {
  44. echo " [$i] $name\n";
  45. }
  46. echo "\n Your choice [0-" . (count($types) - 1) . ', or "q" to quit] ';
  47. $answer = trim(fgets(STDIN));
  48. if (!ctype_digit($answer) || !in_array($answer, range(0, count($types) - 1))) {
  49. printError("Please enter the correct nunmber");
  50. exit();
  51. }
  52. $type = $answer;
  53. //TODO 后面希望通过这个脚本 可以创建web后台 H5前台 API等程序的基础模板,这些模板保存在environment目录里面
  54. /**
  55. * Prints error message.
  56. * @param string $message message
  57. */
  58. function printError($message) {
  59. echo "\n " . formatMessage("Error. $message", ['fg-red']) . " \n";
  60. }
  61. /**
  62. * Returns true if the stream supports colorization. ANSI colors are disabled if not supported by the stream.
  63. *
  64. * - windows without ansicon
  65. * - not tty consoles
  66. *
  67. * @return boolean true if the stream supports ANSI colors, otherwise false.
  68. */
  69. function ansiColorsSupported() {
  70. return DIRECTORY_SEPARATOR === '\\' ? getenv('ANSICON') !== false || getenv('ConEmuANSI') === 'ON' : function_exists('posix_isatty') && @posix_isatty(STDOUT);
  71. }
  72. /**
  73. * Get ANSI code of style.
  74. * @param string $name style name
  75. * @return integer ANSI code of style.
  76. */
  77. function getStyleCode($name) {
  78. $styles = [
  79. 'bold' => 1,
  80. 'fg-black' => 30,
  81. 'fg-red' => 31,
  82. 'fg-green' => 32,
  83. 'fg-yellow' => 33,
  84. 'fg-blue' => 34,
  85. 'fg-magenta' => 35,
  86. 'fg-cyan' => 36,
  87. 'fg-white' => 37,
  88. 'bg-black' => 40,
  89. 'bg-red' => 41,
  90. 'bg-green' => 42,
  91. 'bg-yellow' => 43,
  92. 'bg-blue' => 44,
  93. 'bg-magenta' => 45,
  94. 'bg-cyan' => 46,
  95. 'bg-white' => 47,
  96. ];
  97. return $styles[$name];
  98. }
  99. /**
  100. * Formats message using styles if STDOUT supports it.
  101. * @param string $message message
  102. * @param string[] $styles styles
  103. * @return string formatted message.
  104. */
  105. function formatMessage($message, $styles) {
  106. if (empty($styles) || !ansiColorsSupported()) {
  107. return $message;
  108. }
  109. return sprintf("\x1b[%sm", implode(';', array_map('getStyleCode', $styles))) . $message . "\x1b[0m";
  110. }
  111. /**
  112. * 获取给定目录底下的所有文件
  113. * @param string $root
  114. * @param string $basePath
  115. * @return array
  116. */
  117. function getFileList($root, $basePath = '') {
  118. $files = [];
  119. $handle = opendir($root);
  120. while (($path = readdir($handle)) !== false) {
  121. if ($path === '.git' || $path === '.svn' || $path === '.' || $path === '..') {
  122. continue;
  123. }
  124. $fullPath = "$root/$path";
  125. $relativePath = $basePath === '' ? $path : "$basePath/$path";
  126. if (is_dir($fullPath)) {
  127. $files = array_merge($files, getFileList($fullPath, $relativePath));
  128. } else {
  129. $files[] = $relativePath;
  130. }
  131. }
  132. closedir($handle);
  133. return $files;
  134. }
  135. /**
  136. * 文件复制
  137. * @param string $root
  138. * @param string $source
  139. * @param string $target
  140. * @param boolean $all
  141. * @param array $params
  142. * @return boolean
  143. */
  144. function copyFile($root, $source, $target, &$all, $params) {
  145. if (!is_file($root . '/' . $source)) {
  146. echo " skip $target ($source not exist)\n";
  147. return true;
  148. }
  149. if (is_file($root . '/' . $target)) {
  150. if (file_get_contents($root . '/' . $source) === file_get_contents($root . '/' . $target)) {
  151. echo " unchanged $target\n";
  152. return true;
  153. }
  154. if ($all) {
  155. echo " overwrite $target\n";
  156. } else {
  157. echo " exist $target\n";
  158. echo " ...overwrite? [Yes|No|All|Quit] ";
  159. $answer = !empty($params['overwrite']) ? $params['overwrite'] : trim(fgets(STDIN));
  160. if (!strncasecmp($answer, 'q', 1)) {
  161. return false;
  162. } else {
  163. if (!strncasecmp($answer, 'y', 1)) {
  164. echo " overwrite $target\n";
  165. } else {
  166. if (!strncasecmp($answer, 'a', 1)) {
  167. echo " overwrite $target\n";
  168. $all = true;
  169. } else {
  170. echo " skip $target\n";
  171. return true;
  172. }
  173. }
  174. }
  175. }
  176. file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
  177. return true;
  178. }
  179. echo " generate $target\n";
  180. @mkdir(dirname($root . '/' . $target), 0777, true);
  181. file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
  182. return true;
  183. }
  184. /**
  185. * 获取输出参数
  186. * @return array
  187. */
  188. function getParams() {
  189. $rawParams = [];
  190. if (isset($_SERVER['argv'])) {
  191. $rawParams = $_SERVER['argv'];
  192. array_shift($rawParams);
  193. }
  194. $params = [];
  195. foreach ($rawParams as $param) {
  196. if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
  197. $name = $matches[1];
  198. $params[$name] = isset($matches[3]) ? $matches[3] : true;
  199. } else {
  200. $params[] = $param;
  201. }
  202. }
  203. return $params;
  204. }
  205. ?>