diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 8cfade513f..95cf18b200 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -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" diff --git a/index.bzl b/index.bzl index ebe37f6ce0..f3c312482d 100644 --- a/index.bzl +++ b/index.bzl @@ -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 diff --git a/internal/node/node_repositories.bzl b/internal/node/node_repositories.bzl index d2df6890d5..f63fe0f63e 100644 --- a/internal/node/node_repositories.bzl +++ b/internal/node/node_repositories.bzl @@ -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 @@ -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}", @@ -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") diff --git a/internal/pkg_npm/npm_script_generator.js b/internal/pkg_npm/npm_script_generator.js index 098e3d49d6..606510d189 100644 --- a/internal/pkg_npm/npm_script_generator.js +++ b/internal/pkg_npm/npm_script_generator.js @@ -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) { diff --git a/internal/pkg_npm/pkg_npm.bzl b/internal/pkg_npm/pkg_npm.bzl index 6eda132386..0e72366cec 100644 --- a/internal/pkg_npm/pkg_npm.bzl +++ b/internal/pkg_npm/pkg_npm.bzl @@ -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, @@ -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 @@ -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 @@ -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", + }), + ) diff --git a/internal/pkg_npm/test/BUILD.bazel b/internal/pkg_npm/test/BUILD.bazel index 8d5c2353a4..a2dfe29e02 100644 --- a/internal/pkg_npm/test/BUILD.bazel +++ b/internal/pkg_npm/test/BUILD.bazel @@ -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"], diff --git a/internal/pkg_npm/test/diff_pack_dir.sh b/internal/pkg_npm/test/diff_pack_dir.sh new file mode 100755 index 0000000000..31bcf6700e --- /dev/null +++ b/internal/pkg_npm/test/diff_pack_dir.sh @@ -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 \ No newline at end of file