用PHP做构建工具

最近,基于Javascript的构建工具泛滥式增长,几乎每半年流行一种构建工具。

我的一个开源项目CodeMommy WebPHP一直使用Gulp作为构建工具,那个时候Grunt已经落伍,WebPack还没有流行。

相关代码:

// https://github.com/CodeMommy/WebPHP/blob/df43012ad0b27535a0259883e7385c239592b793/gulpfile.js

// Config
const version = '1.1.0';

// Require
const del = require('del');
const gulp = require('gulp');
const zip = require('gulp-zip');
const git = require('gulp-git');
const bump = require('gulp-bump');

// Task Clear
gulp.task('clear', function (cb) {
    return del([
        './application/cache/smarty_cache/*.php',
        './application/cache/smarty_template/*.php',
        './application/cache/log/*.log',
        '!.gitkeep'
    ], cb);
});

// Task New Version
gulp.task('version', function () {
    return gulp.src(['./package.json', './composer.json'])
        .pipe(bump({version: version}))
        .pipe(gulp.dest('./'));
});

// Task Publish
var filename = 'CodeMommyWebPHP-v' + version + '.zip';
gulp.task('publish_clear', function (cb) {
    return del(['./publish/' + filename], cb);
});
gulp.task('publish_pack', function (cb) {
    return gulp.src(['./**', '!./publish', '!./node_modules/**', '!./node_modules', '!./package.json', '!./gulpfile.js', '!./composer.lock'])
        .pipe(zip(filename))
        .pipe(gulp.dest('./publish'));
});
gulp.task('publish', gulp.series('publish_clear', 'publish_pack'));

// Task Git
gulp.task('git-add', function () {
    return gulp.src('.')
        .pipe(git.add());
});
gulp.task('git-commit', function () {
    return gulp.src('.')
        .pipe(git.commit('Bumped version number to v' + version));
});
gulp.task('git-push', function (cb) {
    return git.push('origin', 'master', cb);
});
gulp.task('git-new-tag', function (cb) {
    return git.tag('v' + version, 'v' + version, function (error) {
        if (error) {
            return cb(error);
        }
        return git.push('origin', 'master', {args: '--tags'}, cb);
    });
});

// Task Default
var taskGit = gulp.series('git-add', 'git-commit', 'git-push', 'git-new-tag');
var taskPublish = 'publish';
var taskDefault = gulp.series('clear', 'version', gulp.parallel(taskPublish, taskGit));
gulp.task('default', taskDefault);

昨天突发奇想,既然我的这个开源项目是使用PHP写的,为什么构建工具不用PHP写呢?

于是我写了如下代码:

<?php
// https://github.com/CodeMommy/WebPHP/blob/master/task.php
/**
 * CodeMommy WebPHP
 * @author Candison November <www.kandisheng.com>
 */
require_once(__DIR__ . '/vendor/autoload.php');
use CodeMommy\TaskPHP\Task;
use Symfony\Component\Filesystem\Filesystem;
/**
 * Task Update Version
 */
function taskUpdateVersion()
{
    $file = __DIR__ . '/composer.json';
    $composer = file_get_contents($file);
    $composer = json_decode($composer, true);
    $version = $composer['version'];
    $version = explode('.', $version);
    $version[2] = intval($version[2]) + 1;
    $version = implode('.', $version);
    $composer['version'] = $version;
    $composer = json_encode($composer, JSON_PRETTY_PRINT);
    $composer = str_replace('\\/', '/', $composer);
    file_put_contents($file, $composer);
    echo(sprintf('Updated version to %s.', $version));
}
Task::add('update-version', 'Update Version', 'taskUpdateVersion');
/**
 * Task Clear
 */
function taskClear()
{
    $removeList = array();
    array_push($removeList, __DIR__ . '/application/_runtime');
    $fileSystem = new Filesystem();
    foreach ($removeList as $value) {
        $fileSystem->remove($value);
    }
    echo(sprintf('Clear Finished.'));
}
Task::add('clear', 'Clear', 'taskClear');
/**
 * Task Run
 */
function taskRun()
{
    $port = intval(Task::getParameter(0, 80));
    if ($port == 80) {
        echo(sprintf('Visit http://localhost%s', PHP_EOL));
    } else {
        echo(sprintf('Visit http://localhost:%s%s', $port, PHP_EOL));
    }
    system(sprintf('cd public && php -S 0.0.0.0:%s', $port));
}
Task::add('run', 'Run', 'taskRun');
/**
 * Task Update
 */
function taskUpdate()
{
    system('php -v');
    Task::line();
    system('git pull');
    Task::line();
    system('composer self-update');
    Task::line();
    system('composer update');
}
Task::add('update', 'Update Composer', 'taskUpdate');
/**
 * Task Test
 */
function taskTest()
{
    $test = Task::getParameter(0);
    if (empty($test)) {
        system('phpunit');
    } else {
        system(sprintf('cd test && phpunit %sTest --repeat 100', $test));
    }
}
Task::add('test', 'Test', 'taskTest');
/**
 * Start
 */
Task::config('CodeMommy WebPHP Task', '');
Task::run();

突然发现,原来用PHP做构建工具简直不能再快捷优雅了!

您可能还喜欢...