MinifyCommand.php 6.3 KB

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