Skip to content

Commit 0a54acb

Browse files
committed
Add unified installation contract primitives
1 parent 7a2e641 commit 0a54acb

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

scripts/install-contract.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env bash
2+
# NWE Unified Installation Contract
3+
# Shared primitives for every installer/module.
4+
# Contract: resolve root -> validate -> plan -> change -> verify -> accurate exit.
5+
set -euo pipefail
6+
7+
NWE_INSTALL_CONTRACT_VERSION="1.0"
8+
9+
nwe_contract_root() {
10+
local source_file="${1:-${BASH_SOURCE[1]}}"
11+
cd "$(dirname "$source_file")/.." && pwd
12+
}
13+
14+
nwe_require_command() {
15+
command -v "$1" >/dev/null 2>&1 || {
16+
echo "[FAIL] Required command not found: $1" >&2
17+
return 1
18+
}
19+
}
20+
21+
nwe_is_root() { [[ "$(id -u)" -eq 0 ]]; }
22+
23+
nwe_run_privileged() {
24+
if nwe_is_root; then
25+
"$@"
26+
else
27+
command -v sudo >/dev/null 2>&1 || { echo "[FAIL] sudo is required: $*" >&2; return 1; }
28+
sudo "$@"
29+
fi
30+
}
31+
32+
nwe_step() { echo "[NWE] $*"; }
33+
nwe_warn() { echo "[WARN] $*" >&2; }
34+
nwe_fail() { echo "[FAIL] $*" >&2; return 1; }
35+
36+
nwe_require_linux() {
37+
[[ "$(uname -s)" == "Linux" ]] || nwe_fail "This operation requires Linux."
38+
}
39+
40+
nwe_verify_path() {
41+
[[ -e "$1" ]] || nwe_fail "Expected path does not exist: $1"
42+
}
43+
44+
nwe_source_optional() {
45+
local file="$1"
46+
if [[ -f "$file" ]]; then
47+
# shellcheck source=/dev/null
48+
source "$file"
49+
return 0
50+
fi
51+
return 1
52+
}
53+
54+
nwe_module_root() {
55+
local install_dir="$1"
56+
cd "$install_dir/.." && pwd
57+
}
58+
59+
nwe_repo_root_from_module() {
60+
local module_root="$1"
61+
cd "$module_root/../.." && pwd
62+
}
63+
64+
nwe_verify_module() {
65+
local module_root="$1"
66+
nwe_verify_path "$module_root"
67+
[[ -d "$module_root/source" || -d "$module_root/servlets" ]] || \
68+
nwe_fail "Module does not contain expected source/servlets directories: $module_root"
69+
}
70+
71+
nwe_install_contract_banner() {
72+
echo "NWE Unified Installation Contract v$NWE_INSTALL_CONTRACT_VERSION"
73+
echo " root: ${NWE_ROOT:-unknown}"
74+
}

0 commit comments

Comments
 (0)