瀏覽代碼

init

master
Jan Lehnardt 14 年之前
當前提交
5f122d32ca
共有 1 個文件被更改,包括 99 次插入0 次删除
  1. +99
    -0
      erlccache

+ 99
- 0
erlccache 查看文件

@@ -0,0 +1,99 @@
#!/usr/bin/env node
/*
erlccache — (c) 2010 Jan Lehnardt <jan@apache.org>
use instead of erlc and your builds will magically speed up
somebody needs to turn this into proper node code, or sh or whatever
MIT License
*/

// CUSTOMIZE HERE
var erlc = "/Users/jan/.local/bin/erlc";
var dir = "/Users/jan/.erlccache/";
// STOP CUSTOMIZING


var sys = require("sys");
var child_process = require("child_process");
var crypto = require("crypto");
var fs = require("fs");


var args = process.argv.slice(2).toString();
var cmd = erlc + " " + args;

// execute a shell command
function exec(cmd, fun) {
fun = fun || function (error, stdout, stderr) {
sys.print("stdout: " + stdout);
sys.print("stderr: " + stderr);
if (error !== null) {
sys.puts("exec error: " + error);
}
};
child_process.exec(cmd, fun);
}

// get the .erl file out of cmd
function erl_parse_file(cmd) {
var file = cmd.match(/[\w]+\.erl\s*/).toString();
return file;
}

// find location of .beam file given the cmd
function erl_beamfile(cmd) {
return process.cwd() + "/" + erl_parse_file(cmd).replace(/.erl/, ".beam");
}

// create a sha1 hex digest from the cmd .erl file
function cache_hash(cmd) {
var hash = crypto.createHash("sha1");
var file = erl_parse_file(cmd);
hash.update(fs.readFileSync(file, "binary"));
return hash.digest("hex");
}

// copy(from, to); -- synchronous
function cache_file_copy(from, to) {
fs.writeFileSync(to, fs.readFileSync(from, "binary"), "binary");
}

// see if the filename in cmd is cached, if not compile and cache
function cache_hit(cmd) {
// make cache dir if it doesn't exist
try {
fs.statSync(dir);
} catch(e) {
fs.mkdirSync(dir, 0700);
}

// hash cmd
var hash = cache_hash(cmd);
try {
fs.statSync(dir + hash);
} catch(e) {
// compile
exec(cmd, function(error, stdout, stderr) {
if(!error) {
var beamfile = erl_beamfile(cmd);
// add beam file to dir + hash(cmd)
cache_file_copy(beamfile, dir + hash);
}
});
return false;
}
return true
}

// copy the cached beamfile to the final destination as if erlc did it
function cache_copy(cmd) {
var beamfile = erl_beamfile(cmd);
var cachefile = dir + cache_hash(cmd);
cache_file_copy(cachefile, beamfile);
}

if(cache_hit(cmd)) {
cache_copy(cmd);
}

//done

Loading…
取消
儲存