Archive for the ‘Programming’ Category

Conditional tasks in Ant

Thursday, February 28th, 2013 | Programming, Tech

If you’re using Ant build scripts, there is a good chance that you will want to do some tasks conditionally – for example, if you’re using CI you will want to run your unit tests every time some code is committed – but you might only want to regenerate documentation or update a stakeholders’ preview build every night.

There is a lot of talk about conditional tags, but all these really allow you to do is set even more variables. You can use if/then/else from Ant-Contrib but that involves adding extra libraries and complicating the issue.

Actually, it turns out it is really simple to set up conditional tasks in your build process. All you need to do is call the task, but in the task header use the if attribute.

For example, I have a build task which calls all the other tasks.

<target name="build" depends="clean,checkout,nightly,phpunit,documentation" />

Even though I’m running this every time, I’m calling the “nighty” task. Below shows how I define that.

<target name="nightly" if="${env.NIGHTLY}">

Finally, in my Jenkins CI install, I make the project a parameterised build, and add a boolean called NIGHTLY that defaults to false. I can then also trigger a build by cron that specifies the NIGHTLY parameter as true, so that when it runs on a night, it runs the additional tasks as well.

Installing Imagick on Ubuntu 12

Sunday, February 10th, 2013 | Programming, Tech

To install Imagick, you would normally do this via PECL.

sudo pecl install imagick

However, when trying this on Ubuntu 12.04 LTS, you may get an error similar to the following.

checking ImageMagick MagickWand API configuration program...
configure: error: not found. Please provide a path to MagickWand-config or Wand-config program

You can resolve this by installing a few extra packages.

sudo apt-get install libmagickwand-dev libmagickcore-dev

Now try re-running the original command and it should be successful.

No acceptable C compiler found on Mac

Monday, February 4th, 2013 | Programming, Tech

If you’re trying to compile from source, you may get an error similar to the following.

mac configure: error: no acceptable C compiler found in $PATH

This may be because you haven’t installed Xcode. If you have, something has broken, and you need to reinstall it. First, remove it with the following command.

mac configure: error: no acceptable C compiler found in $PATH

Now restart your system and once it has started again, go to Applications and select Install Xcode. This will reinstall it.

Web workers

Saturday, February 2nd, 2013 | Limited, Programming

These days, we’re all trying to do very clever things in the browser, that take up heaps of system resources. Often, your application will be doing so much that the system can barely cope – and with JavaScript being a single thread language, heavy processing can tie the UI up and make the user think the page has crashed.

Enter web workers – a background process that essentially allows you to do concurrent JavaScript. You can create a background process from a script, and then send messages back and forth between it and the page, ensuring you don’t tie up the UI.

They’re really simple to use too.

var worker = new Worker('worker.js');

worker.addEventListener('message', function(e) {
  console.log('Message from worker: ', e.data);
}, false);

worker.postMessage('Hello, World!');

Then create a simple JavaScript for your worker.

this.addEventListener('message', function(message) {
  this.postMessage(message.data);
}, false);

You can read more about web workers on HTML5 Rocks.

Beanstalkd

Sunday, January 27th, 2013 | Limited, Programming

Do you need a work queue system but think a message queuing system like Rabbit or Active MQ would simply be too complicated? Probably not, but imagine you did. Enter Beanstalkd. The super simple work queue daemon.

I went down to Leeds PHP for the first time in a while last week, where there was a talk about Beanstalkd. It’s effectively a FIFO queue – you can put a message in the end, and have another process take a message out of the start, the effect being that you can take some of the load off server intensive processes, and do them later when you’re not busy.

I had a play around with it in PHP, using the Pheanstalk and it really is a simple as that to work. You create a connection to your Beanstalkd server, and then just push and pull messages from the various queues, that you can create on the fly, like Mongo.

Installing PECL YAML on Mac OSX Lion

Sunday, December 16th, 2012 | Programming, Tech

If you are trying to install YAML via PECL on Lion, you may get an error such as the following.

configure: error: Please install libyaml
ERROR: `/private/tmp/pear/install/yaml/configure --with-yaml' failed

Some solutions on the internet suggest installing it via a package manager.

rvm pkg install libyaml

However, although this process will claim to work, when you come to run the command again, it will fail at the same point. Instead, you need to download libyaml, which you can do here, then extract it, cd into the directory and run the following commands.

./configure
make
make install

You don’t need to sudo. You should now be able to run the install command again.

sudo pecl install --ignore-errors yaml

This time it should be successful.

Conflict error on PECL YAML

Monday, December 10th, 2012 | Programming, Tech

If you are trying to install PECL YAML on Mac OS X Lion, you may find you get a conflict with an error message similar to the following.

WARNING: pecl.php.net/yaml: conflicting files found:
yaml/LICENSE (pear.symfony-project.com/yaml)

This error is documented on GitHub and as it is not critical, can be overcome by turning off errors.

pecl install --ignore-errors yaml

On running that command, it should no longer stop at that point.

PHP complains date.timezone is not set

Wednesday, November 28th, 2012 | Programming, Tech

Sometimes, PHP will kick up a fuss complaining about date.timezone not being set. We found this on the Symfony framework and were able to replicate it on standalone scripts.

[Exception]
DateTime::__construct(): It is not safe to rely on the system's timezone settings.
You are required to use the date.timezone setting or the date_default_timezone_set()
function. In case you used any of those methods and you are still getting this
warning, you most likely misspelled the timezone identifier.
We selected 'Europe/Berlin' for 'CEST/2.0/DST' instead

Normally, you would fix this by editing your php.ini file and adding a declaration there.

date.timezone = "Europe/Berlin"

But we had already done this and it still wasn’t working! After some further investigation, it seemed that PHP simply couldn’t access the value.

echo(ini_get("date.timezone"));

This doesn’t make any sense, and we never got to the bottom of what was going on, but there are two ways around the problem. Firstly, you could modify your PHP script so that it makes a call to set the system timezone.

date_default_timezone_set("Europe/Berlin");

However, this involves having to modify your code, which is bad as you don’t want to have to set the timezone manually, especially in a piece of code which could be deployed to servers in different timezones.

A better approach is to set it in the vhosts directive in Apache.

php_value "date.timezone" "Europe/Berlin"

This isn’t the cleanest solution but allowed us to solve an otherwise unexplainable error.

Missing psych (for YAML output)

Friday, November 16th, 2012 | Programming, Tech

If you’re using YAML in Ruby and have it installed via RVM, you might get a notice similar to the following.

It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.

You can solve this using the following commands.

rvm pkg install libyaml
rvm reinstall 1.9.3

You may (or even need) to replace 1.9.3 with your exact version number.

Wing Commander using Composer

Wednesday, November 14th, 2012 | Programming

Back in July, I wrote about Wing Commander, a little library I had written to draw together the very awesome Flight PHP framework and Mustache.php template library.

Times have changed though and everyone is using Composer now (you are using Composer, right? 😉 ), not to mention that even at the time, Wing Commander was using a slightly dated version of the Mustache library. So I decided it was time for an update.

Said update was then completed in a lunch break (and a bit of an evening too) and is now available, and listed on Packagist, which means you can easily install it via Composer.

{
	"require": {
		"xmeltrut/wing-commander": "dev-master"
	}
}

That is all you need in your Composer configuration file, and it will install Flight, Wing Commander and Mustache – plus, it’s now even easier to use than it was before. Full information can be found in the readme.