Last active
July 16, 2022 08:31
-
-
Save duarten/db10109f88b6e3899d51d7fdf23cd41f to your computer and use it in GitHub Desktop.
Baze rule for 99designs/gqlgen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load("@bazel_skylib//lib:paths.bzl", "paths") | |
load( | |
"@io_bazel_rules_go//go:def.bzl", | |
_go_context = "go_context", | |
_go_rule = "go_rule", | |
) | |
_CONFIG = """ | |
schema: | |
{schemas} | |
exec: | |
filename: {exec} | |
package: {exec_package} | |
model: | |
filename: {model} | |
package: {model_package} | |
resolver: | |
filename: {resolver} | |
package: {resolver_package} | |
type: Resolver | |
autobind: | |
- {autobind} | |
""" | |
def _extract_go_package(file, ctx): | |
segments = paths.dirname(file.basename).split("/") | |
if len(segments) == 0 or len(segments[0]) == 0: | |
return ctx.attr.default_package | |
return segments[-1] | |
def _gqlgen_impl(ctx): | |
go = _go_context(ctx) | |
schemas = [] | |
for s in ctx.attr.schemas: | |
schemas.append("- {}".format(s.label.name)) | |
schemas = "\n".join(schemas) | |
config_file = ctx.actions.declare_file(ctx.label.name + ".yml") | |
ctx.actions.write( | |
content = _CONFIG.format( | |
schemas = schemas, | |
exec = ctx.outputs.parser.basename, | |
exec_package = _extract_go_package(ctx.outputs.parser, ctx), | |
model = ctx.outputs.model.basename, | |
model_package = _extract_go_package(ctx.outputs.model, ctx), | |
resolver = ctx.outputs.resolver.basename, | |
resolver_package = _extract_go_package(ctx.outputs.resolver, ctx), | |
autobind = ctx.attr.autobind, | |
), | |
output = config_file, | |
) | |
out_files = [ctx.outputs.parser, ctx.outputs.model, ctx.outputs.resolver] | |
inputs = [config_file] | |
for s in ctx.attr.schemas: | |
inputs.extend(s.files.to_list()) | |
env = { | |
"GOCACHE": "/tmp", | |
"GOPACKAGESPRINTGOLISTERRORS": "1", | |
"GOPACKAGESDEBUG": "1", | |
} | |
env.update(go.env) | |
env["PATH"] = go.env["PATH"] + ":{}/bin".format(go.env["GOROOT"]) | |
ctx.actions.run( | |
tools = [go.go], | |
inputs = inputs, | |
outputs = out_files, | |
arguments = ["--config", config_file.path], | |
executable = ctx.executable._gqlgen, | |
env = env, | |
) | |
_gqlgen = _go_rule( | |
implementation = _gqlgen_impl, | |
attrs = { | |
"autobind": attr.string( | |
doc = "The directory containing Go files that bind to GraphQL types", | |
), | |
"default_package": attr.string( | |
doc = "The default package for the generated files", | |
mandatory = True, | |
), | |
"model": attr.output( | |
doc = "The generated model file", | |
mandatory = True, | |
), | |
"parser": attr.output( | |
doc = "The generated parser", | |
mandatory = True, | |
), | |
"resolver": attr.output( | |
doc = "The resolver code skeleton", | |
mandatory = True, | |
), | |
"schemas": attr.label_list( | |
allow_files = [".graphql"], | |
doc = "The schema files from which to generate Go code", | |
mandatory = True, | |
), | |
"_gqlgen": attr.label( | |
default = "@com_github_99designs_gqlgen//:gqlgen", | |
cfg = "host", | |
executable = True, | |
), | |
}, | |
) | |
def gqlgen(**kwargs): | |
tags = kwargs.get("tags", []) | |
if "manual" not in tags: | |
tags.append("manual") | |
kwargs["tags"] = tags | |
_gqlgen(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | |
rules_go_version = "0.19.1" | |
http_archive( | |
name = "io_bazel_rules_go", | |
sha256 = "8df59f11fb697743cbb3f26cfb8750395f30471e9eabde0d174c3aebc7a1cd39", | |
urls = [ | |
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/{}/rules_go-{}.tar.gz".format(rules_go_version, rules_go_version), | |
"https://github.com/bazelbuild/rules_go/releases/download/{}/rules_go-{}.tar.gz".format(rules_go_version, rules_go_version), | |
], | |
) | |
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") | |
go_rules_dependencies() | |
go_register_toolchain() | |
go_repository( | |
name = "com_github_99designs_gqlgen", | |
importpath = "github.com/99designs/gqlgen", | |
sum = "h1:sYL+xoM+PeMyh7RWwikT+1G+6GVtFur5ZJ1V73ra1w4=", | |
version = "v0.9.2", | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment