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 $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. $this->files->delete($this->getMixBakFile());
  114. $this->files->copy($this->getMixFile(), $this->getMixBakFile());
  115. $this->files->delete($this->getColorBakFile());
  116. $this->files->copy($this->getColorFile(), $this->getColorBakFile());
  117. }
  118. /**
  119. * 重置文件.
  120. */
  121. protected function resetFiles()
  122. {
  123. $mixFile = $this->getMixFile();
  124. $mixBakFile = $this->getMixBakFile();
  125. if (is_file($mixBakFile)) {
  126. $this->files->delete($mixFile);
  127. $this->files->copy($mixBakFile, $mixFile);
  128. $this->files->delete($mixBakFile);
  129. }
  130. $colorFile = $this->getColorFile();
  131. $colorBakFile = $this->getColorBakFile();
  132. if (is_file($colorBakFile)) {
  133. $this->files->delete($colorFile);
  134. $this->files->copy($colorBakFile, $colorFile);
  135. $this->files->delete($colorBakFile);
  136. }
  137. }
  138. /**
  139. * @return string
  140. */
  141. protected function getMixFile()
  142. {
  143. return $this->packagePath.'/webpack.mix.js';
  144. }
  145. /**
  146. * @return mixed
  147. */
  148. protected function getMixBakFile()
  149. {
  150. return str_replace('.js', '.bak.js', $this->getMixFile());
  151. }
  152. /**
  153. * @return string
  154. */
  155. protected function getColorFile()
  156. {
  157. return $this->packagePath.'/resources/assets/dcat/sass/theme/_primary.scss';
  158. }
  159. /**
  160. * @return mixed
  161. */
  162. protected function getColorBakFile()
  163. {
  164. return str_replace('.scss', '.bak.scss', $this->getColorFile());
  165. }
  166. /**
  167. * 安装依赖.
  168. */
  169. protected function npmInstall()
  170. {
  171. if (is_dir($this->packagePath.'/node_modules')) {
  172. return;
  173. }
  174. $this->info('npm install...');
  175. $this->runProcess("cd {$this->packagePath} && npm install");
  176. }
  177. /**
  178. * 获取颜色.
  179. *
  180. * @param string $name
  181. *
  182. * @return string
  183. */
  184. protected function getColor($name)
  185. {
  186. if ($name === static::DEFAULT) {
  187. return '';
  188. }
  189. INPUT_COLOR:
  190. $color = $this->option('color');
  191. if (! $color && isset($this->themes[$name])) {
  192. return $this->themes[$name];
  193. }
  194. if (! $color) {
  195. $color = $this->formatColor($this->ask('Please enter a color code(hex)'));
  196. }
  197. if (! $color) {
  198. goto INPUT_COLOR;
  199. }
  200. return $this->formatColor($color);
  201. }
  202. /**
  203. * @param string $color
  204. *
  205. * @return string
  206. */
  207. protected function formatColor($color)
  208. {
  209. if ($color && ! Str::startsWith($color, '#')) {
  210. $color = "#$color";
  211. }
  212. return $color;
  213. }
  214. /**
  215. * 执行命令.
  216. *
  217. * @param string $command
  218. * @param int $timeout
  219. */
  220. protected function runProcess($command, $timeout = 1800)
  221. {
  222. $process = Helper::process($command, $timeout);
  223. $process->run(function ($type, $data) {
  224. if ($type === Process::ERR) {
  225. $this->warn($data);
  226. } else {
  227. $this->info($data);
  228. }
  229. });
  230. }
  231. }