I use NetBeans to write PHP code. One of my biggest concern was the inability to launch external shell scripts without leaving NB. I came up with a hack a few months ago, but it was a HACK… up until yesterday.

I discovered Command-shortcuts, an awesome NB plugin. It lets you launch any external command from NB. It also lets you pass a #{file} argument, so the command can operate on the file at hand.

The only problem was the inability to launch a command and pass it the base directory of the project. Command-shortcuts has a #{mainproject} parameter, but you need to define the project you are working on as a main project in NetBeans (seems like you need to do it every time you open the project – a sure way to waste time finding why it doesn’t work).

So i wrote a simple helper script to find the project base directory and invoke the Phing buildfile in that directory.

#!/usr/bin/php -q
<?php

$logfile = sprintf('/tmp/nbphing_helper.%d.log', getmypid());

if (! isset($argv[1])) {
    file_put_contents($logfile, "\n  No file specified\n\n");
    bailout($logfile);
}

if (! is_readable($argv[1])) {
    file_put_contents($logfile, "\n  File not found\n\n");
    bailout($logfile);
}

if (! isset($argv[2])) {
    file_put_contents($logfile, "\n  No target specified\n\n");
    bailout($logfile);
}

$search = $argv[1];
do {
    $search = dirname($search);
} while(! is_readable(sprintf('%s/build.xml', $search)) && strlen($search) > 2);

if (! is_readable(sprintf('%s/build.xml', $search))) {
    file_put_contents($logfile, "\n  build.xml not found\n\n");
    bailout($logfile);
}

chdir($search);
exec('phing ' . $argv[2], $output);
file_put_contents($logfile, implode("\n", $output));
bailout($logfile);


function bailout($logfile) {
    exec('konsole -e less -r '.$logfile);
    sleep(1);
    unlink($logfile);
    exit;
}

It’s a dirty hack, but I can now invoke several phing build targets without leaving the IDE.

Any suggestion, improvement, criticism is welcome!