Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ go_sdk.download(version = "1.24.0")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_work = "//:go.work")
use_repo(go_deps, "com_github_getkin_kin_openapi", "com_github_golang_glog", "com_github_googlecloudplatform_declarative_resource_client_library", "com_github_hashicorp_hcl", "com_github_kylelemons_godebug", "com_github_nasa9084_go_openapi", "com_github_otiai10_copy", "in_gopkg_yaml_v2", "in_gopkg_yaml_v3", "org_bitbucket_creachadair_stringset", "org_golang_x_exp")
use_repo(go_deps, "com_github_getkin_kin_openapi", "com_github_golang_glog", "com_github_google_go_cmp", "com_github_googlecloudplatform_declarative_resource_client_library", "com_github_hashicorp_hcl", "com_github_kylelemons_godebug", "com_github_nasa9084_go_openapi", "com_github_otiai10_copy", "in_gopkg_yaml_v2", "in_gopkg_yaml_v3", "org_bitbucket_creachadair_stringset", "org_golang_x_exp")
155 changes: 89 additions & 66 deletions build_defs/product.bzl
Original file line number Diff line number Diff line change
@@ -1,84 +1,107 @@
"""
Product-related custom build rules for Magic Modules.
"""

load("//build_defs:providers.bzl", "ProductInfo", "ResourceInfo", "TpgResourceInfo")

def _mm_product_impl(ctx):
return [ProductInfo(
name = ctx.label.name,
yaml = ctx.file.src,
version = ctx.attr.version,
)]
return [ProductInfo(
name = ctx.label.name,
yaml = ctx.file.src,
version = ctx.attr.version,
)]

mm_product = rule(
implementation = _mm_product_impl,
attrs = {
"version": attr.string(default="ga", mandatory=False),
"src": attr.label(
allow_single_file = [".yaml"],
mandatory = True,
),
},
implementation = _mm_product_impl,
attrs = {
"version": attr.string(default = "ga", mandatory = False),
"src": attr.label(
allow_single_file = [".yaml"],
mandatory = True,
),
},
)

def _tpg_product_impl(ctx):
product = ctx.attr.product[ProductInfo]
resources = [res[ResourceInfo] for res in ctx.attr.resources]
tpg_resources = [res[TpgResourceInfo] for res in ctx.attr.resources]
operations = [res for res in resources if res.has_operation]
inputs = [product.yaml] + [res.metadata for res in tpg_resources] + [res.src for res in tpg_resources] + [res.yaml for res in resources]
outputs = [
ctx.actions.declare_file("{}/product.go".format(product.version)),
]
ctx.actions.run(
executable = ctx.executable._compiler,
arguments = [
"--product", product.yaml.path,
"--version", product.version,
"--product_name", product.name,
"--type", "product",
"--provider", "tpg",
"--output", outputs[0].path
],
inputs = depset([i for i in inputs]),
outputs = outputs
)
if operations:
operation_go = ctx.actions.declare_file("{}/{}_operation.go".format(product.version, product.name))
product = ctx.attr.product[ProductInfo]
resources = [res[ResourceInfo] for res in ctx.attr.resources]
tpg_resources = [res[TpgResourceInfo] for res in ctx.attr.resources]
operations = [res for res in resources if res.has_operation]
inputs = [product.yaml] + [res.metadata for res in tpg_resources] + [res.src for res in tpg_resources] + [res.yaml for res in resources] + [f for f in ctx.files._templates]

outputs = [
ctx.actions.declare_file("{}/product.go".format(product.version)),
]
ctx.actions.run(
executable = ctx.executable._compiler,
arguments = [
"--product", product.yaml.path,
"--resource", operations[0].yaml.path,
"--version", product.version,
"--product_name", product.name,
"--type", "operation",
"--provider", "tpg",
"--output", operation_go.path
"--product",
product.yaml.path,
"--version",
product.version,
"--product_name",
product.name,
"--type",
"product",
"--provider",
"tpg",
"--output",
outputs[0].path,
],
inputs = depset([i for i in inputs]),
outputs = [operation_go]
outputs = outputs,
mnemonic = "TpgGenerateProduct",
)
outputs.append(operation_go)
if operations:
operation_go = ctx.actions.declare_file("{}/{}_operation.go".format(product.version, product.name))
ctx.actions.run(
executable = ctx.executable._compiler,
arguments = [
"--product",
product.yaml.path,
"--resource",
operations[0].yaml.path,
"--version",
product.version,
"--product_name",
product.name,
"--type",
"operation",
"--provider",
"tpg",
"--output",
operation_go.path,
],
inputs = depset([i for i in inputs]),
outputs = [operation_go],
mnemonic = "TpgGenerateProductOperation",
)
outputs.append(operation_go)

return [
ctx.attr.product[ProductInfo],
DefaultInfo(files = depset([out for out in outputs])),
]
return [
ctx.attr.product[ProductInfo],
DefaultInfo(files = depset([out for out in outputs])),
]

tpg_product = rule(
implementation = _tpg_product_impl,
attrs = {
"product": attr.label(
providers = [ProductInfo],
mandatory = True,
),
"resources": attr.label_list(
providers = [ResourceInfo, TpgResourceInfo],
mandatory=True,
),
"_compiler": attr.label(
default = Label("//mmv1/cmd"),
allow_single_file = True,
executable = True,
cfg = "exec",
),
},
implementation = _tpg_product_impl,
attrs = {
"product": attr.label(
providers = [ProductInfo],
mandatory = True,
),
"resources": attr.label_list(
providers = [ResourceInfo, TpgResourceInfo],
mandatory = True,
),
"_compiler": attr.label(
default = Label("//mmv1/cmd"),
allow_single_file = True,
executable = True,
cfg = "exec",
),
"_templates": attr.label(
default = Label("//mmv1/templates"),
),
},
)
42 changes: 23 additions & 19 deletions build_defs/providers.bzl
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
"""
Provider definitions for Magic Modules custom build rules.
"""

ProductInfo = provider(
"Provider for Magic Modules products.",
fields = {
"name": "name of the product",
"version": "version of the provider to be generated",
"yaml": "the product.yaml file",
},
"Provider for Magic Modules products.",
fields = {
"name": "name of the product",
"version": "version of the provider to be generated",
"yaml": "the product.yaml file",
},
)

ResourceInfo = provider(
"Provider for Magic Modules resources.",
fields = {
"name": "name of the resource",
"yaml": "the [resource].yaml input file",
"has_sweeper": "whether a sweeper must be generated",
"has_operation": "whether an operation must be generated",
},
"Provider for Magic Modules resources.",
fields = {
"name": "name of the resource",
"yaml": "the [resource].yaml input file",
"has_sweeper": "whether a sweeper must be generated",
"has_operation": "whether an operation must be generated",
},
)

TpgResourceInfo = provider(
"Provider for TPG resources.",
fields = {
"src": "the generated go file",
"metadata": "the generated metadata.yaml file",
"sweeper": "the optional generated sweeper file",
},
"Provider for TPG resources.",
fields = {
"src": "the generated go file",
"metadata": "the generated metadata.yaml file",
"sweeper": "the optional generated sweeper file",
},
)
Loading
Loading