Skip to content

Commit

Permalink
fix: npm_package.pack should work in windows os
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and Alex Eagle committed Nov 10, 2020
1 parent c89ff38 commit 503d6fb
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ tasks:
build_flags:
# TODO(gregmagolan): figure out how to install missing shared libs
# Without this filter the @cypress external repository will be built and that build will fail due to missing shared libs.
- "--build_tag_filters=-cypress"
- "--build_tag_filters=-cypress,-pkg_npm.pack"
test_flags:
# TODO(gregmagolan): figure out how to install missing shared libs
- "--test_arg=-cypress"
2 changes: 1 addition & 1 deletion index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ load(
load("//internal/node:node_repositories.bzl", _node_repositories = "node_repositories")
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_npm:pkg_npm.bzl", _pkg_npm = "pkg_npm_macro")
load("//internal/pkg_web:pkg_web.bzl", _pkg_web = "pkg_web")

check_bazel_version = _check_bazel_version
Expand Down
9 changes: 9 additions & 0 deletions internal/node/node_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ if %errorlevel% neq 0 exit /b %errorlevel%
script = repository_ctx.path(npm_script),
))

repository_ctx.file("run_npm.bat.template", content = """
"{node}" "{script}" TMPL_args "%*"
""".format(
node = repository_ctx.path(node_entry),
script = repository_ctx.path(npm_script),
))

# The entry points for yarn for osx/linux and windows.
# Runs yarn using appropriate node entry point.
# Unset YARN_IGNORE_PATH before calling yarn incase it is set so that
Expand Down Expand Up @@ -634,6 +641,7 @@ if %errorlevel% neq 0 exit /b %errorlevel%
package(default_visibility = ["//visibility:public"])
exports_files([
"run_npm.sh.template",
"run_npm.bat.template",
"bin/node_repo_args.sh",{node_bin_export}{npm_bin_export}{npx_bin_export}{yarn_bin_export}
"{node_entry}",
"{npm_entry}",
Expand Down Expand Up @@ -699,6 +707,7 @@ def _nodejs_host_os_alias_impl(repository_ctx):
package(default_visibility = ["//visibility:public"])
# aliases for exports_files
alias(name = "run_npm.sh.template", actual = "{node_repository}//:run_npm.sh.template")
alias(name = "run_npm.bat.template", actual = "{node_repository}//:run_npm.bat.template")
alias(name = "bin/node_repo_args.sh", actual = "{node_repository}//:bin/node_repo_args.sh")
# aliases for other aliases
alias(name = "node_bin", actual = "{node_repository}//:node_bin")
Expand Down
12 changes: 10 additions & 2 deletions internal/pkg_npm/npm_script_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@
const fs = require('fs');

function main(args) {
const [outDir, packPath, publishPath, runNpmTemplatePath] = args;
const npmTemplate = fs.readFileSync(runNpmTemplatePath, {encoding: 'utf-8'});
const
[outDir, packPath, publishPath, runNpmTemplatePath, packBatPath, publishBatPath,
runNpmBatTemplatePath] = args;
const cwd = process.cwd();
if (/[\//]sandbox[\//]/.test(cwd)) {
console.error('Error: npm_script_generator must be run with no sandbox');
process.exit(1);
}

const npmTemplate = fs.readFileSync(runNpmTemplatePath, {encoding: 'utf-8'});
fs.writeFileSync(packPath, npmTemplate.replace('TMPL_args', `pack "${cwd}/${outDir}"`));
fs.writeFileSync(publishPath, npmTemplate.replace('TMPL_args', `publish "${cwd}/${outDir}"`));

const npmBatTemplate = fs.readFileSync(runNpmBatTemplatePath, {encoding: 'utf-8'});
fs.writeFileSync(packBatPath, npmBatTemplate.replace('TMPL_args', `pack "${cwd}/${outDir}"`));
fs.writeFileSync(
publishBatPath, npmBatTemplate.replace('TMPL_args', `publish "${cwd}/${outDir}"`));
}

if (require.main === module) {
Expand Down
42 changes: 36 additions & 6 deletions internal/pkg_npm/pkg_npm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ See the section on stamping in the [README](stamping)
cfg = "host",
executable = True,
),
"_run_npm_bat_template": attr.label(
default = Label("@nodejs//:run_npm.bat.template"),
allow_single_file = True,
),
"_run_npm_template": attr.label(
default = Label("@nodejs//:run_npm.sh.template"),
allow_single_file = True,
Expand All @@ -130,8 +134,10 @@ See the section on stamping in the [README](stamping)

# Used in angular/angular /packages/bazel/src/ng_package/ng_package.bzl
PKG_NPM_OUTPUTS = {
"pack": "%{name}.pack",
"publish": "%{name}.publish",
"pack_bat": "%{name}.pack.bat",
"pack_sh": "%{name}.pack.sh",
"publish_bat": "%{name}.publish.bat",
"publish_sh": "%{name}.publish.sh",
}

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

def _create_npm_scripts(ctx, package_dir):
args = ctx.actions.args()

args.add_all([
package_dir.path,
ctx.outputs.pack.path,
ctx.outputs.publish.path,
ctx.outputs.pack_sh.path,
ctx.outputs.publish_sh.path,
ctx.file._run_npm_template.path,
ctx.outputs.pack_bat.path,
ctx.outputs.publish_bat.path,
ctx.file._run_npm_bat_template.path,
])

ctx.actions.run(
progress_message = "Generating npm pack & publish scripts",
mnemonic = "GenerateNpmScripts",
executable = ctx.executable._npm_script_generator,
inputs = [ctx.file._run_npm_template, package_dir],
outputs = [ctx.outputs.pack, ctx.outputs.publish],
inputs = [ctx.file._run_npm_template, ctx.file._run_npm_bat_template, package_dir],
outputs = [ctx.outputs.pack_sh, ctx.outputs.publish_sh, ctx.outputs.pack_bat, ctx.outputs.publish_bat],
arguments = [args],
# Must be run local (no sandbox) so that the pwd is the actual execroot
# in the script which is used to generate the path in the pack & publish
Expand Down Expand Up @@ -307,3 +317,23 @@ pkg_npm = rule(
doc = _DOC,
outputs = PKG_NPM_OUTPUTS,
)

def pkg_npm_macro(name, **kwargs):
pkg_npm(
name = name,
**kwargs
)
native.alias(
name = name + ".pack",
actual = select({
"@bazel_tools//src/conditions:host_windows": name + ".pack.bat",
"//conditions:default": name + ".pack.sh",
}),
)
native.alias(
name = name + ".publish",
actual = select({
"@bazel_tools//src/conditions:host_windows": name + ".publish.bat",
"//conditions:default": name + ".publish.sh",
}),
)
26 changes: 26 additions & 0 deletions internal/pkg_npm/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ pkg_npm(
srcs = [":rollup/bundle/subdirectory"],
)

genrule(
name = "gen_test_pkg_pack",
outs = [
"test-pkg.tgz",
],
cmd = "$(location :test_pkg.pack) && cp test-pkg-1.2.3.tgz $@",
tags = ["pkg_npm.pack"],
tools = [
":test_pkg.pack",
],
)

sh_test(
name = "test_pkg_pack",
srcs = ["diff_pack_dir.sh"],
args = [
"$(location :gen_test_pkg_pack)",
"$(location :test_pkg)",
],
data = [
":gen_test_pkg_pack",
":test_pkg",
],
tags = ["pkg_npm.pack"],
)

jasmine_node_test(
name = "test",
srcs = ["pkg_npm.spec.js"],
Expand Down
25 changes: 25 additions & 0 deletions internal/pkg_npm/test/diff_pack_dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
archived=$1
if [ -f "$archived" ]; then
echo "RUNFILES are supported in this OS"
# run files are supported (not windows os), we can read the
# input from arguments directly
rm -rf ./pack && mkdir ./pack
tar -xvzf $1 -C ./pack
diff -r ./pack/package $2
if [ $? -ne 0 ]; then
echo "The directory was modified";
exit 1
fi
else
# runfiles are not supported in Windows,
# hard coding the test input for now
rm -rf ./pack && mkdir ./pack
tar -xvzf ../../test-pkg.tgz -C ./pack
diff ./pack/package ../../test_pkg
if [ $? -ne 0 ]; then
echo "The directory was modified";
exit 1
fi
fi

exit 0

0 comments on commit 503d6fb

Please sign in to comment.