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.

109 lines
2.8 KiB

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. $root = str_replace('\\', '/', __DIR__);
  12. /* 输入app名称 */
  13. echo "Please enter the app name:";
  14. $appName = trim(strtolower(fgets(STDIN)));
  15. if (!preg_match('/^[_a-z]{3,12}$/i', $appName)) {
  16. printError("App name must be 3 to 12 letters or underline");
  17. exit();
  18. }
  19. if (is_dir($root . '/' . $appName)) {
  20. printError("App {$appName} is exists");
  21. exit();
  22. }
  23. /* 输入app类型 */
  24. $types = ["web application", "wechat application", "restfull api"];
  25. echo "\n\nPlease select the type of your app.\n";
  26. foreach ($type as $i => $name) {
  27. echo " [$i] $name\n";
  28. }
  29. echo "\n Your choice [0-" . (count($types) - 1) . ', or "q" to quit] ';
  30. $answer = trim(fgets(STDIN));
  31. if (!ctype_digit($answer) || !in_array($answer, range(0, count($types) - 1))) {
  32. printError("Please enter the correct nunmber");
  33. exit();
  34. }
  35. $type = $answer;
  36. //TODO 后面希望通过这个脚本 可以创建web后台 H5前台 API等程序的基础模板,这些模板保存在environment目录里面
  37. /**
  38. * Prints error message.
  39. * @param string $message message
  40. */
  41. function printError($message) {
  42. echo "\n " . formatMessage("Error. $message", ['fg-red']) . " \n";
  43. }
  44. /**
  45. * Returns true if the stream supports colorization. ANSI colors are disabled if not supported by the stream.
  46. *
  47. * - windows without ansicon
  48. * - not tty consoles
  49. *
  50. * @return boolean true if the stream supports ANSI colors, otherwise false.
  51. */
  52. function ansiColorsSupported() {
  53. return DIRECTORY_SEPARATOR === '\\' ? getenv('ANSICON') !== false || getenv('ConEmuANSI') === 'ON' : function_exists('posix_isatty') && @posix_isatty(STDOUT);
  54. }
  55. /**
  56. * Get ANSI code of style.
  57. * @param string $name style name
  58. * @return integer ANSI code of style.
  59. */
  60. function getStyleCode($name) {
  61. $styles = [
  62. 'bold' => 1,
  63. 'fg-black' => 30,
  64. 'fg-red' => 31,
  65. 'fg-green' => 32,
  66. 'fg-yellow' => 33,
  67. 'fg-blue' => 34,
  68. 'fg-magenta' => 35,
  69. 'fg-cyan' => 36,
  70. 'fg-white' => 37,
  71. 'bg-black' => 40,
  72. 'bg-red' => 41,
  73. 'bg-green' => 42,
  74. 'bg-yellow' => 43,
  75. 'bg-blue' => 44,
  76. 'bg-magenta' => 45,
  77. 'bg-cyan' => 46,
  78. 'bg-white' => 47,
  79. ];
  80. return $styles[$name];
  81. }
  82. /**
  83. * Formats message using styles if STDOUT supports it.
  84. * @param string $message message
  85. * @param string[] $styles styles
  86. * @return string formatted message.
  87. */
  88. function formatMessage($message, $styles) {
  89. if (empty($styles) || !ansiColorsSupported()) {
  90. return $message;
  91. }
  92. return sprintf("\x1b[%sm", implode(';', array_map('getStyleCode', $styles))) . $message . "\x1b[0m";
  93. }
  94. ?>