Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update to latest rules_nodejs #1207

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# First [install pre-commit](https://pre-commit.com/#installation),
# then run
# pre-commit install
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks

repos:
# Check formatting and lint for starlark code
- repo: https://github.com/keith/pre-commit-buildifier
rev: 6.3.3
hooks:
- id: buildifier
- id: buildifier-lint
39 changes: 28 additions & 11 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,37 @@ load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_depen

buildifier_dependencies()

# We don't use any nodejs but this includes a rule for publishing releases to npm
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "8a7c981217239085f78acc9898a1f7ba99af887c1996ceb3b4504655383a2c3c",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.0.0/rules_nodejs-4.0.0.tar.gz"],
name = "rules_nodejs",
sha256 = "162f4adfd719ba42b8a6f16030a20f434dc110c65dc608660ef7b3411c9873f9",
strip_prefix = "rules_nodejs-6.0.2",
url = "https://github.com/bazelbuild/rules_nodejs/releases/download/v6.0.2/rules_nodejs-v6.0.2.tar.gz",
)

load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "npm_install")
load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

node_repositories(node_version = "16.7.0")
nodejs_register_toolchains(
name = "nodejs",
node_version = "16.7.0",
)

# Fetch third-party dependencies for building npm packages
npm_install(
name = "buildozer_npm_deps",
package_json = "//buildozer/npm:package.json",
package_lock_json = "//buildozer/npm:package-lock.json",
http_archive(
name = "aspect_rules_js",
sha256 = "7ab9776bcca823af361577a1a2ebb9a30d2eb5b94ecc964b8be360f443f714b2",
strip_prefix = "rules_js-1.32.6",
url = "https://github.com/aspect-build/rules_js/releases/download/v1.32.6/rules_js-v1.32.6.tar.gz",
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

rules_js_dependencies()

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

load("@aspect_bazel_lib//lib:repositories.bzl", "register_copy_to_directory_toolchains", "register_jq_toolchains")

register_jq_toolchains()

register_copy_to_directory_toolchains()
21 changes: 11 additions & 10 deletions buildifier/npm/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@aspect_rules_js//js:defs.bzl", "js_test")
load("@aspect_rules_js//npm:defs.bzl", "npm_package", "stamped_package_json")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test", "pkg_npm")

copy_file(
name = "copy_LICENSE",
Expand Down Expand Up @@ -39,22 +40,22 @@ _PARENT_PACKAGE_FILES = [
for s in _PARENT_PACKAGE_FILES
]

pkg_npm(
stamped_package_json(
name = "package",
stamp_var = "BUILD_SCM_VERSION",
)

npm_package(
name = "buildifier",
package_name = "@bazel/buildifier",
srcs = [
"package.json",
],
substitutions = {
"0.0.0-PLACEHOLDER": "{BUILD_SCM_VERSION}",
},
deps = [
"LICENSE",
"buildifier.js",
":package",
] + _PARENT_PACKAGE_FILES,
package = "@bazel/buildifier",
)

nodejs_test(
js_test(
name = "integration_test",
data = [":buildifier"],
entry_point = "test.js",
Expand Down
2 changes: 1 addition & 1 deletion buildifier/npm/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const dir = require('path').join(
process.env['TEST_SRCDIR'],
process.env['BAZEL_WORKSPACE'],
process.env['JS_BINARY__WORKSPACE'],
'buildifier/npm/buildifier');
process.chdir(dir);
const {stdout} = require('child_process').spawnSync(
Expand Down
42 changes: 11 additions & 31 deletions buildozer/npm/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
load("@aspect_rules_js//js:defs.bzl", "js_test")
load("@aspect_rules_js//npm:defs.bzl", "npm_package", "stamped_package_json")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test", "pkg_npm")
load("@buildozer_npm_deps//typescript:index.bzl", "tsc")

copy_file(
name = "copy_LICENSE",
src = "//:LICENSE",
out = "LICENSE",
)

tsc(
name = "compile_ts",
outs = [
"index.d.ts",
"index.js",
],
args = [
"$(execpath index.ts)",
"--outDir $(RULEDIR)",
"--target es2015",
"--module commonjs",
"--declaration",
],
data = [
"index.ts",
"@buildozer_npm_deps//@types/node",
],
)

genrule(
name = "buildozer_launcher",
srcs = ["//:launcher.js"],
Expand Down Expand Up @@ -59,24 +40,23 @@ _PARENT_PACKAGE_FILES = [
for s in _PARENT_PACKAGE_FILES
]

pkg_npm(
stamped_package_json(
name = "package",
stamp_var = "BUILD_SCM_VERSION",
)

npm_package(
name = "buildozer",
package_name = "@bazel/buildozer",
srcs = [
"package.json",
],
substitutions = {
"0.0.0-PLACEHOLDER": "{BUILD_SCM_VERSION}",
},
deps = [
"LICENSE",
"buildozer.js",
"index.d.ts",
"index.js",
":package",
] + _PARENT_PACKAGE_FILES,
package = "@bazel/buildozer",
)

nodejs_test(
js_test(
name = "integration_test",
data = [":buildozer"],
entry_point = "test.js",
Expand Down
30 changes: 8 additions & 22 deletions buildozer/npm/index.ts → buildozer/npm/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
/**
* @fileoverview NodeJS binding to run the buildozer command through a convenient API
*/
import {spawnSync} from 'child_process';
// Use require() here since the file doesn't exist at design-time;
const {spawnSync} = require('node:child_process');
// bazel will copy the /launcher.js in this repo while building the package
const {getNativeBinary} = require('./buildozer');

/**
* Models the data structure of a buildozer command file.
* You should group as many commands as possible into one call to `run` for efficiency.
* https://github.com/bazelbuild/buildtools/tree/master/buildozer#do-multiple-changes-at-once
*/
export interface CommandBatch {
/**
* Each entry is a buildozer edit command like 'new cc_library foo' or a print command
* @see https://github.com/bazelbuild/buildtools/tree/master/buildozer#edit-commands
*/
commands: string[];
/**
* Each entry is like a Bazel label
* @see https://github.com/bazelbuild/buildtools/tree/master/buildozer#targets
*/
targets: string[];
};

/**
* run buildozer with a list of commands
* @see https://github.com/bazelbuild/buildtools/tree/master/buildozer#do-multiple-changes-at-once
* @returns The standard out of the buildozer command, split by lines
*/
export function run(...commands: CommandBatch[]): string[] {
function run(...commands) {
return runWithOptions(commands, {});
}

Expand All @@ -39,7 +20,7 @@ export function run(...commands: CommandBatch[]): string[] {
* @param options Options to pass to spawn
* @param flags any buildozer flags to pass
*/
export function runWithOptions(commands: CommandBatch[], options: {cwd?: string}, flags: string[] = []): string[] {
function runWithOptions(commands, options, flags = []) {
// From https://github.com/bazelbuild/buildtools/tree/master/buildozer#usage:
// Here, label-list is a space-separated list of Bazel labels,
// for example //path/to/pkg1:rule1 //path/to/pkg2:rule2.
Expand All @@ -59,3 +40,8 @@ export function runWithOptions(commands: CommandBatch[], options: {cwd?: string}
console.error(`buildozer exited with status ${status}\n${stderr}`);
throw error;
}

module.exports = {
runWithOptions,
run,
};
20 changes: 0 additions & 20 deletions buildozer/npm/package-lock.json

This file was deleted.

4 changes: 0 additions & 4 deletions buildozer/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,5 @@
},
"bugs": {
"url": "https://github.com/bazelbuild/buildtools/issues"
},
"devDependencies": {
"@types/node": "^13.9.0",
"typescript": "^3.8.3"
}
}
2 changes: 1 addition & 1 deletion buildozer/npm/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const dir = require('path').join(
process.env['TEST_SRCDIR'],
process.env['BAZEL_WORKSPACE'],
process.env['JS_BINARY__WORKSPACE'],
'buildozer/npm/buildozer');
process.chdir(dir);
const {stderr} = require('child_process').spawnSync(
Expand Down