审查视图

application/admin/command/Min.php 6.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;

class Min extends Command
{

    /**
     * 路径和文件名配置
     */
    protected $options = [
        'cssBaseUrl'  => 'public/assets/css/',
        'cssBaseName' => '{module}',
        'jsBaseUrl'   => 'public/assets/js/',
        'jsBaseName'  => 'require-{module}',
    ];

    protected function configure()
    {
        $this
                ->setName('min')
                ->addOption('module', 'm', Option::VALUE_REQUIRED, 'module name(frontend or backend),use \'all\' when build all modules', null)
                ->addOption('resource', 'r', Option::VALUE_REQUIRED, 'resource name(js or css),use \'all\' when build all resources', null)
30
                ->addOption('optimize', 'o', Option::VALUE_OPTIONAL, 'optimize type(uglify|closure|none)', 'none')
31 32 33 34 35
                ->setDescription('Compress js and css file');
    }

    protected function execute(Input $input, Output $output)
    {
Karson authored
36 37
        $module = $input->getOption('module') ?: '';
        $resource = $input->getOption('resource') ?: '';
38
        $optimize = $input->getOption('optimize') ?: 'none';
39
40
        if (!$module || !in_array($module, ['frontend', 'backend', 'all'])) {
41 42
            throw new Exception('Please input correct module name');
        }
43
        if (!$resource || !in_array($resource, ['js', 'css', 'all'])) {
44 45 46 47 48 49 50 51 52 53
            throw new Exception('Please input correct resource name');
        }

        $moduleArr = $module == 'all' ? ['frontend', 'backend'] : [$module];
        $resourceArr = $resource == 'all' ? ['js', 'css'] : [$resource];

        $minPath = __DIR__ . DS . 'Min' . DS;
        $publicPath = ROOT_PATH . 'public' . DS;
        $tempFile = $minPath . 'temp.js';
54
        $nodeExec = '';
55
56 57
        if (!$nodeExec) {
            if (IS_WIN) {
58
                // Winsows下请手动配置配置该值,一般将该值配置为 '"C:\Program Files\nodejs\node.exe"',除非你的Node安装路径有变更
59
                $nodeExec = 'C:\Program Files\nodejs\node.exe';
60
                if (file_exists($nodeExec)) {
61
                    $nodeExec = '"' . $nodeExec . '"';
62
                } else {
63 64 65 66
                    // 如果 '"C:\Program Files\nodejs\node.exe"' 不存在,可能是node安装路径有变更
                    // 但安装node会自动配置环境变量,直接执行 '"node.exe"' 提高第一次使用压缩打包的成功率
                    $nodeExec = '"node.exe"';
                }
67 68
            } else {
                try {
69
                    $nodeExec = exec("which node");
70
                    if (!$nodeExec) {
71 72
                        throw new Exception("node environment not found!please install node first!");
                    }
73
                } catch (Exception $e) {
74
                    throw new Exception($e->getMessage());
75
                }
76 77 78
            }
        }
79 80
        foreach ($moduleArr as $mod) {
            foreach ($resourceArr as $res) {
81 82 83 84 85 86
                $data = [
                    'publicPath'  => $publicPath,
                    'jsBaseName'  => str_replace('{module}', $mod, $this->options['jsBaseName']),
                    'jsBaseUrl'   => $this->options['jsBaseUrl'],
                    'cssBaseName' => str_replace('{module}', $mod, $this->options['cssBaseName']),
                    'cssBaseUrl'  => $this->options['cssBaseUrl'],
87 88
                    'jsBasePath'  => str_replace(DS, '/', ROOT_PATH . $this->options['jsBaseUrl']),
                    'cssBasePath' => str_replace(DS, '/', ROOT_PATH . $this->options['cssBaseUrl']),
89
                    'optimize'    => $optimize,
90 91 92 93 94
                    'ds'          => DS,
                ];

                //源文件
                $from = $data["{$res}BasePath"] . $data["{$res}BaseName"] . '.' . $res;
95
                if (!is_file($from)) {
96 97 98
                    $output->error("{$res} source file not found!file:{$from}");
                    continue;
                }
99
                if ($res == "js") {
100
                    $content = file_get_contents($from);
101
                    preg_match("/require\.config\(\{[\r\n]?[\n]?+(.*?)[\r\n]?[\n]?}\);/is", $content, $matches);
102
                    if (!isset($matches[1])) {
103 104 105 106 107 108 109 110 111 112 113 114
                        $output->error("js config not found!");
                        continue;
                    }
                    $config = preg_replace("/(urlArgs|baseUrl):(.*)\n/", '', $matches[1]);
                    $data['config'] = $config;
                }
                // 生成压缩文件
                $this->writeToFile($res, $data, $tempFile);

                $output->info("Compress " . $data["{$res}BaseName"] . ".{$res}");

                // 执行压缩
115
                $command = "{$nodeExec} \"{$minPath}r.js\" -o \"{$tempFile}\" >> \"{$minPath}node.log\"";
116
                if ($output->isDebug()) {
117 118 119
                    $output->warning($command);
                }
                echo exec($command);
120 121 122
            }
        }
123
        if (!$output->isDebug()) {
124 125
            @unlink($tempFile);
        }
126 127 128 129 130 131 132 133 134 135 136 137 138 139

        $output->info("Build Successed!");
    }

    /**
     * 写入到文件
     * @param string $name
     * @param array $data
     * @param string $pathname
     * @return mixed
     */
    protected function writeToFile($name, $data, $pathname)
    {
        $search = $replace = [];
140
        foreach ($data as $k => $v) {
141 142 143 144 145 146
            $search[] = "{%{$k}%}";
            $replace[] = $v;
        }
        $stub = file_get_contents($this->getStub($name));
        $content = str_replace($search, $replace, $stub);
147
        if (!is_dir(dirname($pathname))) {
148 149 150 151 152 153 154 155 156 157 158 159
            mkdir(strtolower(dirname($pathname)), 0755, true);
        }
        return file_put_contents($pathname, $content);
    }

    /**
     * 获取基础模板
     * @param string $name
     * @return string
     */
    protected function getStub($name)
    {
Karson authored
160
        return __DIR__ . DS . 'Min' . DS . 'stubs' . DS . $name . '.stub';
161 162
    }
}