Skip to content

Commit

Permalink
build: compile js protos with protoc-gen-es
Browse files Browse the repository at this point in the history
This change introduces a new JS package that compiles
JS protobuf client code with help of `protoc` compiler and `protoc-gen-es` plugin.
It is supposed that this package will be consumed by CC Console.

Release note: None
  • Loading branch information
koorosh committed Mar 6, 2024
1 parent ce5f34e commit d2bd0ec
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 61 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ pkg/ui/workspaces/db-console/src/js/node_modules
pkg/ui/workspaces/db-console/ccl/src/js/node_modules
pkg/ui/workspaces/e2e-tests/node_modules
pkg/ui/workspaces/eslint-plugin-crdb/node_modules
pkg/ui/workspaces/crdb-api-client/node_modules
tmp
vendor
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ exports_files([
# gazelle:exclude pkg/ui/workspaces/db-console/src/js/node_modules
# gazelle:exclude pkg/ui/workspaces/e2e-tests/node_modules
# gazelle:exclude pkg/ui/workspaces/eslint-plugin-crdb/node_modules
# gazelle:exclude pkg/ui/workspaces/crdb-api-client/node_modules
# gazelle:exclude vendor
# gazelle:exclude .vendor.tmp.*
# gazelle:exclude **/zcgo_flags.go
Expand Down
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ npm_translate_lock(
"//pkg/ui:pnpm-workspace.yaml",
"//pkg/ui/patches:[email protected]",
"//pkg/ui/workspaces/cluster-ui:package.json",
"//pkg/ui/workspaces/crdb-api-client:package.json",
"//pkg/ui/workspaces/db-console:package.json",
"//pkg/ui/workspaces/db-console/src/js:package.json",
"//pkg/ui/workspaces/e2e-tests:package.json",
Expand Down
62 changes: 3 additions & 59 deletions docs/generated/http/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,64 +1,8 @@
load(":defs.bzl", "PROTOBUF_TARGETS")

genrule(
name = "http",
srcs = [
"//pkg/build:build_proto",
"//pkg/clusterversion:clusterversion_proto",
"//pkg/config/zonepb:zonepb_proto",
"//pkg/geo/geopb:geopb_proto",
"//pkg/gossip:gossip_proto",
"//pkg/jobs/jobspb:jobspb_proto",
"//pkg/kv/kvpb:kvpb_proto",
"//pkg/kv/kvserver/concurrency/isolation:isolation_proto",
"//pkg/kv/kvserver/concurrency/lock:lock_proto",
"//pkg/kv/kvserver/kvserverpb:kvserverpb_proto",
"//pkg/kv/kvserver/liveness/livenesspb:livenesspb_proto",
"//pkg/kv/kvserver/loqrecovery/loqrecoverypb:loqrecoverypb_proto",
"//pkg/kv/kvserver/readsummary/rspb:rspb_proto",
"//pkg/multitenant/mtinfopb:mtinfopb_proto",
"//pkg/multitenant/tenantcapabilities/tenantcapabilitiespb:tenantcapabilitiespb_proto",
"//pkg/roachpb:roachpb_proto",
"//pkg/rpc/rpcpb:rpcpb_proto",
"//pkg/server/diagnostics/diagnosticspb:diagnosticspb_proto",
"//pkg/server/serverpb:serverpb_proto",
"//pkg/server/status/statuspb:statuspb_proto",
"//pkg/settings:settings_proto",
"//pkg/sql/appstatspb:appstatspb_proto",
"//pkg/sql/catalog/catenumpb:catenumpb_proto",
"//pkg/sql/catalog/catpb:catpb_proto",
"//pkg/sql/catalog/descpb:descpb_proto",
"//pkg/sql/catalog/fetchpb:fetchpb_proto",
"//pkg/sql/contentionpb:contentionpb_proto",
"//pkg/sql/execinfrapb:execinfrapb_proto",
"//pkg/sql/sem/semenumpb:semenumpb_proto",
"//pkg/sql/inverted:inverted_proto",
"//pkg/sql/lex:lex_proto",
"//pkg/sql/pgwire/pgerror:pgerror_proto",
"//pkg/sql/schemachanger/scpb:scpb_proto",
"//pkg/sql/sessiondatapb:sessiondatapb_proto",
"//pkg/sql/sqlstats/insights:insights_proto",
"//pkg/sql/types:types_proto",
"//pkg/storage/enginepb:enginepb_proto",
"//pkg/ts/catalog:catalog_proto",
"//pkg/ts/tspb:tspb_proto",
"//pkg/util/duration:duration_proto",
"//pkg/util/hlc:hlc_proto",
"//pkg/util/admission/admissionpb:admissionpb_proto",
"//pkg/util/log/logpb:logpb_proto",
"//pkg/util/metric:metric_proto",
"//pkg/util/timeutil/pgdate:pgdate_proto",
"//pkg/util/tracing/tracingpb:tracingpb_proto",
"//pkg/util:util_proto",
"@com_github_prometheus_client_model//io/prometheus/client:io_prometheus_client_proto",
"@com_github_cockroachdb_errors//errorspb:errorspb_proto",
"@com_github_gogo_protobuf//gogoproto:gogo_proto",
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:timestamp_proto",
"@go_googleapis//google/api:annotations_proto",
"@go_googleapis//google/api:http_proto",
"@io_etcd_go_raft_v3//raftpb:raftpb_proto",
],
srcs = PROTOBUF_TARGETS,
outs = [
"full.md",
"health-other.md",
Expand Down
62 changes: 62 additions & 0 deletions docs/generated/http/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# PROTOBUF_TARGETS lists the indirect dependencies needed to compile proto dependencies
# of //pkg/server/serverpb:serverpb_proto target
PROTOBUF_TARGETS = [
"//pkg/build:build_proto",
"//pkg/clusterversion:clusterversion_proto",
"//pkg/config/zonepb:zonepb_proto",
"//pkg/geo/geopb:geopb_proto",
"//pkg/gossip:gossip_proto",
"//pkg/jobs/jobspb:jobspb_proto",
"//pkg/kv/kvpb:kvpb_proto",
"//pkg/kv/kvserver/concurrency/isolation:isolation_proto",
"//pkg/kv/kvserver/concurrency/lock:lock_proto",
"//pkg/kv/kvserver/kvserverpb:kvserverpb_proto",
"//pkg/kv/kvserver/liveness/livenesspb:livenesspb_proto",
"//pkg/kv/kvserver/loqrecovery/loqrecoverypb:loqrecoverypb_proto",
"//pkg/kv/kvserver/readsummary/rspb:rspb_proto",
"//pkg/kv/kvserver/kvflowcontrol/kvflowcontrolpb:kvflowcontrolpb_proto",
"//pkg/multitenant/mtinfopb:mtinfopb_proto",
"//pkg/multitenant/tenantcapabilities/tenantcapabilitiespb:tenantcapabilitiespb_proto",
"//pkg/roachpb:roachpb_proto",
"//pkg/rpc/rpcpb:rpcpb_proto",
"//pkg/server/diagnostics/diagnosticspb:diagnosticspb_proto",
"//pkg/server/serverpb:serverpb_proto",
"//pkg/server/status/statuspb:statuspb_proto",
"//pkg/settings:settings_proto",
"//pkg/sql/appstatspb:appstatspb_proto",
"//pkg/sql/catalog/catenumpb:catenumpb_proto",
"//pkg/sql/catalog/catpb:catpb_proto",
"//pkg/sql/catalog/descpb:descpb_proto",
"//pkg/sql/catalog/fetchpb:fetchpb_proto",
"//pkg/sql/contentionpb:contentionpb_proto",
"//pkg/sql/execinfrapb:execinfrapb_proto",
"//pkg/sql/sem/semenumpb:semenumpb_proto",
"//pkg/sql/inverted:inverted_proto",
"//pkg/sql/lex:lex_proto",
"//pkg/sql/pgwire/pgerror:pgerror_proto",
"//pkg/sql/schemachanger/scpb:scpb_proto",
"//pkg/sql/sessiondatapb:sessiondatapb_proto",
"//pkg/sql/sqlstats/insights:insights_proto",
"//pkg/sql/types:types_proto",
"//pkg/storage/enginepb:enginepb_proto",
"//pkg/ts/catalog:catalog_proto",
"//pkg/ts/tspb:tspb_proto",
"//pkg/util/duration:duration_proto",
"//pkg/util/hlc:hlc_proto",
"//pkg/util/admission/admissionpb:admissionpb_proto",
"//pkg/util/log/logpb:logpb_proto",
"//pkg/util/metric:metric_proto",
"//pkg/util/timeutil/pgdate:pgdate_proto",
"//pkg/util/tracing/tracingpb:tracingpb_proto",
"//pkg/util:util_proto",
"@com_github_prometheus_client_model//io/prometheus/client:io_prometheus_client_proto",
"@com_github_cockroachdb_errors//errorspb:errorspb_proto",
"@com_github_gogo_protobuf//gogoproto:gogo_proto",
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:timestamp_proto",
"@go_googleapis//google/api:annotations_proto",
"@go_googleapis//google/api:http_proto",
"@io_etcd_go_raft_v3//raftpb:raftpb_proto",
]
56 changes: 56 additions & 0 deletions pkg/ui/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/ui/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ packages:
- "./workspaces/cluster-ui"
- "./workspaces/db-console"
- "./workspaces/e2e-tests"
- "./workspaces/crdb-api-client"
88 changes: 88 additions & 0 deletions pkg/ui/workspaces/crdb-api-client/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary", "js_test")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//docs/generated/http:defs.bzl", "PROTOBUF_TARGETS")
load("//pkg/ui/workspaces/db-console/src/js:defs.bzl", "proto_sources")

npm_link_all_packages(name = "node_modules")

genrule(
name = "protos",
srcs = [
":node_modules/@bufbuild/protoc-gen-es/bin/protoc-gen-es",
":proto_sources",
] + PROTOBUF_TARGETS,
outs = ["dist"],
cmd = """
mkdir -p $(OUTS);
DESCRIPTOR_SET_IN=$$(for word in $(SRCS); do echo $$word; done | grep '\\.bin$$' | tr -s '\\n' ':');
PROTO_PATHS=$$(for f in $(locations :proto_sources);
do if [[ "$$f" =~ ^.*/pkg/.*/_virtual_imports/.*_proto/.*.proto$$ ]];
then echo $$f | sed 's|.*pkg/.*/_virtual_imports/.*_proto/||' | sed 's|.bin$$||';
fi; done | sort -u);
export PATH=$$PATH:$$(dirname $(NODE_PATH))
$(location @com_google_protobuf//:protoc) \
--plugin=$(location :node_modules/@bufbuild/protoc-gen-es/bin/protoc-gen-es) \
--es_out $(OUTS) \
--descriptor_set_in $$DESCRIPTOR_SET_IN \
--es_opt target=js+dts \
$$PROTO_PATHS""",
toolchains = ["@nodejs_toolchains//:resolved_toolchain"],
tools = [
"@com_google_protobuf//:protoc",
"@nodejs_toolchains//:resolved_toolchain",
],
)

proto_sources(
name = "proto_sources",
protos = [
"//pkg/server/serverpb:serverpb_proto",
"//pkg/ts/tspb:tspb_proto",
],
)

genrule(
name = "index_ts",
srcs = [":protos"],
outs = ["index.ts"],
cmd = """
ABS_ROOT_PATH=$$(realpath $(SRCS))
for f in $$(find $$ABS_ROOT_PATH -name *.js)
do
filename=$$(basename -s .js $$f)
filepath=$${f#$$ABS_ROOT_PATH/}
filepath=$${filepath%.js}
import_name=$${filepath/-/_}; \
echo export "*" as $${import_name////_} from \\"./$$(basename $(SRCS))/$${filepath}\\"\\; >> $@
done
""",
)

ts_project(
name = "ts_project",
srcs = [
"tsconfig.json",
":index_ts",
":protos",
],
tsconfig = "tsconfig.json",
validate = True,
deps = [":node_modules"],
)

npm_package(
name = "crdb-api-client",
srcs = [
"package.json",
"tsconfig.json",
":index_ts",
":protos",
":ts_project",
],
package = "@cockroachlabs/crdb-api-client",
visibility = ["//visibility:public"],
)
12 changes: 12 additions & 0 deletions pkg/ui/workspaces/crdb-api-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@cockroachlabs/crdb-api-client",
"version": "1.0.0",
"main": "index.ts",
"devDependencies": {
"@bufbuild/protoc-gen-es": "^1.7.2",
"typescript": "5.1.6"
},
"dependencies": {
"@bufbuild/protobuf": "^1.7.2"
}
}
11 changes: 11 additions & 0 deletions pkg/ui/workspaces/crdb-api-client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2016",
"lib": ["ESNext"],
"module": "ESNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": false
}
}
4 changes: 2 additions & 2 deletions pkg/ui/workspaces/db-console/src/js/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def _proto_sources_impl(ctx):
transitive = [p[ProtoInfo].transitive_sources for p in ctx.attr.protos],
))

_proto_sources = rule(
proto_sources = rule(
doc = """Provider Adapter from ProtoInfo to DefaultInfo.
Extracts the transitive_sources from the ProtoInfo provided by the proto attr.
This allows a macro to access the complete set of .proto files needed during compilation.
Expand All @@ -37,7 +37,7 @@ def protobufjs_library(name, out_name, protos, **kwargs):
ts_target = "_%s_pbts" % name

# grab the transitive .proto files needed to compile the given one
_proto_sources(
proto_sources(
name = proto_target,
protos = protos,
)
Expand Down

0 comments on commit d2bd0ec

Please sign in to comment.