...

Text file src/edge-infra.dev/hack/build/rules/shellcheck/shellcheck.bzl

Documentation: edge-infra.dev/hack/build/rules/shellcheck

     1"""Implementation for shellcheck_test."""
     2
     3def _impl(ctx):
     4    """Implementation for shellchecking a single file. 
     5
     6    A macro is provided to allow declaring multiple at one time."""
     7    ctx.actions.expand_template(
     8        template = ctx.file._shellcheck_sh_tpl,
     9        output = ctx.outputs.executable,
    10        is_executable = True,
    11        substitutions = {
    12            "{{file}}": ctx.file.src.short_path,
    13            "{{shell2junit_path}}": ctx.file._sh2junit_sh.short_path,
    14            "{{shellcheck_path}}": ctx.executable._shellcheck.short_path,
    15        },
    16    )
    17
    18    return DefaultInfo(
    19        executable = ctx.outputs.executable,
    20        runfiles = ctx.runfiles(
    21            files = [
    22                ctx.file._shellcheck,
    23                ctx.file._sh2junit_sh,
    24                ctx.file.src,
    25            ],
    26        ),
    27    )
    28
    29_shellcheck_test = rule(
    30    doc = "Bazel rule for linting shell scripts using shellcheck as a test.",
    31    implementation = _impl,
    32    test = True,
    33    attrs = {
    34        "src": attr.label(
    35            doc = "Shell script to lint",
    36            allow_single_file = [".sh"],
    37            mandatory = True,
    38        ),
    39        "_shellcheck": attr.label(
    40            default = Label("//hack/build/rules/shellcheck:shellcheck"),
    41            allow_single_file = True,
    42            executable = True,
    43            cfg = "exec",
    44        ),
    45        "_shellcheck_sh_tpl": attr.label(
    46            default = Label("//hack/build/rules/shellcheck:shellcheck.sh.tpl"),
    47            allow_single_file = True,
    48            executable = False,
    49        ),
    50        "_sh2junit_sh": attr.label(
    51            default = Label("//third_party/sh/shell2junit"),
    52            allow_single_file = True,
    53            executable = False,
    54        ),
    55    },
    56)
    57
    58# buildifier: disable=unnamed-macro # the name comes from the files
    59def shellcheck_test(srcs):
    60    for s in srcs:
    61        _shellcheck_test(
    62            name = "{0}_test".format(s.replace("-", "_").replace(".", "_")),
    63            src = s,
    64        )

View as plain text