/* * Jakefile * Objective-J * * Created by Francisco Tolmasky. * Copyright 2009, 280 North, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ require("../common.jake"); // Make sure we can use new functions in an in old javascript engines like Rhino // This is only needed if new things are introduced in OldBrowserCompatibility.js // as we are running in an already built Objective-J environment require("./OldBrowserCompatibility"); /* var FILE = require("file"), OS = require("os"), stream = require("narwhal/term").stream; */ var path = require("path"); var fs = require("fs"); var terser = require("terser"); var walk = require("./acornwalk").acorn.walk; acorn = {walk: walk}; acorn = require("./acorn").acorn; var compiler = require("./ObjJAcornCompiler").ObjJCompiler; const term = ObjectiveJ.term; //$BUILD_CONFIGURATION_DIR = "../Build" $BROWSER_FILE = path.join("Browser", "Objective-J.js"); $BUILD_OBJECTIVE_J = path.join($BUILD_CONFIGURATION_DIR, "Objective-J"); $BUILD_BROWSER_FILE = path.join($BUILD_OBJECTIVE_J, "Objective-J.js"); $INCLUDE_FLAGS = ["'-I" + process.cwd() + "'"]; $DEBUG_FLAGS = $CONFIGURATION === "Debug" ? ["-DDEBUG=1"] : [""]; $OBJECTIVEJ_FILES = new FileList("*.js"); $BROWSER_FILES = new FileList($BROWSER_FILE).include($OBJECTIVEJ_FILES); filedir($BUILD_BROWSER_FILE, $BROWSER_FILES, async function(aTask) { gcc($BROWSER_FILE, $BUILD_BROWSER_FILE, environmentFlags("Browser", "ObjJ").concat($INCLUDE_FLAGS, $DEBUG_FLAGS, ["-Wno-unused-but-set-variable"]), $CONFIGURATION !== "Debug"); }); $LICENSE = path.join("CommonJS", "lib", "objective-j", "jake", "LICENSES", "LGPL-v2.1"); $BUILD_LICENSE = path.join($BUILD_OBJECTIVE_J, "LICENSE"); filedir($BUILD_LICENSE, [$LICENSE], function() { fs.copyFileSync($LICENSE, $BUILD_LICENSE); }); new FileList("CommonJS/**/*").forEach( async function(aFilename) { // . files should be left out of FileList... if (!fs.lstatSync(aFilename).isFile() || path.basename(aFilename).charAt(0) === ".") return; var buildFilename = path.join($BUILD_CJS_OBJECTIVE_J, path.relative("CommonJS/", aFilename)); filedir (buildFilename, new FileList(aFilename).include($OBJECTIVEJ_FILES), function () { if (path.extname(aFilename) !== ".js") { term.stream.print("Copying... \0green(" + aFilename +"\0)"); cp(aFilename, buildFilename); } else gcc(aFilename, buildFilename, environmentFlags("CommonJS", "ObjJ").concat($INCLUDE_FLAGS, $DEBUG_FLAGS, ["-Wno-unused-but-set-variable"]), false); }); // HACK: narwhal should copy permissions if (path.dirname(aFilename) === path.join("CommonJS", "bin")) { filedir (buildFilename, function () { fs.chmodSync(buildFilename, 0o755); }); } task ("build", buildFilename); CLOBBER.include(buildFilename); }); task ("build", function() { setPackageMetadata(path.join($BUILD_CJS_OBJECTIVE_J, "package.json")); }); $BUILD_CJS_OBJECTIVE_J_FRAMEWORK = path.join($BUILD_CJS_OBJECTIVE_J, "Frameworks", "Objective-J"); filedir($BUILD_CJS_OBJECTIVE_J_FRAMEWORK, function() { cp_r($BUILD_OBJECTIVE_J, $BUILD_CJS_OBJECTIVE_J_FRAMEWORK); }); CLOBBER.include($BUILD_OBJECTIVE_J); function environmentFlags() { var environments = Array.prototype.slice.call(arguments) return environments.map(function(anEnvironment) { return "-D" + anEnvironment.toUpperCase(); }).concat("-DENVIRONMENTS=" + JSON.stringify(environments)); } async function compressor(srcCode) { return (await terser.minify(srcCode)).code; } var headerText = fs.readFileSync("header.txt", { encoding: "utf8" }); async function gcc(inputFilePath, outputFilePath, flags, compress) { term.stream.print("Building... \0green(" + outputFilePath +"\0)"); var source = fs.readFileSync(inputFilePath, { charset : "utf8" }).toString(); var compilerOptions = compiler.parseGccCompilerFlags(flags.join(" ")); var acornOptions = compilerOptions.acornOptions || (compilerOptions.acornOptions = {}); acornOptions.preprocessGetIncludeFile = function(filePath, isQuoted) { var includeContent = fs.readFileSync(filePath, { charset : "utf8" }).toString(); return {include: includeContent, sourceFile: filePath}; } var c = compiler.compile(source, inputFilePath, compilerOptions); var warnings = [], anyErrors = false; for (var i = 0; i < c.warningsAndErrors.length; i++) { var warning = c.warningsAndErrors[i], message = c.prettifyMessage(warning); // Set anyErrors to 'true' if there are any errors in the list anyErrors = anyErrors || warning.messageType === "ERROR"; console.log(message); } if (anyErrors) throw "Compilation Error"; var code = c.code(); var contents = headerText + code; if (path.extname(inputFilePath) === ".js" && compress) { contents = await compressor(contents); } fs.writeFileSync(outputFilePath, contents, { encoding : "utf8" }); } task("build", [$BUILD_BROWSER_FILE, $BUILD_LICENSE, $BUILD_CJS_OBJECTIVE_J_FRAMEWORK]);