Skip to content

Instantly share code, notes, and snippets.

@chamberlainpi
Last active August 29, 2015 14:18
Show Gist options
  • Save chamberlainpi/b8643ddcf8596a023cd6 to your computer and use it in GitHub Desktop.
Save chamberlainpi/b8643ddcf8596a023cd6 to your computer and use it in GitHub Desktop.
Build several JS scripts in one pass.
package macros;
import haxe.io.Path;
import haxe.Json;
import sys.FileSystem;
import sys.io.File;
/**
* This script finds Haxe-files in a directory and compiles them to individual JS files.
* A JSON file must be supplied to indicate the CWD, outputDir, fileNamePattern to find the files,
* and the compiler-arguments to apply to all files.
* (ie: Class-paths, DCE setting, Debug flags, other macro commands)
*
* @usage:
* --macro macros.BuildCommands.run("config.json")
*
* @author Pierre Chamberlain
*/
class BuildCommands
{
private static var fileNamePattern:EReg;
private static var config:Dynamic;
static function run(configJsonURI:String) {
config = readJSON( configJsonURI );
//Store the Current Working Directory to reset it back at the end.
var prevCWD:String = Sys.getCwd();
//Set the CWD
Sys.setCwd(config.currentWorkingDir);
trace("Building in: " + Sys.getCwd());
var regexStr:Array<String> = config.fileNamePattern;
fileNamePattern = regexStr != null ? new EReg(regexStr[0], regexStr[1]) : null;
buildAll(".");
Sys.setCwd(prevCWD);
}
static function buildAll(path:String) {
for (file in FileSystem.readDirectory(path)) {
if (fileNamePattern != null && !fileNamePattern.match(file)) {
continue;
} else if (FileSystem.isDirectory(file)) {
continue;
}
var main = Path.withoutExtension(file);
var allParams:Array<String> = [
'-main', main,
'-js', config.outputDir + '/$main.jsfl'
].concat(config.compilerArgs);
trace("Building: " + main + ".jsfl\n " + allParams.join(" "));
Sys.command("haxe", allParams);
}
}
static function readJSON(fileURI):Dynamic {
var content:String = File.getContent(fileURI);
return Json.parse(content);
}
}
{
"author" : "Pierre Chamberlain",
"title" : "Config file for BuildCommands.",
"currentWorkingDir" : "src_commands",
"outputDir" : "../bin/commands",
"fileNamePattern" : ["Command[a-z]*\\.hx", "i"],
"compilerArgs" : [
"-cp", "../src_folder1",
"-cp", "../src_folder2",
"-dce", "std",
"-D", "js-es5",
"--macro", "exclude('jsfl')"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment