diff --git a/config.js b/config.js index 0eb11c31..fa2e08d6 100644 --- a/config.js +++ b/config.js @@ -4,7 +4,8 @@ const log = require("./logger.js"); const Lab = { descriptor_name: "lab-descriptor", build_dir: "build", - exp_dir: "exp", + exp_build_dir: "exprepos", + exp_deploy_dir: "exp", deployment_dest: "/var/www/html", stage_dir: "stage", ui_template_name: "templates", @@ -31,7 +32,7 @@ const Lab = { } ] -} +}; const Experiment = { descriptor_name: "experiment-descriptor.json", diff --git a/exp_build/task.js b/exp_build/task.js index 4d06e8d1..57c4d1a8 100644 --- a/exp_build/task.js +++ b/exp_build/task.js @@ -164,6 +164,7 @@ class Task extends Unit { plugins: exp_info.plugins, page_plugins: page_plugins, local: options.local, + addAnalytics: options.addAnalytics, units: this.setCurrent(this.getMenu(exp_info.menu)), experiment_name: exp_info.name, meta: { diff --git a/exp_build/templates/partials/analytics-body.handlebars b/exp_build/templates/partials/analytics-body.handlebars index a4af4371..be3692c1 100644 --- a/exp_build/templates/partials/analytics-body.handlebars +++ b/exp_build/templates/partials/analytics-body.handlebars @@ -1,3 +1,4 @@ +{{#if addAnalytics}} {{#if production}} {{/if}} +{{/if}} diff --git a/exp_build/templates/partials/analytics-head.handlebars b/exp_build/templates/partials/analytics-head.handlebars index f977869f..a41597e2 100644 --- a/exp_build/templates/partials/analytics-head.handlebars +++ b/exp_build/templates/partials/analytics-head.handlebars @@ -1,3 +1,4 @@ +{{#if addAnalytics}} {{#if production}} +{{/if}} {{/if}} \ No newline at end of file diff --git a/lab_build/exp_utils.js b/lab_build/exp_utils.js index 273b583d..87939f43 100644 --- a/lab_build/exp_utils.js +++ b/lab_build/exp_utils.js @@ -1,6 +1,6 @@ const path = require("path"); -const {toDirName} = require("./lab_utils.js"); -const {run} = require("../exp_build/exp_gen.js"); +const { toDirName } = require("./lab_utils.js"); +const { run } = require("../exp_build/exp_gen.js"); const chalk = require("chalk"); const shell = require("shelljs"); const log = require("../logger.js"); @@ -23,17 +23,18 @@ function exp_clone(e, exp_dir) { log.debug(`Cloning ${e.repo} to ${exp_dir}`); const e_short_name = e["short-name"]; shell.mkdir("-p", path.resolve(exp_dir)); + shell.mkdir("-p", path.resolve(exp_dir, e_short_name)); shell.rm("-rf", path.resolve(exp_dir, e_short_name)); shell.exec( `git clone -b ${e.tag} --depth 1 ${e.repo} "${path.resolve( exp_dir, e_short_name )}"`, - {silent: false} + { silent: false } ); } -function exp_build(e, ld, exp_dir) { +function exp_build(e, ld, exp_dir, build_options) { const e_short_name = e["short-name"]; log.debug(`Building experiment ${e_short_name} at ${path.resolve(exp_dir, e_short_name)}`); /* @@ -46,40 +47,44 @@ function exp_build(e, ld, exp_dir) { ld.exp_name = e.name; ld.exp_short_name = e_short_name; - const build_options = { - env: BuildEnvs.PRODUCTION, - isValidate: false, - plugins: false - } + // const build_options = { + // env: BuildEnvs.PRODUCTION, + // isValidate: false, + // plugins: false + // }; run(path.resolve(exp_dir, e_short_name), ld, build_options); } function exp_stage(e, exp_dir, deployment_dest) { const e_short_name = toDirName(e["short-name"]); - log.debug(`Staging experiment ${e_short_name} to ${path.resolve(deployment_dest, "stage", "exp", e_short_name)}`); + log.debug(`Staging experiment ${e_short_name} to ${path.resolve(deployment_dest, e_short_name)}`); - shell.rm("-rf", `${deployment_dest}/stage/exp/${e_short_name}/`); + shell.rm("-rf", `${deployment_dest}/${e_short_name}/`); shell.mkdir( "-p", - path.resolve(deployment_dest, "stage", "exp", e_short_name) + path.resolve(deployment_dest, e_short_name) ); shell.cp( "-rf", `${exp_dir}/${e_short_name}/build/*`, - `${deployment_dest}/stage/exp/${e_short_name}/` + `${deployment_dest}/${e_short_name}/` ); } -function loadExperiments(labpath) { +function loadExperiments(labpath, build_options) { const ldpath = path.resolve(labpath, "lab-descriptor.json"); const lab_descriptor = require(ldpath); - const config = require("./lab_config.json"); - const exp_dir = config["exp_dir"]; - const deployment_dest = config["deployment_dest"]; + //const config = require("./lab_config.json"); + const config = require("../config.js"); + const exp_dir = config.Lab.exp_build_dir; + const deployment_dest = config.Lab.deployment_dest; const lab_dir_name = toDirName(lab_descriptor.lab); const deployment_path = path.join(deployment_dest, lab_dir_name); + const lab_build_dir = config.Lab.build_dir; + const exp_deploy_dir = config.Lab.exp_deploy_dir; + const exp_deploy_path = path.join(labpath, lab_build_dir, exp_deploy_dir); const experiments = expList(lab_descriptor); const num_experiments = experiments.length; @@ -87,12 +92,13 @@ function loadExperiments(labpath) { experiments.forEach((e) => { log.info(`Loading experiment ${e["short-name"]} (${exp_count}/${num_experiments})`); exp_clone(e, exp_dir); - exp_build(e, lab_descriptor, exp_dir); - exp_stage(e, exp_dir, deployment_path); + exp_build(e, lab_descriptor, exp_dir, build_options); + // exp_stage(e, exp_dir, deployment_path); + exp_stage(e, exp_dir, exp_deploy_path); }); } module.exports = { - loadExperiments, - expList + loadExperiments, + expList }; \ No newline at end of file diff --git a/lab_build/lab_config.json b/lab_build/lab_config.json index c5cefa7c..89e82d3b 100644 --- a/lab_build/lab_config.json +++ b/lab_build/lab_config.json @@ -4,7 +4,8 @@ "lab-name.html", "broad-area.html", "sidebar.html", - "navbar.html" + "navbar.html", + "analytics-body.html" ], "pages" : [ { diff --git a/lab_build/lab_gen.js b/lab_build/lab_gen.js index 1a94a9c3..1f39fc0e 100644 --- a/lab_build/lab_gen.js +++ b/lab_build/lab_gen.js @@ -4,7 +4,7 @@ const fs = require("fs"); const glob = require("glob"); const { nextVersion } = require("./tags.js"); const { loadExperiments, expList } = require("./exp_utils"); -const { validateLabDescriptor } = require("../validation/validate_descriptor.js") +const { validateLabDescriptor } = require("../validation/validate_descriptor.js"); const config = require("./lab_config.json"); const log = require("../logger.js"); const { @@ -18,11 +18,16 @@ const { processLabDescriptor, } = require("./lab_utils"); const { renderTemplate } = require("./template.js"); +const { BuildEnvs } = require("../enums.js"); shell.config.silent = true; -function generateLab(labpath) { - const data = processLabDescriptor(path.join(labpath, "lab-descriptor.json")); +function generateLab(labpath, build_options) { + const data = processLabDescriptor(path.join(labpath, "lab-descriptor.json"), build_options); + data.options = { ...build_options }; + data.options.production = (build_options.env === BuildEnvs.PRODUCTION); + data.options.testing = (build_options.env === BuildEnvs.TESTING); + data.options.local = (build_options.env === BuildEnvs.LOCAL); const template_file = "skeleton.html"; const component_files = config.commonComponents; @@ -65,20 +70,20 @@ function moveToDeployDir(labpath) { elist.forEach((e) => { if (e.deploy) { - log.debug(`Deploying experiment ${e["short-name"]} to ${path.resolve(deployment_path, "exp", e["short-name"])}`); - shell.mkdir("-p", path.resolve(deployment_path, "exp", e["short-name"])); - // shell.exec(`rsync -arv --exclude .git \ - // '${deployment_path}/stage/exp/${e["short-name"]}/'* '${deployment_path}/exp/${e["short-name"]}'`); - // alternative - shell.rm( - "-rf", - path.resolve(deployment_path, "exp", e["short-name"], "**", ".git/") - ); - shell.cp( - "-r", - `${deployment_path}/stage/exp/${e["short-name"]}/*`, - `${deployment_path}/exp/${e["short-name"]}` - ); + log.debug(`Deploying experiment ${e["short-name"]} to ${path.resolve(deployment_path, "exp", e["short-name"])}`); + shell.mkdir("-p", path.resolve(deployment_path, "exp", e["short-name"])); + // shell.exec(`rsync -arv --exclude .git \ + // '${deployment_path}/stage/exp/${e["short-name"]}/'* '${deployment_path}/exp/${e["short-name"]}'`); + // alternative + shell.rm( + "-rf", + path.resolve(deployment_path, "exp", e["short-name"], "**", ".git/") + ); + shell.cp( + "-r", + `${deployment_path}/stage/exp/${e["short-name"]}/*`, + `${deployment_path}/exp/${e["short-name"]}` + ); } }); @@ -98,7 +103,7 @@ function moveToDeployDir(labpath) { ); } -function validation(labpath){ +function validation(labpath) { const lab_descriptor_path = path.resolve(labpath, "lab-descriptor.json"); log.info("Validating lab descriptor"); @@ -143,7 +148,7 @@ function deployLab(labpath, release_type) { } } -function buildLab(labpath) { +function buildLab(labpath, build_options) { log.info(`Building lab at ${labpath}`); // Generate lab @@ -154,10 +159,10 @@ function buildLab(labpath) { } else { // 1 : Build all lab pages by rendering templates and loading components log.info("Generating lab pages"); - generateLab(labpath); + generateLab(labpath, build_options); // 2 : Load all experiments in the lab (Clone, build, and stage) log.info("Loading all experiments"); - loadExperiments(labpath); + loadExperiments(labpath, build_options); log.info("Lab build complete"); } } diff --git a/lab_build/lab_utils.js b/lab_build/lab_utils.js index a0117380..5d24ddbc 100644 --- a/lab_build/lab_utils.js +++ b/lab_build/lab_utils.js @@ -7,6 +7,7 @@ const { buildPage } = require("./template.js"); const moment = require("moment"); const log = require("../logger.js"); const Config = require("../config.js"); +const { BuildEnvs } = require("vlabs-buildexp/enums.js"); shell.config.silent = true; @@ -14,8 +15,11 @@ function toDirName(n) { return n.toLowerCase().trim().replace(/–/g, "").replace(/ +/g, "-"); } -function generateLink(baseUrl, expName, index_fn = "") { - const expUrl = new URL(`https://${baseUrl}/exp/${expName}/${index_fn}`); +function generateLink(baseUrl, expName, env, index_fn = "") { + const expUrl = (env === BuildEnvs.LOCAL) ? + `./exp/${expName}/${index_fn}` + : `https://${baseUrl}/exp/${expName}/${index_fn}`; + // : new URL(`https://${baseUrl}/exp/${expName}/${index_fn}`); return expUrl; } @@ -74,7 +78,7 @@ function prepareStructure(labpath) { shell.mkdir("-p", path.resolve(labpath, "build")); shell.cp( "-r", - path.resolve(Config.assets_path(),"*"), + path.resolve(Config.assets_path(), "*"), path.resolve(labpath, "build") ); } @@ -92,14 +96,14 @@ function buildLabPages(pages, labpath, template_file, component_files) { }); } -function processLabDescriptor(descriptor_path) { +function processLabDescriptor(descriptor_path, build_options) { log.debug(`Processing lab descriptor`); const lab_descriptor = JSON.parse(fs.readFileSync(descriptor_path)); if (lab_descriptor.experiments) { lab_descriptor.experiments = lab_descriptor.experiments.map((e) => { - const exp_url = generateLink(lab_descriptor.baseUrl, e["short-name"]); - return { name: e.name, link: exp_url.toString() }; + const exp_url = generateLink(lab_descriptor.baseUrl, e["short-name"], build_options.env); + return { name: e.name, link: exp_url }; }); return lab_descriptor; } else { @@ -113,9 +117,10 @@ function processLabDescriptor(descriptor_path) { const exp_url = generateLink( lab_descriptor.baseUrl, e["short-name"], + build_options.env, (index_fn = "index.html") ); - return { name: e.name, link: exp_url.toString() }; + return { name: e.name, link: exp_url }; }), }; }); diff --git a/lab_build/page-templates/analytics-body.handlebars b/lab_build/page-templates/analytics-body.handlebars new file mode 100644 index 00000000..94bc6c03 --- /dev/null +++ b/lab_build/page-templates/analytics-body.handlebars @@ -0,0 +1,10 @@ +{{#if options.addAnalytics}} + +{{#if options.production}} + +{{else if options.testing}} + +{{/if}} +{{/if}} diff --git a/lab_build/page-templates/head.handlebars b/lab_build/page-templates/head.handlebars index 046ff5c3..2012cdcd 100644 --- a/lab_build/page-templates/head.handlebars +++ b/lab_build/page-templates/head.handlebars @@ -1,4 +1,6 @@ +{{#if options.addAnalytics}} +{{#if options.production}} var dataLayer = [{ {{#if escapeCharOnFlag }} @@ -16,4 +18,24 @@ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-W59SWTR'); + +{{else if options.testing}} + var dataLayer = [{ + {{#if escapeCharOnFlag }} + 'labName': '{{lab}}', + {{else}} + 'labName': '{{{lab}}}', + {{/if}} + 'discipline': '{{broadArea.name}}', + 'college': '{{collegeName}}', + 'phase': '{{phase}}', + }]; + + (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': + new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], + j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= + 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); + })(window,document,'script','dataLayer','GTM-5NMQ9NQ'); +{{/if}} +{{/if}} diff --git a/lab_build/skeleton.html b/lab_build/skeleton.html index b49057c2..d5d874a1 100644 --- a/lab_build/skeleton.html +++ b/lab_build/skeleton.html @@ -1,255 +1,173 @@ -
- - - - - - -