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

rules_powershell registers a default toolchain hub named powershell_toolchains for every consumer. It downloads a pinned release of PowerShell for the current platform, so no further setup is required to start using pwsh_binary, pwsh_library and pwsh_test.

To use a different version of PowerShell, declare a hub with a unique name through the powershell.toolchain module extension and register it. Toolchains registered by the root module take precedence over the default.

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.

The script runs with the same semantics as pwsh -File: param() blocks receive the command line arguments, exit N sets the process exit code, and an uncaught terminating error exits with code 1.

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 pwsh_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 pwsh_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_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 pwsh_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, transitive_srcs)

A provider for Powershell rules.

FIELDS

NameDescription
importsDepset[str]: The rlocationpaths of all transitive module files (.psm1, .psd1) used to set up PSModulePath.
srcsDepset[File]: The source files directly associated with the powershell target.
transitive_srcsDepset[File]: The source files of this target and all of its transitive dependencies.

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.

rules_powershell declares and registers a default hub named powershell_toolchains for all consumers, so no setup is required to get started. To use a different version of Powershell, declare a hub with a unique name and register it. Toolchains registered by the root module take precedence over the default. If two modules declare a hub with the same name, the root module's declaration wins.

An example of defining and registering toolchains:

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

register_toolchains(
    "@powershell_7_5_3//:all",
)

Attributes

NameDescriptionTypeMandatoryDefault
nameThe name of the toolchain hub repository.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"