rules_powershell

Bazel rules for PowerShell.

Setup

To begin using the rules, add the following to your MODULE.bazel file:

# Available versions can be found here: https://github.com/periareon/rules_powershell/releases
bazel_dep(name = "rules_powershell", version = "{version}")

Toolchains

By default no toolchain is registered for the rules. To register toolchains, see the module extension powershell.toolchain

Powershell rules

Rules

Providers

pwsh_binary

load("@rules_powershell//powershell:defs.bzl", "pwsh_binary")

pwsh_binary(name, deps, srcs, data, env)

The pwsh_binary rule is used to declare executable powershell scripts. (pwsh_binary is a misnomer: its outputs aren't necessarily binaries.) This rule ensures that all dependencies are built, and appear in the runfiles area at execution time. We recommend that you name your pwsh_binary() rules after the name of the script minus the extension (e.g. .ps1); the rule name and the file name must be distinct.

For a simple Powershell script with no dependencies and some data files:

pwsh_binary(
    name = "foo",
    srcs = ["foo.ps1"],
    data = glob(["datafiles/*.txt"]),
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsThe list of "library" targets to be aggregated into this target. See general comments about deps at Typical attributes defined by most build rules.

This attribute should be used to list other sh_library rules that provide interpreted program source code depended on by the code in srcs. The files provided by these rules will be present among the runfiles of this target.
List of labelsoptional[]
srcsThe list of source (.ps1) files that are processed to create the target.List of labelsoptional[]
dataFiles needed by this rule at runtime. May list file or rule targets. Generally allows any target.List of labelsoptional[]
envDictionary of strings; values are subject to $(location) and "Make variable" substitution.Dictionary: String -> Stringoptional{}

pwsh_library

load("@rules_powershell//powershell:defs.bzl", "pwsh_library")

pwsh_library(name, deps, srcs, data)

The main use for this rule is to aggregate together a logical "library" consisting of related scripts and modules.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsThe list of "library" targets to be aggregated into this target. See general comments about deps at Typical attributes defined by most build rules.

This attribute should be used to list other sh_library rules that provide interpreted program source code depended on by the code in srcs. The files provided by these rules will be present among the runfiles of this target.
List of labelsoptional[]
srcsThe list of source files that are processed to create the target.List of labelsoptional[]
dataFiles needed by this rule at runtime. May list file or rule targets. Generally allows any target.List of labelsoptional[]

pwsh_test

load("@rules_powershell//powershell:defs.bzl", "pwsh_test")

pwsh_test(name, deps, srcs, data, env, env_inherit)

A pwsh_test rule creates a test written as a Powershell script.

pwsh_test(
    name = "foo_integration_test",
    size = "small",
    srcs = ["foo_integration_test.ps1"],
    deps = [":foo_sh_lib"],
    data = glob(["testdata/*.txt"]),
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsThe list of "library" targets to be aggregated into this target. See general comments about deps at Typical attributes defined by most build rules.

This attribute should be used to list other sh_library rules that provide interpreted program source code depended on by the code in srcs. The files provided by these rules will be present among the runfiles of this target.
List of labelsoptional[]
srcsThe list of source (.ps1) files that are processed to create the target.List of labelsoptional[]
dataFiles needed by this rule at runtime. May list file or rule targets. Generally allows any target.List of labelsoptional[]
envDictionary of strings; values are subject to $(location) and "Make variable" substitution.Dictionary: String -> Stringoptional{}
env_inheritSpecifies additional environment variables to inherit from the external environment when the test is executed by bazel test.List of stringsoptional[]

PwshInfo

load("@rules_powershell//powershell:defs.bzl", "PwshInfo")

PwshInfo(imports, srcs)

A provider for Powershell rules.

FIELDS

NameDescription
importsDepset[str]: The list of rlocation paths to module files (.psm1, .psd1) for PSModulePath setup.
srcsDepset[File]: The list of source files associated with the powershell target.

Powershell bzlmod extensions

Module Extensions

powershell

powershell = use_extension("@rules_powershell//powershell:extensions.bzl", "powershell")
powershell.toolchain(name, urls, version)

Bzlmod extensions for Powershell

TAG CLASSES

toolchain

An extension for defining a pwsh_toolchain from a download archive.

An example of defining and registering toolchains:

powershell = use_extension("//powershell:extensions.bzl", "powershell", dev_dependency = True)
powershell.toolchain(
    name = "powershell_toolchains",
    version = "7.5.3",
)
use_repo(powershell, "powershell_toolchains")

register_toolchains(
    "@powershell_toolchains//:all",
    dev_dependency = True,
)

Attributes

NameDescriptionTypeMandatoryDefault
nameThe name of the toolchain.Namerequired
urlsUrl templates to use for downloading Powershell.List of stringsoptional["https://github.com/PowerShell/PowerShell/releases/download/v{semver}/{artifact}"]
versionThe version of Powershell to download.Stringoptional"7.5.4"