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

Add app global vars #313

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
36 changes: 36 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,42 @@ command. You can do this by adding this line to your `.zshrc` file :

alias jake="noglob jake"

### Setup jake program global settings

Jake supports the ability to setup global settings (to program) and later use this settings in tasks.

This is may be useful when tasks was split to some files in `jakelib` but must use some settings, f.e. path to directory:

```javascript
// At Jakefile
jake.program.setAppVars('libDir', '../foo/lib');
jake.program.setAppVars({'srcDir': '../bazz/src', 'binDir': '../foo/bin'});
```

And later use it:

```javascript
// At jakelib/common.jake
desc('Clear lib.');
task('clearLib', function () {
var libDir = jake.program.appVars.libDir;
// Do something with libDir value
});
```

```javascript
// At jakelib/build.jake
desc('Build lib.');
task('buildLib', function () {
var libDir = jake.program.appVars.libDir,
srcDir = jake.program.appVars.srcDir,
binDir = jake.program.appVars.binDir;
// Do something with libDir value
});
```

With application variables it will be easy to have one source of settings to rule it.

### Cleanup after all tasks run, jake 'complete' event

The base 'jake' object is an EventEmitter, and fires a 'start' event before
Expand Down
18 changes: 18 additions & 0 deletions lib/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Program = function () {
this.taskNames = null;
this.taskArgs = null;
this.envVars = null;
this.appVars = {};
};

Program.prototype = new (function () {
Expand Down Expand Up @@ -175,6 +176,23 @@ Program.prototype = new (function () {
this.envVars = vars || null;
};

// support both setAppVars(object) and setAppVars(key, value)
this.setAppVars = function (key, value) {
var newObject = {};

if(typeof(key) === 'string'){
if(typeof(value) === 'undefined'){
throw new Error('Void value detected, halt!');
} else {
newObject[key] = value;
}
} else {
newObject = key;
}

this.appVars = utils.mixin({}, this.appVars, newObject);
};

this.firstPreemptiveOption = function () {
var opts = this.opts;
for (var p in opts) {
Expand Down
11 changes: 11 additions & 0 deletions test/Jakefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ task('argsEnvVars', function () {
console.log(JSON.stringify(res));
});

desc('Use app vars.');
task('appVars', function () {
var res;
jake.program.setAppVars('foo', 'bar');
jake.program.setAppVars({baz : 'qux'});
res = {
appVars: jake.program.appVars
};
console.log(JSON.stringify(res));
});

namespace('foo', function () {
desc('The foo:bar task.');
task('bar', function () {
Expand Down
10 changes: 10 additions & 0 deletions test/task_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ var tests = {
});
}

, 'test using app vars': function (next) {
h.exec('../bin/cli.js appVars', function (out) {
var parsed = h.parse(out)
, appVars = parsed.appVars;
assert.equal(appVars.foo, 'bar');
assert.equal(appVars.baz, 'qux');
next();
});
}

, 'test a simple prereq': function (next) {
h.exec('../bin/cli.js foo:baz', function (out) {
assert.equal('foo:bar task\nfoo:baz task', out);
Expand Down