jaz303/phake

PHP 5.3 的 Ruby 的 rake 的一个小克隆。使用闭包实现极致的酷炫。

v0.6.0 2014-01-04 20:27 UTC

This package is auto-updated.

Last update: 2024-08-23 23:47:54 UTC


README

© 2010 Jason Frame [ jason@onehackoranother.com / @jaz303 ]
在 MIT 许可下发布。

PHP 5.3 的 Ruby 的 rake 的小克隆。使用闭包实现极致的酷炫。

关于 phake 的疑问?来 Freenode 的 #phake 通道聊天吧!

使用方法

  • 下载
  • 在当前目录或父目录中创建 Phakefile
  • 使用 ./phake task:name 运行任务或 ./phake -T 列出定义的任务

定义任务

按照这种方式定义任务

task('dependency1', function() {
    echo "i will run first!\n";
});

task('dependency2', function() {
    echo "i will run second!\n";
});

task('task_name', 'dependency1', 'dependency2', function() {
    echo "i will run last!\n";
});

此任务将通过命令行 ./phake task_name 运行

如果想要创建一些“超级任务”,只需调用其他任务,则任务体是可选的

task('foo', 'dep1', 'dep2');

可以向任务添加多个体,当任务被调用时,所有体都将被执行

task('foo', function() { echo "task work item 1\n"; });
task('foo', function() { echo "task work item 2\n"; });

任务分组

像 Rake 一样,我们可以分组任务

group('db', function() {
    task('init', function() {
        echo "i'm initialising the database\n";
    });
});

将通过 ./phake db:init 调用此任务

描述任务

在定义任务之前立即调用 desc() 来设置其描述

desc("Initialises the database");
task('db_init', function() { echo "oh hai it's a database\n"; });

./phake -T 的输出

db_init    Initialises the database

后/前块

有时您可能想要指定某些代码应该在任务之前或之后运行(与依赖项不同),就像 Capistrano 一样。Phake 支持

before('foo', function() { ... });
after('baz:bar', function() { ... });

任务参数

Phake 允许在命令行上指定参数

# Execute task `quux` with the given args
./phake quux name=Jason city=Glasgow

此格式必须完全匹配;在 = 和参数名称/值之间不要放置空格。如果需要在参数值中放置空格,请将整个赋值放在引号中。

参数通过应用程序对象的 ArrayAccess 实现提供给任务

task('task_with_args', function($app) {
    $name = $app['name'];
    $city = $app['city'];
    // do some stuff...
});

中止执行

要中止任务序列的执行,只需抛出异常。

desc('Demonstrate failure');
task('fail', function() {
    throw new Exception;
});

运行 phake fail 将产生

- jason@disco phake % ./bin/phake fail
(in /Users/jason/dev/projects/phake)
aborted!
Exception 

(See full trace by running task with --trace)

一个相对完整的示例

这是一个完整的 Phakefile 可能的样子。它还突出了一些处理组时出现的更复杂的名分辨率问题。

<?php
desc('Load the application environment');
task('environment', function() {
    echo "I am the outer environment. I should run first.\n";
});

desc('Initialises the database connection');
task('database', function() {
    echo "I am initialising the database...\n";
});

group('test', function() {

    // 'environment' dependency for this task is resolved locally to
    // task in same group. There is no 'database' task defined in this
    // group so it drops back to a search of the root group.
    desc('Run the unit tests');
    task('units', 'environment', ':environment', 'database', function() {
        echo "Running unit tests...\n";
    });

    // another level of nesting; application object is passed to all
    // executing tasks
    group('all', function() {
        desc('Run absolutely every test everywhere!');
        task('run', 'test:units', function($application) {
            echo "All tests complete! ($application)\n";
        });
    });

});

// duplicate group definitions are merged
group('test', function() {

    // duplicate task definitions are merged
    // (although the first description takes precedence when running with -T)
    desc("You won't see this description");
    task('units', function() {
        echo "Running a second batch of unit tests...\n";
    });

    // use ':environment' to refer to task in root group
    // we currently have no cyclic dependency checking, you have been warned.
    task('environment', ':environment', function() {
        echo "I am the inner environment. I should run second.\n";
    });

});

task('default', 'test:all:run');
?>

这是 ./phake 的输出(隐含要运行的任务是 default

jason@ratchet phake [master*] $ ./phake
(in /Users/jason/dev/projects/phake)
I am the outer environment. I should run first.
I am the inner environment. I should run second.
I am initialising the database...
Running unit tests...
Running a second batch of unit tests...
All tests complete! (<phake\Application>)

以及相应的 phake -T 输出

jason@ratchet phake [master*] $ ./phake -T
(in /Users/jason/dev/projects/phake)
database        Initialises the database connection
environment     Load the application environment
test:all:run    Run absolutely every test everywhere!
test:units      Run the unit tests

Bash 自动完成

Bashkim Isai 创建了 phake-autocomplete,这是一个用于 phake 任务名称的 bash-completion 脚本。