Skip to content

Commit bc36519

Browse files
JiaLiPassionalexeagle
authored andcommitted
fix: npm_package.pack should work in windows os
1 parent 51de4e0 commit bc36519

File tree

7 files changed

+108
-10
lines changed

7 files changed

+108
-10
lines changed

.bazelci/presubmit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ tasks:
462462
build_flags:
463463
# TODO(gregmagolan): figure out how to install missing shared libs
464464
# Without this filter the @cypress external repository will be built and that build will fail due to missing shared libs.
465-
- "--build_tag_filters=-cypress"
465+
- "--build_tag_filters=-cypress,-pkg_npm.pack"
466466
test_flags:
467467
# TODO(gregmagolan): figure out how to install missing shared libs
468468
- "--test_arg=-cypress"

index.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ load(
3232
load("//internal/node:node_repositories.bzl", _node_repositories = "node_repositories")
3333
load("//internal/node:npm_package_bin.bzl", _npm_bin = "npm_package_bin")
3434
load("//internal/npm_install:npm_install.bzl", _npm_install = "npm_install", _yarn_install = "yarn_install")
35-
load("//internal/pkg_npm:pkg_npm.bzl", _pkg_npm = "pkg_npm")
35+
load("//internal/pkg_npm:pkg_npm.bzl", _pkg_npm = "pkg_npm_macro")
3636
load("//internal/pkg_web:pkg_web.bzl", _pkg_web = "pkg_web")
3737

3838
check_bazel_version = _check_bazel_version

internal/node/node_repositories.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,13 @@ if %errorlevel% neq 0 exit /b %errorlevel%
552552
script = repository_ctx.path(npm_script),
553553
))
554554

555+
repository_ctx.file("run_npm.bat.template", content = """
556+
"{node}" "{script}" TMPL_args "%*"
557+
""".format(
558+
node = repository_ctx.path(node_entry),
559+
script = repository_ctx.path(npm_script),
560+
))
561+
555562
# The entry points for yarn for osx/linux and windows.
556563
# Runs yarn using appropriate node entry point.
557564
# Unset YARN_IGNORE_PATH before calling yarn incase it is set so that
@@ -634,6 +641,7 @@ if %errorlevel% neq 0 exit /b %errorlevel%
634641
package(default_visibility = ["//visibility:public"])
635642
exports_files([
636643
"run_npm.sh.template",
644+
"run_npm.bat.template",
637645
"bin/node_repo_args.sh",{node_bin_export}{npm_bin_export}{npx_bin_export}{yarn_bin_export}
638646
"{node_entry}",
639647
"{npm_entry}",
@@ -699,6 +707,7 @@ def _nodejs_host_os_alias_impl(repository_ctx):
699707
package(default_visibility = ["//visibility:public"])
700708
# aliases for exports_files
701709
alias(name = "run_npm.sh.template", actual = "{node_repository}//:run_npm.sh.template")
710+
alias(name = "run_npm.bat.template", actual = "{node_repository}//:run_npm.bat.template")
702711
alias(name = "bin/node_repo_args.sh", actual = "{node_repository}//:bin/node_repo_args.sh")
703712
# aliases for other aliases
704713
alias(name = "node_bin", actual = "{node_repository}//:node_bin")

internal/pkg_npm/npm_script_generator.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@
2323
const fs = require('fs');
2424

2525
function main(args) {
26-
const [outDir, packPath, publishPath, runNpmTemplatePath] = args;
27-
const npmTemplate = fs.readFileSync(runNpmTemplatePath, {encoding: 'utf-8'});
26+
const
27+
[outDir, packPath, publishPath, runNpmTemplatePath, packBatPath, publishBatPath,
28+
runNpmBatTemplatePath] = args;
2829
const cwd = process.cwd();
2930
if (/[\//]sandbox[\//]/.test(cwd)) {
3031
console.error('Error: npm_script_generator must be run with no sandbox');
3132
process.exit(1);
3233
}
34+
35+
const npmTemplate = fs.readFileSync(runNpmTemplatePath, {encoding: 'utf-8'});
3336
fs.writeFileSync(packPath, npmTemplate.replace('TMPL_args', `pack "${cwd}/${outDir}"`));
3437
fs.writeFileSync(publishPath, npmTemplate.replace('TMPL_args', `publish "${cwd}/${outDir}"`));
38+
39+
const npmBatTemplate = fs.readFileSync(runNpmBatTemplatePath, {encoding: 'utf-8'});
40+
fs.writeFileSync(packBatPath, npmBatTemplate.replace('TMPL_args', `pack "${cwd}/${outDir}"`));
41+
fs.writeFileSync(
42+
publishBatPath, npmBatTemplate.replace('TMPL_args', `publish "${cwd}/${outDir}"`));
3543
}
3644

3745
if (require.main === module) {

internal/pkg_npm/pkg_npm.bzl

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ See the section on stamping in the [README](stamping)
122122
cfg = "host",
123123
executable = True,
124124
),
125+
"_run_npm_bat_template": attr.label(
126+
default = Label("@nodejs//:run_npm.bat.template"),
127+
allow_single_file = True,
128+
),
125129
"_run_npm_template": attr.label(
126130
default = Label("@nodejs//:run_npm.sh.template"),
127131
allow_single_file = True,
@@ -130,8 +134,10 @@ See the section on stamping in the [README](stamping)
130134

131135
# Used in angular/angular /packages/bazel/src/ng_package/ng_package.bzl
132136
PKG_NPM_OUTPUTS = {
133-
"pack": "%{name}.pack",
134-
"publish": "%{name}.publish",
137+
"pack_bat": "%{name}.pack.bat",
138+
"pack_sh": "%{name}.pack.sh",
139+
"publish_bat": "%{name}.publish.bat",
140+
"publish_sh": "%{name}.publish.sh",
135141
}
136142

137143
# Takes a depset of files and returns a corresponding list of file paths without any files
@@ -240,19 +246,23 @@ def create_package(ctx, deps_files, nested_packages):
240246

241247
def _create_npm_scripts(ctx, package_dir):
242248
args = ctx.actions.args()
249+
243250
args.add_all([
244251
package_dir.path,
245-
ctx.outputs.pack.path,
246-
ctx.outputs.publish.path,
252+
ctx.outputs.pack_sh.path,
253+
ctx.outputs.publish_sh.path,
247254
ctx.file._run_npm_template.path,
255+
ctx.outputs.pack_bat.path,
256+
ctx.outputs.publish_bat.path,
257+
ctx.file._run_npm_bat_template.path,
248258
])
249259

250260
ctx.actions.run(
251261
progress_message = "Generating npm pack & publish scripts",
252262
mnemonic = "GenerateNpmScripts",
253263
executable = ctx.executable._npm_script_generator,
254-
inputs = [ctx.file._run_npm_template, package_dir],
255-
outputs = [ctx.outputs.pack, ctx.outputs.publish],
264+
inputs = [ctx.file._run_npm_template, ctx.file._run_npm_bat_template, package_dir],
265+
outputs = [ctx.outputs.pack_sh, ctx.outputs.publish_sh, ctx.outputs.pack_bat, ctx.outputs.publish_bat],
256266
arguments = [args],
257267
# Must be run local (no sandbox) so that the pwd is the actual execroot
258268
# in the script which is used to generate the path in the pack & publish
@@ -307,3 +317,23 @@ pkg_npm = rule(
307317
doc = _DOC,
308318
outputs = PKG_NPM_OUTPUTS,
309319
)
320+
321+
def pkg_npm_macro(name, **kwargs):
322+
pkg_npm(
323+
name = name,
324+
**kwargs
325+
)
326+
native.alias(
327+
name = name + ".pack",
328+
actual = select({
329+
"@bazel_tools//src/conditions:host_windows": name + ".pack.bat",
330+
"//conditions:default": name + ".pack.sh",
331+
}),
332+
)
333+
native.alias(
334+
name = name + ".publish",
335+
actual = select({
336+
"@bazel_tools//src/conditions:host_windows": name + ".publish.bat",
337+
"//conditions:default": name + ".publish.sh",
338+
}),
339+
)

internal/pkg_npm/test/BUILD.bazel

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@ pkg_npm(
7676
srcs = [":rollup/bundle/subdirectory"],
7777
)
7878

79+
genrule(
80+
name = "gen_test_pkg_pack",
81+
outs = [
82+
"test-pkg.tgz",
83+
],
84+
cmd = "$(location :test_pkg.pack) && cp test-pkg-1.2.3.tgz $@",
85+
tags = ["pkg_npm.pack"],
86+
tools = [
87+
":test_pkg.pack",
88+
],
89+
)
90+
91+
sh_test(
92+
name = "test_pkg_pack",
93+
srcs = ["diff_pack_dir.sh"],
94+
args = [
95+
"$(location :gen_test_pkg_pack)",
96+
"$(location :test_pkg)",
97+
],
98+
data = [
99+
":gen_test_pkg_pack",
100+
":test_pkg",
101+
],
102+
tags = ["pkg_npm.pack"],
103+
)
104+
79105
jasmine_node_test(
80106
name = "test",
81107
srcs = ["pkg_npm.spec.js"],
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
archived=$1
2+
if [ -f "$archived" ]; then
3+
echo "RUNFILES are supported in this OS"
4+
# run files are supported (not windows os), we can read the
5+
# input from arguments directly
6+
rm -rf ./pack && mkdir ./pack
7+
tar -xvzf $1 -C ./pack
8+
diff -r ./pack/package $2
9+
if [ $? -ne 0 ]; then
10+
echo "The directory was modified";
11+
exit 1
12+
fi
13+
else
14+
# runfiles are not supported in Windows,
15+
# hard coding the test input for now
16+
rm -rf ./pack && mkdir ./pack
17+
tar -xvzf ../../test-pkg.tgz -C ./pack
18+
diff ./pack/package ../../test_pkg
19+
if [ $? -ne 0 ]; then
20+
echo "The directory was modified";
21+
exit 1
22+
fi
23+
fi
24+
25+
exit 0

0 commit comments

Comments
 (0)