github.com/cheshirekow/buildtools@v0.0.0-20200224190056-5d637702fe81/buildifier/def.bzl (about) 1 load("@bazel_skylib//lib:shell.bzl", "shell") 2 3 def _buildifier_impl(ctx): 4 # Do not depend on defaults encoded in the binary. Always use defaults set 5 # on attributes of the rule. 6 args = [ 7 "-mode=%s" % ctx.attr.mode, 8 "-v=%s" % str(ctx.attr.verbose).lower(), 9 ] 10 11 if ctx.attr.lint_mode: 12 args.append("-lint=%s" % ctx.attr.lint_mode) 13 if ctx.attr.lint_warnings: 14 if not ctx.attr.lint_mode: 15 fail("Cannot pass 'lint_warnings' without a 'lint_mode'") 16 args.append("--warnings={}".format(",".join(ctx.attr.lint_warnings))) 17 if ctx.attr.multi_diff: 18 args.append("-multi_diff") 19 if ctx.attr.diff_command: 20 args.append("-diff_command=%s" % ctx.attr.diff_command) 21 if ctx.attr.add_tables: 22 args.append("-add_tables=%s" % ctx.file.add_tables.path) 23 24 exclude_patterns_str = "" 25 if ctx.attr.exclude_patterns: 26 exclude_patterns = ["\\! -path %s" % shell.quote(pattern) for pattern in ctx.attr.exclude_patterns] 27 exclude_patterns_str = " ".join(exclude_patterns) 28 29 out_file = ctx.actions.declare_file(ctx.label.name + ".bash") 30 substitutions = { 31 "@@ARGS@@": shell.array_literal(args), 32 "@@BUILDIFIER_SHORT_PATH@@": shell.quote(ctx.executable._buildifier.short_path), 33 "@@EXCLUDE_PATTERNS@@": exclude_patterns_str, 34 } 35 ctx.actions.expand_template( 36 template = ctx.file._runner, 37 output = out_file, 38 substitutions = substitutions, 39 is_executable = True, 40 ) 41 runfiles = ctx.runfiles(files = [ctx.executable._buildifier]) 42 return [DefaultInfo( 43 files = depset([out_file]), 44 runfiles = runfiles, 45 executable = out_file, 46 )] 47 48 _buildifier = rule( 49 implementation = _buildifier_impl, 50 attrs = { 51 "verbose": attr.bool( 52 doc = "Print verbose information on standard error", 53 ), 54 "mode": attr.string( 55 default = "fix", 56 doc = "Formatting mode", 57 values = ["check", "diff", "fix", "print_if_changed"], 58 ), 59 "lint_mode": attr.string( 60 doc = "Linting mode", 61 values = ["", "fix", "warn"], 62 ), 63 "lint_warnings": attr.string_list( 64 allow_empty = True, 65 doc = "all prefixed with +/- if you want to include in or exclude from the default set of warnings, or none prefixed with +/- if you want to override the default set, or 'all' for all avaliable warnings", 66 ), 67 "exclude_patterns": attr.string_list( 68 allow_empty = True, 69 doc = "A list of glob patterns passed to the find command. E.g. './vendor/*' to exclude the Go vendor directory", 70 ), 71 "diff_command": attr.string( 72 doc = "Command to use to show diff, with mode=diff. E.g. 'diff -u'", 73 ), 74 "multi_diff": attr.bool( 75 default = False, 76 doc = "Set to True if the diff command specified by the 'diff_command' can diff multiple files in the style of 'tkdiff'", 77 ), 78 "add_tables": attr.label( 79 mandatory = False, 80 doc = "path to JSON file with custom table definitions which will be merged with the built-in tables", 81 allow_single_file = True, 82 ), 83 "_buildifier": attr.label( 84 default = "@com_github_bazelbuild_buildtools//buildifier", 85 cfg = "host", 86 executable = True, 87 ), 88 "_runner": attr.label( 89 default = "@com_github_bazelbuild_buildtools//buildifier:runner.bash.template", 90 allow_single_file = True, 91 ), 92 }, 93 executable = True, 94 ) 95 96 def buildifier(**kwargs): 97 tags = kwargs.get("tags", []) 98 if "manual" not in tags: 99 tags.append("manual") 100 kwargs["tags"] = tags 101 _buildifier(**kwargs)