codegyre/robo
Task runner for PHP that lets you write automation scripts in OO PHP. Provides built-in tasks for common workflows (filesystem, git, composer, ssh, testing, packaging) and is easily extensible for custom tasks and CLI commands.
Installation Add Robo to your project via Composer:
composer require codegyre/robo --dev
Initialize a basic RoboFile.php in your project root:
vendor/bin/robo init
This generates a skeleton file with common tasks.
First Task
Define a simple task in RoboFile.php:
<?php
class RoboFile extends \Robo\Tasks
{
public function hello()
{
$this->yell('Hello, Robo!');
}
}
Run it via CLI:
vendor/bin/robo hello
Key Files to Explore
RoboFile.php: Your primary task definition file.robo.phar: The standalone binary (if using the PHAR version).Task Composition Break tasks into reusable subtasks:
public function deploy()
{
$this->taskExec('git pull')
->run();
$this->taskComposerInstall()
->run();
$this->taskExec('php artisan migrate')
->run();
}
Argument Handling
Use @param annotations for CLI arguments:
/**
* @param string $name
*/
public function backup($name = 'default')
{
$this->taskExec("mysqldump -u user -p db_name > backups/{$name}.sql")
->run();
}
Run with:
vendor/bin/robo backup:custom
YAML/JSON Configuration Load external configs for flexibility:
$config = $this->yaml()->load('config/tasks.yml');
$this->taskExec($config['command'])->run();
Parallel Execution Run tasks concurrently for speed:
$this->taskExec('php artisan queue:work')
->parallel()
->run();
Integration with Laravel Use Robo for Laravel-specific tasks (e.g., testing, deployments):
public function test()
{
$this->taskExec('php artisan test')
->dir(base_path())
->run();
}
composer.json scripts:
"scripts": {
"pre-commit": "robo lint"
}
- run: vendor/bin/robo deploy:staging
composer.json:
"extra": {
"robo": {
"commands": {
"build": "build:prod"
}
}
}
Task Naming Conflicts
Avoid naming tasks the same as existing Robo tasks (e.g., exec, composerInstall).
Fix: Use unique names like customExec.
Working Directory Issues
taskExec runs in the project root by default. Use ->dir() to change context:
$this->taskExec('npm install')
->dir('resources/assets')
->run();
Error Handling
Robo tasks fail silently if not wrapped in try/catch:
try {
$this->taskExec('failing-command')->run();
} catch (\Robo\Exception\TaskException $e) {
$this->yell('Task failed: ' . $e->getMessage());
}
PHAR vs. Composer
robo.phar for standalone scripts (e.g., in Docker).vendor/bin/robo for project-specific tasks.Dependency Loading
Ensure RoboFile.php is autoloaded. Add to composer.json:
"autoload": {
"files": ["RoboFile.php"]
}
Command Registration Changes (4.0.4+)
In versions 4.0.4+, the command registration system has been refactored. If you were previously extending Robo\Runner to customize command registration, update your code to leverage the new Robo class methods:
// Old approach (pre-4.0.4)
class CustomRunner extends \Robo\Runner {
public function registerCommands() { ... }
}
// New approach (4.0.4+)
class RoboFile extends \Robo\Tasks {
public function __construct() {
parent::__construct();
$this->registerCustomCommands();
}
protected function registerCustomCommands() {
$this->addCommand('custom:task', function () { ... });
}
}
Verbose Output: Enable debug mode:
$this->logger()->debug('Debug message');
Or run with:
vendor/bin/robo --verbose task:name
Dry Runs: Test tasks without execution:
$this->taskExec('php artisan migrate')->dryRun()->run();
Logging: Use $this->logger() to track task progress:
$this->logger()->info('Starting deployment...');
Custom Tasks
Extend \Robo\Tasks to add domain-specific tasks:
class MyTasks extends \Robo\Tasks
{
public function myCustomTask() { ... }
}
Plugins Use Robo’s plugin system for shared tasks:
composer require codegyre/robo-plugin-example
Event Listeners Hook into Robo’s lifecycle (e.g., pre/post-task):
$this->eventDispatcher()->addListener('robo.task.before', function ($event) { ... });
PSR-15 Middleware Intercept task execution with middleware:
$this->taskExec('command')->middleware(MyMiddleware::class)->run();
PHP 8.2/8.1 Compatibility Leverage new PHP features like named arguments and union types in your tasks:
public function deploy(string $env = 'production', bool $force = false) { ... }
$this->taskExec('command')->arg('value')->run();
public function backup(string|array $target) { ... }
#[Task]
public function deploy() { ... }
How can I help you explore Laravel packages today?