Skip to content

Commit

Permalink
devenv: Use tools from environment, where possible
Browse files Browse the repository at this point in the history
Initially, I had wanted to also use the devcontainer in the CI pipelines
so that there was a consistent environment used everywhere.

However, the devcontainer is built with a `vscode` user so all tools
live under `/home/vscode/.local/bin`. But when the container is run
by Github Actions, `HOME` is set to something like `/github/home` so
none of the pre-installed tools are found!

Not only does this lead to re-installing all the required tools with
each build, scripts like `make_release.py` break as they expect tools
like `hatch` to be sitting on the `PATH`.

I did find that there is a `devcontainers/ci` action available which
looks like it could work, but has its own set of trade-offs with would
require re-designing the pipelines altogether...

Instead, this commit introduces a pattern which *assuming it works*
should lead to a much nicer experience all around:

   TOOL ?= $(or $(shell command v tool), $(BIN)/tool)

where

- `?=` means the expression is only evaluated if `TOOL` is not already
  set. Allowing someone to override it at invocation time e.g.
  `TOOL=/path/to/tool make <target>`

- `$(shell command -v tool)` will resolve to the full path to the `tool`
  executable, if it exists on the user's `PATH`

- `$(BIN)/tool`, being the second option passed to `$(or` this will be
  the fallback option if the user does not already have `tool` available
  on their `PATH`

When coupled with the `$(TOOL):` rules, this has the effect of
installing the given tool, only if the user does not have a version of
it installed already.

Hopefully, this should mean that the `Makefiles` will be useful both
inside and outside of a devcontainer!
  • Loading branch information
alcarney committed May 2, 2024
1 parent a6e49a5 commit 334e38b
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions .devcontainer/tools.mk
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
BIN:=$(HOME)/.local/bin
BIN ?= $(HOME)/.local/bin

PY38 := $(BIN)/python3.8
PY39 := $(BIN)/python3.9
PY310 := $(BIN)/python3.10
PY311 := $(BIN)/python3.11
PY312 := $(BIN)/python3.12
PY38 ?= $(or $(shell command -v python3.8), $(BIN)/python3.8)
PY39 ?= $(or $(shell command -v python3.9), $(BIN)/python3.9)
PY310 ?= $(or $(shell command -v python3.10), $(BIN)/python3.10)
PY311 ?= $(or $(shell command -v python3.11), $(BIN)/python3.11)
PY312 ?= $(or $(shell command -v python3.12), $(BIN)/python3.12)

# Set a default python
PY := $(BIN)/python
PY ?= $(or $(shell command -v python), $(BIN)/python)
PY_INTERPRETERS := $(PY) $(PY38) $(PY39) $(PY310) $(PY311) $(PY312)

HATCH := $(BIN)/hatch
HATCH ?= $(or $(shell command -v hatch), $(BIN)/hatch)
HATCH_VERSION = 1.9.7
PRE_COMMIT := $(BIN)/pre-commit
TOWNCRIER := $(BIN)/towncrier
PRE_COMMIT ?= $(or $(shell command -v pre-commit), $(BIN)/pre-commit)
TOWNCRIER ?= $(or $(shell command -v towncrier), $(BIN)/towncrier)

PY_TOOLS := $(HATCH) $(PRE_COMMIT) $(TOWNCRIER)

# Node JS
NPM := $(BIN)/npm
NODE := $(BIN)/node
NPM ?= $(or $(shell command -v npm), $(BIN)/npm)
NODE ?= $(or $(shell command -v node), $(BIN)/node)
NODE_VERSION := 18.20.2
NODE_DIR := $(HOME)/.local/node

Expand Down

0 comments on commit 334e38b

Please sign in to comment.