Skip to content

Commit 7e1b7df

Browse files
gregmagolanalexeagle
authored andcommitted
refactor: pkg_npm attributes renames packages=>nested_packages & replacements=>substitutions
BREAKING CHANGE: * pkg_npm attribute packages renamed to nested_packages * pkg_npm attribute replacements renamed to substitutions
1 parent 6d731cf commit 7e1b7df

9 files changed

Lines changed: 34 additions & 34 deletions

File tree

BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pkg_npm(
7272
# Don't replace the default 0.0.0-PLACEHOLDER for this pkg_npm since
7373
# we are packaging up the packager itself and this replacement will break it
7474
replace_with_version = "",
75-
replacements = COMMON_REPLACEMENTS,
75+
substitutions = COMMON_REPLACEMENTS,
7676
deps = [
7777
"//internal:package_contents",
7878
"//internal/bazel_integration_test:package_contents",

e2e/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ e2e_integration_test(
102102
"//packages/typescript:npm_package": "@bazel/typescript",
103103
},
104104
# use these package.json packages instead
105-
package_json_replacements = {
105+
package_json_substitutions = {
106106
"typescript": tsc_version,
107107
},
108108
workspace_root = "typescript",

internal/bazel_integration_test/bazel_integration_test.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = {{
5252
bazelrcImports: {{ {TMPL_bazelrc_imports} }},
5353
npmPackages: {{ {TMPL_npm_packages} }},
5454
checkNpmPackages: [ {TMPL_check_npm_packages} ],
55-
packageJsonRepacements: {{ {TMPL_package_json_replacements} }},
55+
packageJsonRepacements: {{ {TMPL_package_json_substitutions} }},
5656
}};
5757
""".format(
5858
TMPL_workspace_root = ctx.files.workspace_files[0].dirname,
@@ -63,7 +63,7 @@ module.exports = {{
6363
TMPL_bazelrc_imports = ", ".join(["'%s': '%s'" % (ctx.attr.bazelrc_imports[f], _to_manifest_path(ctx, f.files.to_list()[0])) for f in ctx.attr.bazelrc_imports]),
6464
TMPL_npm_packages = ", ".join(["'%s': '%s'" % (ctx.attr.npm_packages[f], _to_manifest_path(ctx, f.files.to_list()[0])) for f in ctx.attr.npm_packages]),
6565
TMPL_check_npm_packages = ", ".join(["'%s'" % s for s in ctx.attr.check_npm_packages]),
66-
TMPL_package_json_replacements = ", ".join(["'%s': '%s'" % (f, ctx.attr.package_json_replacements[f]) for f in ctx.attr.package_json_replacements]),
66+
TMPL_package_json_substitutions = ", ".join(["'%s': '%s'" % (f, ctx.attr.package_json_substitutions[f]) for f in ctx.attr.package_json_substitutions]),
6767
),
6868
)
6969

@@ -194,15 +194,15 @@ npm_packages = {
194194
}
195195
```""",
196196
),
197-
"package_json_replacements": attr.string_dict(
197+
"package_json_substitutions": attr.string_dict(
198198
doc = """A string dictionary of other package.json package replacements to make.
199199
200200
This can be used for integration testing against multiple external npm dependencies without duplicating code. For example,
201201
```
202202
[bazel_integration_test(
203203
name = "e2e_typescript_%s" % tsc_version.replace(".", "_"),
204204
...
205-
package_json_replacements = {
205+
package_json_substitutions = {
206206
"typescript": tsc_version,
207207
},
208208
...

internal/pkg_npm/packager.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ function mkdirp(p) {
2929
}
3030
}
3131

32-
function copyWithReplace(src, dest, replacements, renameBuildFiles) {
32+
function copyWithReplace(src, dest, substitutions, renameBuildFiles) {
3333
mkdirp(path.dirname(dest));
3434
if (!isBinary(src)) {
3535
let content = fs.readFileSync(src, {encoding: 'utf-8'});
36-
replacements.forEach(r => {
36+
substitutions.forEach(r => {
3737
const [regexp, newvalue] = r;
3838
content = content.replace(regexp, newvalue);
3939
});
@@ -59,20 +59,20 @@ function unquoteArgs(s) {
5959
function main(args) {
6060
args = fs.readFileSync(args[0], {encoding: 'utf-8'}).split('\n').map(unquoteArgs);
6161
const
62-
[outDir, baseDir, srcsArg, binDir, genDir, depsArg, packagesArg, replacementsArg, packPath,
62+
[outDir, baseDir, srcsArg, binDir, genDir, depsArg, packagesArg, substitutionsArg, packPath,
6363
publishPath, replaceWithVersion, stampFile, vendorExternalArg, renameBuildFilesArg,
6464
runNpmTemplatePath] = args;
6565
const renameBuildFiles = parseInt(renameBuildFilesArg);
6666

67-
const replacements = [
67+
const substitutions = [
6868
// Strip content between BEGIN-INTERNAL / END-INTERNAL comments
6969
[/(#|\/\/)\s+BEGIN-INTERNAL[\w\W]+?END-INTERNAL/g, ''],
7070
];
71-
const rawReplacements = JSON.parse(replacementsArg);
71+
const rawReplacements = JSON.parse(substitutionsArg);
7272
for (let key of Object.keys(rawReplacements)) {
73-
replacements.push([new RegExp(key, 'g'), rawReplacements[key]])
73+
substitutions.push([new RegExp(key, 'g'), rawReplacements[key]])
7474
}
75-
// Replace version last so that earlier replacements can add
75+
// Replace version last so that earlier substitutions can add
7676
// the version placeholder
7777
if (replaceWithVersion) {
7878
let version = '0.0.0';
@@ -92,7 +92,7 @@ function main(args) {
9292
version = versionTag.split(' ')[1].trim();
9393
}
9494
}
95-
replacements.push([new RegExp(replaceWithVersion, 'g'), version]);
95+
substitutions.push([new RegExp(replaceWithVersion, 'g'), version]);
9696
}
9797

9898
// src like baseDir/my/path is just copied to outDir/my/path
@@ -101,7 +101,7 @@ function main(args) {
101101
if (src.startsWith('external/')) {
102102
// If srcs is from external workspace drop the external/wksp portion
103103
copyWithReplace(
104-
src, path.join(outDir, src.split('/').slice(2).join('/')), replacements,
104+
src, path.join(outDir, src.split('/').slice(2).join('/')), substitutions,
105105
renameBuildFiles);
106106
} else {
107107
// Source is from local workspace
@@ -111,7 +111,7 @@ function main(args) {
111111
`generated file should belong in 'deps' instead.`);
112112
}
113113
copyWithReplace(
114-
src, path.join(outDir, path.relative(baseDir, src)), replacements, renameBuildFiles);
114+
src, path.join(outDir, path.relative(baseDir, src)), substitutions, renameBuildFiles);
115115
}
116116
}
117117

@@ -140,7 +140,7 @@ function main(args) {
140140
// Deps like bazel-bin/baseDir/my/path is copied to outDir/my/path.
141141
for (dep of depsArg.split(',').filter(s => !!s)) {
142142
try {
143-
copyWithReplace(dep, outPath(dep), replacements, renameBuildFiles);
143+
copyWithReplace(dep, outPath(dep), substitutions, renameBuildFiles);
144144
} catch (e) {
145145
console.error(`Failed to copy ${dep} to ${outPath(dep)}`);
146146
throw e;
@@ -168,7 +168,7 @@ function main(args) {
168168
return file;
169169
}
170170
copyWithReplace(
171-
path.join(base, file), path.join(outDir, outFile()), replacements, renameBuildFiles);
171+
path.join(base, file), path.join(outDir, outFile()), substitutions, renameBuildFiles);
172172
}
173173
}
174174
fs.readdirSync(pkg).forEach(f => {

internal/pkg_npm/pkg_npm.bzl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def create_package(ctx, deps_sources, nested_packages):
5959
args.add(ctx.genfiles_dir.path)
6060
args.add_joined(filtered_deps_sources, join_with = ",", omit_if_empty = False)
6161
args.add_joined([p.path for p in nested_packages], join_with = ",", omit_if_empty = False)
62-
args.add(ctx.attr.replacements)
62+
args.add(ctx.attr.substitutions)
6363
args.add_all([ctx.outputs.pack.path, ctx.outputs.publish.path])
6464
args.add(ctx.attr.replace_with_version)
6565
args.add(ctx.version_file.path if stamp else "")
@@ -111,7 +111,7 @@ def _pkg_npm(ctx):
111111
sources = depset(transitive = sources_depsets)
112112

113113
# Note: to_list() should be called once per rule!
114-
package_dir = create_package(ctx, sources.to_list(), ctx.files.packages)
114+
package_dir = create_package(ctx, sources.to_list(), ctx.files.nested_packages)
115115

116116
return [DefaultInfo(
117117
files = depset([package_dir]),
@@ -123,15 +123,15 @@ PKG_NPM_ATTRS = {
123123
doc = """Files inside this directory which are simply copied into the package.""",
124124
allow_files = True,
125125
),
126+
"nested_packages": attr.label_list(
127+
doc = """Other pkg_npm rules whose content is copied into this package.""",
128+
allow_files = True,
129+
),
126130
"node_context_data": attr.label(
127131
default = "@build_bazel_rules_nodejs//internal:node_context_data",
128132
providers = [NodeContextInfo],
129133
doc = "Internal use only",
130134
),
131-
"packages": attr.label_list(
132-
doc = """Other pkg_npm rules whose content is copied into this package.""",
133-
allow_files = True,
134-
),
135135
"rename_build_files": attr.bool(
136136
doc = """If set BUILD and BUILD.bazel files are prefixed with `_` in the npm package.
137137
The default is True since npm packages that contain BUILD files don't work with
@@ -143,7 +143,7 @@ PKG_NPM_ATTRS = {
143143
See the section on stamping in the README.""",
144144
default = "0.0.0-PLACEHOLDER",
145145
),
146-
"replacements": attr.string_dict(
146+
"substitutions": attr.string_dict(
147147
doc = """Key-value pairs which are replaced in all the files while building the package.""",
148148
),
149149
"vendor_external": attr.string_list(
@@ -184,7 +184,7 @@ pkg_npm(
184184
name = "my_package",
185185
srcs = ["package.json"],
186186
deps = [":my_typescript_lib"],
187-
replacements = {"//internal/": "//"},
187+
substitutions = {"//internal/": "//"},
188188
)
189189
```
190190

internal/pkg_npm/test/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ pkg_npm(
3535
"some_file",
3636
"@internal_npm_package_test_vendored_external//:vendored_external_file",
3737
],
38+
nested_packages = [":dependent_pkg"],
3839
node_context_data = ":force_stamp",
39-
packages = [":dependent_pkg"],
40-
replacements = {"replace_me": "replaced"},
40+
substitutions = {"replace_me": "replaced"},
4141
vendor_external = [
4242
"internal_npm_package_test_vendored_external",
4343
],

packages/typescript/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ pkg_npm(
3636
"@npm_bazel_typescript//:package_contents",
3737
],
3838
build_file_content = "",
39-
packages = [
39+
nested_packages = [
4040
"@build_bazel_rules_typescript//:npm_bazel_typescript_package",
4141
],
42-
replacements = TYPESCRIPT_REPLACEMENTS,
42+
substitutions = TYPESCRIPT_REPLACEMENTS,
4343
vendor_external = [
4444
"npm_bazel_typescript",
4545
"build_bazel_rules_typescript",

packages/worker/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pkg_npm(
5151
"README.md",
5252
"package.json",
5353
],
54-
replacements = {
54+
substitutions = {
5555
# Fix the require() statement that loads the worker_protocol.proto file
5656
# we are re-rooting the sources into the @bazel/worker package so it's no longer
5757
# relative to the build_bazel_rules_typescript workspace.

tools/defaults.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def pkg_npm(**kwargs):
4343
"//examples:__pkg__",
4444
])
4545

46-
# Default replacements to scrub things like skylib references
47-
replacements = kwargs.pop("replacements", _COMMON_REPLACEMENTS)
46+
# Default substitutions to scrub things like skylib references
47+
substitutions = kwargs.pop("substitutions", _COMMON_REPLACEMENTS)
4848

4949
# Finally call through to the rule with our defaults set
5050
_pkg_npm(
5151
deps = deps,
52-
replacements = replacements,
52+
substitutions = substitutions,
5353
visibility = visibility,
5454
**kwargs
5555
)

0 commit comments

Comments
 (0)