Robo – The Awesome Task Runner For PHP
Web Development is Hard. There are so many things you have to take into consideration during the course of building web applications, especially when you want to follow best practices and deliver the best product.
One of those several actions is automating tasks.
We have task runners like Grunt and Gulp, these two do an amazing job of automating several tasks. In short, virtually every web developer has adopted its use in building quality apps.
Grunt and Gulp are built in Javascript. I am a core PHP and JS developer, but I have discovered over time that some( maybe a few ) PHP devs don’t really know a lot about what’s dumped in the grunt and gulp files, instead they have the know-how to tweak things a little bit to get by,..which is good!
What if you had a replica of that?..What if it’s written in the language you shield as a sword daily?. What if you understand every single thing about automating those tasks?
Yes, there is a PHP TASK RUNNER called ROBO
Robo allows you to write tasks in common OOP PHP Style. You can use it to automate your tasks, start workers, run parallel tasks, execute commands, run tests and also watch file changes.
Let’s get to work
1. Installation
You can easily install it via Composer
Most times I like to do a global installation like so:
1 |
composer global require codegyre/robo |
Then I can just call robo from any directory.
2 .Create a folder, our directory structure would be like this:
——— webapp
————–app
—————–cache
—————–logs
—————–site.php
————-css
—————–app.css
—————–boostrap.css
—————–style.css
————-dist
——————uploads
————–vendor
——————bower.json
3. Now just run robo from the terminal. You should see a prompt like this:
Agree by pressing Y.
Now you have a RoboFile.php created. The RoboFile will reside in the root of your directory.
Go ahead and dump all sorts of different css content into app.css, bootstrap.css and style.css.
Create several .txt
files inside the cache
and logs
folder.
Let your RoboFile.php look like so:
RoboFile.php
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php /** * This is project's console commands configuration for Robo task runner. * * @see http://robo.li/ */ class RoboFile extends RoboTasks { // define public methods as commands public function startServer() { $this->taskServer(8000) ->dir('app') ->arg('app/site.php') ->run(); } public function clean() { $this->_cleanDir(['app/cache', 'app/logs']); $this->_deleteDir(['dist-extra/uploads']); } public function build() { // concat CSS files $this->taskConcat(['css/app.css','css/bootstrap.css', 'css/style.css']) ->to('css/all.css') ->run(); // minify CSS files $this->taskMinify('css/all.css') ->to('css/all.min.css') ->run(); // install Bower dependencies $this->taskBowerInstall() ->dir('vendor') ->run(); } public function test() { // runs PHPUnit tests $this->taskPHPUnit() ->run(); } } |
Every public method serves as a command and it obeys camelCase format in writing the methods.
So I can run the first method from the terminal like so:
1 |
robo start:server |
That will start the inbuilt PHP server and load site.php
Second method , clean()
1 2 3 4 5 |
public function clean() { $this->_cleanDir(['app/cache', 'app/logs']); $this->_deleteDir(['dist-extra/uploads']); } |
_cleanDir
deletes all files in a folder. In this instance, it deletes all the files in the cache
and logs
folder.
_deleteDir
deletes the folder directory. In this case, it deletes the uploads
folder.
So, run robo clean
like so:
Third method, build()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function build() { // concat CSS files $this->taskConcat(['css/app.css','css/bootstrap.css', 'css/style.css']) ->to('css/all.css') ->run(); // minify CSS files $this->taskMinify('css/all.css') ->to('css/all.min.css') ->run(); // install Bower dependencies $this->taskBowerInstall() ->dir('vendor') ->run(); } |
As you can easily decipher from the code, it concatenates all the three css files into just one and helps to create a minified version too.
It can also install the bower dependencies present in our bower.json file.
Run it like so: robo build
Fourth method, test()
1 2 3 4 5 6 |
public function test() { // runs PHPUnit tests $this->taskPHPUnit() ->run(); } |
This invokes a globally or locally installed PHPUnit.
Run it like so: robo test
I have an error on my terminal because I don’t have any version of PHPUnit installed.
Conclusion
There are so many things you can accomplish with Robo. Git, ssh, Docker, Vagrant and so many other tasks that need automation. Check out one example used in Codeception here. You can now imagine the vast possibilities after going through that file.
Head over to their documentation, dig deeper and do exploits with Robo
Please if you have any observations or comments, feel free to drop it in the comments section below.
- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016