Skip to content

cboyden/Robo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RoboTask

Modern and simple PHP task runner inspired by Grunt and Rake aimed to automate common tasks:

  • executing daemons (and workers)
  • watching filesystem changes
  • performing cleanups
  • building releases
  • running multiple Symfony Commands
  • starting PHP server
  • running tests
  • writing cross-platform scripts

Based on Symfony2 Console Component.

Installing

Composer

  • Add "codegyre/robo": "*" to composer.json.
  • Run composer install
  • Use vendor/bin/robo to execute robo tasks.

Usage

All tasks are defined as public methods in RoboFile.php. It can be created by running robo init. RoboFile has a set of predefined tasks taken from \Robo\Tasks. All tasks are included with traits. All protected methods in traits that start with task prefix are tasks and can be configured and executed in your tasks.

List of bundled tasks that can be executed from RoboFile

  • taskExec executes script. Optionally can be started in background.
  • taskServer starts PHP server. Optionally can be stopped on exit
  • taskCopyDir copies one dir into another
  • taskCleanDir empties specified dir
  • taskDeleteDir removes dir
  • taskReplaceInFile replaces string in a file
  • taskComposerInstall installs composer packages
  • taskComposerUpdate updates composer packages
  • taskSymfonyCommand running Symfony Command. (requires \Robo\Task\SymfonyCommand trait)
  • taskPackPhar creating phar archive (requires \Robo\Task\PackPhar trait)
  • taskChangeLog creating and maintaining changelog (requires \Robo\Task\Changelog trait)
  • taskWatch monitoring dir for changes and running tests when files changes (requires \Robo\Task\Watch trait)
  • taskGitHubRelease to create a GitHub release (requires \Robo\Task\Watch trait)

You can write your own tasks or execute any PHP code within tasks.

Example: running Codeception Acceptance Test

To run test we need to start a server first, and launch a Selenium Server

<?php
class RoboFile extends \Robo\Tasks
{

    function testAcceptance($seleniumPath = '~/selenium-server-standalone-2.39.0.jar')
    {
       // launches PHP server on port 8000 for web dir
       // server will be executed in background and stopped in the end
       $this->taskServer(8000)
            ->background()
            ->dir('web')
            ->run();

       // running Selenium server in background
        $this->taskExec('java -jar '.$pathToSelenium)
            ->background()
            ->run();

        // loading Symfony Command and running with passed argument
        $this->taskCommand(new \Codeception\Command\Run('run'))
            ->arg('suite','acceptance')
            ->run();
    }
}
?>

If you execute robo you will see this task added to list of available task with name: test:acceptance. To execute it you shoud run robo test:acceptance. You may change path to selenium server by passing new path as a argument:

robo test:acceptance "C:\Downloads\selenium.jar"

Example: Run Composer Update when composer.json Changes

Actually this task is useless, but it demonstrates you of using watch task so you can use it for running tests or building assets.

<?php
class RoboFile extends \Robo\Tasks {
    use \Robo\Task\Watch;

    function watchComposer()
    {
        // when composer.json changes `composer update` will be executed
        $this->taskWatch()->monitor('composer.json', function() {
            $this->taskComposerUpdate()->run();
        })->run();
    }
}
?>

Example: Cleaning Logs and Cache

<?php
class RoboFile extends \Robo\Tasks
{
    public function clean()
    {
        $this->taskCleanDir([
            'app/cache'
            'app/logs'
        ])->run();

        $this->taskDeleteDir([
            'web/assets/tmp_uploads',
        ])->run();
    }

?>

This task cleans app/cache and app/logs dirs (ignoreing .gitignore and .gitkeep files) Can be executed by running:

robo clean

Example: Creating Phar Archive

This example was extracted from Codeception and simplified:

function buildPhar()
{
    $pharTask = $this->taskPackPhar('package/codecept.phar')
        ->compress()
        ->stub('package/stub.php');

    $finder = Finder::create()
        ->ignoreVCS(true)
        ->name('*.php')
        ->in('src');

    foreach ($finder as $file) {
        $pharTask->addFile('src/'.$file->getRelativePathname(), $file->getRealPath());
    }

    $finder = Finder::create()->files()
        ->ignoreVCS(true)
        ->name('*.php')
        ->exclude('Tests')
        ->exclude('tests')
        ->in('vendor');

    foreach ($finder as $file) {
        $pharTask->addStripped('vendor/'.$file->getRelativePathname(), $file->getRealPath());
    }

    $pharTask->addFile('autoload.php', 'autoload.php')
        ->addFile('codecept', 'package/bin')
        ->run();

    $code = $this->taskExec('php package/codecept.phar')->run();
    if ($code !== 0) {
        throw new Exception("There was problem compiling phar");
    }
}

This and other example tasks. can be found in Codeception repo

Example: Publishing New Release of Robo

<?php
class Robofile extends \Robo\Tasks
{
    public function release()
    {
        // print new message
        $this->say("Releasing Robo");

        // ask for changes in this release
        $changelog = $this->taskChangelog()
            ->version(\Robo\Runner::VERSION)
            ->askForChanges();
        $changelog->run();

        // adding changelog and pushing it
        $this->taskExec('git add CHANGELOG.md')->run();
        $this->taskExec('git commit -m "updated changelog"')->run();
        $this->taskExec('git push')->run();

        // create GitHub release
        $this->taskGitHubRelease(\Robo\Runner::VERSION)
            ->uri('Codegyre/Robo')
            ->askDescription()
            ->changes($changelog->getChanges())
            ->run();
    }
}

To create new release we run:

✗ ./robo release
➜  Releasing Robo
?  Changed in this release:Mered Tasks and Traits to same file
?  Changed in this release:Added Watcher task
?  Changed in this release:Added GitHubRelease task
?  Changed in this release:Added Changelog task
?  Changed in this release:Added ReplaceInFile task
?  Changed in this release:
 [Robo\Task\ChangelogTask] Creating CHANGELOG.md
 [Robo\Task\ReplaceInFileTask] CHANGELOG.md updated
 [Robo\Task\ExecTask] running git add CHANGELOG.md
 [Robo\Task\ExecTask] running git commit -m "updated changelog"
 [Robo\Task\GitHubReleaseTask] {"url":"https://api.github.com/repo...

API

Tasks are classes that implement Robo\TaskInterface with method run defined. Each other method of task should be used for specifing task options and returns $this for fluent interface:

Tasks are including into RoboFile with traits. Traits should contain protected methods with task prefix that return new instance of a task.

See: Bundled Tasks

About

Modern task runner for PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.8%
  • Other 0.2%