|
| 1 | +""" |
| 2 | +This module contains a rule for running cue commands. |
| 3 | +""" |
| 4 | + |
| 5 | +def _cue_cmd_impl(ctx): |
| 6 | + cue_tool = ctx.toolchains["@rules_cue//tools/cue:toolchain_type"].cueinfo.tool |
| 7 | + cmd_sh = ctx.actions.declare_file(ctx.attr.name + ".sh") |
| 8 | + substitutions = { |
| 9 | + "%{BUILT_IN}": ctx.attr.built_in, |
| 10 | + "%{COMMAND}": ctx.attr.command, |
| 11 | + "%{CUE}": cue_tool.path, |
| 12 | + "%{CWD}": ctx.label.package, |
| 13 | + } |
| 14 | + ctx.actions.expand_template( |
| 15 | + template = ctx.file._cmd_tpl, |
| 16 | + output = cmd_sh, |
| 17 | + substitutions = substitutions, |
| 18 | + ) |
| 19 | + |
| 20 | + return DefaultInfo( |
| 21 | + executable = cmd_sh, |
| 22 | + runfiles = ctx.runfiles( |
| 23 | + files = [ |
| 24 | + cue_tool, |
| 25 | + ], |
| 26 | + ), |
| 27 | + ) |
| 28 | + |
| 29 | +cue_cmd = rule( |
| 30 | + attrs = { |
| 31 | + # The cue built-in command to run (e.g., "fmt", "vet"). |
| 32 | + # See https://cuelang.org/docs/reference/command/ |
| 33 | + "built_in": attr.string( |
| 34 | + mandatory = False, |
| 35 | + default = "", |
| 36 | + ), |
| 37 | + # User command to run with cue cmd {command}. |
| 38 | + # keep name 'command' for backward compatibility |
| 39 | + # See https://cuelang.org/docs/reference/command/cue-help-commands/ |
| 40 | + "command": attr.string( |
| 41 | + mandatory = False, |
| 42 | + ), |
| 43 | + "_cmd_tpl": attr.label( |
| 44 | + default = Label("//tools/cue:cmd.sh.tpl"), |
| 45 | + allow_single_file = True, |
| 46 | + ), |
| 47 | + }, |
| 48 | + implementation = _cue_cmd_impl, |
| 49 | + executable = True, |
| 50 | + toolchains = ["@rules_cue//tools/cue:toolchain_type"], |
| 51 | +) |
| 52 | + |
| 53 | +def cue_binary(name, **kwargs): |
| 54 | + """ |
| 55 | + A convenience alias for cue_cmd. |
| 56 | +
|
| 57 | + Args: |
| 58 | + name: The name of the rule. |
| 59 | + **kwargs: Additional arguments to pass to cue_cmd. |
| 60 | + """ |
| 61 | + cue_cmd( |
| 62 | + name = name, |
| 63 | + built_in = "", |
| 64 | + **kwargs |
| 65 | + ) |
0 commit comments