Skip to content

Instantly share code, notes, and snippets.

@ProfAvery
Created September 12, 2012 01:07
Show Gist options
  • Save ProfAvery/3703426 to your computer and use it in GitHub Desktop.
Save ProfAvery/3703426 to your computer and use it in GitHub Desktop.
jrunscript utilities
var createTextArchive = (function() {
var root;
function enumerate(e, callback) {
while (e.hasMoreElements()) {
callback(e.nextElement());
}
}
function canonicalPath(filename) {
var file = File(filename);
return file.getCanonicalPath();
}
function toRegExp(str) {
var re = RegExp("\\\\", "g"),
s = str.replace(re, "\\\\", "g");
return RegExp(s);
}
return {
each: function(arr, callback) {
var i = 0,
len = arr.length;
for(; i < len; i = i + 1) {
callback(arr[i]);
}
},
canonicalPath: function(path) {
var file = File(path);
return file.getCanonicalPath()
},
setRoot: function(dir) {
root = canonicalPath(dir);
},
outputText: function(filename) {
var absPath = String(filename),
rootPath = toRegExp("^" + root + sysProps["file.separator"]),
relativePath = absPath.replace(rootPath, "");
printf("-- %s --\n\n", relativePath);
cat(filename);
printf("\n");
}
};
})();
if (!arguments.length) {
arguments = ["."];
}
createTextArchive.each(arguments, function (argument) {
createTextArchive.setRoot(argument);
find(
createTextArchive.canonicalPath(argument),
/.*/,
createTextArchive.outputText
);
});
var extractTextArchive = (function() {
function eachLine(scanner, callback) {
while (scanner.hasNextLine()) {
callback(scanner.nextLine());
}
}
function canonicalPath(filename) {
var file = File(filename);
return file.getCanonicalPath();
}
return {
each: function(arr, callback) {
var i = 0,
len = arr.length;
for (; i < len; i = i + 1) {
callback(arr[i]);
}
},
readLines: function(filename) {
var reader = java.io.BufferedReader(
java.io.FileReader(filename)),
scanner = java.util.Scanner(reader),
delimiter = /^--\s+(.*)\s+--\s*$/,
writer = null,
filename,
file,
parentDir;
eachLine(scanner, function (line) {
if (delimiter.test(line)) {
filename = RegExp.$1;
println(filename);
file = File(filename);
parentDir = file.getParentFile();
if (parentDir) {
parentDir.mkdirs();
}
if (writer) {
writer.close();
}
writer = java.io.BufferedWriter(
java.io.FileWriter(file));
} else {
if (writer) {
writer.write(line + sysProps["line.separator"]);
}
}
});
if (writer) {
writer.close();
}
}
};
})();
if (!arguments.length) {
println("Usage: jrunscript extract-text-archive.js FILE ...");
exit();
}
extractTextArchive.each(arguments, function (argument) {
extractTextArchive.readLines(argument);
});
var findJars = (function() {
function enumerate(e, callback) {
while (e.hasMoreElements()) {
callback(e.nextElement());
}
}
return {
each: function(arr, callback) {
var i = 0,
len = arr.length;
for(; i < len; i = i + 1) {
callback(arr[i]);
}
},
canonicalPath: function(path) {
var file = File(path);
return file.getCanonicalPath();
},
listJar: function(filename) {
var jar = java.util.jar.JarFile(filename),
entries = jar.entries();
enumerate(entries, function (entry) {
printf("%s!%s\n", filename, entry);
});
}
};
})();
if (!arguments.length) {
arguments = ["."];
}
findJars.each(arguments, function (argument) {
find(
findJars.canonicalPath(argument),
/\.jar$/,
findJars.listJar
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment