Skip to content

Commit

Permalink
proto_repository: add attributes to write a proto_repository_info pro…
Browse files Browse the repository at this point in the history
…vider (#328)

* Use non-zip archive for @protoapis dep
* Add proto_repository_info provider, rule, and github_proto_repository macro
* Upgrade to rules_go 0.39.1
* Bump minimum bazel version to 5.4.0
* Add test for ProtoRepositoryInfo provider
* Bump bazelci version
  • Loading branch information
pcj committed Jun 18, 2023
1 parent 361cff2 commit a78c49d
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
buildifier:
version: 5.1.0
warnings: "-function-docstring,-function-docstring-header,-module-docstring,-function-docstring-args,-list-append"
bazel: 5.1.1
bazel: 5.4.0
tasks:
default_workspace_ubuntu1804:
platform: ubuntu1804
Expand Down
1 change: 0 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ build:bazelci --deleted_packages=docs

# workaround for scala
build --incompatible_java_common_parameters=false

2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.1.1
5.4.0
16 changes: 8 additions & 8 deletions deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ depsgen(
deps = ["//deps:io_grpc_grpc_java"],
)

# Release: v0.35.0
# TargetCommitish: release-0.35
# Date: 2022-09-11 15:59:49 +0000 UTC
# URL: https://github.com/bazelbuild/rules_go/releases/tag/v0.35.0
# Size: 931734 (932 kB)
# Release: v0.39.1
# TargetCommitish: release-0.39
# Date: 2023-04-20 04:35:08 +0000 UTC
# URL: https://github.com/bazelbuild/rules_go/releases/tag/v0.39.1
# Size: 1759832 (1.8 MB)
proto_dependency(
name = "io_bazel_rules_go",
repository_rule = "http_archive",
sha256 = "cc027f11f98aef8bc52c472ced0714994507a16ccd3a0820b2df2d6db695facd",
strip_prefix = "rules_go-0.35.0",
urls = ["https://github.com/bazelbuild/rules_go/archive/v0.35.0.tar.gz"],
sha256 = "473a064d502e89d11c497a59f9717d1846e01515a3210bd169f22323161c076e",
strip_prefix = "rules_go-0.39.1",
urls = ["https://github.com/bazelbuild/rules_go/archive/v0.39.1.tar.gz"],
visibility = ["//visibility:public"],
)

Expand Down
6 changes: 3 additions & 3 deletions deps/core_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def io_bazel_rules_go():
_maybe(
http_archive,
name = "io_bazel_rules_go",
sha256 = "cc027f11f98aef8bc52c472ced0714994507a16ccd3a0820b2df2d6db695facd",
strip_prefix = "rules_go-0.35.0",
sha256 = "473a064d502e89d11c497a59f9717d1846e01515a3210bd169f22323161c076e",
strip_prefix = "rules_go-0.39.1",
urls = [
"https://github.com/bazelbuild/rules_go/archive/v0.35.0.tar.gz",
"https://github.com/bazelbuild/rules_go/archive/v0.39.1.tar.gz",
],
)

Expand Down
3 changes: 3 additions & 0 deletions example/golden/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func TestRunGazelle(t *testing.T) {
if err := bazel_testing.RunBazel("build", "..."); err != nil {
t.Fatal(err)
}
if err := bazel_testing.RunBazel("test", "..."); err != nil {
t.Fatal(err)
}
}
""",
strip_prefix = "example/golden/testdata/proto_repository",
Expand Down
11 changes: 11 additions & 0 deletions example/golden/testdata/proto_repository/BUILD.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
load("@build_stack_rules_proto//rules:proto_gazelle.bzl", "DEFAULT_LANGUAGES", "proto_gazelle")
load("//:proto_repository_info_test.bzl", "proto_repository_info_test")

# gazelle:prefix github.com/corp/repo
# gazelle:go_generate_proto false
Expand All @@ -19,4 +20,14 @@ proto_gazelle(
imports = ["@googleapis//:imports.csv"],
)

proto_repository_info_test(
name = "proto_repository_info_test",
info = "@googleapis//:proto_repository",
want_source_host = "github.com",
want_source_owner = "googleapis",
want_source_repo = "googleapis",
want_source_commit = "66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
want_source_prefix = "",
)

exports_files(["config.yaml"])
11 changes: 11 additions & 0 deletions example/golden/testdata/proto_repository/BUILD.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

load("@bazel_gazelle//:def.bzl", "gazelle_binary")
load("@build_stack_rules_proto//rules:proto_gazelle.bzl", "DEFAULT_LANGUAGES", "proto_gazelle")
load("//:proto_repository_info_test.bzl", "proto_repository_info_test")

# gazelle:prefix github.com/corp/repo
# gazelle:go_generate_proto false
Expand All @@ -19,4 +20,14 @@ proto_gazelle(
imports = ["@googleapis//:imports.csv"],
)

proto_repository_info_test(
name = "proto_repository_info_test",
info = "@googleapis//:proto_repository",
want_source_commit = "66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
want_source_host = "github.com",
want_source_owner = "googleapis",
want_source_prefix = "",
want_source_repo = "googleapis",
)

exports_files(["config.yaml"])
3 changes: 3 additions & 0 deletions example/golden/testdata/proto_repository/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ proto_repository(
cfgs = ["//:config.yaml"],
reresolve_known_proto_imports = True,
sha256 = "b9dbc65ebc738a486265ef7b708e9449bf361541890091983e946557ee0a4bfc",
source_commit = "66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
source_owner = "googleapis",
source_repo = "googleapis",
strip_prefix = "googleapis-66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe",
type = "zip",
urls = ["https://codeload.github.com/googleapis/googleapis/zip/66759bdf6a5ebb898c2a51c8649aefd1ee0b7ffe"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
load("@build_stack_rules_proto//rules:providers.bzl", "ProtoRepositoryInfo")

def _proto_repository_info_test_impl(ctx):
info = ctx.attr.info[ProtoRepositoryInfo]

must_attr(info, ctx.attr, "source_host")
must_attr(info, ctx.attr, "source_owner")
must_attr(info, ctx.attr, "source_repo")
must_attr(info, ctx.attr, "source_commit")
must_attr(info, ctx.attr, "source_prefix")

ctx.actions.write(ctx.outputs.json, info.to_json())

# we're checking attr values in the provider, so the script really does not
# need to do anything
ctx.actions.write(ctx.outputs.executable, "echo PASS")

return [DefaultInfo(
files = depset([ctx.outputs.json, ctx.outputs.executable]),
)]

proto_repository_info_test = rule(
implementation = _proto_repository_info_test_impl,
attrs = {
"info": attr.label(
providers = [ProtoRepositoryInfo],
mandatory = True,
),
"want_source_host": attr.string(),
"want_source_owner": attr.string(),
"want_source_repo": attr.string(),
"want_source_commit": attr.string(),
"want_source_prefix": attr.string(),
},
outputs = {
"json": "%{name}.json",
},
test = True,
)

def must_attr(info, attr, name):
got = getattr(info, name)
want = getattr(attr, "want_" + name)
if got != want:
fail(".%s: want %s, got %s" % (name, want, got))
1 change: 0 additions & 1 deletion pkg/protoc/starlark_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ func (s *starlarkRuleProvider) Rule(othergen ...*rule.Rule) *rule.Rule {

// Resolve implements part of the RuleProvider interface.
func (s *starlarkRuleProvider) Resolve(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {

}

// Imports implements part of the RuleProvider interface.
Expand Down
5 changes: 2 additions & 3 deletions proto_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ def proto_repositories():
"google/protobuf/compiler/cpp/test_bad_identifiers.proto",
],
reresolve_known_proto_imports = True,
sha256 = "087c2ec84a07308318d35e0e39717e2037e05d14e628244602a2c78fbe203fa5",
strip_prefix = "protobuf-a74f54b724bdc2fe0bfc271f4dc0ceb159805625/src",
type = "zip",
# https://github.com/protocolbuffers/protobuf/releases/tag/v23.2
urls = ["https://codeload.github.com/protocolbuffers/protobuf/zip/a74f54b724bdc2fe0bfc271f4dc0ceb159805625"],
urls = ["https://github.com/protocolbuffers/protobuf/archive/a74f54b724bdc2fe0bfc271f4dc0ceb159805625.tar.gz"],
)

proto_repository(
Expand Down
14 changes: 1 addition & 13 deletions rules/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
filegroup(
name = "all_files",
testonly = True,
srcs = [
srcs = glob(["*.bzl"]) + [
"BUILD.bazel",
"depsgen.bzl",
"example.bzl",
"proto_compile.bzl",
"proto_compile_gencopy.bzl",
"proto_compiled_source_update.bzl",
"proto_compiled_sources.bzl",
"proto_dependency.bzl",
"proto_descriptor_set.bzl",
"proto_gazelle.bzl",
"proto_plugin.bzl",
"protogenrule.bzl",
"providers.bzl",
"//rules/cc:all_files",
"//rules/closure:all_files",
"//rules/go:all_files",
Expand Down
39 changes: 0 additions & 39 deletions rules/private/proto_repository_tools_srcs.bzl
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
""" Code generated by list_repository_tools_srcs.go; DO NOT EDIT."""
PROTO_REPOSITORY_TOOLS_SRCS = [
"@build_stack_rules_proto//:BUILD.bazel",
"@build_stack_rules_proto//cmd/depsgen:BUILD.bazel",
"@build_stack_rules_proto//cmd/depsgen:config.go",
"@build_stack_rules_proto//cmd/depsgen:depsgen.go",
"@build_stack_rules_proto//cmd/depsgen:generator.go",
"@build_stack_rules_proto//cmd/depsgen:template.go",
"@build_stack_rules_proto//cmd/examplegen:BUILD.bazel",
"@build_stack_rules_proto//cmd/examplegen:config.go",
"@build_stack_rules_proto//cmd/examplegen:examplegen.go",
"@build_stack_rules_proto//cmd/examplegen:generator.go",
"@build_stack_rules_proto//cmd/examplegen:linewriter.go",
"@build_stack_rules_proto//cmd/examplegen:template.go",
"@build_stack_rules_proto//cmd/gazelle:BUILD.bazel",
"@build_stack_rules_proto//cmd/gazelle:diff.go",
"@build_stack_rules_proto//cmd/gazelle:fix-update.go",
Expand All @@ -26,34 +15,9 @@ PROTO_REPOSITORY_TOOLS_SRCS = [
"@build_stack_rules_proto//cmd/gazelle:print.go",
"@build_stack_rules_proto//cmd/gazelle:update-repos.go",
"@build_stack_rules_proto//cmd/gazelle:wspace.go",
"@build_stack_rules_proto//cmd/gencopy:BUILD.bazel",
"@build_stack_rules_proto//cmd/gencopy:gencopy.go",
"@build_stack_rules_proto//deps:BUILD.bazel",
"@build_stack_rules_proto//docs:BUILD.bazel",
"@build_stack_rules_proto//example:BUILD.bazel",
"@build_stack_rules_proto//example/golden:BUILD.bazel",
"@build_stack_rules_proto//example/person:BUILD.bazel",
"@build_stack_rules_proto//example/place:BUILD.bazel",
"@build_stack_rules_proto//example/routeguide:BUILD.bazel",
"@build_stack_rules_proto//example/routeguide/cc:BUILD.bazel",
"@build_stack_rules_proto//example/routeguide/closure:BUILD.bazel",
"@build_stack_rules_proto//example/routeguide/java:BUILD.bazel",
"@build_stack_rules_proto//example/routeguide/nodejs:BUILD.bazel",
"@build_stack_rules_proto//example/routeguide/scala:BUILD.bazel",
"@build_stack_rules_proto//example/thing:BUILD.bazel",
"@build_stack_rules_proto//example/toolchain/prebuilt:BUILD.bazel",
"@build_stack_rules_proto//example/toolchain/standard:BUILD.bazel",
"@build_stack_rules_proto//language/example:BUILD.bazel",
"@build_stack_rules_proto//language/example:example.go",
"@build_stack_rules_proto//language/protobuf:BUILD.bazel",
"@build_stack_rules_proto//language/protobuf/oldtestdata/gogo:BUILD.bazel",
"@build_stack_rules_proto//language/protobuf/oldtestdata/java:BUILD.bazel",
"@build_stack_rules_proto//language/protobuf:protobuf.go",
"@build_stack_rules_proto//pkg:BUILD.bazel",
"@build_stack_rules_proto//pkg/goldentest:BUILD.bazel",
"@build_stack_rules_proto//pkg/goldentest:cases.go",
"@build_stack_rules_proto//pkg/language/noop:BUILD.bazel",
"@build_stack_rules_proto//pkg/language/noop:noop.go",
"@build_stack_rules_proto//pkg/language/protobuf:BUILD.bazel",
"@build_stack_rules_proto//pkg/language/protobuf:config.go",
"@build_stack_rules_proto//pkg/language/protobuf:fix.go",
Expand Down Expand Up @@ -171,8 +135,6 @@ PROTO_REPOSITORY_TOOLS_SRCS = [
"@build_stack_rules_proto//plugin/grpc/grpc-go:BUILD.bazel",
"@build_stack_rules_proto//plugin/grpc/grpc-java:BUILD.bazel",
"@build_stack_rules_proto//plugin/grpc/grpc-node:BUILD.bazel",
"@build_stack_rules_proto//plugin/grpc/grpc-web:BUILD.bazel",
"@build_stack_rules_proto//plugin/grpc-ecosystem/grpc-gateway:BUILD.bazel",
"@build_stack_rules_proto//plugin/scalapb/scalapb:BUILD.bazel",
"@build_stack_rules_proto//plugin/stackb/grpc_js:BUILD.bazel",
"@build_stack_rules_proto//plugin/stephenh/ts-proto:BUILD.bazel",
Expand All @@ -187,7 +149,6 @@ PROTO_REPOSITORY_TOOLS_SRCS = [
"@build_stack_rules_proto//rules/proto:BUILD.bazel",
"@build_stack_rules_proto//rules/py:BUILD.bazel",
"@build_stack_rules_proto//rules/scala:BUILD.bazel",
"@build_stack_rules_proto//rules/ts:BUILD.bazel",
"@build_stack_rules_proto//toolchain:BUILD.bazel",
"@build_stack_rules_proto//vendor/github.com/bazelbuild/bazel-gazelle/config:config.go",
"@build_stack_rules_proto//vendor/github.com/bazelbuild/bazel-gazelle/config:constants.go",
Expand Down
57 changes: 56 additions & 1 deletion rules/proto/proto_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,23 @@ def _proto_repository_impl(ctx):

# Populate Gazelle directive at root build file and
lines = ["# " + d for d in ctx.attr.build_directives] + [
"",
'load("@build_stack_rules_proto//rules:proto_repository_info.bzl", "proto_repository_info")',
'exports_files(["%s"])' % ctx.attr.imports_out,
"proto_repository_info(",
" name = 'proto_repository',",
" commit = '%s'," % ctx.attr.commit,
" tag = '%s'," % ctx.attr.tag,
" vcs = '%s'," % ctx.attr.vcs,
" urls = %s," % ctx.attr.urls,
" sha256 = '%s'," % ctx.attr.sha256,
" strip_prefix = '%s'," % ctx.attr.strip_prefix,
" source_host = '%s'," % ctx.attr.source_host,
" source_owner = '%s'," % ctx.attr.source_owner,
" source_repo = '%s'," % ctx.attr.source_repo,
" source_prefix = '%s'," % ctx.attr.source_prefix,
" source_commit = '%s'," % ctx.attr.source_commit,
" visibility = ['//visibility:public'],",
")",
]
ctx.file(
build_file_name,
Expand Down Expand Up @@ -262,6 +277,7 @@ def _proto_repository_impl(ctx):
go_repository = repository_rule(
implementation = _proto_repository_impl,
attrs = {

# Fundamental attributes of a go repository
"importpath": attr.string(mandatory = False), # True in go_repository

Expand All @@ -286,6 +302,13 @@ go_repository = repository_rule(
"type": attr.string(),
"sha256": attr.string(),

# Attributes for a repository that is publically hosted by github
"source_host": attr.string(default = "github.com"),
"source_owner": attr.string(),
"source_repo": attr.string(),
"source_prefix": attr.string(),
"source_commit": attr.string(),

# Attributes for a module that should be downloaded with the Go toolchain.
"version": attr.string(),
"sum": attr.string(),
Expand Down Expand Up @@ -383,3 +406,35 @@ def patch(ctx):
if st.return_code:
fail("Error applying patch command %s:\n%s%s" %
(cmd, st.stdout, st.stderr))

def github_proto_repository(name, owner, repo, commit, prefix = "", build_file_expunge = True, build_file_proto_mode = "file", **kwargs):
"""github_proto_repository is a macro for a proto_repository hosted at github.com
Args:
name: the name of the rule
owner: the github owner (e.g. 'protocolbuffers')
repo: the github repo name (e.g. 'protobuf')
prefix: the strip_prefix value for the repo (e.g. 'src')
commit: the git commit (required for this macro)
build_file_expunge: defaults to true for this macro.
build_file_proto_mode: defaults to 'file' for this macro.
**kwargs: the kwargs accumulator
"""
strip_prefix = "%s-%s" % (repo, commit)
if prefix:
strip_prefix += "/" + prefix

proto_repository(
name = name,
source_host = "github.com",
source_owner = owner,
source_repo = repo,
source_commit = commit,
source_prefix = prefix,
strip_prefix = strip_prefix,
build_file_expunge = build_file_expunge,
build_file_proto_mode = build_file_proto_mode,
urls = ["https://%s/%s/archive/%s.tar.gz" % (owner, repo, commit)],
**kwargs
)
Loading

0 comments on commit a78c49d

Please sign in to comment.