wake-alarm/scripts/pinned_flutter.sh
Krzysztof kuhy Rudnicki 3b7f561452
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / free-days-interop (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Run the .fvmrc Flutter in repo scripts
The free-day hook ran the system flutter (3.47.9). On a fresh
checkout its pub get rewrote pubspec.lock to its own test_api, so
pre-commit failed with "files were modified". Hooks and
test_changed.sh now put the pinned fvm SDK on PATH.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:10:57 +01:00

68 lines
2.7 KiB
Bash

#!/bin/bash
# ============================================================================
# Source this to make `flutter` mean the SDK pinned in the repo's .fvmrc.
#
# Why: a bare `flutter` is whatever is first on PATH (here the system
# /usr/bin/flutter). When that SDK differs from .fvmrc, its implicit
# `pub get` re-resolves phone_app/pubspec.lock against its own pinned
# test_api and the hook "modifies files" on a fresh checkout. The fix is to
# run the pinned SDK, never to commit a lock for whichever SDK ran last.
#
# With fvm: the pinned SDK's bin goes on PATH (installed on first use),
# placed after any leading */shims entries so build-lock shims still wrap it.
# Without fvm (CI installs the pinned version itself): fail fast on a
# mismatch instead of silently rewriting the lock.
#
# Usage: source "$REPO_DIR/scripts/pinned_flutter.sh" && use_pinned_flutter "$REPO_DIR"
# Requires: jq; fvm when the active flutter is not already the pinned one.
# ============================================================================
# pinned_flutter_version REPO_DIR: print the version pinned in .fvmrc.
pinned_flutter_version() {
local fvmrc="$1/.fvmrc"
[[ -f "$fvmrc" ]] || { echo "Error: no $fvmrc" >&2; return 1; }
jq -er '.flutter' "$fvmrc" || { echo "Error: no .flutter in $fvmrc" >&2; return 1; }
}
# path_with_bin BIN: print PATH with BIN inserted after leading */shims dirs.
path_with_bin() {
local bin="$1" out="" placed=0 part
local -a parts
IFS=':' read -r -a parts <<<"$PATH"
for part in "${parts[@]}"; do
if [[ $placed -eq 0 && $part != */shims ]]; then
out="${out:+$out:}$bin"
placed=1
fi
[[ $part == "$bin" ]] && continue
out="${out:+$out:}$part"
done
[[ $placed -eq 1 ]] || out="${out:+$out:}$bin"
printf '%s\n' "$out"
}
# use_pinned_flutter REPO_DIR: export PATH so `flutter` is the pinned SDK.
use_pinned_flutter() {
local version cache bin
version="$(pinned_flutter_version "$1")" || return 1
if command -v fvm >/dev/null; then
cache="$(fvm api context | jq -er '.context.config.cachePath')" || {
echo "Error: fvm reported no cache path" >&2
return 1
}
bin="$cache/versions/$version/bin"
if [[ ! -x "$bin/flutter" ]]; then
fvm install "$version" >&2 || return 1
fi
PATH="$(path_with_bin "$bin")"
export PATH
return 0
fi
local active
active="$(flutter --version --machine 2>/dev/null | jq -r '.frameworkVersion')"
if [[ "$active" != "$version" ]]; then
echo "Error: flutter $active is active but .fvmrc pins $version (install fvm)" >&2
return 1
fi
}