Skip to content

Commit

Permalink
Fix release archive npm_package rule
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored and alexeagle committed Feb 8, 2019
1 parent dacf0d2 commit bd4823a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
6 changes: 6 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ npm_package(
"//internal/web_package:package_contents",
"//tools:package_contents",
],
# Don't replace the default 0.0.0-PLACEHOLDER for this npm_package since
# we are packaging up the packager itself and this replacement will break it
replace_with_version = "",
)

npm_package(
Expand All @@ -48,6 +51,9 @@ npm_package(
# The WORKSPACE file isn't in the distribution, but is required for local_repository
srcs = ["WORKSPACE"],
packages = [":rules_nodejs_package"],
# Don't replace the default 0.0.0-PLACEHOLDER for this npm_package since
# we are packaging up the packager itself and this replacement will break it
replace_with_version = "",
)

# This artifact is published to GitHub along with each release
Expand Down
11 changes: 7 additions & 4 deletions internal/npm_package/npm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def create_package(ctx, deps_sources, nested_packages):
args.add_joined([p.path for p in nested_packages], join_with = ",", omit_if_empty = False)
args.add(ctx.attr.replacements)
args.add_all([ctx.outputs.pack.path, ctx.outputs.publish.path])
args.add(ctx.attr.replace_with_version)
args.add(ctx.version_file.path if ctx.version_file else "")

inputs = ctx.files.srcs + deps_sources + nested_packages + [ctx.file._run_npm_template]
Expand Down Expand Up @@ -115,10 +116,12 @@ NPM_PACKAGE_ATTRS = {
allow_files = True,
),
"replacements": attr.string_dict(
doc = """Key-value pairs which are replaced in all the files while building the package.
Note that the special value 0.0.0-PLACEHOLDER is always replaced with the version stamp data.
See the section on stamping in the README.
""",
doc = """Key-value pairs which are replaced in all the files while building the package.""",
),
"replace_with_version": attr.string(
doc = """If set this value is replaced with the version stamp data.
See the section on stamping in the README.""",
default = "0.0.0-PLACEHOLDER",
),
"deps": attr.label_list(
doc = """Other targets which produce files that should be included in the package, such as `rollup_bundle`""",
Expand Down
36 changes: 19 additions & 17 deletions internal/npm_package/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,32 @@ function main(args) {
args = fs.readFileSync(args[0], {encoding: 'utf-8'}).split('\n').map(unquoteArgs);
const
[outDir, baseDir, srcsArg, binDir, genDir, depsArg, packagesArg, replacementsArg, packPath,
publishPath, stampFile] = args;
publishPath, replaceWithVersion, stampFile] = args;

const replacements = [
// Strip content between BEGIN-INTERNAL / END-INTERNAL comments
[/(#|\/\/)\s+BEGIN-INTERNAL[\w\W]+?END-INTERNAL/g, ''],
];
let version = '0.0.0';
if (stampFile) {
// The stamp file is expected to look like
// BUILD_SCM_HASH 83c699db39cfd74526cdf9bebb75aa6f122908bb
// BUILD_SCM_LOCAL_CHANGES true
// BUILD_SCM_VERSION 6.0.0-beta.6+12.sha-83c699d.with-local-changes
// BUILD_TIMESTAMP 1520021990506
//
// We want version to be the 6.0.0-beta... part
const versionTag = fs.readFileSync(stampFile, {encoding: 'utf-8'})
.split('\n')
.find(s => s.startsWith('BUILD_SCM_VERSION'));
// Don't assume BUILD_SCM_VERSION exists
if (versionTag) {
version = versionTag.split(' ')[1].trim();
if (replaceWithVersion) {
let version = '0.0.0';
if (stampFile) {
// The stamp file is expected to look like
// BUILD_SCM_HASH 83c699db39cfd74526cdf9bebb75aa6f122908bb
// BUILD_SCM_LOCAL_CHANGES true
// BUILD_SCM_VERSION 6.0.0-beta.6+12.sha-83c699d.with-local-changes
// BUILD_TIMESTAMP 1520021990506
//
// We want version to be the 6.0.0-beta... part
const versionTag = fs.readFileSync(stampFile, {encoding: 'utf-8'})
.split('\n')
.find(s => s.startsWith('BUILD_SCM_VERSION'));
// Don't assume BUILD_SCM_VERSION exists
if (versionTag) {
version = versionTag.split(' ')[1].trim();
}
}
replacements.push([new RegExp(replaceWithVersion, 'g'), version]);
}
replacements.push([/0.0.0-PLACEHOLDER/g, version]);
const rawReplacements = JSON.parse(replacementsArg);
for (let key of Object.keys(rawReplacements)) {
replacements.push([new RegExp(key, 'g'), rawReplacements[key]])
Expand Down
3 changes: 3 additions & 0 deletions internal/npm_package/test/npm_package.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ describe('npm_package srcs', () => {
it('copies data dependencies', () => {
expect(read('data.json')).toEqual('[]');
});
it('replaced 0.0.0-PLACEHOLDER', () => {
expect(read('package.json').version).not.toEqual('0.0.0-PLACEHOLDER');
});
});

0 comments on commit bd4823a

Please sign in to comment.