Files
code-tools/terraform/scripts/dev-tools.sh
2025-09-29 14:14:30 +00:00

122 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
log() {
printf '[dev-tools] %s\n' "$1"
}
PATH="/home/coder/.venv/bin:/home/coder/.local/bin:/home/coder/bin:/home/coder/.cargo/bin:/usr/local/go/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:${PATH-}"
export PATH
GO_VERSION="${GO_VERSION:-1.25.1}"
ensure_go() {
if command -v go >/dev/null 2>&1; then
return 0
fi
local go_root="${HOME}/.local/share/go"
local go_bin="${go_root}/bin/go"
if [ -x "${go_bin}" ]; then
PATH="${go_root}/bin:${PATH}"
export PATH
mkdir -p "${HOME}/bin"
ln -sf "${go_root}/bin/go" "${HOME}/bin/go"
ln -sf "${go_root}/bin/gofmt" "${HOME}/bin/gofmt"
return 0
fi
if ! command -v curl >/dev/null 2>&1; then
log "curl not available; cannot install Go automatically"
return 1
fi
local archive="go${GO_VERSION}.linux-amd64.tar.gz"
local url="https://go.dev/dl/${archive}"
local tmp_dir
tmp_dir=$(mktemp -d)
log "Installing Go ${GO_VERSION} locally (missing from base image)"
if curl -fsSL "${url}" -o "${tmp_dir}/${archive}" && tar -C "${tmp_dir}" -xzf "${tmp_dir}/${archive}"; then
local base_dir
base_dir="$(dirname "${go_root}")"
mkdir -p "${base_dir}"
rm -rf "${go_root}"
mv "${tmp_dir}/go" "${go_root}"
PATH="${go_root}/bin:${PATH}"
export PATH
mkdir -p "${HOME}/bin"
ln -sf "${go_root}/bin/go" "${HOME}/bin/go"
ln -sf "${go_root}/bin/gofmt" "${HOME}/bin/gofmt"
rm -rf "${tmp_dir}"
return 0
fi
log "Failed to install Go ${GO_VERSION}; please rebuild the base image"
rm -rf "${tmp_dir}"
return 1
}
ensure_go || true
NVM_DIR="${NVM_DIR:-/usr/local/share/nvm}"
if [ -d "$NVM_DIR" ]; then
if [ -s "$NVM_DIR/nvm.sh" ]; then
# shellcheck disable=SC1090,SC1091
. "$NVM_DIR/nvm.sh" >/dev/null 2>&1 || true
fi
shopt -s nullglob
node_bins=("$NVM_DIR"/versions/node/*/bin)
shopt -u nullglob
if [ ${#node_bins[@]} -gt 0 ]; then
for node_bin in "${node_bins[@]}"; do
PATH="$node_bin:$PATH"
done
fi
fi
export PATH
tools=(node npm pnpm yarn python3 uv go rustc cargo)
failure=0
for tool in "${tools[@]}"; do
if command -v "$tool" >/dev/null 2>&1; then
case "$tool" in
go)
version_output=$(go version 2>/dev/null | head -n 1)
;;
*)
version_output=$("$tool" --version 2>/dev/null | head -n 1)
;;
esac
if [ -n "$version_output" ]; then
log "$tool available: $version_output"
else
log "$tool available"
fi
if [ "$tool" = "python3" ]; then
py_minor=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
if python3 -c 'import sys; exit(0) if sys.version_info >= (3, 12) else exit(1)'; then
log "python3 meets minimum version requirement (>= 3.12)"
else
log "python3 version ${py_minor} is below required 3.12; update the base image"
failure=1
fi
fi
else
log "$tool not found in PATH"
failure=1
fi
done
if [ "$failure" -eq 1 ]; then
log "Developer tooling validation failed"
exit 1
fi
log "Developer tooling check completed."