Skip to content

Commit

Permalink
Switch to using aspect_rules_ts (#2)
Browse files Browse the repository at this point in the history
* Migrate ts-proto to use aspect_rules_ts.

* Undo change to py_proto_library in thing example

* Start repairing the typescript examples

* Add an empty package.json at the root level -- needed for bzlmod
  • Loading branch information
sacsar authored Apr 28, 2023
1 parent f74497c commit 046f39d
Show file tree
Hide file tree
Showing 24 changed files with 681 additions and 562 deletions.
4 changes: 4 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
docs/
plugin/stephenh/ts-proto/node_modules
rules/ts/node_modules
node_modules
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ __pycache__
docs/vendor
node_modules
vendor/**/BUILD.bazel
bazel-*
.idea
.ijwb
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hoist=false
12 changes: 12 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
load("@npm_ts_proto//:defs.bzl", "npm_link_all_packages")
load("@pnpm__links//:defs.bzl", npm_link_pnpm = "npm_link_imported_package")

# ----------------------------------------------------
# Buildifier
Expand Down Expand Up @@ -64,3 +66,13 @@ filegroup(
] + glob(["vendor/**/*"]),
visibility = ["//visibility:public"],
)

# Link all packages from the /WORKSPACE npm_translate_lock(name = "npm") and also packages from
# manual /WORKSPACE npm_import rules to bazel-bin/node_modules as well as the virtual store
# bazel-bin/node_modules/.aspect_rules_js since /pnpm-lock.yaml is the root of the pnpm workspace
npm_link_all_packages(
name = "node_modules",
imported_links = [
npm_link_pnpm,
],
)
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BAZEL := bzl
BAZEL := bazel

.PHONY: tidy
tidy: deps
Expand Down
43 changes: 43 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,46 @@ rules_closure_dependencies()
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories")

node_repositories()

# ----------------------------------------------------
# Typescript
# ----------------------------------------------------
load("@aspect_rules_ts//ts:repositories.bzl", "rules_ts_dependencies")
load("@aspect_rules_ts//ts:repositories.bzl", "LATEST_VERSION")

rules_ts_dependencies(
# This keeps the TypeScript version in-sync with the editor, which is typically best.
# ts_version_from = "//:package.json",

# Alternatively, you could pick a specific version, or use
ts_version = LATEST_VERSION,
)

# Fetch and register node, if you haven't already
load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "node",
node_version = DEFAULT_NODE_VERSION,
)

load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock")

npm_translate_lock(
name = "npm_ts_proto",
data = [
"//:package.json",
"//:pnpm-workspace.yaml",
"//plugin/stephenh/ts-proto:package.json",
"//rules/ts:package.json",
],
generate_bzl_library_targets = True,
npmrc = "//:.npmrc",
pnpm_lock = "//:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)

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

# Declares npm_import rules from the pnpm-lock.yaml file
npm_repositories()
46 changes: 28 additions & 18 deletions deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ depsgen(
depsgen(
name = "ts_proto",
deps = [
":npm_ts_proto",
":npm_tsc",
":aspect_rules_ts",
],
)

Expand Down Expand Up @@ -386,6 +385,19 @@ proto_dependency(
version = "v1.1.0",
)

proto_dependency(
name = "aspect_rules_ts",
repository_rule = "http_archive",
sha256 = "e81f37c4fe014fc83229e619360d51bfd6cb8ac405a7e8018b4a362efa79d000",
strip_prefix = "rules_ts-1.0.4",
urls = ["https://github.com/aspect-build/rules_ts/archive/refs/tags/v1.0.4.tar.gz"],
deps = [
"aspect_bazel_lib",
"aspect_rules_js",
"rules_nodejs",
],
)

proto_dependency(
name = "build_bazel_rules_nodejs",
repository_rule = "http_archive",
Expand All @@ -395,29 +407,27 @@ proto_dependency(
)

proto_dependency(
name = "rules_nodejs",
name = "aspect_rules_js",
repository_rule = "http_archive",
sha256 = "08337d4fffc78f7fe648a93be12ea2fc4e8eb9795a4e6aa48595b66b34555626",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.0/rules_nodejs-core-5.8.0.tar.gz"],
sha256 = "66ecc9f56300dd63fb86f11cfa1e8affcaa42d5300e2746dba08541916e913fd",
strip_prefix = "rules_js-1.13.0",
urls = ["https://github.com/aspect-build/rules_js/archive/refs/tags/v1.13.0.tar.gz"],
deps = ["aspect_bazel_lib"],
)

proto_dependency(
name = "npm_ts_proto",
package_json = "@build_stack_rules_proto//plugin/stephenh/ts-proto:package.json",
package_lock_json = "@build_stack_rules_proto//plugin/stephenh/ts-proto:package-lock.json",
repository_rule = "npm_install",
symlink_node_modules = False,
deps = ["build_bazel_rules_nodejs"],
name = "aspect_bazel_lib",
repository_rule = "http_archive",
sha256 = "a7bfc7aed7b86a4caaba382116e0214ebbaa623f393a9e716d87a3e1bab29d78",
strip_prefix = "bazel-lib-1.19.0",
urls = ["https://github.com/aspect-build/bazel-lib/archive/refs/tags/v1.19.0.tar.gz"],
)

proto_dependency(
name = "npm_tsc",
frozen_lockfile = True,
package_json = "@build_stack_rules_proto//rules/ts:package.json",
repository_rule = "yarn_install",
symlink_node_modules = False,
yarn_lock = "@build_stack_rules_proto//rules/ts:yarn.lock",
deps = ["build_bazel_rules_nodejs"],
name = "rules_nodejs",
repository_rule = "http_archive",
sha256 = "08337d4fffc78f7fe648a93be12ea2fc4e8eb9795a4e6aa48595b66b34555626",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.0/rules_nodejs-core-5.8.0.tar.gz"],
)

# Commit: 42195b5ca136f78d28819ef486e3a7b02ad45146
Expand Down
54 changes: 29 additions & 25 deletions deps/ts_proto_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ GENERATED FILE - DO NOT EDIT (created via @build_stack_rules_proto//cmd/depsgen)
"""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install", "yarn_install")

def _maybe(repo_rule, name, **kwargs):
if name not in native.existing_rules():
Expand All @@ -12,45 +11,50 @@ def _maybe(repo_rule, name, **kwargs):
def ts_proto_deps():
"""ts_proto dependency macro
"""
rules_nodejs() # via build_bazel_rules_nodejs
build_bazel_rules_nodejs() # via npm_ts_proto
npm_ts_proto() # via <TOP>
npm_tsc() # via <TOP>
aspect_bazel_lib() # via aspect_rules_ts
aspect_rules_js() # via aspect_rules_ts
rules_nodejs() # via aspect_rules_ts
aspect_rules_ts() # via <TOP>

def rules_nodejs():
def aspect_bazel_lib():
_maybe(
http_archive,
name = "rules_nodejs",
sha256 = "08337d4fffc78f7fe648a93be12ea2fc4e8eb9795a4e6aa48595b66b34555626",
name = "aspect_bazel_lib",
sha256 = "a7bfc7aed7b86a4caaba382116e0214ebbaa623f393a9e716d87a3e1bab29d78",
strip_prefix = "bazel-lib-1.19.0",
urls = [
"https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.0/rules_nodejs-core-5.8.0.tar.gz",
"https://github.com/aspect-build/bazel-lib/archive/refs/tags/v1.19.0.tar.gz",
],
)

def build_bazel_rules_nodejs():
def aspect_rules_js():
_maybe(
http_archive,
name = "build_bazel_rules_nodejs",
sha256 = "dcc55f810142b6cf46a44d0180a5a7fb923c04a5061e2e8d8eb05ccccc60864b",
name = "aspect_rules_js",
sha256 = "66ecc9f56300dd63fb86f11cfa1e8affcaa42d5300e2746dba08541916e913fd",
strip_prefix = "rules_js-1.13.0",
urls = [
"https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.0/rules_nodejs-5.8.0.tar.gz",
"https://github.com/aspect-build/rules_js/archive/refs/tags/v1.13.0.tar.gz",
],
)

def npm_ts_proto():
def rules_nodejs():
_maybe(
npm_install,
name = "npm_ts_proto",
package_json = "@build_stack_rules_proto//plugin/stephenh/ts-proto:package.json",
package_lock_json = "@build_stack_rules_proto//plugin/stephenh/ts-proto:package-lock.json",
symlink_node_modules = False,
http_archive,
name = "rules_nodejs",
sha256 = "08337d4fffc78f7fe648a93be12ea2fc4e8eb9795a4e6aa48595b66b34555626",
urls = [
"https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.0/rules_nodejs-core-5.8.0.tar.gz",
],
)

def npm_tsc():
def aspect_rules_ts():
_maybe(
yarn_install,
name = "npm_tsc",
package_json = "@build_stack_rules_proto//rules/ts:package.json",
yarn_lock = "@build_stack_rules_proto//rules/ts:yarn.lock",
frozen_lockfile = True,
http_archive,
name = "aspect_rules_ts",
sha256 = "e81f37c4fe014fc83229e619360d51bfd6cb8ac405a7e8018b4a362efa79d000",
strip_prefix = "rules_ts-1.0.4",
urls = [
"https://github.com/aspect-build/rules_ts/archive/refs/tags/v1.0.4.tar.gz",
],
)
8 changes: 4 additions & 4 deletions example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
# gazelle:proto_plugin ts_proto option esModuleInterop=true
# gazelle:proto_rule proto_ts_library implementation stackb:rules_proto:proto_ts_library
# gazelle:proto_rule proto_ts_library visibility //visibility:public
# gazelle:proto_rule proto_ts_library attr tsc @npm_tsc//typescript/bin:tsc
# gazelle:proto_rule proto_ts_library attr args --lib ES2015
# gazelle:proto_rule proto_ts_library deps @npm_tsc//protobufjs
# gazelle:proto_rule proto_ts_library deps @npm_tsc//long
# gazelle:proto_rule proto_ts_library deps //:node_modules/protobufjs
# gazelle:proto_rule proto_ts_library deps //:node_modules/long
# Set an out_dir so that we don't conflict with the closurejs output
# gazelle:proto_rule proto_ts_library attr out_dir ts

# gazelle:proto_language ts_proto plugin ts_proto
# gazelle:proto_language ts_proto rule proto_compile
Expand Down
7 changes: 3 additions & 4 deletions example/person/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,13 @@ proto_compile(
proto_ts_library(
name = "person_ts_proto",
srcs = ["person.ts"],
args = ["--lib ES2015"],
out_dir = "ts",
tags = ["manual"],
tsc = "@npm_tsc//typescript/bin:tsc",
visibility = ["//visibility:public"],
deps = [
"//:node_modules/long",
"//:node_modules/protobufjs",
"//example/place:place_ts_proto",
"@npm_tsc//long",
"@npm_tsc//protobufjs",
],
)

Expand Down
7 changes: 3 additions & 4 deletions example/place/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,13 @@ proto_compile(
proto_ts_library(
name = "place_ts_proto",
srcs = ["place.ts"],
args = ["--lib ES2015"],
out_dir = "ts",
tags = ["manual"],
tsc = "@npm_tsc//typescript/bin:tsc",
visibility = ["//visibility:public"],
deps = [
"//:node_modules/long",
"//:node_modules/protobufjs",
"//example/thing:thing_ts_proto",
"@npm_tsc//long",
"@npm_tsc//protobufjs",
],
)

Expand Down
7 changes: 3 additions & 4 deletions example/routeguide/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,13 @@ proto_compile(
proto_ts_library(
name = "routeguide_ts_proto",
srcs = ["routeguide.ts"],
args = ["--lib ES2015"],
out_dir = "ts",
tags = ["manual"],
tsc = "@npm_tsc//typescript/bin:tsc",
visibility = ["//visibility:public"],
deps = [
"//:node_modules/long",
"//:node_modules/protobufjs",
"@npm_tsc//@nestjs/microservices",
"@npm_tsc//long",
"@npm_tsc//protobufjs",
],
)

Expand Down
8 changes: 3 additions & 5 deletions example/thing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,11 @@ proto_compile(
proto_ts_library(
name = "thing_ts_proto",
srcs = ["thing.ts"],
args = ["--lib ES2015"],
tags = ["manual"],
tsc = "@npm_tsc//typescript/bin:tsc",
out_dir = "ts",
visibility = ["//visibility:public"],
deps = [
"@npm_tsc//long",
"@npm_tsc//protobufjs",
"//:node_modules/long",
"//:node_modules/protobufjs",
],
)

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
34 changes: 22 additions & 12 deletions pkg/rule/rules_nodejs/proto_ts_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ func (s *protoTsLibrary) Name() string {
func (s *protoTsLibrary) KindInfo() rule.KindInfo {
return rule.KindInfo{
MergeableAttrs: map[string]bool{
"srcs": true,
"tsc": true,
"args": true,
"data": true,
"srcs": true,
"tsc": true,
"args": true,
"data": true,
"tsconfig": true,
"out_dir": true,
},
ResolveAttrs: map[string]bool{
"deps": true,
Expand Down Expand Up @@ -124,19 +126,27 @@ func (s *tsLibrary) Rule(otherGen ...*rule.Rule) *rule.Rule {
newRule.SetAttr("deps", deps)
}

tsc := s.RuleConfig.GetAttr("tsc")
if len(tsc) > 0 {
if len(tsc) > 1 {
log.Printf("warning (%s): found multiple entries for 'tsc', choosing last one: %v", s.Kind(), tsc)
}
newRule.SetAttr("tsc", tsc[len(tsc)-1])
}

args := s.RuleConfig.GetAttr("args")
if len(args) > 0 {
newRule.SetAttr("args", args)
}

tsconfig := s.RuleConfig.GetAttr("tsconfig")
if len(tsconfig) > 0 {
if len(tsconfig) > 1 {
log.Printf("warning (%s) found multiple entries for 'tsconfig', choosing last one: %v", s.Kind(), tsconfig)
}
newRule.SetAttr("tsconfig", tsconfig[len(tsconfig)-1])
}

outdir := s.RuleConfig.GetAttr("out_dir")
if len(outdir) > 0 {
if len(outdir) > 1 {
log.Printf("warning (%s) found multiple entries for 'out_dir', choosing last one: %v", s.Kind(), outdir)
}
newRule.SetAttr("out_dir", outdir[len(outdir)-1])
}

if s.flags.includeProtoLibraryData {
newRule.SetAttr("data", []string{s.Config.Library.Name()})
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/rule/rules_scala/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ go_test(
embed = [":rules_scala"],
deps = [
"//pkg/protoc",
"@bazel_gazelle//config:go_default_library",
"@bazel_gazelle//label:go_default_library",
"@bazel_gazelle//resolve:go_default_library",
"@bazel_gazelle//rule:go_default_library",
"@com_github_google_go_cmp//cmp",
],
)
Expand Down
Loading

0 comments on commit 046f39d

Please sign in to comment.