Skip to content

Commit

Permalink
Start support for volofile. Still has rough edges
Browse files Browse the repository at this point in the history
  • Loading branch information
jrburke committed Jan 11, 2012
1 parent 20f9e20 commit dc330e8
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 6 deletions.
1 change: 1 addition & 0 deletions tests/commands/create/support/hasOnCreate/sample.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello {name}. Your code is {code}
27 changes: 27 additions & 0 deletions tests/commands/create/support/hasOnCreate/volofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
/*jslint */
/*global define, console, process */

define(function (require) {
var fs = require('fs'),
voloTemplate = require('volo/template'),
templatePath = require.toUrl('./sample.template'),
template = require('text!./sample.template');

return {
onCreate: function (deferred, namedArgs) {
//Generate templatized output
debugger;
var output = voloTemplate(template, namedArgs);

//Write out the contents of the generated template
fs.writeFileSync(require.toUrl('./output.text'), output, 'utf8');

//Clean up.
fs.unlinkSync(templatePath);
fs.unlinkSync(require.toUrl('./volofile.').replace(/\.$/, ''));

deferred.resolve();
}
};
});
1 change: 1 addition & 0 deletions tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'volo/main',
'volo/github',
'volo/resolve/github',
'volo/template',
'help',
'acquire',
'rejuvenate',
Expand Down
19 changes: 13 additions & 6 deletions volo/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define(function (require, exports, module) {
file = require('volo/file'),
download = require('volo/download'),
tar = require('volo/tar'),
volofile = require('volo/volofile'),
create;

create = {
Expand Down Expand Up @@ -50,7 +51,6 @@ define(function (require, exports, module) {
//Download and unpack the template.
.then(function (tempDirName) {
var tarFileName = path.join(tempDirName, 'template.tar.gz'),
d = q.defer(),
step;

//Function used to clean up in case of errors.
Expand All @@ -60,8 +60,7 @@ define(function (require, exports, module) {
}

//Download
step = d.resolve()
.then(function () {
step = q.call(function () {
return download(archiveInfo.url, tarFileName);
}, errCleanUp);

Expand All @@ -83,17 +82,25 @@ define(function (require, exports, module) {
//Clean up temp area.
file.rmdir(tempDirName);

return archiveInfo.url + ' used to create ' + appName;
return undefined;
} else {
return errCleanUp(new Error('Unexpected tarball configuration'));
}
}, errCleanUp);
}, errCleanUp)

//If there is a volofile with an onCreate, run it.
.then(function () {
return volofile.run(appName, 'onCreate', namedArgs, appName);
})
.then(function (commandOutput) {
return (commandOutput ? commandOutput : '') +
archiveInfo.url + ' used to create ' + appName;
});

return step;
}));
}
};


return require('volo/commands').register(module.id, create);
});
32 changes: 32 additions & 0 deletions volo/volo/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @license Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/volojs/volo for details
*/

'use strict';
/*jslint */
/*global define */

/**
* Reads a volofile from a target directory, and exports the data as a
* set of modules.
*/
define(function (require) {
var tokenRegExp = /\{(\w+)\}/g;

function template(contents, data) {
return contents.replace(tokenRegExp, function (match, token) {
var result = data[token];

//Just use empty string for null or undefined
if (result === null || result === undefined) {
result = '';
}

return result;
});
}

return template;
});
108 changes: 108 additions & 0 deletions volo/volo/volofile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* @license Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/volojs/volo for details
*/

'use strict';
/*jslint */
/*global define */

/**
* Reads a volofile from a target directory, and exports the data as a
* set of modules.
*/
define(function (require) {
var path = require('path'),
q = require('q'),
qutil = require('volo/qutil');

function volofile(basePath, callback, errback) {
var d = qutil.convert(callback, errback),
volofilePath = path.resolve(path.join(basePath, 'volofile'));

if (path.existsSync(volofilePath)) {
require([volofilePath], function (value) {
d.resolve(value);
});
} else {
d.resolve();
}

return d.promise;
}

/**
* Loads the volofile inside basePath, and if there, and if it
* supports the command, then runs it, running dependencies for
* the command if specified.
* @returns {Promise} that resolves to false exactly, otherwise it has the
* commmand output, if any.
*/
volofile.run = function (basePath, commandName, namedArgs /*other args can be passed*/) {
var d = q.defer(),
args = [].slice.call(arguments, 2);

volofile(basePath).then(function (vfMod) {
var command = vfMod && vfMod[commandName];

if (command) {
if (typeof command === 'function') {
//Just normalize to advanced structure.
command = {
before: [],
run: command
};
}
d.resolve(volofile.runCommand.apply(volofile, [command].concat(args)));
} else {
d.resolve(false);
}
});

return d.promise;
};

volofile.runCommand = function (command, namedArgs /*other args can be passed*/) {
var d = q.defer(),
args;

if (!command) {
d.resolve();
} else {
args = [].slice.call(arguments, 1);

q.call(function () {
if (command.before.length) {
return command.before.reduce(function (done, command) {
return q.wait(done,
volofile.runCommand.apply(volofile,
[command].concat(args)));
});
}
return undefined;
})
.then(function () {
var commandDeferred = q.defer(),
err;

//Call validate if it is on the command.
if (command.validate) {
err = command.validate(args);
if (err) {
commandDeferred.reject(err);
return commandDeferred.promise;
}
}

command.run.apply(command, [commandDeferred].concat(args));
return commandDeferred.promise;
})
.then(d.resolve, d.reject);
}

return d.promise;
};

return volofile;
});

0 comments on commit dc330e8

Please sign in to comment.