Min.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\admin\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\input\Option;
  6. use think\console\Output;
  7. use think\Exception;
  8. class Min extends Command
  9. {
  10. /**
  11. * 路径和文件名配置
  12. */
  13. protected $options = [
  14. 'cssBaseUrl' => 'public/assets/css/',
  15. 'cssBaseName' => '{module}',
  16. 'jsBaseUrl' => 'public/assets/js/',
  17. 'jsBaseName' => 'require-{module}',
  18. ];
  19. protected function configure()
  20. {
  21. $this
  22. ->setName('min')
  23. ->addOption('module', 'm', Option::VALUE_REQUIRED, 'module name(frontend or backend),use \'all\' when build all modules', null)
  24. ->addOption('resource', 'r', Option::VALUE_REQUIRED, 'resource name(js or css),use \'all\' when build all resources', null)
  25. ->setDescription('Compress js and css file');
  26. }
  27. protected function execute(Input $input, Output $output)
  28. {
  29. $module = $input->getOption('module') ? : '';
  30. $resource = $input->getOption('resource') ? : '';
  31. if (!$module || !in_array($module, ['frontend', 'backend', 'all']))
  32. {
  33. throw new Exception('Please input correct module name');
  34. }
  35. if (!$resource || !in_array($resource, ['js', 'css', 'all']))
  36. {
  37. throw new Exception('Please input correct resource name');
  38. }
  39. $moduleArr = $module == 'all' ? ['frontend', 'backend'] : [$module];
  40. $resourceArr = $resource == 'all' ? ['js', 'css'] : [$resource];
  41. $minPath = __DIR__ . DS . 'Min' . DS;
  42. $publicPath = ROOT_PATH . 'public' . DS;
  43. $tempFile = $minPath . 'temp.js';
  44. // Winsows下请手动配置配置该值
  45. $nodeExec = "";
  46. if (!$nodeExec)
  47. {
  48. if (IS_WIN)
  49. {
  50. throw new Exception("node environment require nodejs!please check http://doc.fastadmin.net/322813 !");
  51. }
  52. try
  53. {
  54. $nodeExec = exec("which node");
  55. if (!$nodeExec)
  56. {
  57. throw new Exception("node environment not found!please install node first!");
  58. }
  59. }
  60. catch (Exception $e)
  61. {
  62. throw new Exception($e->getMessage());
  63. }
  64. }
  65. foreach ($moduleArr as $mod)
  66. {
  67. foreach ($resourceArr as $res)
  68. {
  69. $data = [
  70. 'publicPath' => $publicPath,
  71. 'jsBaseName' => str_replace('{module}', $mod, $this->options['jsBaseName']),
  72. 'jsBaseUrl' => $this->options['jsBaseUrl'],
  73. 'cssBaseName' => str_replace('{module}', $mod, $this->options['cssBaseName']),
  74. 'cssBaseUrl' => $this->options['cssBaseUrl'],
  75. 'jsBasePath' => ROOT_PATH . $this->options['jsBaseUrl'],
  76. 'cssBasePath' => ROOT_PATH . $this->options['cssBaseUrl'],
  77. 'ds' => DS,
  78. ];
  79. //源文件
  80. $from = $data["{$res}BasePath"] . $data["{$res}BaseName"] . '.' . $res;
  81. if (!is_file($from))
  82. {
  83. $output->error("{$res} source file not found!file:{$from}");
  84. continue;
  85. }
  86. if ($res == "js")
  87. {
  88. $content = file_get_contents($from);
  89. preg_match("/require\.config\(\{[\n]+(.*?)\n\}\);/is", $content, $matches);
  90. if (!isset($matches[1]))
  91. {
  92. $output->error("js config not found!");
  93. continue;
  94. }
  95. $config = preg_replace("/(urlArgs|baseUrl):(.*)\n/", '', $matches[1]);
  96. $data['config'] = $config;
  97. }
  98. // 生成压缩文件
  99. $this->writeToFile($res, $data, $tempFile);
  100. $output->info("Compress " . $data["{$res}BaseName"] . ".{$res}");
  101. // 执行压缩
  102. echo exec("{$nodeExec} {$minPath}r.js -o {$tempFile} >> {$minPath}node.log");
  103. }
  104. }
  105. @unlink($tempFile);
  106. $output->info("Build Successed!");
  107. }
  108. /**
  109. * 写入到文件
  110. * @param string $name
  111. * @param array $data
  112. * @param string $pathname
  113. * @return mixed
  114. */
  115. protected function writeToFile($name, $data, $pathname)
  116. {
  117. $search = $replace = [];
  118. foreach ($data as $k => $v)
  119. {
  120. $search[] = "{%{$k}%}";
  121. $replace[] = $v;
  122. }
  123. $stub = file_get_contents($this->getStub($name));
  124. $content = str_replace($search, $replace, $stub);
  125. if (!is_dir(dirname($pathname)))
  126. {
  127. mkdir(strtolower(dirname($pathname)), 0755, true);
  128. }
  129. return file_put_contents($pathname, $content);
  130. }
  131. /**
  132. * 获取基础模板
  133. * @param string $name
  134. * @return string
  135. */
  136. protected function getStub($name)
  137. {
  138. return __DIR__ . '/Min/stubs/' . $name . '.stub';
  139. }
  140. }