Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds feature to allow tasks to be greedy, consuming all remaining CLI… #382

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ Program.prototype = new (function () {
}
}

// if task sets option greedy, then add all remaining CLI params
// are consumed by the task and passed as arguments
try {
taskNames = taskNames.reduceRight((accum, taskName) => {
let task = jake.rootNamespace.resolveTask(taskName);
if (task && task.greedy && accum.length) {
accum[0]=[`${taskName}[${accum[0].join(",")}]`];
accum.unshift([]);
} else {
accum[0].unshift(taskName);
}
return accum;
}, [[]]).flat();
} catch (err) {
// Might be possible for resolveTask to throw an exception.
// If this happens, fall back to standard argument handling,
// leaving taskNames unchanged.
}

rootTask = task(Task.ROOT_TASK_NAME, taskNames, function () {});
rootTask._internal = true;

Expand Down
4 changes: 4 additions & 0 deletions lib/task/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Task extends EventEmitter {
this.endTime = null;
this.directory = null;
this.namespace = null;
this.greedy = false;

// Support legacy async-flag -- if not explicitly passed or falsy, will
// be set to empty-object
Expand All @@ -81,6 +82,9 @@ class Task extends EventEmitter {
if (opts.concurrency) {
this.concurrency = opts.concurrency;
}
if (opts.greedy) {
this.greedy = true;
}
}

//Do a test on self dependencies for this task
Expand Down
6 changes: 6 additions & 0 deletions test/integration/jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,10 @@ namespace("large", function () {
task("different", different, { concurrency: 2 } , function () {
console.log("large:different")
})

});

desc("Task that greedily consumes all remaining CLI params");
task("oink", { greedy: true } , function (...args) {
console.log("greedy task consumed", args.length, "remaining arguments: ", args);
})
5 changes: 5 additions & 0 deletions test/integration/task_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,9 @@ suite('taskBase', function () {
assert.equal('one:one\none:two', out);
});

test('greedy task consumes all remaining CLI params as arguments', function () {
let out = exec('./node_modules/.bin/jake -q oink one two three').toString().trim();
assert.equal("greedy task consumed 3 remaining arguments: [ \'one\', \'two\', \'three\' ]", out);
});

});
4 changes: 4 additions & 0 deletions test/unit/jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ namespace('zooby', function () {
});

task('asdf', function () {});

task('zumm', { greedy: true }, function (args) {
console.log('ran zooby:frang:zumm');
});
});

});
Expand Down
7 changes: 7 additions & 0 deletions test/unit/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ suite('namespace', function () {
let curr = Namespace.ROOT_NAMESPACE.resolveNamespace('hurr:durr');
let task = curr.resolveTask('zooby:frang:w00t:bar');
assert.ok(task.action.toString().indexOf('zooby:frang:w00t:bar') > -1);
assert.ok(!task.greedy);
});

test('resolution miss with throw error', function () {
Expand All @@ -74,4 +75,10 @@ suite('namespace', function () {
assert.ok(!task);
});

test('resolution task with greedy option set', function () {
let curr = Namespace.ROOT_NAMESPACE;
let task = curr.resolveTask('zooby:frang:zumm');
assert.ok(task.action.toString().indexOf('zooby:frang:zumm') > -1);
assert.ok(task.greedy);
});
});