<commit_before>var OS = require("os");
if (system.engine !== "rhino") {
    system.args.splice(1,2); // remove library path and main.j
    var cmd = "NARWHAL_ENGINE_HOME='' NARWHAL_ENGINE='rhino' " + system.args.map(OS.enquote).join(" ");
    OS.exit(OS.system(cmd));
}

@import <Foundation/Foundation.j>

@import "objj-analysis-tools.j"

var ARGS = require("args");
var FILE = require("file");
var OS = require("os");
var DOM = require("browser/dom");

var serializer = new DOM.XMLSerializer();

var parser = new ARGS.Parser();

parser.usage("INPUT_PROJECT OUTPUT_PROJECT");
parser.help("Optimizes Cappuccino applications for deployment to the web.");

parser.option("-m", "--main", "main")
    .def("main.j")
    .set()
    .help("The relative path (from INPUT_PROJECT) to the main file (default: 'main.j')");

parser.option("-F", "--framework", "frameworks")
    .def(["Frameworks"])
    .push()
    .help("Add a frameworks directory, relative to INPUT_PROJECT (default: ['Frameworks'])");

parser.option("-E", "--environment", "environments")
    .def(['W3C', 'IE7', 'IE8'])
    .push()
    .help("Add a platform name (default: ['W3C', 'IE7', 'IE8'])");

parser.option("-f", "--flatten", "flatten")
    .def(false)
    .set(true)
    .help("Flatten all code into a single Application.js file and attempt add script tag to index.html (useful for Adobe AIR and CDN deployment)");

parser.option("-n", "--nostrip", "strip")
    .def(true)
    .set(false)
    .help("Do not strip any files");

parser.option("-p", "--pngcrush", "png")    
    .def(false)
    .set(true)
    .help("Run pngcrush on all PNGs (pngcrush must be installed!)");

parser.option("-v", "--verbose", "verbose")
    .def(false)
    .set(true)
    .help("Verbose logging");

parser.helpful();

function main(args)
{
    var options = parser.parse(args);
    
    if (options.args.length < 2) {
        parser.printUsage(options);
        return;
    }
    
    //if (options.verbose)
        CPLogRegister(CPLogPrint);
    //else
    //    CPLogRegisterRange(CPLogPrint, "fatal", "info");

    // HACK: ensure trailing slashes for "relative" to work correctly
    var rootPath = FILE.path(options.args[0]).join("").absolute();
    var outputPath = FILE.path(options.args[1]).join("").absolute();

    press(rootPath, outputPath, options);
}

function press(rootPath, outputPath, options) {
    CPLog.info("===========================================");
    CPLog.info("Application root:    " + rootPath);
    CPLog.info("Output directory:    " + outputPath);
    
    var outputFiles = {};
    
    // analyze and gather files for each environment:
    options.environments.forEach(function(environment) {
        pressEnvironment(rootPath, outputFiles, environment, options);
    });
    
    // phase 4: copy everything and write out the new files
    CPLog.error("PHASE 4: copy to output ("+rootPath+" to "+outputPath+")");
    
    FILE.copyTree(rootPath, outputPath);
    
    for (var path in outputFiles) {
        CPLog.trace("Writing: " + path);
        
        var file = outputPath.join(rootPath.relative(path));
        
        var parent = file.dirname();
        if (!parent.exists()) {
            CPLog.warn(parent + " doesn't exist, creating directories.");
            parent.mkdirs();
        }
        
        if (typeof outputFiles[path] !== "string")
            outputFiles[path] = outputFiles[path].join("");
        
        CPLog.info((file.exists() ? "Overwriting: " : "Writing:     ") + file);
        FILE.write(file, outputFiles[path], { charset : "UTF-8" });
    }
    
    // outputPath.glob("**/Frameworks/Debug").forEach(function(debugFramework) {
    //     outputPath.join(debugFramework).rmtree();
    // });
    // outputPath.join("index-debug.html").remove();
    
    if (options.png) {
        pngcrushDirectory(outputPath);
    }
}

function pressEnvironment(rootPath, outputFiles, environment, options) {
    
    var mainPath = String(rootPath.join(options.main));
    var frameworks = options.frameworks.map(function(framework) { return rootPath.join(framework); });
    
    CPLog.info("===========================================");
    CPLog.info("Main file:           " + mainPath)
    CPLog.info("Frameworks:          " + frameworks);
    CPLog.info("Environment:         " + environment);
    
    // get a Rhino context
    var ctx = Packages.org.mozilla.javascript.Context.getCurrentContext();
    var scope = makeObjjScope(ctx); // "scope" is the same as require("objective-j").window;
    
    scope.OBJJ_INCLUDE_PATHS = frameworks;<commit_msg>Add --force option, change shorthand --flatten to -l<commit_after>var OS = require("os");
if (system.engine !== "rhino") {
    system.args.splice(1,2); // remove library path and main.j
    var cmd = "NARWHAL_ENGINE_HOME='' NARWHAL_ENGINE='rhino' " + system.args.map(OS.enquote).join(" ");
    OS.exit(OS.system(cmd));
}

@import <Foundation/Foundation.j>

@import "objj-analysis-tools.j"

var ARGS = require("args");
var FILE = require("file");
var OS = require("os");
var DOM = require("browser/dom");

var serializer = new DOM.XMLSerializer();

var parser = new ARGS.Parser();

parser.usage("INPUT_PROJECT OUTPUT_PROJECT");
parser.help("Optimizes Cappuccino applications for deployment to the web.");

parser.option("-m", "--main", "main")
    .def("main.j")
    .set()
    .help("The relative path (from INPUT_PROJECT) to the main file (default: 'main.j')");

parser.option("-F", "--framework", "frameworks")
    .def(["Frameworks"])
    .push()
    .help("Add a frameworks directory, relative to INPUT_PROJECT (default: ['Frameworks'])");

parser.option("-E", "--environment", "environments")
    .def(['W3C'])//, 'IE7', 'IE8'])
    .push()
    .help("Add a platform name (default: ['W3C', 'IE7', 'IE8'])");

parser.option("-l", "--flatten", "flatten")
    .def(false)
    .set(true)
    .help("Flatten all code into a single Application.js file and attempt add script tag to index.html (useful for Adobe AIR and CDN deployment)");

parser.option("-f", "--force", "force")
   .def(false)
   .set(true)
   .help("Force overwriting OUTPUT_PROJECT if it exists");

parser.option("-n", "--nostrip", "strip")
    .def(true)
    .set(false)
    .help("Do not strip any files");

parser.option("-p", "--pngcrush", "png")    
    .def(false)
    .set(true)
    .help("Run pngcrush on all PNGs (pngcrush must be installed!)");

parser.option("-v", "--verbose", "verbose")
    .def(false)
    .set(true)
    .help("Verbose logging");

parser.helpful();

function main(args)
{
    var options = parser.parse(args);
    
    if (options.args.length < 2) {
        parser.printUsage(options);
        return;
    }
    
    //if (options.verbose)
        CPLogRegister(CPLogPrint);
    //else
    //    CPLogRegisterRange(CPLogPrint, "fatal", "info");

    // HACK: ensure trailing slashes for "relative" to work correctly
    var rootPath = FILE.path(options.args[0]).join("").absolute();
    var outputPath = FILE.path(options.args[1]).join("").absolute();

    if (!options.force && outputPath.exists()) {
        CPLog.error("OUTPUT_PROJECT " + outputPath + " exists. Use -f to overwrite.");
        OS.exit(1);
    }

    press(rootPath, outputPath, options);
}

function press(rootPath, outputPath, options) {
    CPLog.info("===========================================");
    CPLog.info("Application root:    " + rootPath);
    CPLog.info("Output directory:    " + outputPath);
    
    var outputFiles = {};
    
    // analyze and gather files for each environment:
    options.environments.forEach(function(environment) {
        pressEnvironment(rootPath, outputFiles, environment, options);
    });
    
    // phase 4: copy everything and write out the new files
    CPLog.error("PHASE 4: copy to output ("+rootPath+" to "+outputPath+")");
    
    FILE.copyTree(rootPath, outputPath);
    
    for (var path in outputFiles) {
        var file = outputPath.join(rootPath.relative(path));
        
        var parent = file.dirname();
        if (!parent.exists()) {
            CPLog.warn(parent + " doesn't exist, creating directories.");
            parent.mkdirs();
        }
        
        if (typeof outputFiles[path] !== "string")
            outputFiles[path] = outputFiles[path].join("");
        
        CPLog.info((file.exists() ? "Overwriting: " : "Writing:     ") + file);
        FILE.write(file, outputFiles[path], { charset : "UTF-8" });
    }
    
    // strip known unnecessary files
    // outputPath.glob("**/Frameworks/Debug").forEach(function(debugFramework) {
    //     outputPath.join(debugFramework).rmtree();
    // });
    // outputPath.join("index-debug.html").remove();
    
    if (options.png) {
        pngcrushDirectory(outputPath);
    }
}

function pressEnvironment(rootPath, outputFiles, environment, options) {
    
    var mainPath = String(rootPath.join(options.main));
    var frameworks = options.frameworks.map(function(framework) { return rootPath.join(framework); });
    
    CPLog.info("===========================================");
    CPLog.info("Main file:           " + mainPath)
    CPLog.info("Frameworks:          " + frameworks);
    CPLog.info("Environment:         " + environment);
    
    // get a Rhino context
    var ctx = Packages.org.mozilla.javascript.Context.getCurrentContext();
    var scope = makeObjjScope(ctx); // "scope" is the same as require("objective-j").window;
    
    scope.OBJJ_INCLUDE_PATHS = frameworks;
{'fraction_non_alphanumeric': 0.18250950570342206, 'fraction_numerical': 0.003062948880439375, 'mean_word_length': 3.099134199134199, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7175791', 'n_tokens_mistral': 2955, 'n_tokens_neox': 2942, 'n_words': 662}