MinifyCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace Dcat\Admin\Console;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Str;
  6. use Symfony\Component\Process\Process;
  7. class MinifyCommand extends Command
  8. {
  9. const ALL = 'all';
  10. const DEFAULT = 'default';
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'admin:minify {name}
  17. {--color= : Theme color code}
  18. {--publish : Publish assets files}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Minify the CSS and JS';
  25. /**
  26. * @var array
  27. */
  28. protected $colors = [
  29. self::DEFAULT => '',
  30. 'blue' => '#6d8be6',
  31. 'blue-light' => '#62a8ea',
  32. 'green' => '#4e9876',
  33. ];
  34. /**
  35. * @var string
  36. */
  37. protected $packagePath;
  38. /**
  39. * @var \Illuminate\Filesystem\Filesystem
  40. */
  41. protected $files;
  42. /**
  43. * Execute the console command.
  44. */
  45. public function handle()
  46. {
  47. $this->packagePath = realpath(__DIR__.'/../..');
  48. $this->files = $this->laravel['files'];
  49. $name = $this->argument('name');
  50. if ($name === static::ALL) {
  51. // 编译所有内置主题色
  52. return $this->compileAllColors();
  53. }
  54. $publish = $this->option('publish');
  55. $color = $this->getColor($name);
  56. $this->backupFiles();
  57. $this->replaceFiles($name, $color);
  58. try {
  59. $this->npmInstall();
  60. $this->info("[$name][$color] npm run production...");
  61. // 编译
  62. $this->runProcess("cd {$this->packagePath} && npm run prod", 1800);
  63. if ($publish) {
  64. $this->publishAssets();
  65. }
  66. } finally {
  67. // 重置文件
  68. $this->resetFiles();
  69. }
  70. }
  71. /**
  72. * 编译所有内置主题.
  73. */
  74. protected function compileAllColors()
  75. {
  76. foreach ($this->colors as $name => $_) {
  77. $this->call('admin:minify', ['name' => $name]);
  78. }
  79. }
  80. /**
  81. * 发布静态资源.
  82. */
  83. protected function publishAssets()
  84. {
  85. $options = ['--provider' => 'Dcat\Admin\AdminServiceProvider', '--force' => true, '--tag' => 'dcat-admin-assets'];
  86. $this->call('vendor:publish', $options);
  87. }
  88. /**
  89. * 替换文件.
  90. *
  91. * @param $name
  92. * @param $color
  93. */
  94. protected function replaceFiles($name, $color)
  95. {
  96. if ($name === static::DEFAULT) {
  97. return;
  98. }
  99. $mixFile = $this->getMixFile();
  100. $contents = str_replace('let theme = null', "let theme = '{$name}'", $this->files->get($mixFile));
  101. $this->files->put($mixFile, $contents);
  102. $colorFile = $this->getColorFile();
  103. $this->files->put($colorFile, "\$primary: $color;");
  104. }
  105. /**
  106. * 备份文件.
  107. */
  108. protected function backupFiles()
  109. {
  110. if (! is_file($this->getMixBakFile())) {
  111. $this->files->copy($this->getMixFile(), $this->getMixBakFile());
  112. } else {
  113. $this->files->delete($this->getMixFile());
  114. $this->files->copy($this->getMixBakFile(), $this->getMixFile());
  115. }
  116. if (! is_file($this->getColorBakFile())) {
  117. $this->files->copy($this->getColorFile(), $this->getColorBakFile());
  118. }
  119. }
  120. /**
  121. * 重置文件.
  122. */
  123. protected function resetFiles()
  124. {
  125. $mixFile = $this->getMixFile();
  126. $mixBakFile = $this->getMixBakFile();
  127. if (is_file($mixBakFile)) {
  128. $this->files->delete($mixFile);
  129. $this->files->copy($mixBakFile, $mixFile);
  130. $this->files->delete($mixBakFile);
  131. }
  132. $colorFile = $this->getColorFile();
  133. $colorBakFile = $this->getColorBakFile();
  134. if (is_file($colorBakFile)) {
  135. $this->files->delete($colorFile);
  136. $this->files->copy($colorBakFile, $colorFile);
  137. $this->files->delete($colorBakFile);
  138. }
  139. }
  140. /**
  141. * @return string
  142. */
  143. protected function getMixFile()
  144. {
  145. return $this->packagePath.'/webpack.mix.js';
  146. }
  147. /**
  148. * @return mixed
  149. */
  150. protected function getMixBakFile()
  151. {
  152. return str_replace('.js', '.bak.js', $this->getMixFile());
  153. }
  154. /**
  155. * @return string
  156. */
  157. protected function getColorFile()
  158. {
  159. return $this->packagePath.'/resources/assets/dcat/sass/theme/_primary.scss';
  160. }
  161. /**
  162. * @return mixed
  163. */
  164. protected function getColorBakFile()
  165. {
  166. return str_replace('.scss', '.bak.scss', $this->getColorFile());
  167. }
  168. /**
  169. * 安装依赖.
  170. */
  171. protected function npmInstall()
  172. {
  173. if (is_dir($this->packagePath.'/node_modules')) {
  174. return;
  175. }
  176. $this->info('npm install...');
  177. $this->runProcess("cd {$this->packagePath} && npm install");
  178. }
  179. /**
  180. * 获取颜色.
  181. *
  182. * @param string $name
  183. *
  184. * @return string
  185. */
  186. protected function getColor($name)
  187. {
  188. if ($name === static::DEFAULT) {
  189. return '';
  190. }
  191. INPUT_COLOR:
  192. $color = $this->option('color');
  193. if (! $color && isset($this->colors[$name])) {
  194. return $this->colors[$name];
  195. }
  196. if (! $color) {
  197. $color = $this->formatColor($this->ask('Please enter a color code(hex)'));
  198. }
  199. if (! $color) {
  200. goto INPUT_COLOR;
  201. }
  202. return $this->formatColor($color);
  203. }
  204. /**
  205. * @param string $color
  206. *
  207. * @return string
  208. */
  209. protected function formatColor($color)
  210. {
  211. if ($color && ! Str::startsWith($color, '#')) {
  212. $color = "#$color";
  213. }
  214. return $color;
  215. }
  216. /**
  217. * 执行命令.
  218. *
  219. * @param string $command
  220. * @param int $timeout
  221. */
  222. protected function runProcess($command, $timeout = 1800)
  223. {
  224. $process = Helper::process($command, $timeout);
  225. $process->run(function ($type, $data) {
  226. if ($type === Process::ERR) {
  227. $this->warn($data);
  228. } else {
  229. $this->info($data);
  230. }
  231. });
  232. }
  233. }