Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .devcontainer/init.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
!/usr/bin/env bash
#!/usr/bin/env bash

# Setup script run once for new devcontainers to init the environment.

set -euo pipefail

curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash

echo "PATH=\"\$(make -s tools-path):\${PATH}\"" >> ~/.zshrc
echo "PATH=\"\$(make -s tools-path):\${PATH}\"" >> ~/.bashrc
echo "PATH=\"\$(make -s tools-path):\${PATH}\"" | tee -a ~/.zshrc ~/.bashrc >/dev/null
echo "source \"$(pwd)/build/scripts/completions.sh\"" | tee -a ~/.zshrc ~/.bashrc >/dev/null

make tools wit

Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build/ synced, never edited in the repository
VERSION the release synced, e.g. v0.2.0
MANIFEST a checksum of each synced file, checked by `make check-build`
build.mk the targets every repository has, includes the other make files
tools.mk wit.mk components.mk publish.mk sync.mk pinned.jq local.jq
tools.mk wit.mk components.mk publish.mk sync.mk pinned.jq local.jq completions.jq
tools/Cargo.toml the tools the build provides, see Tools
scripts/ publish.sh, bump-version.sh, update-build.sh, ...
.github/workflows/ ci.yaml, bump-version.yaml, release.yaml, update-build.yaml: synced
Expand Down Expand Up @@ -67,6 +67,15 @@ A repository changes a tool the build provides, or adds its own, without changin
- **A binary built elsewhere**, to try a change: set its variable on the command line, e.g. `make WKG=../wkg/target/release/wkg`. The scripts the recipes run find the installed tools on the `PATH`.
- **Its own tools**, e.g. `wasmtime-cli` for its tests: pin them in `tools/Cargo.toml` like an override. A repository with no tools of its own needs no `tools/Cargo.toml`, nor its dependabot entry.

The tools complete in bash and zsh once their completions are registered at the shell's startup, after the tools are on the `PATH`:

```sh
PATH="$(make -s tools-path):${PATH}"
source /path/to/repository/build/scripts/completions.sh
```

A binary that prints its completions is listed in `[package.metadata.completions]` with the arguments that print them, the shell's name appended, e.g. `wasm-tools = ["completion"]` for `wasm-tools completion bash`. The build lists those of the tools it provides, cosign too, and a repository lists its own tools in its `tools/Cargo.toml`, or overrides one of the build's, `[]` for none. `make tools` generates `target/tools/completions` from them, read as each shell starts, a tool not installed yet is skipped.

## Targets

`make` runs `tools`, `generate`, `components` and `test`. Other targets: `wit`, `publish`, `tools-path`, `clean`, `clean-all`, `update-build`, `check-build`, `print-<variable>`.
Expand Down
15 changes: 15 additions & 0 deletions build/completions.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# generates target/tools/completions from `cargo metadata --no-deps` of build/tools/Cargo.toml,
# followed by that of the repository's tools/Cargo.toml when it has one, slurped, see build/tools.mk.
# Each line is a binary and the arguments that print its completions, quoted for the shell, e.g.
# `'wasm-tools' 'completion'`, read by build/scripts/completions.sh. A binary the repository lists
# overrides the build's, one listed with no arguments has none.
def completions($manifest):
(.packages[0].metadata.completions // {})
| to_entries[]
| if (.value | type) == "array" and all(.value[]; type == "string") then .
else error("\(.key) in [package.metadata.completions] in \($manifest) is not a list of arguments") end;
([.[0] | completions("build/tools/Cargo.toml")] + [.[1] // empty | completions("tools/Cargo.toml")])
| reduce .[] as $binary ({}; .[$binary.key] = $binary.value)
| to_entries | sort_by(.key)[]
| select(.value != [])
| [.key] + .value | @sh
40 changes: 40 additions & 0 deletions build/scripts/completions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# shellcheck shell=bash
# Registers the shell completions of the installed tools, sourced by bash or zsh at startup once the
# tools are on the PATH, e.g. in ~/.bashrc:
#
# PATH="$(make -s tools-path):${PATH}"
# source /path/to/repository/build/scripts/completions.sh
#
# The tools that have completions, and how each prints them, are listed in
# [package.metadata.completions] of build/tools/Cargo.toml and the repository's tools/Cargo.toml,
# generated into target/tools/completions by `make tools`. Generated as each shell starts, so a tool
# installed or bumped since completes in new shells. A tool that isn't installed is skipped, as is
# one that fails to print its completions.

# the arguments are the ones each line of completions sets
# shellcheck disable=SC2120
__componentized_completions() {
local script completions shell line
if [ -n "${ZSH_VERSION:-}" ]; then
# zsh's path of the sourced script, BASH_SOURCE in bash
# shellcheck disable=SC2296
script="${(%):-%x}"
shell=zsh
else
script="${BASH_SOURCE[0]}"
shell=bash
fi
completions="$(cd "$(dirname "$script")/../.." && pwd)/target/tools/completions"
[ -f "$completions" ] || return 0
while IFS= read -r line; do
# the binary and its arguments, quoted by build/completions.jq
eval "set -- $line"
command -v "$1" > /dev/null 2>&1 || continue
# only a script the tool printed, one that fails may print its usage to stdout, which isn't
# run, and none reads the terminal, which would hang the shell
line=$("$@" "$shell" < /dev/null 2> /dev/null) || continue
[ -n "$line" ] && eval "$line"
done < "$completions"
}
__componentized_completions
unset -f __componentized_completions
12 changes: 12 additions & 0 deletions build/tools.mk
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ $(TOOLS_MK): $(TOOLS_MANIFESTS) build/pinned.jq
@echo "TOOLS_MANIFESTS_PINNED := $(TOOLS_MANIFESTS)" >> $@.tmp
@mv $@.tmp $@

# the binaries that print shell completions and their arguments, read by build/scripts/completions.sh
# as a shell starts, see [package.metadata.completions] in build/tools/Cargo.toml. Generated with the
# tools, the repository's tools/Cargo.toml is read last, overriding the build's.
TOOLS_COMPLETIONS := target/tools/completions

tools: $(TOOLS_COMPLETIONS)

$(TOOLS_COMPLETIONS): $(TOOLS_MANIFESTS) build/completions.jq
@mkdir -p $(@D)
@set -o pipefail; for manifest in $(TOOLS_MANIFESTS); do cargo metadata --manifest-path $$manifest --no-deps --format-version 1; done | jq -rs -f build/completions.jq > $@.tmp
@mv $@.tmp $@

# the packages of the workspace, generated again when LOCAL_TOOLS changes. The workspace's members may
# change without changing Cargo.toml or Cargo.lock, `make clean-tools` generates it again.
$(LOCAL_TOOLS_MK): Cargo.toml Cargo.lock $(filter-out $(TOOLS_MK) $(LOCAL_TOOLS_MK),$(MAKEFILE_LIST)) build/local.jq
Expand Down
9 changes: 9 additions & 0 deletions build/tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ wasm-opt = ["wasm-opt"]
wasm-tools = ["wasm-tools"]
wkg = ["wkg"]

# the arguments of the binaries that print their shell completions, the shell's name is appended,
# e.g. `wasm-tools completion bash`, registered by build/scripts/completions.sh. A binary installed
# other than with cargo is listed too, e.g. cosign, see build/tools.mk. A repository adds its own
# in its tools/Cargo.toml, or overrides one with `[]`, e.g. for a tool it builds without them.
[package.metadata.completions]
componentized-build = ["completions"]
cosign = ["completion"]
wasm-tools = ["completion"]

[dependencies]
cargo-cyclonedx = "=0.5.10"
componentized-build-cli = "=0.1.6"
Expand Down
4 changes: 4 additions & 0 deletions tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ path = "lib.rs"
[package.metadata.bins]
wasmtime-cli = ["wasmtime"]

# the arguments of the binaries that print their shell completions, see build/tools/Cargo.toml
[package.metadata.completions]
wasmtime = ["completion"]

[dependencies]
wasmtime-cli = "=50.0.1"

Expand Down
Loading