#!/usr/bin/jjs -scripting

var File = Java.type("java.io.File");

$EXEC("pwd");

var path = $OUT.trim() + "/Nakefile";

var nakefile = new File(path);

if (!nakefile.exists()) {
  print('Nakefile does not exist');
  exit();
}

var tasks = {};

var task = function(name, description, action) {
  tasks[name] = {
    name: name,
    description: description,
    action: action
  };
};

var printTasks = function() {
  print('Tasks:');
  for (var task in tasks) {
    print(" - ${task}");
  }
};

// evaluate Nakefile
load(nakefile.getAbsolutePath());

if (arguments.length == 0) {
  printTasks();
  exit(0);
}

var taskName = arguments[0];
var context = tasks[taskName];
if (!context) {
  print("unknown task: ${taskName}");
  exit(1);
}

context.action.call(this);

#!/usr/bin/jjs -scripting

var global = this;

(function() {
  var File = Java.type("java.io.File");

  var fatalError = function (message) {
    print(message);
    exit(1);
  };


  $EXEC("pwd");

  global.path = $OUT.trim();

  var nakefile = new File(global.path + "/Nakefile");

  if (!nakefile.exists()) {
    fatalError("Nakefile not found: ${global.path}");
  }

  var tasks = {};

  var task = function(name, description, action) {
    tasks[name] = {
      name: name,
      description: description,
      action: action
    };
  };

  global.task = task;

  var printTasks = function() {
    print("Tasks defined in ${global.path}/Nakefile:");
    for (var taskName in tasks) {
      var task = tasks[taskName];
      print(" - ${task.name} [${task.description}]");
    }
    print("\nuse 'nake -- taskName' to execute a task");
  };


  // evaluate Nakefile
  load(nakefile.getAbsolutePath());


  if (global.arguments.length == 0) {
    printTasks();
    exit(0);
  }

  var taskName = global.arguments[0];
  var currentTask = tasks[taskName];
  if (!currentTask) {
    fatalError("no such task: ${taskName}\nuse 'nake' to list all available tasks");
  }

  try {
    currentTask.action.call(global);
  }
  catch (e) {
    fatalError("failed to execute task ${currentTask.name}: ${e}");
  }

})();

introduce local scope for nake internal stuff