github.com/wolfd/bazel-gazelle@v0.14.0/def.bzl (about) 1 # Copyright 2017 The Bazel Authors. All rights reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 load("@bazel_skylib//:lib.bzl", "shell") 16 load("//internal:go_repository.bzl", "go_repository") 17 load("//internal:overlay_repository.bzl", "git_repository", "http_archive") 18 19 def _gazelle_runner_impl(ctx): 20 args = [ 21 ctx.attr.command, 22 "-mode", 23 ctx.attr.mode, 24 "-external", 25 ctx.attr.external, 26 ] 27 if ctx.attr.prefix: 28 args.extend(["-go_prefix", ctx.attr.prefix]) 29 if ctx.attr.build_tags: 30 args.extend(["-build_tags", ",".join(ctx.attr.build_tags)]) 31 args.extend(ctx.attr.extra_args) 32 33 out_file = ctx.actions.declare_file(ctx.label.name + ".bash") 34 substitutions = { 35 "@@ARGS@@": shell.array_literal(args), 36 "@@GAZELLE_LABEL@@": shell.quote(str(ctx.attr.gazelle.label)), 37 "@@GAZELLE_SHORT_PATH@@": shell.quote(ctx.executable.gazelle.short_path), 38 "@@GENERATED_MESSAGE@@": """ 39 # Generated by {label} 40 # DO NOT EDIT 41 """.format(label = str(ctx.label)), 42 "@@RUNNER_LABEL@@": shell.quote(str(ctx.label)), 43 } 44 ctx.actions.expand_template( 45 template = ctx.file._template, 46 output = out_file, 47 substitutions = substitutions, 48 is_executable = True, 49 ) 50 runfiles = ctx.runfiles(files = [ctx.executable.gazelle, ctx.file.workspace]) 51 return [DefaultInfo( 52 files = depset([out_file]), 53 runfiles = runfiles, 54 executable = out_file, 55 )] 56 57 _gazelle_runner = rule( 58 implementation = _gazelle_runner_impl, 59 attrs = { 60 "gazelle": attr.label( 61 default = "@bazel_gazelle//cmd/gazelle", 62 executable = True, 63 cfg = "host", 64 ), 65 "command": attr.string( 66 values = ["update", "fix"], 67 default = "update", 68 ), 69 "mode": attr.string( 70 values = ["print", "fix", "diff"], 71 default = "fix", 72 ), 73 "external": attr.string( 74 values = ["external", "vendored"], 75 default = "external", 76 ), 77 "build_tags": attr.string_list(), 78 "prefix": attr.string(), 79 "extra_args": attr.string_list(), 80 "workspace": attr.label( 81 mandatory = True, 82 allow_single_file = True, 83 cfg = "data", 84 ), 85 "_template": attr.label( 86 default = "@bazel_gazelle//internal:gazelle.bash.in", 87 allow_single_file = True, 88 ), 89 }, 90 executable = True, 91 ) 92 93 def gazelle(name, **kwargs): 94 if "args" in kwargs: 95 # The args attribute has special meaning for executable rules, but we 96 # always want extra_args here instead. 97 if "extra_args" in kwargs: 98 fail("{}: both args and extra_args were provided".format(name)) 99 kwargs["extra_args"] = kwargs["args"] 100 kwargs.pop("args") 101 _gazelle_runner( 102 name = name, 103 args = ["-bazel_run"], 104 workspace = "//:WORKSPACE", 105 tags = ["manual"], 106 **kwargs 107 )