KeiSeiKit-1.0/install/lib-profile.sh
Parfii-bot 03d1dc7362 refactor(v0.16): split install.sh monolith (1238 LOC) into 17 cubes
Constructor Pattern (RULE ZERO). Zero behaviour change, zero flag
drift — all original CLI flags preserved verbatim.

Before: install.sh — 1238 LOC monolith
After:  install.sh — 138 LOC dispatcher (sources libs in order)
        install/lib-*.sh — 16 cubes, max 183 LOC (lib-menu)

Cubes:
  lib-log       21 LOC — logging primitives
  lib-backup    63 LOC — rollback trap + BACKUP_PAIRS
  lib-profile  115 LOC — MANIFEST.toml profile resolution
  lib-args      92 LOC — CLI parsing + --help heredoc
  lib-menu     183 LOC — whiptail/dialog/plain-text interactive picker
  lib-plan     150 LOC — dry-run --no-execute output
  lib-prereqs   91 LOC — hard + soft dependency checks
  lib-primitives 131 LOC — primitive copy + MANIFEST drive
  lib-rust     114 LOC — cargo workspace build + pre-built support
  lib-scaffold 144 LOC — agent/skill/block scaffolding
  lib-bridges   31 LOC — project-bridge install
  lib-hooks    104 LOC — settings.json jq merge
  lib-agents    77 LOC — assembled agent output
  lib-skills    23 LOC — skill copy
  lib-wizard    20 LOC — sleep-setup wizard invocation
  lib-summary   59 LOC — post-install summary

Invariants preserved:
  - macOS bash 3.2 compat (no associative arrays, no [[ ]], no ${,,})
  - rollback trap wired via setup_backup_trap early in dispatcher
  - jq-merge behaviour verbatim in lib-hooks
  - scoped Cargo.toml regeneration in lib-rust

Function LOC limits: largest non-heredoc fn 22 LOC (check_soft_prereqs).
Three functions kept >30 LOC because heredoc-dominated (print_help,
print_summary, profile_members); splitting would fragment logical unit.

62 unique function names across cubes, zero duplicates (grep-verified).

bash -n passes on all 17 files. Runtime smoke test deferred to user's
shell (bash-readonly sandbox constraint).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 15:09:35 +08:00

115 lines
3.1 KiB
Bash

# shellcheck shell=bash
# lib-profile.sh — MANIFEST.toml parser + profile resolver.
#
# Tiny awk-based TOML reader with optional Python fallback for robustness.
# Two shapes used:
# 1. profile.<name> = ["a", "b", ...]
# 2. [primitive.<name>] kind/file/crate/deps/desc
#
# If tomllib (python3.11+) or toml is available, prefer it. Otherwise awk.
#
# Requires: $MANIFEST (set by install.sh).
# Requires: err from lib-log.sh.
have_python_toml() {
if command -v python3 >/dev/null 2>&1; then
python3 -c 'import tomllib' >/dev/null 2>&1 && return 0
python3 -c 'import toml' >/dev/null 2>&1 && return 0
fi
return 1
}
# Echo space-separated primitive names for a given profile.
# Usage: profile_members <profile-name>
profile_members() {
local profile="$1"
[ -f "$MANIFEST" ] || { err "MANIFEST.toml not found at $MANIFEST"; return 1; }
if have_python_toml; then
python3 - "$MANIFEST" "$profile" <<'PY' 2>/dev/null || return 1
import sys
try:
import tomllib
mode = "rb"
except ImportError:
import toml as tomllib
mode = "r"
path, prof = sys.argv[1], sys.argv[2]
with open(path, mode) as f:
data = tomllib.load(f) if mode == "rb" else tomllib.load(f)
members = data.get("profile", {}).get(prof)
if members is None:
sys.exit(2)
print(" ".join(members))
PY
else
# awk fallback — only handles `profile.<name> = [...]` on one line
awk -v prof="$profile" '
/^\[profile\]/ { in_profile=1; next }
/^\[/ && !/^\[profile\]/ { in_profile=0 }
in_profile && $0 ~ "^[[:space:]]*" prof "[[:space:]]*=" {
line = $0
sub(/^[^\[]*\[/, "", line)
sub(/\].*$/, "", line)
gsub(/"/, "", line)
gsub(/,/, " ", line)
print line
exit
}
' "$MANIFEST"
fi
}
# Echo a field of a primitive. Usage: primitive_field <name> <field>
# field ∈ { kind, file, crate, desc, deps }
primitive_field() {
local name="$1" field="$2"
[ -f "$MANIFEST" ] || return 1
if have_python_toml; then
python3 - "$MANIFEST" "$name" "$field" <<'PY' 2>/dev/null
import sys
try:
import tomllib
mode = "rb"
except ImportError:
import toml as tomllib
mode = "r"
path, name, field = sys.argv[1], sys.argv[2], sys.argv[3]
with open(path, mode) as f:
data = tomllib.load(f) if mode == "rb" else tomllib.load(f)
p = data.get("primitive", {}).get(name)
if p is None:
sys.exit(2)
v = p.get(field, "")
if isinstance(v, list):
print("; ".join(v))
else:
print(v)
PY
else
awk -v pname="$name" -v fname="$field" '
$0 ~ "^\\[primitive\\." pname "\\]" { in_p=1; next }
/^\[/ && in_p { in_p=0 }
in_p && $0 ~ "^[[:space:]]*" fname "[[:space:]]*=" {
line = $0
sub(/^[^=]*=[[:space:]]*/, "", line)
gsub(/^"/, "", line)
gsub(/"$/, "", line)
print line
exit
}
' "$MANIFEST"
fi
}
# Echo all primitive names defined in MANIFEST.
all_primitive_names() {
[ -f "$MANIFEST" ] || return 1
awk '
/^\[primitive\./ {
name = $0
sub(/^\[primitive\./, "", name)
sub(/\]$/, "", name)
print name
}
' "$MANIFEST"
}