Skip to main content

Using jrunscript as a build tool

Posted by forax on September 14, 2006 at 3:34 PM PDT

In my last post, i've described how to use jrunscript to
create a build script.


As olivier wrote in the comments, the build process is usually
a dependency graph and jrunscript doesn't do that by default.


But javascript is far more powerful than XML
thus it's easy to create commands that enable to declare
dependencies betweeen functions as Ant or make does.

I propose the following commands:

  1. depends_on that declares a set of dependencies of a function.
  2. run_targets that explores the graph from a function
    in a dependency first order.

Javascript enables to put data in an existing
object even a one like Object or Function.
In fact, objects are considered as an hashtable of couples
property/value so the dependencies of a function can be stored
in the function itself.

The build script:

function test1() {
  echo("test 1");
}

function test2() {
  echo("test 2");
}
test2.depends_on(test1);

function test3() {
  echo("test 3");
}
test3.depends_on(test1,test2);

run_targets(test3);

And the script that creates the two commands :

function depends_on() {
  var dependencies=new Array();
  for (var i=0;i<arguments.length;i++) {
    dependencies.push(arguments[i]);
  }
  this.dependencies=dependencies;
}
Function.prototype.depends_on=depends_on;

function run_targets(target) {
  run_targets_internal(target,new Object())
}

function run_targets_internal(target,markers) {
  if (markers[target]=="X")
    return
   
  markers[target]="X"
  for each(dependency in target.dependencies) {
    run_targets_internal(dependency,markers);
  }
 
  echo("target "+target.name);
  target();
}

Now, it's time to drop Ant :)

Related Topics >>