-
-
Save ekhabarov/9628640d0fdf2e6febe96927e7e979ea to your computer and use it in GitHub Desktop.
Baze rule for 99designs/gqlgen
This file contains hidden or 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 hidden or 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