Forked from selvakn/rhino-handlebars-precompiler.js
Last active
August 29, 2015 14:05
-
-
Save cahrehn/dfe718332f54d9f21517 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
importPackage(java.io); | |
(function(args) { | |
var output = [], | |
console = { | |
log: print | |
}, | |
showUsage = function() { | |
console.log('Usage: java -jar <rhino.jar> rhino-handlebars-compiler.js --handlebars <handlebars library path> --template <template file> --output <output file>'); | |
}, | |
handlebarsLibrary, | |
outStream, | |
templateFile, | |
templateContents, | |
argumentsParser, | |
options; | |
argumentsParser = function() { | |
var arg, parse = function(args) { | |
var options = {}; | |
args = Array.prototype.slice.call(args); | |
arg = args.shift(); | |
while (arg) { | |
if (arg.indexOf("--") === 0) { | |
options[arg.substring(2)] = args.shift(); | |
} | |
arg = args.shift(); | |
} | |
return options; | |
}; | |
return { parse: parse }; | |
}; | |
options = new argumentsParser().parse(args); | |
console.log(options) | |
handlebarsLibrary = options.handlebars; | |
if (undefined === handlebarsLibrary) { | |
showUsage(); | |
java.lang.System.exit(1); | |
} | |
load(handlebarsLibrary); | |
templateFile = new File(options.template); | |
templateName = templateFile.getName(); | |
templateContents = Handlebars.precompile(readFile(templateFile.getAbsolutePath())); | |
output.push('(function() {'); | |
output.push('\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n'); | |
output.push('templates[\'' + templateName + '\'] = template(' + templateContents + ');\n'); | |
output.push('})();'); | |
outStream = new BufferedWriter(new FileWriter(options.output)); | |
outStream.write(output.join('')); | |
outStream.close(); | |
}(arguments)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment