Created
May 8, 2012 13:11
-
-
Save tosyu/2634837 to your computer and use it in GitHub Desktop.
ugly merger for mamd namespace definitions (use with rhino)
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
/* | |
Usage: | |
java -jar js.jar INDIR OUTFILE | |
ex: | |
java -jar js.jar exampleDirectory filesMerged.js | |
*/ | |
var fileList = []; | |
var inputDir = arguments[0] || "example"; | |
var outFile = arguments[1] || "example.js"; | |
var filesDepts = []; | |
function getInputFiles(path) { | |
var dir = (new java.io.File(path)).getAbsoluteFile(), | |
list = dir.list(), | |
fileName, | |
file, | |
filePath; | |
for (var i = 0; i < list.length; i++) { | |
file = new java.io.File(dir.getAbsolutePath() + java.io.File.separator + list[i]); | |
fileName = String(file.getName()); | |
filePath = String(file.getAbsolutePath()); | |
if (file.isFile() && fileName.match(/\.js/gi)) { | |
fileList.push(file); | |
} else if(file.isDirectory()) { | |
getInputFiles(filePath); | |
} | |
} | |
} | |
getInputFiles(inputDir); | |
var input, | |
buffer, | |
bReader, | |
line, | |
ns, | |
reqs; | |
var mamd = { | |
define: function () { | |
var args = Array.prototype.slice.call(arguments), | |
fc, | |
reqs, | |
nm; | |
if (args.length <= 1) { | |
return false; | |
} | |
fc = args.pop(); | |
reqs = args.length === 2 ? args.pop() : []; | |
nm = args.pop(); | |
reqs.map(function (req) { | |
return req.replace(/\./gi, java.io.File.separator) + ".js" | |
}); | |
filesDepts.push({ | |
file: nm.replace(/\./gi, java.io.File.separator) + ".js", | |
depts: reqs | |
}); | |
}, | |
require: function (depts) { | |
print("require"); | |
} | |
} | |
for (var i = 0; i < fileList.length; i++) { | |
load(String(fileList[i].getAbsolutePath())); | |
} | |
filesDepts.sort(function (a, b) { | |
if (a.depts.length === 0) { | |
return -1; | |
} | |
if (b.depts.length === 0) { | |
return 1; | |
} | |
if (a.depts.indexOf(b.file)) { | |
return -1; | |
} | |
if (b.depts.indexOf(a.file)) { | |
return 1; | |
} | |
return 0; | |
}); | |
var mainBuffer = ''; | |
for (var i = 0; i < filesDepts.length; i++) { | |
input = new java.io.FileReader(inputDir + java.io.File.separator + filesDepts[i].file); | |
bReader = new java.io.BufferedReader(input); | |
do { | |
line = bReader.readLine(); | |
if (line) { | |
mainBuffer += line + "\n"; | |
} | |
} while (line); | |
} | |
var output = new java.io.FileWriter(outFile); | |
var bOut = new java.io.BufferedWriter(output); | |
bOut.write(mainBuffer); | |
bOut.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment