Skip to content

Commit

Permalink
build: drop @cgrindel_bazel_starlib//updatesrc
Browse files Browse the repository at this point in the history
Refactor the remaining targets that depended on @cgrindel_bazel_starlib//updatesrc.
The repository has now been completely migrated to @aspect_bazel_lib//lib:write_source_files.

During the refactor, we dropped the functionality to write the source files in
rules_openapi. Instead, the caller side can decide how to write the source
files. This makes the rules more composable and easier to use. The functionality
has been moved to a macro in private/mgmtapi/api.bzl, which is used inside of
scionproto/scion.
  • Loading branch information
oncilla committed Oct 30, 2023
1 parent 46b055a commit 4a1e331
Show file tree
Hide file tree
Showing 22 changed files with 143 additions and 163 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bin
doc/_build
docker/_build
rules_openapi/tools/node_modules
tools/lint/logctxcheck/testdata/src
19 changes: 10 additions & 9 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ load("//tools/lint:write_source_files.bzl", "write_source_files")
load("//tools/lint/python:flake8_config.bzl", "flake8_lint_config")
load("//:nogo.bzl", "nogo_deps")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
load("@cgrindel_bazel_starlib//updatesrc:defs.bzl", "updatesrc_update_all")

# gazelle:prefix github.com/scionproto/scion
# gazelle:map_kind go_library go_library //tools/lint:go.bzl
Expand Down Expand Up @@ -233,19 +232,21 @@ buildifier(
mode = "check",
)

# Runs all update_src targets in this Workspace. Currently, generating the
# OpenAPI specs is the last target that depends on update_src. Eventually,
# this should be transitioned to write_all_source_files below.
updatesrc_update_all(
name = "update_all",
)

# Runs all write_source_files targets in this Workspace. To update the list run
# bazel run @com_github_bazelbuild_buildtools//buildozer -- --root_dir $PWD "add additional_update_targets $( bazel query 'filter("^.*[^\d]$", kind(_write_source_file, //...)) except //:write_all_source_files' | tr '\n' ' ')" //:write_all_source_files
write_source_files(
name = "write_all_source_files",
additional_update_targets = [
"//doc/_build/_static/command:write_files",
"//control/mgmtapi:write_files",
"//daemon/mgmtapi:write_files",
"//dispatcher/mgmtapi:write_files",
"//doc/command:write_files",
"//gateway/mgmtapi:write_files",
"//private/ca/api:write_files",
"//private/mgmtapi/cppki/api:write_files",
"//private/mgmtapi/health/api:write_files",
"//private/mgmtapi/segments/api:write_files",
"//router/mgmtapi:write_files",
"//spec:write_files",
],
)
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ antlr:

write_all_source_files:
bazel run //:write_all_source_files
bazel run //:update_all

.PHONY: lint lint-bazel lint-bazel-buildifier lint-doc lint-doc-mdlint lint-go lint-go-bazel lint-go-gazelle lint-go-golangci lint-go-semgrep lint-openapi lint-openapi-spectral lint-protobuf lint-protobuf-buf

Expand Down
6 changes: 0 additions & 6 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,3 @@ rules_openapi_dependencies()
load("//rules_openapi:install.bzl", "rules_openapi_install_yarn_dependencies")

rules_openapi_install_yarn_dependencies()

# TODO(lukedirtwalker): can that be integrated in the rules_openapi_dependencies
# call above somehow?
load("@cgrindel_bazel_starlib//:deps.bzl", "bazel_starlib_dependencies")

bazel_starlib_dependencies()
3 changes: 2 additions & 1 deletion control/mgmtapi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//tools/lint:go.bzl", "go_library", "go_test")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_build_docs", "openapi_generate_go")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_build_docs")
load("//private/mgmtapi:api.bzl", "openapi_generate_go")

openapi_build_docs(
name = "doc",
Expand Down
3 changes: 2 additions & 1 deletion daemon/mgmtapi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//tools/lint:go.bzl", "go_library")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_build_docs", "openapi_generate_go")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_build_docs")
load("//private/mgmtapi:api.bzl", "openapi_generate_go")

openapi_build_docs(
name = "doc",
Expand Down
3 changes: 2 additions & 1 deletion dispatcher/mgmtapi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//tools/lint:go.bzl", "go_library")
load("//rules_openapi:defs.bzl", "openapi_build_docs", "openapi_generate_go")
load("//rules_openapi:defs.bzl", "openapi_build_docs")
load("//private/mgmtapi:api.bzl", "openapi_generate_go")

openapi_build_docs(
name = "doc",
Expand Down
3 changes: 2 additions & 1 deletion gateway/mgmtapi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//tools/lint:go.bzl", "go_library")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_build_docs", "openapi_generate_go")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_build_docs")
load("//private/mgmtapi:api.bzl", "openapi_generate_go")

openapi_build_docs(
name = "doc",
Expand Down
2 changes: 1 addition & 1 deletion private/ca/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//tools/lint:go.bzl", "go_library")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_generate_go")
load("//private/mgmtapi:api.bzl", "openapi_generate_go")

openapi_generate_go(
name = "api_generated",
Expand Down
58 changes: 58 additions & 0 deletions private/mgmtapi/api.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""OpenAPI Macros
Macros for generating Go code from OpenAPI specs.
"""

load("//tools/lint:write_source_files.bzl", "write_source_files")
load("//rules_openapi:defs.bzl", _openapi_generate_go = "openapi_generate_go")

def openapi_generate_go(
name,
client = True,
server = True,
spec = True,
types = True,
**kwargs):
"""
Generates Go code from an OpenAPI spec.
This macro creates two additional rules:
- {{name}}_files: A filegroup with the generated files.
- write_files: A write_source_files rule that writes the generated files to
the source directory.
Args:
name: The name of the rule.
client: Whether to generate a client.
server: Whether to generate a server.
spec: Whether to generate a spec.
types: Whether to generate types.
**kwargs: Ad.
"""

_openapi_generate_go(
name = name + "_gen",
out_client = "client.bzl.gen.go" if client else None,
out_server = "server.bzl.gen.go" if server else None,
out_spec = "spec.bzl.gen.go" if spec else None,
out_types = "types.bzl.gen.go" if types else None,
**kwargs
)

out_files = []
write_files = {}
for typ, gen in {"client": client, "server": server, "spec": spec, "types": types}.items():
if not gen:
continue
src = typ + ".bzl.gen.go"
out_files.append(src)
write_files[typ + ".gen.go"] = src

native.filegroup(
name = name,
srcs = out_files,
)

write_source_files(
name = "write_files",
files = write_files,
)
3 changes: 1 addition & 2 deletions private/mgmtapi/cppki/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
load("//tools/lint:go.bzl", "go_library", "go_test")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_generate_go")
load("//private/mgmtapi:api.bzl", "openapi_generate_go")

openapi_generate_go(
name = "api_generated",
src = "//spec:cppki",
server = True,
spec = False,
)

Expand Down
3 changes: 1 addition & 2 deletions private/mgmtapi/health/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
load("//tools/lint:go.bzl", "go_library")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_generate_go")
load("//private/mgmtapi:api.bzl", "openapi_generate_go")

openapi_generate_go(
name = "api_generated",
src = "//spec:health",
server = True,
spec = False,
)

Expand Down
3 changes: 1 addition & 2 deletions private/mgmtapi/segments/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
load("//tools/lint:go.bzl", "go_library", "go_test")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_generate_go")
load("//private/mgmtapi:api.bzl", "openapi_generate_go")

openapi_generate_go(
name = "api_generated",
src = "//spec:segments",
server = True,
spec = False,
)

Expand Down
3 changes: 2 additions & 1 deletion router/mgmtapi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//tools/lint:go.bzl", "go_library", "go_test")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_build_docs", "openapi_generate_go")
load("@com_github_scionproto_scion//rules_openapi:defs.bzl", "openapi_build_docs")
load("//private/mgmtapi:api.bzl", "openapi_generate_go")

openapi_build_docs(
name = "doc",
Expand Down
46 changes: 13 additions & 33 deletions rules_openapi/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
load("//rules_openapi/internal:generate.bzl", _openapi_generate_go = "openapi_generate_go")
load("//rules_openapi/internal:bundle.bzl", _openapi_bundle = "openapi_bundle")
load("//rules_openapi/internal:docs.bzl", _openapi_build_docs = "openapi_build_docs")
load("//rules_openapi/internal:header.bzl", _header = "header")
load("@cgrindel_bazel_starlib//updatesrc:defs.bzl", "updatesrc_update")

def openapi_bundle(
name,
srcs,
entrypoint,
visibility = None):
_openapi_bundle(
name = name,
name = name + "-no-header",
out = name + "-no-header.bzl.gen.yml",
srcs = srcs,
entrypoint = entrypoint,
visibility = visibility,
)

_header_target = name + "-add-header"
_header(
name = _header_target,
srcs = [":" + name],
header = "# GENERATED FILE DO NOT EDIT",
)

updatesrc_update(
name = name + "-update",
deps = [":" + _header_target],
native.genrule(
name = name,
srcs = [name + "-no-header"],
outs = [name + ".bzl.gen.yml"],
cmd = "$(location @com_github_scionproto_scion//rules_openapi/internal:header.sh) \
'$(location " + name + "-no-header)' \
'$(location " + name + ".bzl.gen.yml)' \
'# GENERATED FILE DO NOT EDIT' \
",
tools = ["@com_github_scionproto_scion//rules_openapi/internal:header.sh"],
visibility = visibility,
)

def openapi_generate_go(
Expand All @@ -36,24 +34,6 @@ def openapi_generate_go(
**kwargs
)

generate = {
"types": kwargs.get("types", True),
"server": kwargs.get("server", True),
"client": kwargs.get("client", True),
"spec": kwargs.get("spec", True),
}

srcs = []
for k, v in generate.items():
if not v:
continue
srcs.append(k + ".gen.go")
updatesrc_update(
name = name + "-update",
srcs = srcs,
outs = [":" + name],
)

def openapi_build_docs(
name,
src,
Expand Down
12 changes: 0 additions & 12 deletions rules_openapi/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,3 @@ def rules_openapi_dependencies():
sha256 = "b32a4713b45095e9e1921a7fcb1adf584bc05959f3336e7351bcf77f015a2d7c",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.1.0/rules_nodejs-4.1.0.tar.gz"],
)

maybe(
http_archive,
name = "cgrindel_bazel_starlib",
sha256 = "163a45d949fdb96b328bb44fe56976c610c6728c77118c6cd999f26cedca97eb",
strip_prefix = "bazel-starlib-0.2.1",
urls = [
"http://github.com/cgrindel/bazel-starlib/archive/v0.2.1.tar.gz",
],
patches = ["@com_github_scionproto_scion//rules_openapi:rules_starlib.patch"],
patch_args = ["-p1"],
)
11 changes: 9 additions & 2 deletions rules_openapi/internal/bundle.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
load("@bazel_skylib//lib:shell.bzl", "shell")

def _openapi_bundle_impl(ctx):
prefix = ctx.label.name
out_file = ctx.actions.declare_file(prefix + ".gen.yml")
if ctx.outputs.out:
out_file = ctx.outputs.out
else:
out_file = ctx.actions.declare_file(ctx.label.name + ".bzl.gen.yml")

cmd = "{bin} bundle --output {out} {entrypoint}".format(
bin = ctx.executable._openapi_cli.path,
out = shell.quote(out_file.path),
Expand All @@ -29,6 +32,10 @@ openapi_bundle = rule(
doc = "All files that are referenced in the entrypoint file",
allow_files = [".yml"],
),
"out": attr.output(
doc = "The bundled open API specification file",
mandatory = False,
),
"entrypoint": attr.label(
doc = "The main source to generate files from",
allow_single_file = [".yml"],
Expand Down
45 changes: 22 additions & 23 deletions rules_openapi/internal/generate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ load("@bazel_skylib//lib:shell.bzl", "shell")

def _openapi_generate_go(ctx):
generate = {
"types": ctx.attr.types,
"server": ctx.attr.server,
"client": ctx.attr.client,
"spec": ctx.attr.spec,
"types": ctx.outputs.out_types,
"server": ctx.outputs.out_server,
"client": ctx.outputs.out_client,
"spec": ctx.outputs.out_spec,
}
out_files = []
for k, v in generate.items():
if not v:
for k, out_file in generate.items():
if not out_file:
continue
out_file = ctx.actions.declare_file(k + ".gen.go")
generate_kind = k
if generate_kind == "server":
generate_kind = "chi-server"
Expand Down Expand Up @@ -69,26 +68,10 @@ openapi_generate_go = rule(
doc = "The Go package the generated code should live in.",
default = "api",
),
"types": attr.bool(
doc = "Whether the types file should be generated",
default = True,
),
"types_excludes": attr.label(
doc = "The file containing the schema list to exclude during the types generation.",
allow_single_file = True,
),
"server": attr.bool(
doc = "Whether the server code should be generated",
default = True,
),
"client": attr.bool(
doc = "Whehter the client code should be generated",
default = True,
),
"spec": attr.bool(
doc = "Whether the spec code should be generated",
default = True,
),
"templates": attr.label_list(
doc = """Folder containing Go templates to be used during code generation.
Note that the template file names need to match the ones used by the
Expand All @@ -102,5 +85,21 @@ openapi_generate_go = rule(
executable = True,
cfg = "target",
),
"out_types": attr.output(
doc = "The generated types file.",
mandatory = False,
),
"out_server": attr.output(
doc = "The generated server file.",
mandatory = False,
),
"out_client": attr.output(
doc = "The generated client file.",
mandatory = False,
),
"out_spec": attr.output(
doc = "The generated spec file.",
mandatory = False,
),
},
)
Loading

0 comments on commit 4a1e331

Please sign in to comment.