github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/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", ctx.attr.mode, 23 "-external", ctx.attr.external, 24 ] 25 if ctx.attr.prefix: 26 args.extend(["-go_prefix", ctx.attr.prefix]) 27 if ctx.attr.build_tags: 28 args.extend(["-build_tags", ",".join(ctx.attr.build_tags)]) 29 args.extend(ctx.attr.extra_args) 30 31 out_file = ctx.actions.declare_file(ctx.label.name + ".bash") 32 substitutions = { 33 "@@ARGS@@": shell.array_literal(args), 34 "@@GAZELLE_LABEL@@": shell.quote(str(ctx.attr.gazelle.label)), 35 "@@GAZELLE_SHORT_PATH@@": shell.quote(ctx.file.gazelle.short_path), 36 "@@GENERATED_MESSAGE@@": """ 37 # Generated by {label} 38 # DO NOT EDIT 39 """.format(label = str(ctx.label)), 40 "@@RUNNER_LABEL@@": shell.quote(str(ctx.label)), 41 } 42 ctx.actions.expand_template( 43 template = ctx.file._template, 44 output = out_file, 45 substitutions = substitutions, 46 is_executable = True, 47 ) 48 runfiles = ctx.runfiles(files = [ctx.file.gazelle, ctx.file.workspace]) 49 return [DefaultInfo( 50 files = depset([out_file]), 51 runfiles = runfiles, 52 executable = out_file, 53 )] 54 55 56 _gazelle_runner = rule( 57 implementation = _gazelle_runner_impl, 58 attrs = { 59 "gazelle": attr.label( 60 default = "@bazel_gazelle//cmd/gazelle", 61 allow_single_file = True, 62 cfg = "host", 63 ), 64 "command": attr.string( 65 values = ["update", "fix"], 66 default = "update", 67 ), 68 "mode": attr.string( 69 values = ["print", "fix", "diff"], 70 default="fix" 71 ), 72 "external": attr.string( 73 values = ["external", "vendored"], 74 default="external", 75 ), 76 "build_tags": attr.string_list(), 77 "prefix": attr.string(), 78 "extra_args": attr.string_list(), 79 "workspace": attr.label( 80 mandatory = True, 81 allow_single_file = True, 82 cfg = "data", 83 ), 84 "_template": attr.label( 85 default="@bazel_gazelle//internal:gazelle.bash.in", 86 allow_single_file=True, 87 ), 88 }, 89 executable = True, 90 ) 91 92 def gazelle(name, **kwargs): 93 if "args" in kwargs: 94 # The args attribute has special meaning for executable rules, but we 95 # always want extra_args here instead. 96 if "extra_args" in kwargs: 97 fail("{}: both args and extra_args were provided".format(name)) 98 kwargs["extra_args"] = kwargs["args"] 99 kwargs.pop("args") 100 _gazelle_runner( 101 name = name, 102 args = ["-bazel_run"], 103 workspace = "//:WORKSPACE", 104 tags = ["manual"], 105 **kwargs 106 )