Skip to content

Instantly share code, notes, and snippets.

@zoon
Created May 16, 2013 09:02
Show Gist options
  • Save zoon/5590407 to your computer and use it in GitHub Desktop.
Save zoon/5590407 to your computer and use it in GitHub Desktop.
(function () {
var path = basePath();
var swfs = listUrisRec(path, ["swf"]);
var logURI = path + "/swf_build_log.txt";
var errorsURI = path + "/swf_build_errors.txt";
var built = 0;
var notBuilt = 0;
createLog();
createErrorLog();
var latestScriptModification = getLatestScriptModification();
var projects = listUrisRec(path, ["fla", "xfl"]);
var needToBeBuilt = projects
.filter(getPredicateIsProjectNewer(swfs));
needToBeBuilt
.forEach(buildOneUri);
log("PROJECT FILES: " + projects.length + "\n");
log("NEEDED TO BE BUILT: " + needToBeBuilt.length + "\n");
log(" BUILT OK: " + built + " PROJECT FILE(S).\n");
if (notBuilt > 0) {
log(" NOT BUILT: " + notBuilt + " PROJECT FILE(S).\n");
}
FLfile.remove(errorsURI);
fl.openScript(logURI);
// -----------------------------------------------------------------------
function buildOneUri(uri) {
fl.outputPanel.clear();
fl.compilerErrors.clear();
log("... building: " + FLfile.uriToPlatformPath(uri) + "\n");
var doc = fl.openDocument(uri);
var dom = fl.getDocumentDOM();
log("results: \n");
dom.publish();
fl.outputPanel.save(logURI, true);
fl.compilerErrors.save(logURI, true);
log("\n------------------------------------------------------------\n");
if (wasError()) {
notBuilt += 1;
// NOTE: flash can create swf even if there were errors.
var swfUris = searchSWFByProjectURI(uri);
swfUris.forEach(function (uri) {
FLfile.remove(uri);
});
}
else {
built += 1;
doc.close();
}
}
function getPredicateIsProjectNewer(swfs) {
return function (uri) {
var swfUris = searchSWFByProjectURI(uri, swfs);
if (swfUris.length === 0) {
return true;
}
else {
var swfUri = swfUris[0];
var swfModification = FLfile.getModificationDate(swfUri);
var modification = FLfile.getModificationDate(uri);
return modification > swfModification
? true
: latestScriptModification > swfModification;
}
};
}
function searchSWFByProjectURI(uri, swfs) {
if (typeof swfs === 'undefined') {
swfs = listUrisRec(path, ["swf"]);
}
var stem = stemFromURI(uri);
var swfName = stem + ".swf";
var swfUris = swfs.filter(function (swfUri) {
return swfUri.search(swfName) >= 0;
});
if (swfUris.length > 1) {
var sameNames = swfUris
.map(FLfile.uriToPlatformPath)
.join("\n");
log("WARNING: swf with same names:\n" + sameNames + "\n");
}
return swfUris;
}
function listUrisRec(path, extensions) {
var result = [];
// prependPath :: path -> filename -> uri
function prependPath(path) {
return function (x) {
return path + '/' + x;
}
}
function checkExtension(uri) {
var ext = uri.substring(uri.lastIndexOf('.') + 1, uri.length);
return extensions.some(function (el) {
return el === ext;
});
}
// listDirs :: path -> path
function listDirs(path) {
return FLfile.listFolder(path, "directories")
.map(prependPath(path));
}
// listFiles :: path -> uri
function listFiles(path) {
return FLfile.listFolder(path, "files")
.filter(checkExtension)
.map(prependPath(path));
}
function list(path) {
result = arrayUnique(result.concat(listFiles(path)));
var dirs = listDirs(path);
for (var i = 0; i < dirs.length; ++i) {
result = arrayUnique(result.concat(listFiles(dirs[i])));
list(dirs[i]);
}
}
list(path);
return result;
}
function getLatestScriptModification() {
var max = 0;
var path = basePath();
var scripts = listUrisRec(path, ["as"]);
scripts.forEach(function (uri) {
var timestamp = FLfile.getModificationDate(uri);
max = max > timestamp ? max : timestamp;
});
return max;
}
function basePath() {
var path = fl.scriptURI.substring(0, fl.scriptURI.lastIndexOf("/"));
return (path === "file://") ? "file:///." : path;
}
function stemFromURI(uri) {
var stem = uri.substring(uri.lastIndexOf("/") + 1, uri.length);
return stem.substring(0, stem.lastIndexOf("."));
}
function arrayUnique(array) {
var a = array.concat();
for (var i = 0; i < a.length; ++i) {
for (var j = i + 1; j < a.length; ++j) {
if (a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
}
function createLog() {
var stamp = (new Date()).toUTCString();
FLfile.write(logURI, stamp + "\nCOMPILE LOG ...\n");
}
function createErrorLog() {
FLfile.write(errorsURI, "");
}
// wasError :: void -> bool
function wasError() {
fl.compilerErrors.save(errorsURI, true);
var err = FLfile.read(errorsURI);
// NOTE: some hardcode:
return err.indexOf("0 Error(s)") < 0 || err.indexOf("0 Warning(s)") < 0;
}
function log(message) {
FLfile.write(logURI, message, "append");
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment