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

feat!: convert to ESM, use Aspect build rules #1307

Merged
merged 9 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 0 additions & 9 deletions .eslintignore

This file was deleted.

3 changes: 0 additions & 3 deletions .eslintrc.json

This file was deleted.

4 changes: 0 additions & 4 deletions .github/sync-repo-settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ branchProtectionRules:
- lib-tests (12.x, showcase)
- lib-tests (12.x, texttospeech)
- lib-tests (12.x, translate)
- showcase-test-application (12.x, js)
- showcase-test-application (12.x, ts)
- showcase-test-application (14.x, js)
- showcase-test-application (14.x, ts)
# List of explicit permissions to add (additive only)
permissionRules:
# Team slug to add to repository permissions
Expand Down
46 changes: 4 additions & 42 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,11 @@ jobs:
chmod +x bazel-bin/protoc_plugin.sh
./gapic-error-conformance -plugin="bazel-bin/protoc_plugin.sh"

- name: Run linting
run: bazel run @nodejs//:npm -- run lint

- name: Prepare baseline artifacts
run: |
mkdir -p ~/artifacts/test-application-runners
tar cfz ~/artifacts/test-protos.tar.gz -C test-fixtures protos
cp bazel-bin/typescript/test/test-application/test-*.js ~/artifacts/test-application-runners/
mkdir -p ~/artifacts
cp bazel-testlogs/unit_tests/test.outputs/outputs.zip ~/artifacts/
bazel run -- @pnpm//:pnpm --dir $PWD install
tar cfz ~/artifacts/node_modules.tar.gz node_modules

- name: Save artifacts
Expand All @@ -72,42 +68,6 @@ jobs:
name: artifacts
path: ~/artifacts

showcase-test-application:
needs: build
runs-on: ubuntu-latest
strategy:
matrix:
# Test on the oldest and the newest supported version just in case
node-version: [12.x, 14.x, 16.x]
test: [js, ts]

steps:
- uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: artifacts
path: ~/artifacts

- name: Prepare test applications
run: |
rm -f bazel-testlogs
mkdir -p bazel-testlogs/unit_tests/test.outputs
mv ~/artifacts/outputs.zip bazel-testlogs/unit_tests/test.outputs/
tar xzf ~/artifacts/node_modules.tar.gz
mv ~/artifacts/test-application-runners/test-${{ matrix.test }}.js .

- name: Run ${{ matrix.test }} test application
run: npx mocha test-${{ matrix.test }}.js
env:
NPM_CONFIG_PREFIX: /tmp/.npm-global

lib-tests:
needs: build
runs-on: ubuntu-latest
Expand Down Expand Up @@ -139,6 +99,8 @@ jobs:
npm install
npm test
npm run fix
# Second compile to make sure "gts fix" did not break the code, it happens!
rm -rf build
npm run compile
npm run system-test
npm run docs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ docker/gitlog.txt
.test-application-js
.test-application-ts
bazel-*
.aspect
6 changes: 0 additions & 6 deletions .prettierignore

This file was deleted.

17 changes: 0 additions & 17 deletions .prettierrc.js

This file was deleted.

116 changes: 65 additions & 51 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package(default_visibility = ["//visibility:public"])
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_library", "js_test")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

exports_files(["tsconfig.json"], visibility = ["//visibility:public"])
package(default_visibility = ["//visibility:public"])

load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "nodejs_test", "copy_to_bin")
load("@npm//@bazel/typescript:index.bzl", "ts_project", "ts_config")
npm_link_all_packages(name = "node_modules")

filegroup(
name = "templates",
Expand All @@ -26,95 +27,108 @@ filegroup(
)

npm_runtime_dependencies = [
"@npm//fs-extra",
"@npm//google-gax",
"@npm//js-yaml",
"@npm//long",
"@npm//nunjucks",
"@npm//object-hash",
"@npm//protobufjs",
"@npm//yargs",
"//:node_modules/google-gax",
"//:node_modules/js-yaml",
"//:node_modules/long",
"//:node_modules/nunjucks",
"//:node_modules/object-hash",
"//:node_modules/proto3-json-serializer",
"//:node_modules/protobufjs",
"//:node_modules/yargs",
]

npm_test_dependencies = npm_runtime_dependencies + [
"@npm//espower-typescript",
"@npm//sinon",
"@npm//mocha",
"//:node_modules/espower-typescript",
"//:node_modules/mocha",
]

npm_compile_dependencies = npm_test_dependencies + [
"@npm//@types/js-yaml",
"@npm//@types/fs-extra",
"@npm//@types/long",
"@npm//@types/mocha",
"@npm//@types/module-alias",
"@npm//@types/node",
"@npm//@types/nunjucks",
"@npm//@types/object-hash",
"@npm//@types/sinon",
"@npm//@types/yargs",
"//:node_modules/@types/js-yaml",
"//:node_modules/@types/fs-extra",
"//:node_modules/@types/mocha",
"//:node_modules/@types/module-alias",
"//:node_modules/@types/node",
"//:node_modules/@types/nunjucks",
"//:node_modules/@types/object-hash",
"//:node_modules/@types/yargs",
]

ts_project(
name = "compile",
srcs = glob([
"typescript/**/*.ts",
]) + [
"index.d.ts",
"protos/index.d.ts",
]),
"protos/protos.json",
],
tsconfig = "//:tsconfig.json",
source_map = True,
declaration = True,
source_map = False,
declaration = False,
allow_js = True,
validate = False,
deps = npm_compile_dependencies,
resolve_json_module = True,
args = [
"--module",
"esnext",
"--moduleResolution",
"node",
],
supports_workers = False,
)

copy_to_bin(
name = "copy_deps",
js_library(
name = "library",
srcs = [
":protos",
":templates",
"package.json",
":compile",
],
)

nodejs_binary(
js_binary(
name = "protoc_plugin",
entry_point = "//:typescript/src/protoc-plugin.ts",
link_workspace_root = True,
entry_point = "typescript/src/protoc-plugin.js",
data = [
":compile",
":library",
":templates",
":protos",
] + npm_runtime_dependencies,
templated_args = ["--bazel_patch_module_resolver"],
],
env = {
"BAZEL_BINDIR": ".", # required by the JS rules if we need to use them in the build process
"NODE_OPTIONS": "--no-warnings", # disable ExperimentalWarning about loading JSON modules
},
)

nodejs_binary(
js_binary(
name = "gapic_generator_typescript",
entry_point = "//:typescript/src/gapic-generator-typescript.ts",
link_workspace_root = True,
entry_point = "typescript/src/gapic-generator-typescript.js",
data = [
":library",
":protoc_plugin",
":compile",
":templates",
"@com_google_protobuf//:protoc",
":protos",
] + npm_runtime_dependencies,
templated_args = [
"--bazel_patch_module_resolver",
"--protoc=\"$(rootpath @com_google_protobuf//:protoc)\"",
"@com_google_protobuf//:protoc",
],
env = {
"PROTOC_PATH": "$(rootpath @com_google_protobuf//:protoc)",
"NODE_OPTIONS": "--no-warnings", # disable ExperimentalWarning about loading JSON modules
},
)

nodejs_test(
js_test(
name = "unit_tests",
entry_point = "@npm//:node_modules/mocha/bin/mocha.js",
args = ["typescript/test/unit/"],
entry_point = "typescript/test/unit-test-runner.js",
data = [
"package.json",
":gapic_generator_typescript",
":compile",
":test_fixtures",
":baselines",
"@com_google_protobuf//:protoc",
] + npm_test_dependencies,
templated_args = ["--bazel_patch_module_resolver"],
env = {
"PROTOC_PATH": "$(rootpath @com_google_protobuf//:protoc)",
"NODE_OPTIONS": "--no-warnings", # disable ExperimentalWarning about loading JSON modules
},
)
39 changes: 29 additions & 10 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
# Declares that this directory is the root of a Bazel workspace.
# See https://docs.bazel.build/versions/master/build-ref.html#workspace
workspace(
# How this workspace would be referenced with absolute labels from another workspace
name = "gapic_generator_typescript",
# Map the @npm bazel workspace to the node_modules directory.
# This lets Bazel use the same node_modules as other local tooling.
managed_directories = {"@npm": ["node_modules"]},
)

load("//:repositories.bzl", "gapic_generator_typescript_repositories")
gapic_generator_typescript_repositories()

Expand All @@ -18,10 +13,34 @@ load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_
rules_proto_dependencies()
rules_proto_toolchains()

load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install")
load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")
rules_js_dependencies()

load("@aspect_rules_ts//ts:repositories.bzl", "rules_ts_dependencies")
rules_ts_dependencies(
ts_version_from = "//:package.json",
)

yarn_install(
# Fetch and register node, if you haven't already
load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")
nodejs_register_toolchains(
name = "nodejs",
# This is the version of Node.js that would run the generator, it's unrelated to the versions supported by the generated libraries
node_version = "18.12.1", # https://github.com/bazelbuild/rules_nodejs/blob/stable/nodejs/private/node_versions.bzl
alexander-fenster marked this conversation as resolved.
Show resolved Hide resolved
)

load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock", "pnpm_repository")
npm_translate_lock(
name = "npm",
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
pnpm_lock = "//:pnpm-lock.yaml",
update_pnpm_lock = True,
data = ["@//:package.json"],
)

load("@npm//:repositories.bzl", "npm_repositories")
npm_repositories()

pnpm_repository(name = "pnpm")
# To regenerate the lock file, run:
# bazel run -- @pnpm//:pnpm --dir $PWD install --lockfile-only
# More information: https://github.com/aspect-build/rules_js/blob/main/docs/faq.md#can-i-use-bazel-managed-pnpm
4 changes: 2 additions & 2 deletions baselines/asset/.jsdoc.js.baseline
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = {
includePattern: '\\.js$'
},
templates: {
copyright: 'Copyright 2022 Google LLC',
copyright: 'Copyright 2023 Google LLC',
includeDate: false,
sourceFiles: false,
systemName: '@google-cloud/asset',
Expand Down
2 changes: 1 addition & 1 deletion baselines/asset/.mocharc.js.baseline
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion baselines/asset/.prettierrc.js.baseline
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Loading