My current project at work started in late July, and we have been able to use Jenkins for Continuous Integration and Deployment into our Alpha and Beta environments. We have multiple people committing to the project branch, which is new for us, as historically we had one merge master. Basically this project is a guinea pig for a different approach in our department, which luckily for me, is currently working. The downside to multiple people merging into the project is no overall ownership or gatekeeper to what is on the project branch. This can lead to some issues..
We are doing releases every 3rd week, and getting the code, database configuration and filesystem configuration all rolled onto the production platform is now a doddle since it is automated for Alpha/Beta. Right now we haven’t automated the full roll to production, however we are in the process of making this happen. One part of the deployment we seem to forget however, is removing redundant files. We remove the files from git, great, but we forget to add the files we removed to a bash script which does the file removal for us on the production platform. Not so great really!
As project lead (or a just being conscientious), I really don’t want to be leaving redundant/dead code on the production servers, so I wanted a way of knowing what was in git and what was in production. I therefore came up with “phpflg” which stands for PHP File List Generator. Yes, I know, you can do most, if not all of this using bash commands, however there isn’t much fun in that and I wanted to play with a few things
So, first up, this is how you install it:
$ sudo pear channel-discover benmatselby.github.com/pear $ sudo pear channel-discover pear.phix-project.org $ sudo pear channel-discover pear.phpunit.de $ sudo pear install benmatselby/PHPFLG downloading PHPFLG-1.0.0.tgz ... Starting to download PHPFLG-1.0.0.tgz (8,963 bytes) .....done: 8,963 bytes install ok: channel://benmatselby.github.com/pear/PHPFLG-1.0.0
Notice the GitHub PEAR Channel!! Once you have installed it, you should be able to run it like:
$ phpflg --help
phpflg 1.0.0
Usage: phpflg [switches]
--base-path Folder to generate file list from.
--prefix-path The prefix to be added to all files.
--exclude-path Path to be excluded from the file list.
--suffixes Suffix limiter for the file list.
--prefixes Prefix limiter for the file list.
--transformer Which printer to use to output the file list.
Options include text (Default), json, php.
--report-file Report output filename.
--help Outputs help information.
--version Outputs version information.
As you can see, it’s fairly flexible in the files to generate the list for (–exclude-path, –suffixes, –prefixes etc) and its fairly flexible in the format of the output (text based, json based or php variable and it’s trivial to add more transformers.)
This mainly wraps File_Iterator from Sebastian Bergmann.
So, now I can run “phpflg” from within my git folder, use –prefix-path to add the filesystem path on our production servers to generate me a list of files that is in git. I do the same on our production platform and then do a diff against the two files. Hopefully I only have files to remove, rather than files that are in git and not on our product servers once the product is live!
Some examples of the output are below.
Basic text based output:
$ phpflg --base-path src/php/ phpflg 1.0.0 src/php/PHPFLG/Generator.php src/php/PHPFLG/Printer/File.php src/php/PHPFLG/Printer/Screen.php src/php/PHPFLG/Printer.php src/php/PHPFLG/TextUI/Application.php src/php/PHPFLG/Transformer/Json.php src/php/PHPFLG/Transformer/Php.php src/php/PHPFLG/Transformer/Text.php src/php/PHPFLG/Transformer.php
PHP based output:
$ phpflg --base-path src/php/ --transformer Php phpflg 1.0.0 $phpflgFileList = array ( 0 => 'src/php/PHPFLG/Generator.php', 1 => 'src/php/PHPFLG/Printer/File.php', 2 => 'src/php/PHPFLG/Printer/Screen.php', 3 => 'src/php/PHPFLG/Printer.php', 4 => 'src/php/PHPFLG/TextUI/Application.php', 5 => 'src/php/PHPFLG/Transformer/Json.php', 6 => 'src/php/PHPFLG/Transformer/Php.php', 7 => 'src/php/PHPFLG/Transformer/Text.php', 8 => 'src/php/PHPFLG/Transformer.php', )
Adding prefix to the files:
$ phpflg --base-path src/php/ --prefix-path /path/to/production/folder/ phpflg 1.0.0 /path/to/production/folder/src/php/PHPFLG/Generator.php /path/to/production/folder/src/php/PHPFLG/Printer/File.php /path/to/production/folder/src/php/PHPFLG/Printer/Screen.php /path/to/production/folder/src/php/PHPFLG/Printer.php /path/to/production/folder/src/php/PHPFLG/TextUI/Application.php /path/to/production/folder/src/php/PHPFLG/Transformer/Json.php /path/to/production/folder/src/php/PHPFLG/Transformer/Php.php /path/to/production/folder/src/php/PHPFLG/Transformer/Text.php /path/to/production/folder/src/php/PHPFLG/Transformer.php
So, that gives a basic overview of what the little tool can do, and I imagine I’ll be the only one to ever use it, but it let me learn about:
- Phix
- Making your GitHub account a PEAR channel
Phix
So, phix.
Phix makes it extremely easy to create and maintain your own PEAR-installer compatible components for reuse in your PHP applications
All I can say to that is, Yes it does. When I first starting building phpflg, I did not use Phix, mainly as I was ignorant to it, and wanted to just get some functionality working before looking into all of the options out there.
I spoke to Sebastian Marek who had taken Stuart Herbert’s tutorial at the PHPNW11 Conference: “Maintainable Applications in PHP Using Components” who convinced me to look into Phix, so I did. Within about 20-30 minutes of being told to look into it, I had converted phpflg into a Phix based application, with all the magic you get out of the box: “Pear Packaging, Phing Build File, Autoloading, folder structure ready for most use cases and so on”
Myself and Seb then spent a while just looking over all the things I wanted to change so we could feed this back to Stuart:
- Using phpunit.xml.dist file in build file, so I could have phpunit.xml overriding any local requirements I had.
- I didn’t want the phpunit.xml file to state the logging locations, as when I run the test locally I don’t want all the files it generates, although having the above means I could stop that if I wanted.
- I didn’t want the build.xml file to restrict the running of PHPUnit to ${project.src.testunitdir} as I had phpt tests to test the command based UI.
- PHPUnit to run in –strict mode.
- I wanted to have DocBlox as my Documentation generator.
We past all this information onto Stuart who has been very accommodating with the whole experience.
Excellent stuff, from now on, I’ll be using Phix, and everyone else should at least look into it.
Making your GitHub account a PEAR channel
I saw a tweet that mentioned you could do this, which I saved for later. However, since I had phpflg ready, and was now using Phix, it didn’t seem that arduous to make a pear package and then try and setup a channel on GitHub. So quick search lead me back to the original article I had seen:
How to serve PHP/Pear packages with GitHub
Side note: There are many articles covering this now, I have no idea which was the first, so apologies for not linking to the original!
On this topic, all I can say is follow the instructions and you have yourself a PEAR channel on GitHub. One thing to note here is, I’ve found that the PEAR installation on the machine you want to install a GitHub based PEAR package onto, needs to be running PEAR 1.9.4 (It may be a few dot releases earlier than .4, but certainly .1 doesn’t work)
Lastly, I was building a command line tool, so I had phpflg.php in the bin directory, which I really just wanted to be phpflg when installed on the users machine. Phix, currently as of 3rd November 2011, does not support this in an automated fashion, so you have to make a manual change.
When you have run
$ phing pear-package
In the folder of your Phix based application it will generate you a tarball in the dist folder. You need to edit the package.xml file within it. Right at the bottom of the file just before the closing of the </package> tag, add:
<phprelease>
<filelist>
<install as="phpflg" name="/phpflg.php" />
</filelist>
</phprelease>
Obviously you will need to change the names for your application, but what this tells PEAR to do once installed is create phpflg without the .php extension.
It all then works nicely.
So, Go play around with Phix, it’s fantastic. Go create a GitHub PEAR channel, and if you don’t want to figure out the bash command to generate file lists (Yes, Yes, I hear Seb Marek in my head saying it’s easy Ben, it’s easy!!), then install phpflg.
And if you do create a PEAR channel, do not forget to register it with Pyri
