KeiSeiKit-1.0/install/lib-profile.sh
KeiSei84 742822a499 feat: opt-in hook packs + stack profiles + public-prep repoint (#44)
Mirror of keigit main — Phase 2 (abae256c) + public-prep repoint (518d95df).

Phase 2: safety on by default, discipline packs opt-in; stack profiles
(minimal/web/ml/systems/mobile) pull packs + agent sets; SSoT in
_primitives/hook-packs.toml; filter+prune via lib-hooks.sh; re-runnable
via `kei configure`; 8 hooks gated via _lib/gate.sh.

Public-prep: .gitmodules + README clone + plugin homepage + web-install.sh
repointed to github.com/KeiSeiLab. ADR in DECISIONS.md 2026-05-25.
2026-05-26 13:26:09 +07:00

125 lines
3.3 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
}
# Generic one-line-array TOML reader. Echoes space-separated values of
# [<table>]
# <key> = ["a", "b", ...]
# python-tomllib preferred; awk fallback handles one-line arrays only.
# Usage: _toml_array <file> <table> <key>
_toml_array() {
local file="$1" table="$2" key="$3"
[ -f "$file" ] || return 1
if have_python_toml; then
python3 - "$file" "$table" "$key" <<'PY' 2>/dev/null || return 1
import sys
try:
import tomllib
mode = "rb"
except ImportError:
import toml as tomllib
mode = "r"
path, table, key = sys.argv[1], sys.argv[2], sys.argv[3]
with open(path, mode) as f:
data = tomllib.load(f)
vals = data.get(table, {}).get(key)
if vals is None:
sys.exit(2)
print(" ".join(vals))
PY
else
awk -v table="$table" -v key="$key" '
$0 ~ "^\\[" table "\\]" { in_t=1; next }
/^\[/ { in_t=0 }
in_t && $0 ~ "^[[:space:]]*" key "[[:space:]]*=" {
line = $0
sub(/^[^\[]*\[/, "", line)
sub(/\].*$/, "", line)
gsub(/"/, "", line)
gsub(/,/, " ", line)
print line
exit
}
' "$file"
fi
}
# 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; }
_toml_array "$MANIFEST" "profile" "$profile"
}
# 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"
}