diff --git a/BUILD.bazel b/BUILD.bazel index f1d98cc1..a31306c8 100755 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -54,7 +54,6 @@ bzl_library( "//internal/js_library:bzl", "//internal/linker:bzl", "//internal/pkg_npm:bzl", - "//internal/pkg_web:bzl", "//internal/providers:bzl", "//toolchains/node:bzl", ], diff --git a/index.bzl b/index.bzl index e018fb9c..9a8ff942 100755 --- a/index.bzl +++ b/index.bzl @@ -33,7 +33,6 @@ load("//internal/node:node_repositories.bzl", _node_repositories = "node_reposit load("//internal/node:npm_package_bin.bzl", _npm_bin = "npm_package_bin") load("//internal/npm_install:npm_install.bzl", _npm_install = "npm_install", _yarn_install = "yarn_install") load("//internal/pkg_npm:pkg_npm.bzl", _pkg_npm = "pkg_npm_macro") -load("//internal/pkg_web:pkg_web.bzl", _pkg_web = "pkg_web") load( "//internal/providers:tree_artifacts.bzl", _directory_file_path = "directory_file_path", @@ -45,7 +44,6 @@ nodejs_test = _nodejs_test node_repositories = _node_repositories pkg_npm = _pkg_npm npm_package_bin = _npm_bin -pkg_web = _pkg_web copy_to_bin = _copy_to_bin params_file = _params_file generated_file_test = _generated_file_test diff --git a/index.for_docs.bzl b/index.for_docs.bzl index 79e041ea..b5039329 100755 --- a/index.for_docs.bzl +++ b/index.for_docs.bzl @@ -33,7 +33,6 @@ load("//internal/node:node_repositories.bzl", _node_repositories = "node_reposit load("//internal/node:npm_package_bin.bzl", _npm_bin = "npm_package_bin") load("//internal/npm_install:npm_install.bzl", _npm_install = "npm_install", _yarn_install = "yarn_install") load("//internal/pkg_npm:pkg_npm.bzl", _pkg_npm = "pkg_npm") -load("//internal/pkg_web:pkg_web.bzl", _pkg_web = "pkg_web") check_bazel_version = _check_bazel_version copy_to_bin = _copy_to_bin @@ -45,7 +44,6 @@ pkg_npm = _pkg_npm npm_install = _npm_install yarn_install = _yarn_install npm_package_bin = _npm_bin -pkg_web = _pkg_web generated_file_test = _generated_file_test js_library = _js_library # ANY RULES ADDED HERE SHOULD BE DOCUMENTED, run yarn stardoc to verify diff --git a/internal/node/context.bzl b/internal/node/context.bzl index fba03a19..d511e46a 100755 --- a/internal/node/context.bzl +++ b/internal/node/context.bzl @@ -4,7 +4,7 @@ load("@build_bazel_rules_nodejs//:providers.bzl", "NodeContextInfo") _DOC = """node_context_data gathers information about the build configuration. It is a common dependency of all targets that are sensitive to configuration. -(currently pkg_npm, pkg_web, and rollup_bundle)""" +(currently pkg_npm, and rollup_bundle)""" def _impl(ctx): return [NodeContextInfo(stamp = ctx.attr.stamp)] diff --git a/internal/pkg_web/BUILD.bazel b/internal/pkg_web/BUILD.bazel deleted file mode 100755 index c1d75e29..00000000 --- a/internal/pkg_web/BUILD.bazel +++ /dev/null @@ -1,35 +0,0 @@ -# bazel_skylib mocked out -# load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load("@build_bazel_rules_nodejs//:index.bzl", bzl_library = "dummy_bzl_library") -load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary") - - -package(default_visibility = ["//visibility:public"]) - -exports_files(["pkg_web.bzl"]) - -bzl_library( - name = "bzl", - srcs = glob(["*.bzl"]), - visibility = ["//visibility:public"], -) - -nodejs_binary( - name = "assembler", - data = [ - "assembler.js", - "//third_party/github.com/gjtorikian/isBinaryFile", - ], - entry_point = ":assembler.js", - # TODO: figure out why isbinaryfile isn't resolved properly - templated_args = ["--bazel_patch_module_resolver"], -) - - -filegroup( - name = "package_contents", - srcs = glob( - ["*"], - exclude = ["*_spec.js"], - ), -) diff --git a/internal/pkg_web/assembler.js b/internal/pkg_web/assembler.js deleted file mode 100755 index 9f2d20cd..00000000 --- a/internal/pkg_web/assembler.js +++ /dev/null @@ -1,170 +0,0 @@ -/** - * @license - * Copyright 2018 The Bazel Authors. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const fs = require('fs'); -const path = require('path'); -const isBinary = require('isbinaryfile').isBinaryFileSync; - -/** - * Create a new directory and any necessary subdirectories - * if they do not exist. - */ -function mkdirp(p) { - if (!fs.existsSync(p)) { - mkdirp(path.dirname(p)); - fs.mkdirSync(p); - } -} - -function unquoteArgs(s) { - return s.replace(/^'(.*)'$/, '$1'); -} - -/** -* The status files are expected to look like -* BUILD_SCM_HASH 83c699db39cfd74526cdf9bebb75aa6f122908bb -* BUILD_SCM_LOCAL_CHANGES true -* STABLE_BUILD_SCM_VERSION 6.0.0-beta.6+12.sha-83c699d.with-local-changes -* BUILD_TIMESTAMP 1520021990506 -* -* Parsing regex is created based on Bazel's documentation describing the status file schema: -* The key names can be anything but they may only use upper case letters and underscores. The -* first space after the key name separates it from the value. The value is the rest of the line -* (including additional whitespaces). -* -* @param {string} p the path to the status file -* @returns a two-dimensional array of key/value pairs -*/ -function parseStatusFile(p) { - if (!p) return []; - const results = {}; - const statusFile = fs.readFileSync(p, {encoding: 'utf-8'}); - for (const match of `\n${statusFile}`.matchAll(/^([A-Z_]+) (.*)/gm)) { - // Lines which go unmatched define an index value of `0` and should be skipped. - if (match.index === 0) { - continue; - } - results[match[1]] = match[2]; - } - return results; -} - -function normalizeSubstitutions(substitutionsArg, stampMap) { - const substitutions = JSON.parse(substitutionsArg); - - const normalizedSubstitutions = {}; - - for (const occurrence in substitutions) { - let substituteWith = substitutions[occurrence]; - if (substituteWith.match(/^{.*?}$/)) { - substituteWith = substituteWith.replace(/^{(.*?)}$/, '$1'); - if (!stampMap[substituteWith]) { - throw new Error(`Could not find ${substituteWith} key in status file.`); - } - substituteWith = stampMap[substituteWith]; - } - normalizedSubstitutions[occurrence] = substituteWith; - } - return normalizedSubstitutions; -} - -function main(params) { - const outdir = params.shift(); - - const volatileFilePath = params.shift(); - - const stableFilePath = params.shift(); - - const rawSubstitutions = params.shift().replace(/^'(.*)'$/, '$1'); - - const stampMap = { - ...parseStatusFile(volatileFilePath), - ...parseStatusFile(stableFilePath), - }; - - const normalizedSubstitutions = normalizeSubstitutions(rawSubstitutions, stampMap) - - const substitutions = Object.entries(normalizedSubstitutions); - - const rootDirs = []; - while (params.length && params[0] !== '--assets') { - let r = params.shift(); - if (!r.endsWith('/')) { - r += '/'; - } - rootDirs.push(r); - } - // Always trim the longest prefix - rootDirs.sort((a, b) => b.length - a.length); - - params.shift(); // --assets - - function relative(execPath) { - if (execPath.startsWith('external/')) { - execPath = execPath.substring('external/'.length); - } - for (const r of rootDirs) { - if (execPath.startsWith(r)) { - return execPath.substring(r.length); - } - } - return execPath; - } - - function copy(f, substitutions) { - if (fs.statSync(f).isDirectory()) { - for (const file of fs.readdirSync(f)) { - // Change paths to posix - copy(path.join(f, file).replace(/\\/g, '/'), substitutions); - } - } else if (!isBinary(f)) { - const dest = path.join(outdir, relative(f)); - let content = fs.readFileSync(f, {encoding: 'utf-8'}); - substitutions.forEach(([occurrence, replaceWith]) => { - content = content.replace(occurrence, replaceWith); - }); - fs.mkdirSync(path.dirname(dest), {recursive: true}); - fs.writeFileSync(dest, content); - } else { - const dest = path.join(outdir, relative(f)); - mkdirp(path.dirname(dest)); - fs.copyFileSync(f, dest); - } - } - - // Remove duplicate files (which may come from this rule) from the - // list since fs.copyFileSync may fail with `EACCES: permission denied` - // as it will not have permission to overwrite duplicate files that were - // copied from within bazel-bin. - // See https://github.com/bazelbuild/rules_nodejs/pull/546. - for (const f of new Set(params)) { - copy(f, substitutions); - } - return 0; -} - -module.exports = {main}; - -if (require.main === module) { - // We always require the arguments are encoded into a flagfile - // so that we don't exhaust the command-line limit. - const params = fs.readFileSync(process.argv[2], {encoding: 'utf-8'}) - .split('\n') - .filter(l => !!l) - .map(unquoteArgs); - process.exitCode = main(params); -} diff --git a/internal/pkg_web/pkg_web.bzl b/internal/pkg_web/pkg_web.bzl deleted file mode 100755 index 51a52860..00000000 --- a/internal/pkg_web/pkg_web.bzl +++ /dev/null @@ -1,114 +0,0 @@ -# Copyright 2019 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Contains the pkg_web rule. -""" - -load("//:providers.bzl", "NODE_CONTEXT_ATTRS", "NodeContextInfo") - -_DOC = """Assembles a web application from source files.""" - -_ATTRS = dict(NODE_CONTEXT_ATTRS, **{ - "additional_root_paths": attr.string_list( - doc = """Path prefixes to strip off all srcs relative to the root of the repo, in addition to the current package. Longest wins.""", - ), - "srcs": attr.label_list( - allow_files = True, - doc = """Files which should be copied into the package""", - ), - "substitutions": attr.string_dict( - doc = """Key-value pairs which are replaced in all the files while building the package. - -You can use values from the workspace status command using curly braces, for example -`{"0.0.0-PLACEHOLDER": "{STABLE_GIT_VERSION}"}`. -See the section on stamping in the README.""", - ), - "_assembler": attr.label( - default = "@build_bazel_rules_nodejs//internal/pkg_web:assembler", - executable = True, - cfg = "host", - ), -}) - -def _move_files(ctx, root_paths): - """Moves files into an output directory - - Args: - ctx: bazel's action context - root_paths: Path prefixes to strip off all srcs. Longest wins. - - Returns: - The output directory tree-artifact - """ - www_dir = ctx.actions.declare_directory(ctx.label.name) - args = ctx.actions.args() - inputs = ctx.files.srcs[:] - args.add(www_dir.path) - if ctx.attr.node_context_data[NodeContextInfo].stamp: - args.add(ctx.version_file.path) - inputs.append(ctx.version_file) - args.add(ctx.info_file.path) - inputs.append(ctx.info_file) - else: - args.add_all(["", ""]) - args.add(ctx.attr.substitutions) - args.add_all(root_paths) - args.add("--assets") - args.add_all([f.path for f in ctx.files.srcs]) - args.use_param_file("%s", use_always = True) - - ctx.actions.run( - inputs = inputs, - outputs = [www_dir], - executable = ctx.executable._assembler, - arguments = [args], - execution_requirements = {"local": "1"}, - env = {"COMPILATION_MODE": ctx.var["COMPILATION_MODE"]}, - ) - return depset([www_dir]) - -def additional_root_paths(ctx): - return ctx.attr.additional_root_paths + [ - # also add additional_root_paths variants from genfiles dir and bin dir - "/".join([ctx.genfiles_dir.path, p]) - for p in ctx.attr.additional_root_paths - ] + [ - "/".join([ctx.bin_dir.path, p]) - for p in ctx.attr.additional_root_paths - ] + [ - # package path is the root, including in bin/gen - ctx.label.package, - "/".join([ctx.bin_dir.path, ctx.label.package]), - "/".join([ctx.genfiles_dir.path, ctx.label.package]), - - # bazel-bin/gen dirs to absolute paths - ctx.genfiles_dir.path, - ctx.bin_dir.path, - - # package re-rooted subdirectory - "/".join([p for p in [ctx.bin_dir.path, ctx.label.package, "_" + ctx.label.name, ctx.label.package] if p]), - ] - -def _impl(ctx): - root_paths = additional_root_paths(ctx) - package_layout = _move_files(ctx, root_paths) - return [ - DefaultInfo(files = package_layout), - ] - -pkg_web = rule( - implementation = _impl, - attrs = _ATTRS, - doc = _DOC, -)