chore(6.0): remove pkg_web
diff --git a/BUILD.bazel b/BUILD.bazel index 8521bc6..031c940 100644 --- a/BUILD.bazel +++ b/BUILD.bazel
@@ -55,7 +55,6 @@ "//internal/linker:bzl", "//internal/node:bzl", "//internal/pkg_npm:bzl", - "//internal/pkg_web:bzl", "//internal/providers:bzl", "@rules_nodejs//nodejs/private/providers:bzl", ], @@ -93,7 +92,6 @@ "//internal/node:package_contents", "//internal/npm_install:package_contents", "//internal/pkg_npm:package_contents", - "//internal/pkg_web:package_contents", "//internal/providers:package_contents", "//internal/runfiles:package_contents", "//third_party/github.com/bazelbuild/bazel:package_contents",
diff --git a/docs/Built-ins.md b/docs/Built-ins.md index ae9f91f..8a7a590 100755 --- a/docs/Built-ins.md +++ b/docs/Built-ins.md
@@ -1095,64 +1095,6 @@ Defaults to `[]` -## pkg_web - -**USAGE** - -<pre> -pkg_web(<a href="#pkg_web-name">name</a>, <a href="#pkg_web-additional_root_paths">additional_root_paths</a>, <a href="#pkg_web-srcs">srcs</a>, <a href="#pkg_web-stamp">stamp</a>, <a href="#pkg_web-substitutions">substitutions</a>) -</pre> - -Assembles a web application from source files. - -**ATTRIBUTES** - - -<h4 id="pkg_web-name">name</h4> - -(*<a href="https://bazel.build/docs/build-ref.html#name">Name</a>, mandatory*): A unique name for this target. - - -<h4 id="pkg_web-additional_root_paths">additional_root_paths</h4> - -(*List of strings*): Path prefixes to strip off all srcs relative to the root of the repo, in addition to the current package. Longest wins. - -Defaults to `[]` - -<h4 id="pkg_web-srcs">srcs</h4> - -(*<a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a>*): Files which should be copied into the package - -Defaults to `[]` - -<h4 id="pkg_web-stamp">stamp</h4> - -(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Whether to encode build information into the output. Possible values: - - `@rules_nodejs//nodejs/stamp:always`: - Always stamp the build information into the output, even in [--nostamp][stamp] builds. - This setting should be avoided, since it potentially causes cache misses remote caching for - any downstream actions that depend on it. - - `@rules_nodejs//nodejs/stamp:never`: - Always replace build information by constant values. This gives good build result caching. - - `@rules_nodejs//nodejs/stamp:use_stamp_flag`: - Embedding of build information is controlled by the [--[no]stamp][stamp] flag. - Stamped binaries are not rebuilt unless their dependencies change. - [stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp The dependencies of this attribute must provide: StampSettingInfo - - -Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag` - -<h4 id="pkg_web-substitutions">substitutions</h4> - -(*<a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a>*): 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. - -Defaults to `{}` - - ## yarn_install **USAGE**
diff --git a/docs/stamping.md b/docs/stamping.md index 73c88db..f5420bb 100644 --- a/docs/stamping.md +++ b/docs/stamping.md
@@ -17,7 +17,7 @@ ## Substitutions attribute -In a `pkg_npm` or `pkg_web` you can use the `substitutions` attribute like: +In a `pkg_npm` you can use the `substitutions` attribute like: ```python pkg_npm(
diff --git a/index.bzl b/index.bzl index bb48123..672c863 100644 --- a/index.bzl +++ b/index.bzl
@@ -34,14 +34,12 @@ 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") nodejs_binary = _nodejs_binary 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 63cc625..cc5fe42 100644 --- a/index.for_docs.bzl +++ b/index.for_docs.bzl
@@ -30,7 +30,6 @@ 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") copy_to_bin = _copy_to_bin params_file = _params_file @@ -40,5 +39,4 @@ npm_install = _npm_install yarn_install = _yarn_install npm_package_bin = _npm_bin -pkg_web = _pkg_web js_library = _js_library
diff --git a/internal/pkg_web/BUILD.bazel b/internal/pkg_web/BUILD.bazel deleted file mode 100644 index 2fc7362..0000000 --- a/internal/pkg_web/BUILD.bazel +++ /dev/null
@@ -1,31 +0,0 @@ -load("@bazel_skylib//:bzl_library.bzl", "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.cjs", - "//third_party/github.com/gjtorikian/isBinaryFile", - ], - entry_point = ":assembler.cjs", - # TODO: figure out why isbinaryfile isn't resolved properly - templated_args = ["--bazel_patch_module_resolver"], -) - -filegroup( - name = "package_contents", - srcs = glob( - ["*"], - exclude = ["*_spec.cjs"], - ), -)
diff --git a/internal/pkg_web/assembler.cjs b/internal/pkg_web/assembler.cjs deleted file mode 100644 index 9f2d20c..0000000 --- a/internal/pkg_web/assembler.cjs +++ /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/assembler_spec.cjs b/internal/pkg_web/assembler_spec.cjs deleted file mode 100644 index e3f94fc..0000000 --- a/internal/pkg_web/assembler_spec.cjs +++ /dev/null
@@ -1,56 +0,0 @@ -const assembler = require('./assembler.cjs'); -const path = require('path'); -const fs = require('fs'); - -describe('assembler', () => { - const outdir = 'output'; - const volatilePath = 'path/to/volatile-status.txt'; - const stablePath = 'path/to/stable-status.txt'; - let testCount = 0; - beforeEach(() => { - const now = Date.now() + String(testCount++); - // prevent test isolation failures by running each spec in a separate dir - const uniqueDir = path.join(process.env['TEST_TMPDIR'], String(now)); - fs.mkdirSync(uniqueDir); - process.chdir(uniqueDir); - fs.mkdirSync('path'); - fs.mkdirSync('path/to'); - fs.writeFileSync('path/to/thing1.txt', 'some content', {encoding: 'utf-8'}); - fs.writeFileSync(volatilePath, 'TEST_KEY 41561') - fs.writeFileSync(stablePath, 'STABLE_GIT_VERSION 123abc') - }); - - it('should copy files', () => { - assembler.main([outdir, '', '', '{}', '--assets', 'path/to/thing1.txt']); - expect(fs.readdirSync('output/path/to')).toContain('thing1.txt'); - expect(fs.readFileSync('output/path/to/thing1.txt', {encoding: 'utf-8'})).toBe('some content'); - }); - - it('should strip longest rootdir', () => { - assembler.main([outdir, '', '', '{}', 'path', 'path/to', '--assets', 'path/to/thing1.txt']); - expect(fs.readdirSync('output')).toContain('thing1.txt'); - }); - - it('should handle nested directories', () => { - assembler.main([outdir, '', '', '{}', 'path', '--assets', 'path/to']); - expect(fs.readdirSync('output/to')).toContain('thing1.txt'); - }); - - it('should replace contents with static text', () => { - assembler.main([ - outdir, '', '', '{"some content":"some other content"}', '--assets', 'path/to/thing1.txt' - ]); - expect(fs.readFileSync('output/path/to/thing1.txt', { - encoding: 'utf-8' - })).toBe('some other content'); - }) - - - it('should replace contents with dynamic text from Bazel workspace status', () => { - assembler.main([ - outdir, volatilePath, stablePath, '{"some":"{STABLE_GIT_VERSION}", "content":"{TEST_KEY}"}', - '--assets', 'path/to/thing1.txt' - ]); - expect(fs.readFileSync('output/path/to/thing1.txt', {encoding: 'utf-8'})).toBe('123abc 41561'); - }) -});
diff --git a/internal/pkg_web/pkg_web.bzl b/internal/pkg_web/pkg_web.bzl deleted file mode 100644 index e590d19..0000000 --- a/internal/pkg_web/pkg_web.bzl +++ /dev/null
@@ -1,116 +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("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_common.bzl", "COPY_EXECUTION_REQUIREMENTS") -load("@rules_nodejs//nodejs:providers.bzl", "STAMP_ATTR", "StampSettingInfo") - -_DOC = """Assembles a web application from source files.""" - -_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""", - ), - "stamp": STAMP_ATTR, - "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 = "exec", - ), -} - -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.stamp[StampSettingInfo].value: - 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 = COPY_EXECUTION_REQUIREMENTS, - 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, -)
diff --git a/internal/pkg_web/test-exports/BUILD.bazel b/internal/pkg_web/test-exports/BUILD.bazel deleted file mode 100644 index 89ec417..0000000 --- a/internal/pkg_web/test-exports/BUILD.bazel +++ /dev/null
@@ -1,31 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -EXTS = [ - "js", - "css", -] - -[ - genrule( - name = "bin-" + e, - outs = ["bin." + e], - cmd = "echo '' > $@", - output_to_bindir = True, - ) - for e in EXTS -] - -[ - genrule( - name = "gen-" + e, - outs = ["gen." + e], - cmd = "echo '' > $@", - output_to_bindir = False, - ) - for e in EXTS -] - -exports_files([ - "file.css", - "file.js", -])
diff --git a/internal/pkg_web/test-exports/file.css b/internal/pkg_web/test-exports/file.css deleted file mode 100644 index e69de29..0000000 --- a/internal/pkg_web/test-exports/file.css +++ /dev/null
diff --git a/internal/pkg_web/test-exports/file.js b/internal/pkg_web/test-exports/file.js deleted file mode 100644 index e69de29..0000000 --- a/internal/pkg_web/test-exports/file.js +++ /dev/null
diff --git a/internal/pkg_web/test2/BUILD.bazel b/internal/pkg_web/test2/BUILD.bazel deleted file mode 100644 index d07fcb7..0000000 --- a/internal/pkg_web/test2/BUILD.bazel +++ /dev/null
@@ -1,23 +0,0 @@ -load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web") - - -# Same exts as //internal/pkg_web/test-exports, //internal/pkg_web/test2/rel-exports -EXTS = [ - "js", - "css", -] - -pkg_web( - name = "pkg", - srcs = [ - "index.html", - ] + - # bin + gen + exported files from a different directory - ["//internal/pkg_web/test-exports:bin-" + e for e in EXTS] + - ["//internal/pkg_web/test-exports:gen-" + e for e in EXTS] + - ["//internal/pkg_web/test-exports:file." + e for e in EXTS] + - # bin + gen + exported files from a sub directory - ["//internal/pkg_web/test2/rel-exports:rel-bin-" + e for e in EXTS] + - ["//internal/pkg_web/test2/rel-exports:rel-gen-" + e for e in EXTS] + - ["//internal/pkg_web/test2/rel-exports:rel-file." + e for e in EXTS], -) \ No newline at end of file
diff --git a/internal/pkg_web/test2/index.html b/internal/pkg_web/test2/index.html deleted file mode 100644 index 18ecdcb..0000000 --- a/internal/pkg_web/test2/index.html +++ /dev/null
@@ -1 +0,0 @@ -<html></html>
diff --git a/internal/pkg_web/test2/index_golden.html_ b/internal/pkg_web/test2/index_golden.html_ deleted file mode 100644 index 18ecdcb..0000000 --- a/internal/pkg_web/test2/index_golden.html_ +++ /dev/null
@@ -1 +0,0 @@ -<html></html>
diff --git a/internal/pkg_web/test2/rel-exports/BUILD.bazel b/internal/pkg_web/test2/rel-exports/BUILD.bazel deleted file mode 100644 index dc82bd2..0000000 --- a/internal/pkg_web/test2/rel-exports/BUILD.bazel +++ /dev/null
@@ -1,31 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -EXTS = [ - "js", - "css", -] - -[ - genrule( - name = "rel-bin-" + e, - outs = ["rel-bin." + e], - cmd = "echo '' > $@", - output_to_bindir = True, - ) - for e in EXTS -] - -[ - genrule( - name = "rel-gen-" + e, - outs = ["rel-gen." + e], - cmd = "echo '' > $@", - output_to_bindir = False, - ) - for e in EXTS -] - -exports_files([ - "rel-file.css", - "rel-file.js", -])
diff --git a/internal/pkg_web/test2/rel-exports/rel-file.css b/internal/pkg_web/test2/rel-exports/rel-file.css deleted file mode 100644 index e69de29..0000000 --- a/internal/pkg_web/test2/rel-exports/rel-file.css +++ /dev/null
diff --git a/internal/pkg_web/test2/rel-exports/rel-file.js b/internal/pkg_web/test2/rel-exports/rel-file.js deleted file mode 100644 index e69de29..0000000 --- a/internal/pkg_web/test2/rel-exports/rel-file.js +++ /dev/null
diff --git a/internal/pkg_web/test2/script.js b/internal/pkg_web/test2/script.js deleted file mode 100644 index e2881b8..0000000 --- a/internal/pkg_web/test2/script.js +++ /dev/null
@@ -1,3 +0,0 @@ -const el = document.createElement('div'); -el.innerText = 'Hello, World'; -document.body.appendChild(el);
diff --git a/internal/pkg_web/test2/spec.js b/internal/pkg_web/test2/spec.js deleted file mode 100644 index a86a4e7..0000000 --- a/internal/pkg_web/test2/spec.js +++ /dev/null
@@ -1,14 +0,0 @@ -const fs = require('fs'); -const path = require('path'); -const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']); - -describe('pkg_web paths', () => { - it('should match the golden file', () => { - const output = 'build_bazel_rules_nodejs/internal/pkg_web/test2/pkg/index.html'; - const golden = 'build_bazel_rules_nodejs/internal/pkg_web/test2/index_golden.html_'; - const actual = fs.readFileSync(runfiles.resolve(output), {encoding: 'utf-8'}); - const expected = fs.readFileSync(runfiles.resolve(golden), {encoding: 'utf-8'}); - // make the input hermetic by replacing the cache-buster timestamp - expect(actual.replace(/\?v=\d+/g, '?v=123').trim()).toBe(expected.trim()); - }); -});