Add convenience target for running in the current working directory (#29)

It's common for users of multitool to want to run tools in the current working directory. In #26, @alexeagle documented a technique we've used for a while with creating a script and symlinking to it. Our internal copy of this script is a bit more complicated to help avoid expensive calls to Bazel that simple `bazel run` calls don't really need. More refinements have been proposed in #27. All of these things are fundamentally workarounds for https://github.com/bazelbuild/bazel/issues/3325.

To help simplify things, this PR adds a convenience wrapper that captures the execpath, switches to $BUILD_WORKING_DIRECTORY, and then runs the desired tool. The resulting shell script gets to use a very simple `bazel run`, should be compatible across any slew of Bazel options, and cache as well as any typical run target.
8 files changed
tree: 7bc45c40f49541e0986ee611bb90f29f6d546dba
  1. .bcr/
  2. .github/
  3. examples/
  4. multitool/
  5. .bazelignore
  6. .bazelrc
  7. .bazelversion
  8. .gitattributes
  9. .gitignore
  10. BUILD.bazel
  11. LICENSE
  12. lockfile.schema.json
  13. MODULE.bazel
  14. readme.md
  15. WORKSPACE.bazel
readme.md

rules_multitool

An ergonomic approach to defining a single tool target that resolves to a matching os and CPU architecture variant of the tool.

Usage

For a quickstart, see the module example or workspace example.

Define a lockfile that references the tools to load:

{
  "$schema": "https://raw.githubusercontent.com/theoremlp/rules_multitool/main/lockfile.schema.json",
  "tool-name": {
    "binaries": [
      {
        "kind": "file",
        "url": "https://...",
        "sha256": "sha256 of the file",
        "os": "linux|macos",
        "cpu": "x86_64|arm64"
      }
    ]
  }
}

The lockfile supports the following binary kinds:

  • file: the URL refers to a file to download

    • sha256: the sha256 of the downloaded file
  • archive: the URL referes to an archive to download, specify additional options:

    • file: executable file within the archive
    • sha256: the sha256 of the downloaded archive
  • pkg: the URL refers to a MacOS pkg archive to download, specify additional options:

    • file: executable file within the archive
    • sha256: the sha256 of the downloaded pkg archive

Bazel Module Usage

Once your lockfile is defined, load the ruleset in your MODULE.bazel and create a hub that refers to your lockfile:

bazel_dep(name = "rules_multitool", version = "0.0.0")

multitool = use_extension("@rules_multitool//multitool:extension.bzl", "multitool")
multitool.hub(lockfile = "//:multitool.lock.json")
use_repo(multitool, "multitool")

Tools may then be accessed using @multitool//tools/tool-name.

Workspace Usage

Instructions for using with WORKSPACE may be found in release notes.

Running tools in the current working directory

When running @multitool//tools/tool-name, Bazel will execute the tool at the root of the runfiles tree due to https://github.com/bazelbuild/bazel/issues/3325.

To run a tool in the current working directory, use the convenience target @multitool//tools/tool-name:cwd.

A common pattern we recommend to further simplify invoking tools for repository users it to:

  1. Create a tools/ directory
  2. Create an executable shell script tools/_run_multitool.sh with the following code:
    #!/usr/bin/env bash
    bazel run "@multitool//tools/$( basename $0 ):cwd" -- "$@"
    
  3. Create symlinks of tools/tool-name to tools/_run_multitool.sh