dever 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #!/usr/bin/env php
  2. <?php
  3. define('PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  4. class Dever
  5. {
  6. protected static $package = 'http://dever.shemic.com/';
  7. protected static $command = array();
  8. protected static $check = false;
  9. public static function handle()
  10. {
  11. global $argc, $argv;
  12. if (isset($argv) && isset($argv[1])) {
  13. self::out('loading...');
  14. $method = 'method_' . $argv[1];
  15. if (!method_exists(__CLASS__, $method)) {
  16. self::error();
  17. }
  18. if (isset($argv[3])) {
  19. unset($argv[0]);
  20. unset($argv[1]);
  21. $name = $argv[2];
  22. unset($argv[2]);
  23. self::$method($name, $argv);
  24. } elseif (isset($argv[2])) {
  25. self::$method($argv[2]);
  26. } else {
  27. self::$method();
  28. }
  29. self::exe();
  30. } else {
  31. self::error();
  32. }
  33. }
  34. protected static function input($text, $default = '')
  35. {
  36. if (is_array($text)) {
  37. $text = implode("\n", $text);
  38. }
  39. self::out($text);
  40. $stdin = fopen('php://stdin', 'r');
  41. $shell = trim(fgets($stdin, 100));
  42. return $shell ? $shell : $default;
  43. }
  44. protected static function out($text)
  45. {
  46. echo $text . "\n";
  47. }
  48. protected static function error()
  49. {
  50. self::out('error');
  51. die;
  52. }
  53. protected static function notice()
  54. {
  55. self::out('您的php不支持system,请联系空间商或管理员开启system。');
  56. die;
  57. }
  58. public static function command($command)
  59. {
  60. array_push(self::$command, $command);
  61. }
  62. public static function exe()
  63. {
  64. if (!self::check()) {
  65. self::notice();
  66. }
  67. if (self::$command) {
  68. system(implode(' && ', self::$command));
  69. }
  70. echo "Finished!\n";
  71. }
  72. protected static function check()
  73. {
  74. if (self::$check) {
  75. return true;
  76. }
  77. if (!function_exists('system')) {
  78. return false;
  79. }
  80. return true;
  81. }
  82. protected static function method_init($type = 'main')
  83. {
  84. self::command('sudo cp -R dever /usr/bin/dever');
  85. self::command('sudo chmod +x /usr/bin/dever');
  86. if ($type == 'composer') {
  87. //$HOME
  88. $composer = '/usr/bin/composer';
  89. //$shell = 'sudo curl -sS http://packagist.cn/composer/installer | sudo php -d detect_unicode=Off';
  90. self::command('sudo curl http://getcomposer.org/installer | sudo php -d detect_unicode=Off');
  91. self::command('sudo mv composer.phar ' . $composer);
  92. self::command('sudo chmod +x ' . $composer);
  93. self::command('composer install --optimize-autoloader');
  94. } else {
  95. self::method_install('init');
  96. }
  97. }
  98. protected static function method_up($type = 'main')
  99. {
  100. if ($type == 'composer') {
  101. self::command('composer update --optimize-autoloader');
  102. } else {
  103. self::method_install('init');
  104. }
  105. }
  106. protected static function method_opt()
  107. {
  108. self::command('composer dump-autoload --optimize');
  109. }
  110. protected static function method_git()
  111. {
  112. self::command('sudo apt-get install git-core');
  113. }
  114. protected static function method_pack($name, $path = '')
  115. {
  116. if ($name == 'init') {
  117. $path = array
  118. (
  119. 'build/',
  120. 'config/',
  121. 'boot.php',
  122. 'web/data/assets/index.html',
  123. 'web/data/cache/index.html',
  124. 'web/data/compile/index.html',
  125. 'web/data/database/index.html',
  126. 'web/data/manage/index.html',
  127. 'web/data/node/index.html',
  128. 'web/data/project/index.html',
  129. 'web/data/sql/index.html',
  130. 'web/data/upload/index.html',
  131. 'web/data/index.html',
  132. 'web/package/boot.php',
  133. 'web/package/index.html',
  134. 'web/application/boot.php',
  135. 'web/application/index.html',
  136. );
  137. $path = implode(' ', $path);
  138. } else {
  139. if (!$path) {
  140. $path = $name;
  141. }
  142. if (is_array($path)) {
  143. $method = 1;
  144. if (isset($path[3]) && $path[3] == '-p') {
  145. unset($path[3]);
  146. $method = 2;
  147. }
  148. $temp = $path;
  149. $path = '';
  150. foreach ($temp as $k => $v) {
  151. if ($method == 2) {
  152. $n = explode('/', $v);
  153. $m = count($n) - 2;
  154. $n = $n[$m];
  155. if (!is_dir(PATH . 'web/data/assets/' . $n)) {
  156. $n = 'default';
  157. }
  158. $template = $v . 'template';
  159. if (!is_dir(PATH . $template)) {
  160. $template = $v . 'main/template';
  161. }
  162. $e = ' web/data/assets/' . $n . ' web/data/compile/' . $n . ' --exclude=' . $template;
  163. } else {
  164. $e = ' --exclude=' . $v . 'template';
  165. }
  166. $path .= ' ' . $v . $e;
  167. }
  168. }
  169. }
  170. self::command('tar -czf install/' . $name . '.tar.gz ' . $path . ' --exclude-vcs');
  171. }
  172. protected static function method_init_laravel()
  173. {
  174. self::command('composer create-project laravel/laravel --prefer-dist');
  175. }
  176. protected static function method_create()
  177. {
  178. self::method_install('create');
  179. }
  180. protected static function method_mysql()
  181. {
  182. $text = array
  183. (
  184. '请输入命令以执行相应操作:'
  185. , 'backup:备份mysql'
  186. , 'restore:恢复mysql'
  187. , '请在输入命令之后按回车键',
  188. );
  189. $shell = self::input($text);
  190. switch ($shell) {
  191. case 'backup':
  192. Mysql::backup();
  193. break;
  194. case 'restore':
  195. Mysql::restore();
  196. break;
  197. default:
  198. echo "未定义的方法";
  199. break;
  200. }
  201. }
  202. protected static function method_install($value)
  203. {
  204. $value .= '.tar.gz';
  205. if (self::check()) {
  206. self::command('wget -c ' . self::$package . $value);
  207. self::command('tar -zxvf ' . $value);
  208. self::command('rm -rf ' . $value);
  209. if ($value == 'composer') {
  210. self::method_init($value);
  211. }
  212. } elseif (class_exists('PharData')) {
  213. self::$check = true;
  214. $path = dirname(__FILE__) . '/';
  215. file_put_contents($path . $value, file_get_contents(self::$package . $value));
  216. $phar = new PharData($value);
  217. $phar->extractTo($path, null, true);
  218. unlink($path . $value);
  219. } else {
  220. self::notice();
  221. }
  222. }
  223. private static function copy($src, $dst, $path)
  224. {
  225. if (function_exists('system')) {
  226. system('cp -R ' . $src . ' ' . $dst);
  227. } else {
  228. $path = str_replace(array('/', '..'), '', $path);
  229. $dst = $dst . $path;
  230. if (!is_dir($dst)) {
  231. mkdir($dst);
  232. }
  233. $dir = opendir($src);
  234. while (false !== ($file = readdir($dir))) {
  235. if (($file != '.') && ($file != '..')) {
  236. if (is_dir($src . '/' . $file)) {
  237. $this->copyDir($src . '/' . $file, $dst . '/' . $file);
  238. } else {
  239. copy($src . '/' . $file, $dst . '/' . $file);
  240. }
  241. }
  242. }
  243. closedir($dir);
  244. }
  245. }
  246. protected static function method_build($path = 'vendor')
  247. {
  248. //$exts = ['php','js','css','html'];
  249. $exts = array('php', 'js', 'css', 'html');
  250. $dir = dirname(__FILE__) . '/build/';
  251. $path && system('cp -R ' . $path . ' ' . $dir);
  252. $file = 'dever.phar';
  253. $phar = new Phar($dir . $file, FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, $file);
  254. $phar->startBuffering();
  255. if ($path) {
  256. foreach ($exts as $ext) {
  257. $phar->buildFromDirectory($dir, '/\.' . $ext . '$/');
  258. }
  259. }
  260. $index = 'boot.php';
  261. $phar->buildFromIterator
  262. (
  263. new ArrayIterator
  264. (
  265. array
  266. (
  267. $index => $dir . $index,
  268. )
  269. )
  270. );
  271. //$phar->delete('build.php');
  272. $phar->setStub($phar->createDefaultStub($index, $index));
  273. $phar->compressFiles(Phar::GZ);
  274. $phar->stopBuffering();
  275. //system('cp -R ' . $dir . $file . ' ' . $file);
  276. $path && system('rm -rf ' . $dir . $path);
  277. }
  278. }
  279. class Create extends Dever
  280. {
  281. }
  282. class Mysql extends Dever
  283. {
  284. protected static function common()
  285. {
  286. $info['host'] = self::input('请输入mysql的主机地址:默认为localhost', 'localhost');
  287. $info['username'] = self::input('请输入mysql的账号:默认为root', 'root');
  288. $info['password'] = self::input('请输入mysql的密码:默认为空', '');
  289. $info['database'] = self::input('请输入mysql的数据库名:');
  290. if (!$info['host'] || !$info['username'] || !$info['password'] || !$info['database']) {
  291. self::out('请输入正确的数据库信息!');die;
  292. }
  293. $info['file'] = PATH . 'web/data/sql/' . $info['database'];
  294. $info['shell'] = ' -u' . $info['username'] . ' -p' . $info['password'] . ' -h' . $info['host'] . ' ';
  295. return $info;
  296. }
  297. public static function backup()
  298. {
  299. $info = self::common();
  300. $info['table'] = self::input('请输入mysql的表名(不输入则备份整个' . $info['database'] . '数据库):');
  301. $info['type'] = self::input('请输入备份类型:1为备份全部,2为备份结构,3为备份数据');
  302. $info['shell'] = 'mysqldump ' . $info['shell'];
  303. if ($info['type'] == 2) {
  304. $info['shell'] .= ' -d ' . $info['database'];
  305. } elseif ($info['type'] == 3) {
  306. $info['shell'] .= ' -t ' . $info['database'];
  307. } else {
  308. $info['shell'] .= ' ' . $info['database'];
  309. }
  310. if ($info['table']) {
  311. $info['shell'] .= ' ' . $info['table'];
  312. $info['file'] .= '.' . $info['table'];
  313. }
  314. $info['file'] .= '.sql';
  315. $info['shell'] .= ' > ' . $info['file'];
  316. self::command($info['shell']);
  317. self::out('操作成功,输出路径:' . $info['file']);
  318. }
  319. public static function restore()
  320. {
  321. $info = self::common();
  322. $info['new'] = self::input('请输入要恢复的全新数据库:为空则使用上边填的数据库', $info['database']);
  323. $create_table = 'mysqladmin ' . $info['shell'] . ' create ' . $info['new'];
  324. $info['shell'] = 'mysql ' . $info['shell'] . ' ' . $info['new'];
  325. $info['file'] .= '.sql';
  326. $info['shell'] .= ' < ' . $info['file'];
  327. self::exe($create_table);
  328. self::command($info['shell']);
  329. self::out('操作成功,您已成功恢复' . $info['file'] . '里的数据');
  330. }
  331. }
  332. Dever::handle();