From c204d0f0d4bb643e2f8d6d567acf78eca34ac170 Mon Sep 17 00:00:00 2001 From: John Spackman Date: Mon, 18 Jan 2021 09:45:39 +0000 Subject: [PATCH] adds `preserveEnvironment` setting (#790) * adds `preserveEnvironment` setting * Fix tests Thanks @johnspackman ! --- Manifest.json | 2 +- bootstrap-compiler | 19 +++++++++----- source/class/qx/tool/cli/Cli.js | 6 +++++ source/class/qx/tool/cli/commands/Command.js | 3 --- source/class/qx/tool/cli/commands/Compile.js | 3 +++ .../class/qx/tool/compiler/makers/AppMaker.js | 25 +++++++++++++------ .../class/qx/tool/compiler/targets/Target.js | 14 +++++++++-- .../qx/tool/schema/compile-1-0-0.json | 7 ++++++ test/compiler/test-deps.js | 5 +++- .../source/class/testapp/Application.js | 1 + 10 files changed, 65 insertions(+), 20 deletions(-) diff --git a/Manifest.json b/Manifest.json index 92a904043..65b2b9749 100644 --- a/Manifest.json +++ b/Manifest.json @@ -30,7 +30,7 @@ "translation": "source/translation" }, "requires": { - "@qooxdoo/framework": "^6.0.0-alpha", + "@qooxdoo/framework": "^6.0.0-beta", "@qooxdoo/compiler": "^1.0.0-beta" } } \ No newline at end of file diff --git a/bootstrap-compiler b/bootstrap-compiler index 9280a5993..7228ea743 100755 --- a/bootstrap-compiler +++ b/bootstrap-compiler @@ -4,16 +4,23 @@ const utils = require("./bin/tools/utils"); const fs = require("fs"); (async function() { - let version = process.argv[2]; - if (!version) { - version = require("./package.json").version; - } let upgradeFramework = false; - if (process.argv.length > 2) { - if (process.argv[2] == "--upgrade-framework") { + let version = null; + for (let i = 2; i < process.argv.length; i++) { + let arg = process.argv[i]; + if (arg == "--upgrade-framework") { upgradeFramework = true; + } else if (arg.startsWith("--")) { + console.error("Invalid argument " + arg); + } else { + version = arg; } } + + if (!version) { + version = require("./package.json").version; + } + await utils.bootstrapCompiler(version, upgradeFramework); process.exit(0); })(); diff --git a/source/class/qx/tool/cli/Cli.js b/source/class/qx/tool/cli/Cli.js index 0957c3c2c..096cf655a 100644 --- a/source/class/qx/tool/cli/Cli.js +++ b/source/class/qx/tool/cli/Cli.js @@ -90,6 +90,11 @@ qx.Class.define("qx.tool.cli.Cli", { default: false, type: "boolean" }) + .option("debug", { + describe: "enables debug output", + default: false, + type: "boolean" + }) .option("quiet", { alias: "q", describe: "suppresses normal progress output to console", @@ -126,6 +131,7 @@ Versions: @qooxdoo/compiler v${qx.tool.compiler.Version.VERSION} let yargs = this.__createYargs() .usage(title); this.argv = yargs.argv; + qx.tool.cli.LogAppender.setMinLevel(this.argv.debug ? "debug" : "warn"); }, /** diff --git a/source/class/qx/tool/cli/commands/Command.js b/source/class/qx/tool/cli/commands/Command.js index e81b36221..4558d9c23 100644 --- a/source/class/qx/tool/cli/commands/Command.js +++ b/source/class/qx/tool/cli/commands/Command.js @@ -32,9 +32,6 @@ qx.Class.define("qx.tool.cli.commands.Command", { construct: function(argv) { this.base(arguments); this.argv = argv; - if (argv.verbose) { - qx.tool.cli.LogAppender.setMinLevel("debug"); - } }, properties: { diff --git a/source/class/qx/tool/cli/commands/Compile.js b/source/class/qx/tool/cli/commands/Compile.js index 3fa0fd30c..253543ee0 100644 --- a/source/class/qx/tool/cli/commands/Compile.js +++ b/source/class/qx/tool/cli/commands/Compile.js @@ -846,6 +846,9 @@ Framework: v${await this.getUserQxVersion()} in ${await this.getUserQxPath()}`); if (targetConfig.environment) { target.setEnvironment(targetConfig.environment); } + if (targetConfig.preserveEnvironment) { + target.setPreserveEnvironment(targetConfig.preserveEnvironment); + } if (data["path-mappings"]) { for (var from in data["path-mappings"]) { diff --git a/source/class/qx/tool/compiler/makers/AppMaker.js b/source/class/qx/tool/compiler/makers/AppMaker.js index 7e3635367..8e1fecaea 100644 --- a/source/class/qx/tool/compiler/makers/AppMaker.js +++ b/source/class/qx/tool/compiler/makers/AppMaker.js @@ -69,6 +69,7 @@ qx.Class.define("qx.tool.compiler.makers.AppMaker", { */ async make() { var analyser = this.getAnalyser(); + let target = this.getTarget(); await this.fireEventAsync("making"); this.setSuccess(null); @@ -84,8 +85,17 @@ qx.Class.define("qx.tool.compiler.makers.AppMaker", { "qx.compiler.version": qx.tool.compiler.Version.VERSION }, this.getEnvironment(), - this.getTarget().getDefaultEnvironment(), - this.getTarget().getEnvironment()); + target.getDefaultEnvironment(), + target.getEnvironment()); + + let preserve = target.getPreserveEnvironment(); + if (preserve) { + let tmp = {}; + preserve.forEach(key => tmp[key] = true); + preserve = tmp; + } else { + preserve = {}; + } let appEnvironments = {}; this.getApplications().forEach(app => { @@ -113,7 +123,9 @@ qx.Class.define("qx.tool.compiler.makers.AppMaker", { this.getApplications().forEach(app => { let env = appEnvironments[app.toHashCode()]; Object.keys(allAppEnv).forEach(key => { - if (allAppEnv[key].same) { + if (preserve[key]) { + env[key] = compileEnv[key]; + } else if (allAppEnv[key].same) { delete env[key]; } else if (env[key] === undefined) { env[key] = compileEnv[key]; @@ -123,7 +135,7 @@ qx.Class.define("qx.tool.compiler.makers.AppMaker", { // Cleanup to remove env that have been moved to the app Object.keys(allAppEnv).forEach(key => { - if (allAppEnv[key].same) { + if (!preserve[key] && allAppEnv[key].same) { compileEnv[key] = allAppEnv[key].value; } else { delete compileEnv[key]; @@ -142,9 +154,9 @@ qx.Class.define("qx.tool.compiler.makers.AppMaker", { await qx.tool.utils.Utils.promisifyThis(analyser.initialScan, analyser); await analyser.updateEnvironmentData(); - this.getTarget().setAnalyser(analyser); + target.setAnalyser(analyser); this.__applications.forEach(app => app.setAnalyser(analyser)); - await this.getTarget().open(); + await target.open(); if (this.isOutputTypescript()) { analyser.getLibraries().forEach(library => { @@ -169,7 +181,6 @@ qx.Class.define("qx.tool.compiler.makers.AppMaker", { await analyser.analyseClasses(); await analyser.saveDatabase(); - var target = this.getTarget(); await this.fireEventAsync("writingApplications"); // Detect which applications need to be recompiled by looking for classes recently compiled diff --git a/source/class/qx/tool/compiler/targets/Target.js b/source/class/qx/tool/compiler/targets/Target.js index fdc4bcaf0..05bb52a92 100644 --- a/source/class/qx/tool/compiler/targets/Target.js +++ b/source/class/qx/tool/compiler/targets/Target.js @@ -81,6 +81,16 @@ qx.Class.define("qx.tool.compiler.targets.Target", { inheritable: true, nullable: true }, + + /** + * List of environment keys to preserve in code, ie reserve for runtime detection + * and exclude from code elimination + */ + preserveEnvironment: { + init: null, + nullable: true, + check: "Array" + }, /** * The analyser being generated @@ -181,7 +191,7 @@ qx.Class.define("qx.tool.compiler.targets.Target", { /** @type {qx.tool.compiler.targets.meta.ApplicationMeta} for the current application */ __appMeta: null, - + /** * Initialises the target, creating directories etc */ @@ -203,7 +213,7 @@ qx.Class.define("qx.tool.compiler.targets.Target", { } return value; }, - + /** * Returns the root for applications */ diff --git a/source/resource/qx/tool/schema/compile-1-0-0.json b/source/resource/qx/tool/schema/compile-1-0-0.json index 263f096c5..b3d786b80 100644 --- a/source/resource/qx/tool/schema/compile-1-0-0.json +++ b/source/resource/qx/tool/schema/compile-1-0-0.json @@ -214,6 +214,13 @@ "environment": { "$ref": "#/properties/environment" }, + "preserveEnvironment": { + "description": "a list of environment settings that will be ignored for code elimination, ie settings which *must* be configured at runtime", + "type": "array", + "items": { + "type": "string" + } + }, "writeCompileInfo": { "description": "if true, the target will write a compile-info.json and resources.json into the application's output directory, containing the data structures required to generate an application", "type":"boolean" diff --git a/test/compiler/test-deps.js b/test/compiler/test-deps.js index 65304e757..eaac2cec8 100644 --- a/test/compiler/test-deps.js +++ b/test/compiler/test-deps.js @@ -22,9 +22,11 @@ async function createMaker() { environment: { envVar1: "ONE", envVar2: "TWO", + runtimeVar: "RUNTIMEVAR", "test.overridden4": "target", "test.overridden5": "target" - } + }, + preserveEnvironment: [ "runtimeVar" ] }), locales: ["en"], writeAllTranslations: true, @@ -280,6 +282,7 @@ test("Checks dependencies and environment settings", assert => { assert.ok(src.match(/var envVar2 = "222"/), "environment setting for envVar2"); assert.ok(src.match(/var envVar3 = qx.core.Environment.get\("envVar3"\)/), "environment setting for envVar3"); assert.ok(src.match(/var envVar4 = "four"/), "environment setting for envVar4"); + assert.ok(src.match(/var runtimeVar = qx.core.Environment.get/), "environment setting for runtimeVar"); assert.ok(src.match(/var envTestOverriden3 = "global"/), "environment setting for envTestOverriden3"); assert.ok(src.match(/var envTestOverriden4 = "target"/), "environment setting for envTestOverriden4"); assert.ok(src.match(/var envTestOverriden5 = "application"/), "environment setting for envTestOverriden5"); diff --git a/test/compiler/testapp/source/class/testapp/Application.js b/test/compiler/testapp/source/class/testapp/Application.js index 43095b52c..683b13d03 100644 --- a/test/compiler/testapp/source/class/testapp/Application.js +++ b/test/compiler/testapp/source/class/testapp/Application.js @@ -115,6 +115,7 @@ qx.Class.define("testapp.Application", { var envVar2 = qx.core.Environment.get("envVar2"); var envVar3 = qx.core.Environment.get("envVar3"); var envVar4 = qx.core.Environment.get("envVar4"); + var runtimeVar = qx.core.Environment.get("runtimeVar"); var envTestOverriden3 = qx.core.Environment.get("test.overridden3"); var envTestOverriden4 = qx.core.Environment.get("test.overridden4"); var envTestOverriden5 = qx.core.Environment.get("test.overridden5");