valise-sh 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/CHANGELOG.md +152 -2
  2. package/README.md +99 -146
  3. package/bin/valise +57 -5376
  4. package/docs/architecture.md +77 -121
  5. package/docs/contrib-modules.md +172 -218
  6. package/docs/installation.md +137 -83
  7. package/docs/module-abi.md +125 -126
  8. package/docs/projects.md +249 -0
  9. package/docs/release-policy.md +68 -93
  10. package/docs/shared-modules.md +40 -39
  11. package/examples/README.md +12 -11
  12. package/examples/e2e/README.md +59 -66
  13. package/examples/e2e/bundles.tsv +6 -0
  14. package/examples/e2e/module.d/e2e_aliases.meta +1 -2
  15. package/examples/e2e/module.d/e2e_completions_bash.meta +0 -1
  16. package/examples/e2e/module.d/e2e_completions_zsh.meta +0 -1
  17. package/examples/e2e/module.d/e2e_env_setup.meta +2 -3
  18. package/examples/e2e/module.d/e2e_fish_features.meta +0 -1
  19. package/examples/e2e/module.d/e2e_generated_config.meta +1 -2
  20. package/examples/e2e/module.d/e2e_ksh_features.meta +0 -1
  21. package/examples/e2e/module.d/e2e_layer_probe.meta +1 -2
  22. package/examples/e2e/module.d/e2e_optional_network.meta +1 -2
  23. package/examples/e2e/module.d/e2e_platform_tools.meta +8 -0
  24. package/examples/e2e/module.d/e2e_platform_tools_bsd.meta +2 -3
  25. package/examples/e2e/module.d/e2e_platform_tools_darwin.meta +2 -3
  26. package/examples/e2e/module.d/e2e_platform_tools_linux.meta +2 -3
  27. package/examples/e2e/valise.conf +4 -0
  28. package/examples/e2e/variants.tsv +3 -0
  29. package/examples/local/module.d/hello_valise.meta +1 -2
  30. package/examples/profile-project/README.md +78 -108
  31. package/examples/profile-project/bundles.tsv +8 -0
  32. package/examples/profile-project/{shell/layers → layers}/demo/module.d/contrib_demo_greeting.meta +1 -2
  33. package/examples/profile-project/{shell/module.d → module.d}/greeting.meta +1 -2
  34. package/examples/profile-project/{shell/module.d → module.d}/profile_main.meta +0 -1
  35. package/examples/profile-project/{shell/modules → modules}/greeting.sh +1 -1
  36. package/examples/profile-project/valise.conf +4 -0
  37. package/examples/profile-project/variants.tsv +3 -0
  38. package/examples/shared/README.md +13 -34
  39. package/examples/shared/module.d/contrib_shared_paths.meta +2 -3
  40. package/examples/shared/module.d/contrib_shared_umask.meta +1 -2
  41. package/examples/shared/modules/shared/contrib_shared_paths.sh +2 -2
  42. package/lib/capabilities.sh +40 -13
  43. package/lib/cli/commands.sh +278 -0
  44. package/lib/cli/common.sh +326 -0
  45. package/lib/cli/devtools.sh +539 -0
  46. package/lib/cli/fish-hook.sh +986 -0
  47. package/lib/cli/integration.sh +337 -0
  48. package/lib/cli/lifecycle.sh +578 -0
  49. package/lib/cli/project.sh +1807 -0
  50. package/lib/cli/store.sh +1023 -0
  51. package/lib/cli/usage.sh +308 -0
  52. package/lib/compiler.awk +11 -4
  53. package/lib/fish/loader.fish +69 -9
  54. package/lib/fish/metadata.fish +49 -5
  55. package/lib/fish/profile.fish +170 -0
  56. package/lib/loader.sh +6 -8
  57. package/lib/metadata.sh +111 -5
  58. package/manifests/parser-support.tsv +2 -2
  59. package/module.d/valise_editor.meta +9 -0
  60. package/module.d/valise_history.meta +9 -0
  61. package/module.d/valise_pager.meta +9 -0
  62. package/module.d/valise_path.meta +9 -0
  63. package/module.d/{platform_bsd.meta → valise_platform_bsd.meta} +3 -4
  64. package/module.d/{platform_linux.meta → valise_platform_linux.meta} +3 -4
  65. package/module.d/{platform_macos.meta → valise_platform_macos.meta} +3 -4
  66. package/module.d/{platform_posix.meta → valise_platform_posix.meta} +2 -3
  67. package/module.d/valise_xdg.meta +9 -0
  68. package/modules/system/{contrib_editor.sh → valise_editor.sh} +2 -2
  69. package/modules/system/{contrib_history.sh → valise_history.sh} +2 -2
  70. package/modules/system/{contrib_pager.sh → valise_pager.sh} +2 -2
  71. package/modules/system/{contrib_path.sh → valise_path.sh} +3 -3
  72. package/modules/system/{contrib_xdg.sh → valise_xdg.sh} +2 -2
  73. package/package.json +91 -10
  74. package/docs/variants.md +0 -327
  75. package/entrypoints/.gitkeep +0 -0
  76. package/entrypoints/fish/profile.fish +0 -99
  77. package/examples/e2e/build-manifest.sh +0 -138
  78. package/examples/local/.gitkeep +0 -0
  79. package/examples/profile-project/shell/release/bundle-roots +0 -9
  80. package/examples/profile-project/shell/release/install-variants +0 -3
  81. package/examples/profile-project/shell/release/module-registry.tsv +0 -4
  82. package/examples/profile-project/shell/release/project.conf +0 -3
  83. package/lib/.gitkeep +0 -0
  84. package/manifests/.gitkeep +0 -0
  85. package/module.d/.gitkeep +0 -0
  86. package/module.d/contrib_editor.meta +0 -10
  87. package/module.d/contrib_history.meta +0 -10
  88. package/module.d/contrib_pager.meta +0 -10
  89. package/module.d/contrib_path.meta +0 -10
  90. package/module.d/contrib_xdg.meta +0 -10
  91. package/modules/.gitkeep +0 -0
  92. /package/examples/profile-project/{shell/layers → layers}/demo/modules/shared/contrib_demo_greeting.sh +0 -0
@@ -0,0 +1,1807 @@
1
+ # Copyright (c) 2026 Joseph Cumines
2
+ # SPDX-License-Identifier: MIT
3
+ # valise CLI: project commands: compile, registry, new, module, build, apply,
4
+ # status, explain-variant, explain-bundles, layer-lint.
5
+ # Sourced by bin/valise; not executable on its own.
6
+ #
7
+ # A valise project is the directory that holds valise.conf:
8
+ #
9
+ # valise.conf variant=, bundle=, requires_valise=
10
+ # bundles.tsv bundle<TAB>shell<TAB>space-separated roots
11
+ # variants.tsv variant<TAB>description<TAB>excluded modules
12
+ # module.d/<name>.meta primary modules; bodies resolve against the root
13
+ # layers/<p>/module.d/*.meta contribution layers; bodies resolve in layers/<p>
14
+ #
15
+ # The engine's own modules (module.d/valise_*.meta in the engine) are always
16
+ # available to a build under the registry source "valise". The registry is an
17
+ # internal, generated table; nothing in the project commits it.
18
+
19
+ VALISE_PROJECT_SHELLS="sh bash zsh ksh"
20
+
21
+ # project_find_root [start]
22
+ # Print the nearest ancestor of start (default: the working directory) that
23
+ # holds valise.conf.
24
+ project_find_root() {
25
+ _pfr_dir=${1:-}
26
+ if [ -z "$_pfr_dir" ]; then
27
+ _pfr_dir=$(pwd -P) || return 1
28
+ fi
29
+ while :; do
30
+ if [ -f "$_pfr_dir/valise.conf" ] && [ ! -L "$_pfr_dir/valise.conf" ]; then
31
+ printf '%s\n' "$_pfr_dir"
32
+ return 0
33
+ fi
34
+ [ "$_pfr_dir" != "/" ] && [ -n "$_pfr_dir" ] || return 1
35
+ _pfr_dir=$(path_parent "$_pfr_dir")
36
+ done
37
+ }
38
+
39
+ # version_at_least <have> <want>
40
+ # Succeed when the dotted numeric version have is >= want.
41
+ version_at_least() {
42
+ _val_awk=$(trusted_utility_path awk) || return 1
43
+ "$_val_awk" -v have="$1" -v want="$2" 'BEGIN {
44
+ nh = split(have, h, "."); nw = split(want, w, ".")
45
+ n = nh > nw ? nh : nw
46
+ for (i = 1; i <= n; i++) {
47
+ a = (i <= nh) ? h[i] + 0 : 0; b = (i <= nw) ? w[i] + 0 : 0
48
+ if (a > b) exit 0
49
+ if (a < b) exit 1
50
+ }
51
+ exit 0
52
+ }'
53
+ }
54
+
55
+ # project_config_load
56
+ # Find the project and read valise.conf. Sets _project_root, _project_variant,
57
+ # _project_bundle and _project_requires.
58
+ project_config_load() {
59
+ _project_root=$(project_find_root) ||
60
+ die "not inside a valise project: no valise.conf in $(pwd -P) or any parent (run 'valise new' or pass -C <dir>)"
61
+ [ -n "${HOME:-}" ] || die "HOME is required for project commands"
62
+ _project_variant=default
63
+ _project_bundle=default
64
+ _project_requires=
65
+ _project_lint_forbid=
66
+ _pc_seen=" "
67
+ while IFS= read -r _pc_line || [ -n "$_pc_line" ]; do
68
+ case "$_pc_line" in
69
+ '' | '#'*) continue ;;
70
+ *=*) ;;
71
+ *) die "valise.conf: invalid line: $_pc_line" ;;
72
+ esac
73
+ _pc_key=${_pc_line%%=*}
74
+ case "$_pc_seen" in
75
+ *" $_pc_key "*) die "valise.conf: duplicate key: $_pc_key" ;;
76
+ esac
77
+ _pc_seen="$_pc_seen$_pc_key "
78
+ case "$_pc_key" in
79
+ variant) _project_variant=${_pc_line#*=} ;;
80
+ bundle) _project_bundle=${_pc_line#*=} ;;
81
+ requires_valise) _project_requires=${_pc_line#*=} ;;
82
+ lint_layer_forbid) _project_lint_forbid=${_pc_line#*=} ;;
83
+ *) die "valise.conf: unknown key: $_pc_key (known: variant, bundle, requires_valise, lint_layer_forbid)" ;;
84
+ esac
85
+ done <"$_project_root/valise.conf"
86
+ [ -n "$_project_variant" ] || die "valise.conf: variant must not be empty"
87
+ [ -n "$_project_bundle" ] || die "valise.conf: bundle must not be empty"
88
+ if [ -n "$_project_requires" ]; then
89
+ case "$_project_requires" in
90
+ *[!0-9.]* | .* | *. | *..*) die "valise.conf: requires_valise must be a dotted version: $_project_requires" ;;
91
+ esac
92
+ version_at_least "$VALISE_VERSION" "$_project_requires" ||
93
+ die "this project requires valise >= $_project_requires (this is valise $VALISE_VERSION)"
94
+ fi
95
+ [ -f "$_project_root/bundles.tsv" ] && [ ! -L "$_project_root/bundles.tsv" ] ||
96
+ die "project has no bundles.tsv: $_project_root"
97
+ if [ -e "$_project_root/variants.tsv" ] || [ -L "$_project_root/variants.tsv" ]; then
98
+ [ -f "$_project_root/variants.tsv" ] && [ ! -L "$_project_root/variants.tsv" ] ||
99
+ die "variants.tsv must be a regular file: $_project_root/variants.tsv"
100
+ fi
101
+ unset _pc_line _pc_key _pc_seen
102
+ }
103
+
104
+ # project_source_dir <project-root> <registry-source>
105
+ # Directory a module's load, executables and tests paths resolve against.
106
+ project_source_dir() {
107
+ case "$2" in
108
+ module.d) printf '%s\n' "$1" ;;
109
+ valise) engine_root ;;
110
+ layers/*/module.d) _psd_layer=${2#layers/}; printf '%s\n' "$1/layers/${_psd_layer%/module.d}" ;;
111
+ *) return 1 ;;
112
+ esac
113
+ }
114
+
115
+ # project_release_prefix <registry-source>
116
+ # Release-relative directory holding a module's files.
117
+ project_release_prefix() {
118
+ case "$1" in
119
+ module.d) printf '%s\n' '' ;;
120
+ valise) printf '%s\n' 'valise/' ;;
121
+ layers/*/module.d) _prp_layer=${1#layers/}; printf '%s\n' "layers/${_prp_layer%/module.d}/" ;;
122
+ *) return 1 ;;
123
+ esac
124
+ }
125
+
126
+ # registry_generate <project-root> <output-file>
127
+ # Write the module registry implied by the project's and the engine's
128
+ # metadata. Columns: name version side_effect requires optional shells
129
+ # platforms exports load commands executables tests source.
130
+ registry_generate() {
131
+ _rg_root=$1
132
+ _rg_out=$2
133
+ _rg_engine=$(engine_root) || die "cannot locate the valise engine"
134
+ _rg_metadata="$_rg_engine/lib/metadata.sh"
135
+ [ -r "$_rg_metadata" ] || die "metadata library not found: $_rg_metadata"
136
+ (
137
+ export LC_ALL=C
138
+ # shellcheck disable=SC1090
139
+ . "$_rg_metadata"
140
+ printf 'name\tversion\tside_effect\trequires\toptional\tshells\tplatforms\texports\tload\tcommands\texecutables\ttests\tsource\n'
141
+ for _rg_meta in "$_rg_root"/module.d/*.meta "$_rg_root"/layers/*/module.d/*.meta "$_rg_engine"/module.d/*.meta; do
142
+ [ -f "$_rg_meta" ] || continue
143
+ case "$_rg_meta" in
144
+ "$_rg_engine"/module.d/*)
145
+ _rg_base=$_rg_engine
146
+ _rg_source=valise
147
+ ;;
148
+ *)
149
+ _rg_base=$_rg_root
150
+ _rg_source=${_rg_meta#"$_rg_root"/}
151
+ _rg_source=${_rg_source%/*}
152
+ ;;
153
+ esac
154
+ _rg_rel=${_rg_meta#"$_rg_base"/}
155
+ source_path_is_physical "$_rg_base" "$_rg_rel" || {
156
+ printf 'valise: error: metadata path contains a symlink: %s\n' "$_rg_rel" >&2
157
+ exit 1
158
+ }
159
+ case "$_rg_source" in
160
+ module.d | valise) ;;
161
+ layers/*/module.d)
162
+ _rg_layer=${_rg_source#layers/}
163
+ _rg_layer=${_rg_layer%/module.d}
164
+ case "$_rg_layer" in
165
+ '' | */* | *..* | [!A-Za-z0-9_]* | *[!A-Za-z0-9_.-]*)
166
+ printf 'valise: error: invalid layer directory name: %s\n' "$_rg_rel" >&2
167
+ exit 1
168
+ ;;
169
+ esac
170
+ ;;
171
+ *)
172
+ printf 'valise: error: unsupported metadata location: %s\n' "$_rg_rel" >&2
173
+ exit 1
174
+ ;;
175
+ esac
176
+ _rg_record=$(sp_metadata_read "$_rg_meta" 2>&1) || {
177
+ printf 'valise: error: %s: %s\n' "$_rg_rel" "${_rg_record:-invalid metadata}" >&2
178
+ exit 1
179
+ }
180
+ _rg_name= _rg_version= _rg_side_effect= _rg_requires= _rg_optional= _rg_shells=
181
+ _rg_platforms= _rg_exports= _rg_load= _rg_commands= _rg_executables= _rg_tests=
182
+ while IFS= read -r _rg_line; do
183
+ case "$_rg_line" in
184
+ name=*) _rg_name=${_rg_line#name=} ;;
185
+ version=*) _rg_version=${_rg_line#version=} ;;
186
+ side_effect=*) _rg_side_effect=${_rg_line#side_effect=} ;;
187
+ requires=*) _rg_requires=${_rg_line#requires=} ;;
188
+ optional=*) _rg_optional=${_rg_line#optional=} ;;
189
+ shells=*) _rg_shells=${_rg_line#shells=} ;;
190
+ platforms=*) _rg_platforms=${_rg_line#platforms=} ;;
191
+ exports=*) _rg_exports=${_rg_line#exports=} ;;
192
+ load=*) _rg_load=${_rg_line#load=} ;;
193
+ commands=*) _rg_commands=${_rg_line#commands=} ;;
194
+ executables=*) _rg_executables=${_rg_line#executables=} ;;
195
+ tests=*) _rg_tests=${_rg_line#tests=} ;;
196
+ esac
197
+ done <<EOF
198
+ $_rg_record
199
+ EOF
200
+ _rg_file_name=${_rg_meta##*/}
201
+ _rg_file_name=${_rg_file_name%.meta}
202
+ if [ "$_rg_name" != "$_rg_file_name" ]; then
203
+ printf 'valise: error: %s: metadata name %s does not match file name %s\n' "$_rg_rel" "$_rg_name" "$_rg_file_name" >&2
204
+ exit 1
205
+ fi
206
+ case "$_rg_source:$_rg_name" in
207
+ valise:valise_*) ;;
208
+ valise:*)
209
+ printf 'valise: error: %s: engine module %s must use the valise_ prefix\n' "$_rg_rel" "$_rg_name" >&2
210
+ exit 1
211
+ ;;
212
+ *:valise_*)
213
+ printf 'valise: error: %s: the valise_ prefix is reserved for engine modules: %s\n' "$_rg_rel" "$_rg_name" >&2
214
+ exit 1
215
+ ;;
216
+ layers/*:contrib_*) ;;
217
+ layers/*)
218
+ printf 'valise: error: %s: layer module name %s must use the contrib_ prefix\n' "$_rg_rel" "$_rg_name" >&2
219
+ exit 1
220
+ ;;
221
+ esac
222
+ _rg_dir=${_rg_rel%/module.d/*}
223
+ [ "$_rg_dir" != "$_rg_rel" ] || _rg_dir=
224
+ # Every file the module names must exist inside its own source
225
+ # directory, as a regular file reached without a symlink.
226
+ for _rg_path in $_rg_load $_rg_executables $_rg_tests; do
227
+ _rg_path_rel=${_rg_dir:+$_rg_dir/}$_rg_path
228
+ if ! source_path_is_physical "$_rg_base" "$_rg_path_rel"; then
229
+ printf 'valise: error: %s: path contains a symlink: %s\n' "$_rg_rel" "$_rg_path" >&2
230
+ exit 1
231
+ fi
232
+ if [ ! -f "$_rg_base/$_rg_path_rel" ]; then
233
+ printf 'valise: error: %s: file does not exist: %s\n' "$_rg_rel" "$_rg_path" >&2
234
+ exit 1
235
+ fi
236
+ done
237
+ printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
238
+ "$_rg_name" "$_rg_version" "$_rg_side_effect" "$_rg_requires" \
239
+ "$_rg_optional" "$_rg_shells" "$_rg_platforms" "$_rg_exports" \
240
+ "$_rg_load" "$_rg_commands" "$_rg_executables" "$_rg_tests" "$_rg_source"
241
+ done
242
+ ) >"$_rg_out" || die "cannot generate module registry"
243
+ unset _rg_root _rg_out _rg_engine _rg_metadata
244
+ }
245
+
246
+ cmd_registry() {
247
+ _registry_out=""
248
+ for _registry_arg do
249
+ case "$_registry_arg" in
250
+ --help | -h) usage_registry; return 0 ;;
251
+ --*) die "unknown registry option: $_registry_arg" ;;
252
+ *) [ -z "$_registry_out" ] || die "usage: valise registry [path]"; _registry_out=$_registry_arg ;;
253
+ esac
254
+ done
255
+ project_config_load
256
+ if [ -z "$_registry_out" ]; then
257
+ _rg_tmp=$(trusted_mktemp file "${TMPDIR:-/tmp}/valise-registry.XXXXXX") ||
258
+ die "cannot create registry scratch file"
259
+ registry_generate "$_project_root" "$_rg_tmp"
260
+ _registry_cat=$(trusted_utility_path cat) || die "no trusted cat utility available"
261
+ "$_registry_cat" "$_rg_tmp"
262
+ _registry_rm=$(trusted_utility_path rm) || die "no trusted rm utility available"
263
+ "$_registry_rm" -f "$_rg_tmp"
264
+ _rg_tmp=""
265
+ return 0
266
+ fi
267
+ _registry_out=$(path_normalize "$_registry_out") || die "cannot resolve registry output path"
268
+ if [ -e "$_registry_out" ] || [ -L "$_registry_out" ]; then
269
+ [ -f "$_registry_out" ] && [ ! -L "$_registry_out" ] || die "registry output path is not a regular file: $_registry_out"
270
+ fi
271
+ _registry_parent=$(path_parent "$_registry_out")
272
+ [ -d "$_registry_parent" ] || die "registry output directory does not exist: $_registry_parent"
273
+ _rg_tmp=$(trusted_mktemp_at "$_registry_parent" file "$_registry_parent/.valise-registry.XXXXXX") ||
274
+ die "cannot create registry staging file"
275
+ registry_generate "$_project_root" "$_rg_tmp"
276
+ _registry_chmod=$(trusted_utility_path chmod) || die "no trusted chmod utility available"
277
+ "$_registry_chmod" 0644 "$_rg_tmp" || die "cannot set registry file mode"
278
+ _registry_mv=$(trusted_utility_path mv) || die "no trusted mv utility available"
279
+ "$_registry_mv" -f "$_rg_tmp" "$_registry_out" || die "cannot publish registry: $_registry_out"
280
+ _rg_tmp=""
281
+ }
282
+
283
+ # project_bundle_roots <project-root> <shell> <bundle>
284
+ # Print the roots bundles.tsv gives the shell in the bundle (possibly none).
285
+ # An unknown bundle, an unknown shell name, or a malformed row is an error.
286
+ project_bundle_roots() {
287
+ _pbr_awk=$(trusted_utility_path awk) || return 1
288
+ "$_pbr_awk" -F '\t' -v b="$3" -v s="$2" -v file="$1/bundles.tsv" '
289
+ /^[[:space:]]*#/ || $0 ~ /^[[:space:]]*$/ { next }
290
+ !header_done { header_done = 1; if ($1 == "bundle" && $2 == "shell") next }
291
+ {
292
+ if (NF < 2 || NF > 3 || $1 == "" || $2 !~ /^(sh|bash|zsh|ksh|fish)$/) {
293
+ printf "valise: error: %s:%d: expected bundle<TAB>shell<TAB>roots with shell one of sh bash zsh ksh fish\n", file, NR > "/dev/stderr"
294
+ bad = 1; exit 1
295
+ }
296
+ if ($1 == b) {
297
+ known = 1
298
+ if ($2 == s && $3 != "") out = out (out == "" ? "" : " ") $3
299
+ }
300
+ }
301
+ END {
302
+ if (bad) exit 1
303
+ if (!known) { print "valise: error: unknown bundle: " b > "/dev/stderr"; exit 1 }
304
+ if (out != "") print out
305
+ }
306
+ ' "$1/bundles.tsv"
307
+ }
308
+
309
+ # project_bundle_names <project-root>
310
+ project_bundle_names() {
311
+ _pbn_awk=$(trusted_utility_path awk) || return 1
312
+ "$_pbn_awk" -F '\t' '
313
+ /^[[:space:]]*#/ || $0 ~ /^[[:space:]]*$/ { next }
314
+ !header_done { header_done = 1; if ($1 == "bundle" && $2 == "shell") next }
315
+ !seen[$1]++ { print $1 }
316
+ ' "$1/bundles.tsv"
317
+ }
318
+
319
+ # project_variant_field <project-root> <variant> <description|excluded>
320
+ # Print a variant's field. Without variants.tsv the only variant is default,
321
+ # which excludes nothing. An unknown or duplicated variant is an error.
322
+ project_variant_field() {
323
+ if [ ! -f "$1/variants.tsv" ]; then
324
+ [ "$2" = default ] || { printf 'valise: error: unknown variant: %s (the project has no variants.tsv)\n' "$2" >&2; return 1; }
325
+ [ "$3" != description ] || printf '%s\n' 'All modules'
326
+ return 0
327
+ fi
328
+ _pvf_awk=$(trusted_utility_path awk) || return 1
329
+ "$_pvf_awk" -F '\t' -v v="$2" -v field="$3" '
330
+ /^[[:space:]]*#/ || $0 ~ /^[[:space:]]*$/ { next }
331
+ !header_done { header_done = 1; if ($1 == "variant" || $1 == "variant_name") next }
332
+ $1 == v { count++; desc = $2; excl = $3 }
333
+ END {
334
+ if (count != 1) {
335
+ printf "valise: error: %s variant: %s\n", (count ? "duplicated" : "unknown"), v > "/dev/stderr"
336
+ exit 1
337
+ }
338
+ print (field == "description" ? desc : excl)
339
+ }
340
+ ' "$1/variants.tsv"
341
+ }
342
+
343
+ # project_variant_names <project-root>
344
+ project_variant_names() {
345
+ if [ ! -f "$1/variants.tsv" ]; then
346
+ printf '%s\n' default
347
+ return 0
348
+ fi
349
+ _pvn_awk=$(trusted_utility_path awk) || return 1
350
+ "$_pvn_awk" -F '\t' '
351
+ /^[[:space:]]*#/ || $0 ~ /^[[:space:]]*$/ { next }
352
+ !header_done { header_done = 1; if ($1 == "variant" || $1 == "variant_name") next }
353
+ { print $1 }
354
+ ' "$1/variants.tsv"
355
+ }
356
+
357
+ # project_prelude_functions
358
+ # Print the sp_cap_ functions the capability prelude defines.
359
+ project_prelude_functions() {
360
+ _ppf_prelude="$(engine_root)/lib/capabilities.sh"
361
+ [ -r "$_ppf_prelude" ] || die "capabilities prelude not found: $_ppf_prelude"
362
+ _ppf_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
363
+ _ppf_fns=$("$_ppf_awk" '
364
+ match($0, /^sp_cap_[A-Za-z0-9_]*\(\)[[:space:]]*[({]/) {
365
+ name = substr($0, 1, index($0, "(") - 1)
366
+ out = out (out == "" ? "" : " ") name
367
+ }
368
+ END { print out }
369
+ ' "$_ppf_prelude")
370
+ [ -n "$_ppf_fns" ] || die "capabilities prelude contains no sp_cap_ functions"
371
+ printf '%s\n' "$_ppf_fns"
372
+ }
373
+
374
+ # project_preprocess <registry> <shell> <seeds> <excludes> <prelude-fns>
375
+ # Print the compiler input stream for one shell. Seeds and exclusions are
376
+ # module references: name@source names one registry row exactly; a bare name
377
+ # means the primary module of that name, else the only module of that name.
378
+ # Dependencies resolve within the requiring module's source first, then to the
379
+ # only module of that name. Excluded modules are reported, not dropped, so a
380
+ # dependency on one is diagnosed by name.
381
+ project_preprocess() {
382
+ _pp_awk=$(trusted_utility_path awk) || return 1
383
+ "$_pp_awk" -F '\t' -v shell="$2" -v seeds="$3" -v excludes="$4" -v prelude="$5" '
384
+ function shell_ok(shells, n, i, v) {
385
+ n = split(shells, v, " ")
386
+ for (i = 1; i <= n; i++) {
387
+ if (v[i] == shell) return 1
388
+ if (v[i] == "posix" && shell != "fish") return 1
389
+ }
390
+ return 0
391
+ }
392
+ function resolve_ref(ref, what, count, key, found) {
393
+ if (index(ref, "@") > 0) {
394
+ if (ref in row) return ref
395
+ printf "valise: error: unknown %s: %s\n", what, ref > "/dev/stderr"
396
+ failed = 1
397
+ return ""
398
+ }
399
+ if ((ref "@module.d") in row) return ref "@module.d"
400
+ count = 0
401
+ for (key in row) if (row_name[key] == ref) { count++; found = key }
402
+ if (count == 1) return found
403
+ printf "valise: error: %s %s: %s\n", (count ? "ambiguous" : "unknown"), what, ref > "/dev/stderr"
404
+ if (count) printf "valise: error: qualify it as name@source (see valise registry)\n" > "/dev/stderr"
405
+ failed = 1
406
+ return ""
407
+ }
408
+ function resolve_dep(dep, src, count, key, found) {
409
+ if ((dep "@" src) in avail) return dep "@" src
410
+ count = 0
411
+ for (key in avail) if (row_name[key] == dep) { count++; found = key }
412
+ return count == 1 ? found : dep
413
+ }
414
+ function prefix_of(src, layer) {
415
+ if (src == "module.d") return ""
416
+ if (src == "valise") return "valise/"
417
+ layer = src; sub(/^layers\//, "", layer); sub(/\/module\.d$/, "", layer)
418
+ return "layers/" layer "/"
419
+ }
420
+ NR == 1 { next }
421
+ {
422
+ id = $1 "@" $13
423
+ row[id] = 1; row_name[id] = $1; row_src[id] = $13
424
+ row_req[id] = $4; row_opt[id] = $5; row_load[id] = $9
425
+ row_platforms[id] = $7; row_side[id] = $3; row_exports[id] = $8
426
+ if (shell_ok($6)) avail[id] = 1
427
+ }
428
+ END {
429
+ n = split(excludes, ex, " ")
430
+ for (i = 1; i <= n; i++) {
431
+ if (ex[i] == "") continue
432
+ r = resolve_ref(ex[i], "excluded module")
433
+ if (r != "") excluded[r] = 1
434
+ }
435
+ seed_out = ""
436
+ n = split(seeds, sd, " ")
437
+ for (i = 1; i <= n; i++) {
438
+ if (sd[i] == "") continue
439
+ r = resolve_ref(sd[i], "bundle root")
440
+ if (r == "" || (r in excluded)) continue
441
+ if (!(r in avail)) {
442
+ printf "valise: error: bundle root %s does not support shell %s\n", sd[i], shell > "/dev/stderr"
443
+ failed = 1
444
+ continue
445
+ }
446
+ seed_out = seed_out (seed_out == "" ? "" : " ") r
447
+ }
448
+ if (failed) exit 1
449
+ print "SEED:" seed_out
450
+ print "PRELUDE:" prelude
451
+ for (id in avail) {
452
+ if (id in excluded) { printf "EXCLUDED:%s\t%s\n", id, row_name[id]; continue }
453
+ req = ""
454
+ m = split(row_req[id], parts, " ")
455
+ for (j = 1; j <= m; j++) if (parts[j] != "") req = req (req == "" ? "" : " ") resolve_dep(parts[j], row_src[id])
456
+ opt = ""
457
+ m = split(row_opt[id], parts, " ")
458
+ for (j = 1; j <= m; j++) {
459
+ if (parts[j] == "") continue
460
+ dep = resolve_dep(parts[j], row_src[id])
461
+ if ((dep in avail) && !(dep in excluded)) opt = opt (opt == "" ? "" : " ") dep
462
+ }
463
+ load = row_load[id]
464
+ if (load != "") load = prefix_of(row_src[id]) load
465
+ printf "MOD:%s\t%s\t%s\t%s\t%s\t%s\t%s\n", id, req, load, row_platforms[id], row_side[id], row_exports[id], opt
466
+ }
467
+ }
468
+ ' "$1"
469
+ }
470
+
471
+ # project_effective_roots <registry> <shell> <roots> <excludes>
472
+ # Print the qualified roots that remain for the shell once the variant's
473
+ # exclusions apply: the seeds the compiler starts from.
474
+ project_effective_roots() {
475
+ _per_prelude=$(project_prelude_functions) || exit 1
476
+ _per_stream=$(project_preprocess "$1" "$2" "$3" "$4" "$_per_prelude") ||
477
+ die "cannot resolve the $2 module graph"
478
+ _per_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
479
+ printf '%s\n' "$_per_stream" | "$_per_awk" '/^SEED:/ { print substr($0, 6); exit }'
480
+ unset _per_prelude _per_stream _per_awk
481
+ }
482
+
483
+ # project_compile <registry> <shell> <seeds> <excludes> <output> [plan|closure]
484
+ # Compile one plan (or print its ordered closure) into output.
485
+ project_compile() {
486
+ _pcm_engine=$(engine_root) || die "cannot locate the valise engine"
487
+ _pcm_compiler="$_pcm_engine/lib/compiler.awk"
488
+ [ -r "$_pcm_compiler" ] || die "compiler not found: $_pcm_compiler"
489
+ _pcm_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
490
+ _pcm_stream="$5.stream"
491
+ _pcm_prelude=$(project_prelude_functions) || exit 1
492
+ project_preprocess "$1" "$2" "$3" "$4" "$_pcm_prelude" >"$_pcm_stream" ||
493
+ die "cannot resolve the $2 module graph"
494
+ if ! "$_pcm_awk" -v target_shell="$2" -v plan_name="$2" -v mode="${6:-plan}" -f "$_pcm_compiler" \
495
+ "$_pcm_stream" >"$5"; then
496
+ die "cannot compile the $2 module graph"
497
+ fi
498
+ _pcm_rm=$(trusted_utility_path rm) || die "no trusted rm utility available"
499
+ "$_pcm_rm" -f "$_pcm_stream" || die "cannot remove compiler scratch file"
500
+ unset _pcm_engine _pcm_compiler _pcm_awk _pcm_stream _pcm_rm _pcm_prelude
501
+ }
502
+
503
+ # project_output_check <out> <replace>
504
+ # Validate a build or compile destination. Sets _poc_existed (0 none, 1 empty
505
+ # directory, 2 non-empty directory that may be replaced).
506
+ project_output_check() {
507
+ _poc_existed=0
508
+ if [ -e "$1" ] || [ -L "$1" ]; then
509
+ [ -d "$1" ] && [ ! -L "$1" ] || die "output path is not a directory: $1"
510
+ _poc_existed=1
511
+ for _poc_entry in "$1"/* "$1"/.[!.]* "$1"/..?*; do
512
+ if [ -e "$_poc_entry" ] || [ -L "$_poc_entry" ]; then
513
+ _poc_existed=2
514
+ break
515
+ fi
516
+ done
517
+ [ "$_poc_existed" -eq 1 ] || [ "$2" -eq 1 ] || die "output directory must be empty: $1"
518
+ fi
519
+ }
520
+
521
+ # project_publish <stage> <out>
522
+ # Move a finished stage to out. A previous tree at out is renamed aside first
523
+ # and restored if the move fails, so a failed publication never loses it.
524
+ project_publish() {
525
+ _pub_mv=$(trusted_utility_path mv) || die "no trusted mv utility available"
526
+ _pub_parent=$(path_parent "$2")
527
+ case "$_poc_existed" in
528
+ 0) ;;
529
+ 1)
530
+ _pub_rmdir=$(trusted_utility_path rmdir) || die "no trusted rmdir utility available"
531
+ "$_pub_rmdir" "$2" || die "cannot clear empty output directory: $2"
532
+ ;;
533
+ 2)
534
+ _publish_previous=$(trusted_mktemp_at "$_pub_parent" dir "$_pub_parent/.valise-previous.XXXXXX") ||
535
+ die "cannot reserve a place for the previous output"
536
+ "$_pub_mv" "$2" "$_publish_previous/tree" || die "cannot move the previous output aside: $2"
537
+ ;;
538
+ esac
539
+ if ! "$_pub_mv" "$1" "$2"; then
540
+ if [ -n "${_publish_previous:-}" ] && [ ! -e "$2" ] &&
541
+ "$_pub_mv" "$_publish_previous/tree" "$2" 2>/dev/null; then
542
+ remove_tree "$_publish_previous" || :
543
+ _publish_previous=""
544
+ fi
545
+ die "cannot publish $2"
546
+ fi
547
+ if [ -n "${_publish_previous:-}" ]; then
548
+ remove_tree "$_publish_previous" || info "warning: cannot remove the previous output: $_publish_previous"
549
+ _publish_previous=""
550
+ fi
551
+ unset _pub_mv _pub_parent _pub_rmdir
552
+ }
553
+
554
+ cmd_compile() {
555
+ _co_out=""
556
+ _co_variant=""
557
+ _co_bundle=""
558
+ for _co_arg do
559
+ case "$_co_arg" in
560
+ --variant=*) _co_variant=${_co_arg#--variant=}; [ -n "$_co_variant" ] || die "variant name must not be empty" ;;
561
+ --bundle=*) _co_bundle=${_co_arg#--bundle=}; [ -n "$_co_bundle" ] || die "bundle name must not be empty" ;;
562
+ --help | -h) usage_compile; return 0 ;;
563
+ --*) die "unknown compile option: $_co_arg" ;;
564
+ *) [ -z "$_co_out" ] || die "usage: valise compile [--bundle=<name>] [--variant=<name>] <output-dir>"; _co_out=$_co_arg ;;
565
+ esac
566
+ done
567
+ [ -n "$_co_out" ] || die "usage: valise compile [--bundle=<name>] [--variant=<name>] <output-dir>"
568
+ project_config_load
569
+ [ -n "$_co_variant" ] || _co_variant=$_project_variant
570
+ [ -n "$_co_bundle" ] || _co_bundle=$_project_bundle
571
+ _co_out=$(path_normalize "$_co_out") || die "cannot resolve output path"
572
+ [ "$_co_out" != "/" ] || die "output directory must not be /"
573
+ project_output_check "$_co_out" 0
574
+ _co_excludes=$(project_variant_field "$_project_root" "$_co_variant" excluded) || die "cannot read variant $_co_variant"
575
+ _co_parent=$(path_parent "$_co_out")
576
+ _co_mkdir=$(trusted_utility_path mkdir) || die "no trusted mkdir utility available"
577
+ [ -d "$_co_parent" ] || "$_co_mkdir" -p "$_co_parent" || die "cannot create output parent: $_co_parent"
578
+ _stage=$(trusted_mktemp_at "$_co_parent" dir "$_co_parent/.valise-compile.XXXXXX") ||
579
+ die "cannot create compile staging directory"
580
+ registry_generate "$_project_root" "$_stage/.registry"
581
+ for _co_shell in $VALISE_PROJECT_SHELLS; do
582
+ _co_roots=$(project_bundle_roots "$_project_root" "$_co_shell" "$_co_bundle") ||
583
+ die "cannot read bundle $_co_bundle"
584
+ [ -n "$_co_roots" ] || continue
585
+ project_compile "$_stage/.registry" "$_co_shell" "$_co_roots" "$_co_excludes" "$_stage/$_co_shell.sh"
586
+ info "compiled plan for $_co_shell"
587
+ done
588
+ _co_rm=$(trusted_utility_path rm) || die "no trusted rm utility available"
589
+ "$_co_rm" -f "$_stage/.registry" || die "cannot remove registry scratch file"
590
+ project_publish "$_stage" "$_co_out"
591
+ _stage=""
592
+ }
593
+
594
+ # project_meta_add_require <meta> <module>
595
+ project_meta_add_require() {
596
+ _pm_meta=$1
597
+ _pm_module=$2
598
+ [ -f "$_pm_meta" ] && [ ! -L "$_pm_meta" ] || die "metadata file is missing or unsafe: $_pm_meta"
599
+ _pm_parent=$(path_parent "$_pm_meta")
600
+ _pm_tmp=$(trusted_mktemp_at "$_pm_parent" file "$_pm_parent/.valise-meta.XXXXXX") ||
601
+ die "cannot create metadata staging file"
602
+ _pm_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
603
+ "$_pm_awk" -v module="$_pm_module" '
604
+ BEGIN { have = 0 }
605
+ /^requires=/ {
606
+ count = split(substr($0, 10), parts, " ")
607
+ output = ""
608
+ seen = 0
609
+ for (i = 1; i <= count; i++) {
610
+ if (parts[i] == "") continue
611
+ if (parts[i] == module) seen = 1
612
+ output = output (output == "" ? "" : " ") parts[i]
613
+ }
614
+ if (!seen) output = output (output == "" ? "" : " ") module
615
+ print "requires=" output
616
+ have = 1
617
+ next
618
+ }
619
+ { print }
620
+ END { if (!have) print "requires=" module }
621
+ ' "$_pm_meta" >"$_pm_tmp" || die "cannot update metadata: $_pm_meta"
622
+ project_meta_publish "$_pm_meta"
623
+ }
624
+
625
+ # project_meta_remove_module <meta> <module>
626
+ project_meta_remove_module() {
627
+ _pm_meta=$1
628
+ _pm_module=$2
629
+ [ -f "$_pm_meta" ] && [ ! -L "$_pm_meta" ] || die "metadata file is missing or unsafe: $_pm_meta"
630
+ _pm_parent=$(path_parent "$_pm_meta")
631
+ _pm_tmp=$(trusted_mktemp_at "$_pm_parent" file "$_pm_parent/.valise-meta.XXXXXX") ||
632
+ die "cannot create metadata staging file"
633
+ _pm_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
634
+ "$_pm_awk" -v module="$_pm_module" '
635
+ /^(requires|optional)=/ {
636
+ key = substr($0, 1, index($0, "=") - 1)
637
+ count = split(substr($0, index($0, "=") + 1), parts, " ")
638
+ output = ""
639
+ for (i = 1; i <= count; i++) {
640
+ if (parts[i] == "" || parts[i] == module) continue
641
+ output = output (output == "" ? "" : " ") parts[i]
642
+ }
643
+ print key "=" output
644
+ next
645
+ }
646
+ { print }
647
+ ' "$_pm_meta" >"$_pm_tmp" || die "cannot update metadata: $_pm_meta"
648
+ project_meta_publish "$_pm_meta"
649
+ }
650
+
651
+ # project_meta_publish <meta>: replace meta with _pm_tmp, keeping its mode.
652
+ project_meta_publish() {
653
+ _pm_mode=$(file_mode "$1")
654
+ [ -n "$_pm_mode" ] || die "cannot read metadata mode: $1"
655
+ _pm_chmod=$(trusted_utility_path chmod) || die "no trusted chmod utility available"
656
+ "$_pm_chmod" "$_pm_mode" "$_pm_tmp" || die "cannot preserve metadata mode"
657
+ _pm_mv=$(trusted_utility_path mv) || die "no trusted mv utility available"
658
+ "$_pm_mv" "$_pm_tmp" "$1" || die "cannot publish metadata: $1"
659
+ _pm_tmp=""
660
+ unset _pm_meta _pm_module _pm_parent _pm_awk _pm_mode _pm_chmod _pm_mv
661
+ }
662
+
663
+ # project_meta_loses_all_targets <meta> <module>
664
+ # Succeed when removing module from requires= would leave no load target at all.
665
+ project_meta_loses_all_targets() {
666
+ _pl_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
667
+ "$_pl_awk" -v module="$2" '
668
+ /^load=./ { target = 1 }
669
+ /^requires=/ {
670
+ count = split(substr($0, 10), parts, " ")
671
+ for (i = 1; i <= count; i++) {
672
+ if (parts[i] == module) named = 1
673
+ else if (parts[i] != "") other = 1
674
+ }
675
+ }
676
+ END { exit (named && !other && !target) ? 0 : 1 }
677
+ ' "$1"
678
+ }
679
+
680
+ # project_release_filter <file> <column> <module> <source>
681
+ # Print a project table without the module's names in the given column. A bare
682
+ # name means the module itself; a layer module may also be named
683
+ # module@source. Exit 3 when a row that named the module is left with no names.
684
+ project_release_filter() {
685
+ _rf_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
686
+ "$_rf_awk" -F '\t' -v OFS='\t' -v column="$2" -v module="$3" -v source="$4" '
687
+ /^[[:space:]]*#/ || NF < column { print; next }
688
+ {
689
+ count = split($column, parts, " ")
690
+ output = ""
691
+ hit = 0
692
+ for (i = 1; i <= count; i++) {
693
+ if (parts[i] == "") continue
694
+ if (parts[i] == module "@" source || parts[i] == module) {
695
+ hit = 1
696
+ continue
697
+ }
698
+ output = output (output == "" ? "" : " ") parts[i]
699
+ }
700
+ if (hit) {
701
+ $column = output
702
+ if (output == "") emptied = 1
703
+ }
704
+ print
705
+ }
706
+ END { if (emptied) exit 3 }
707
+ ' "$1"
708
+ }
709
+
710
+ # project_release_strip <file> <column> <module> <source>
711
+ project_release_strip() {
712
+ [ -f "$1" ] || return 0
713
+ _rs_parent=$(path_parent "$1")
714
+ _rr_tmp=$(trusted_mktemp_at "$_rs_parent" file "$_rs_parent/.valise-table.XXXXXX") ||
715
+ die "cannot create table staging file"
716
+ _rs_status=0
717
+ project_release_filter "$1" "$2" "$3" "$4" >"$_rr_tmp" || _rs_status=$?
718
+ case "$_rs_status" in
719
+ 0 | 3) ;;
720
+ *) die "cannot update $1" ;;
721
+ esac
722
+ _rs_mode=$(file_mode "$1")
723
+ [ -n "$_rs_mode" ] || die "cannot read file mode: $1"
724
+ _rs_chmod=$(trusted_utility_path chmod) || die "no trusted chmod utility available"
725
+ "$_rs_chmod" "$_rs_mode" "$_rr_tmp" || die "cannot preserve file mode"
726
+ _rs_mv=$(trusted_utility_path mv) || die "no trusted mv utility available"
727
+ "$_rs_mv" "$_rr_tmp" "$1" || die "cannot publish $1"
728
+ _rr_tmp=""
729
+ unset _rs_parent _rs_status _rs_mode _rs_chmod _rs_mv
730
+ }
731
+
732
+ # integration_render <target> <block>
733
+ # Print the startup file as install would leave it: the block replaces the
734
+ # first managed block, or is appended when there is none. Every other byte is
735
+ # kept, so removing the block restores the file.
736
+ integration_render() {
737
+ if [ -f "$1" ]; then
738
+ _ir_awk=$(trusted_utility_path awk) || return 1
739
+ _ir_tail=$(trusted_utility_path tail) || return 1
740
+ # nonl: the file's last line has no newline. An appended block puts one
741
+ # there and carries a note line, so uninstall can take that newline away
742
+ # again. A block that replaces an existing one leaves the end of the file
743
+ # as it was.
744
+ _ir_nonl=0
745
+ [ ! -s "$1" ] || [ -z "$("$_ir_tail" -c 1 "$1")" ] || _ir_nonl=1
746
+ # The block travels through ENVIRON: awk -v would interpret its backslashes.
747
+ _VALISE_BLOCK=$2 _VALISE_NOTE=$VALISE_NONL_NOTE "$_ir_awk" -v nonl="$_ir_nonl" '
748
+ function emit(with_note, at) {
749
+ if (!with_note) { out = out ENVIRON["_VALISE_BLOCK"] "\n"; return }
750
+ at = index(ENVIRON["_VALISE_BLOCK"], "\n")
751
+ out = out substr(ENVIRON["_VALISE_BLOCK"], 1, at - 1) "\n" ENVIRON["_VALISE_NOTE"] "\n" \
752
+ substr(ENVIRON["_VALISE_BLOCK"], at + 1) "\n"
753
+ }
754
+ /^# BEGIN valise managed block$/ { inblock = 1; had_note = 0; next }
755
+ inblock && $0 == ENVIRON["_VALISE_NOTE"] { had_note = 1; next }
756
+ inblock && /^# END valise managed block$/ {
757
+ inblock = 0
758
+ if (!written) emit(had_note)
759
+ written = 1
760
+ next
761
+ }
762
+ inblock { next }
763
+ { out = out $0 "\n" }
764
+ END {
765
+ if (!written) emit(nonl)
766
+ else if (nonl) out = substr(out, 1, length(out) - 1)
767
+ printf "%s", out
768
+ }
769
+ ' "$1"
770
+ else
771
+ printf '%s\n' "$2"
772
+ fi
773
+ }
774
+
775
+ # project_write_integration <file> <block>
776
+ # Replace the valise managed block in place, or append one. A symlink belongs to
777
+ # whatever manages it, so it is reported and left alone. Unpaired markers mean the
778
+ # file was edited by hand: rewriting could delete the user's lines, so refuse.
779
+ # A file install creates is recorded so uninstall can delete it again.
780
+ project_write_integration() {
781
+ _pi_target=$1
782
+ _pi_snippet=$2
783
+ if [ -L "$_pi_target" ]; then
784
+ info "warning: $_pi_target is a symlink; add the output of 'valise init' to it yourself"
785
+ unset _pi_target _pi_snippet
786
+ return 0
787
+ fi
788
+ if [ -f "$_pi_target" ]; then
789
+ integration_markers_paired "$_pi_target" ||
790
+ die "unpaired valise managed block markers in $_pi_target (restore or remove the BEGIN and END lines, then retry)"
791
+ elif [ -e "$_pi_target" ]; then
792
+ die "startup path is not a regular file: $_pi_target"
793
+ fi
794
+ _pi_parent=$(path_parent "$_pi_target")
795
+ _pi_tmp=$(trusted_mktemp_at "$_pi_parent" file "$_pi_parent/.valise-integration.XXXXXX") ||
796
+ die "cannot create integration staging file"
797
+ integration_render "$_pi_target" "$_pi_snippet" >"$_pi_tmp" || die "cannot read startup file: $_pi_target"
798
+ if [ -f "$_pi_target" ]; then
799
+ _pi_cmp=$(trusted_utility_path cmp) || die "no trusted cmp utility available"
800
+ if "$_pi_cmp" -s "$_pi_tmp" "$_pi_target"; then
801
+ _pi_rm=$(trusted_utility_path rm) || die "no trusted rm utility available"
802
+ "$_pi_rm" -f "$_pi_tmp" || die "cannot remove unchanged integration staging file"
803
+ _pi_tmp=""
804
+ info "shell integration current: $_pi_target"
805
+ unset _pi_target _pi_snippet _pi_parent _pi_cmp _pi_rm
806
+ return 0
807
+ fi
808
+ _pi_mode=$(file_mode "$_pi_target")
809
+ [ -n "$_pi_mode" ] || die "cannot read startup file mode: $_pi_target"
810
+ else
811
+ _pi_mode=0644
812
+ startup_created_add "$_pi_target"
813
+ fi
814
+ _pi_chmod=$(trusted_utility_path chmod) || die "no trusted chmod utility available"
815
+ "$_pi_chmod" "$_pi_mode" "$_pi_tmp" || die "cannot set startup file mode"
816
+ _pi_mv=$(trusted_utility_path mv) || die "no trusted mv utility available"
817
+ "$_pi_mv" "$_pi_tmp" "$_pi_target" || die "cannot publish startup file: $_pi_target"
818
+ _pi_tmp=""
819
+ _txn_changed="$_txn_changed $_pi_target"
820
+ info "shell integration written: $_pi_target"
821
+ unset _pi_target _pi_snippet _pi_parent _pi_cmp _pi_rm _pi_mode _pi_chmod _pi_mv
822
+ }
823
+
824
+ cmd_new() {
825
+ _new_target=
826
+ for _new_arg do
827
+ case "$_new_arg" in
828
+ --help | -h) usage_new; return 0 ;;
829
+ --*) die "unknown new option: $_new_arg" ;;
830
+ *) [ -z "$_new_target" ] || die "usage: valise new [dir]"; _new_target=$_new_arg ;;
831
+ esac
832
+ done
833
+ [ -n "$_new_target" ] || _new_target=.
834
+ _new_target=$(path_normalize "$_new_target") || die "cannot resolve new project path"
835
+ [ "$_new_target" != "/" ] || die "new project path must not be /"
836
+ if [ -e "$_new_target" ] || [ -L "$_new_target" ]; then
837
+ [ -d "$_new_target" ] && [ ! -L "$_new_target" ] || die "new project path is not a directory: $_new_target"
838
+ for _new_entry in "$_new_target"/* "$_new_target"/.[!.]* "$_new_target"/..?*; do
839
+ if [ -e "$_new_entry" ] || [ -L "$_new_entry" ]; then
840
+ die "new project directory must be empty: $_new_target"
841
+ fi
842
+ done
843
+ fi
844
+ _new_mkdir=$(trusted_utility_path mkdir) || die "no trusted mkdir utility available"
845
+ "$_new_mkdir" -p "$_new_target/module.d" "$_new_target/modules" ||
846
+ die "cannot create project directories: $_new_target"
847
+ printf '%s\n' \
848
+ '# valise project configuration.' \
849
+ 'bundle=default' \
850
+ 'variant=default' \
851
+ "requires_valise=$VALISE_VERSION" \
852
+ >"$_new_target/valise.conf"
853
+ printf '%s\n' \
854
+ 'name=profile_main' \
855
+ 'version=' \
856
+ 'side_effect=read-only' \
857
+ 'requires=' \
858
+ 'optional=' \
859
+ 'shells=posix' \
860
+ 'platforms=any' \
861
+ 'exports=profile_main_loaded' \
862
+ 'load=modules/profile_main.sh' \
863
+ >"$_new_target/module.d/profile_main.meta"
864
+ printf '%s\n' \
865
+ '# Shared project initialization. Add shell functions or exports here.' \
866
+ 'profile_main_loaded() { :; }' \
867
+ >"$_new_target/modules/profile_main.sh"
868
+ "$_new_mkdir" -p "$_new_target/modules/fish" ||
869
+ die "cannot create project directories: $_new_target"
870
+ printf '%s\n' \
871
+ 'name=profile_main_fish' \
872
+ 'version=' \
873
+ 'side_effect=read-only' \
874
+ 'requires=' \
875
+ 'optional=' \
876
+ 'shells=fish' \
877
+ 'platforms=any' \
878
+ 'exports=profile_main_loaded' \
879
+ 'load=modules/fish/profile_main.fish' \
880
+ >"$_new_target/module.d/profile_main_fish.meta"
881
+ printf '%s\n' \
882
+ '# Fish project initialization. Add Fish functions here.' \
883
+ 'function profile_main_loaded; end' \
884
+ >"$_new_target/modules/fish/profile_main.fish"
885
+ printf 'variant\tdescription\texcluded\ndefault\tAll project modules\t\n' \
886
+ >"$_new_target/variants.tsv"
887
+ printf 'bundle\tshell\troots\ndefault\tsh\tprofile_main\ndefault\tbash\tprofile_main\ndefault\tzsh\tprofile_main\ndefault\tksh\tprofile_main\ndefault\tfish\tprofile_main_fish\n' \
888
+ >"$_new_target/bundles.tsv"
889
+ printf '%s\n' '/build/' >"$_new_target/.gitignore"
890
+ printf '%s\n' \
891
+ "created valise project: $_new_target" \
892
+ "next steps:" \
893
+ " cd ${_new_target#"$(pwd -P)"/}" \
894
+ " valise module new hello --exports=hello" \
895
+ " valise apply"
896
+ unset _new_target _new_arg _new_entry _new_mkdir
897
+ }
898
+
899
+ # project_default_wire_targets <project-root> <bundle>
900
+ # Print the module a new module is wired into: the single distinct root of the
901
+ # bundle across its POSIX shells. A new module is a POSIX module, so the Fish
902
+ # row never takes it. Fail, listing the candidates, when there is not exactly
903
+ # one.
904
+ project_default_wire_targets() {
905
+ _pdw_all=
906
+ for _pdw_shell in $VALISE_PROJECT_SHELLS; do
907
+ _pdw_roots=$(project_bundle_roots "$1" "$_pdw_shell" "$2") || return 1
908
+ for _pdw_root in $_pdw_roots; do
909
+ case " $_pdw_all " in
910
+ *" $_pdw_root "*) ;;
911
+ *) _pdw_all="${_pdw_all:+$_pdw_all }$_pdw_root" ;;
912
+ esac
913
+ done
914
+ done
915
+ _pdw_bundle=$2
916
+ # shellcheck disable=SC2086
917
+ set -- $_pdw_all
918
+ if [ "$#" -ne 1 ]; then
919
+ if [ "$#" -eq 0 ]; then
920
+ printf 'valise: error: bundle %s has no roots to wire the module into; use --into=<module> or --no-wire\n' "$_pdw_bundle" >&2
921
+ else
922
+ printf 'valise: error: bundle %s has several roots (%s); use --into=<module>[,<module>...] or --no-wire\n' "$_pdw_bundle" "$_pdw_all" >&2
923
+ fi
924
+ return 1
925
+ fi
926
+ printf '%s\n' "$1"
927
+ }
928
+
929
+ # project_meta_path <project-root> <module-ref>
930
+ # Print the metadata file of a project or layer module named by reference.
931
+ project_meta_path() {
932
+ _pmp_name=${2%@*}
933
+ _pmp_found=
934
+ _pmp_count=0
935
+ case "$2" in
936
+ *@*)
937
+ _pmp_source=${2#*@}
938
+ case "$_pmp_source" in
939
+ module.d | layers/*/module.d) ;;
940
+ *) printf 'valise: error: not a project module: %s\n' "$2" >&2; return 1 ;;
941
+ esac
942
+ _pmp_found="$1/$_pmp_source/$_pmp_name.meta"
943
+ [ -f "$_pmp_found" ] || { printf 'valise: error: module not found: %s\n' "$2" >&2; return 1; }
944
+ ;;
945
+ *)
946
+ if [ -f "$1/module.d/$_pmp_name.meta" ]; then
947
+ _pmp_found="$1/module.d/$_pmp_name.meta"
948
+ else
949
+ for _pmp_candidate in "$1"/layers/*/module.d/"$_pmp_name".meta; do
950
+ [ -f "$_pmp_candidate" ] || continue
951
+ _pmp_found=$_pmp_candidate
952
+ _pmp_count=$((_pmp_count + 1))
953
+ done
954
+ [ "$_pmp_count" -le 1 ] || { printf 'valise: error: ambiguous module %s; qualify it as name@layers/<provider>/module.d\n' "$2" >&2; return 1; }
955
+ [ -n "$_pmp_found" ] || { printf 'valise: error: module not found in the project: %s\n' "$2" >&2; return 1; }
956
+ fi
957
+ ;;
958
+ esac
959
+ [ ! -L "$_pmp_found" ] || { printf 'valise: error: metadata file is a symlink: %s\n' "$_pmp_found" >&2; return 1; }
960
+ printf '%s\n' "$_pmp_found"
961
+ }
962
+
963
+ cmd_module() {
964
+ _module_subcmd=${1:-}
965
+ [ -n "$_module_subcmd" ] || die "usage: valise module <new|list|remove> [args]"
966
+ case "$_module_subcmd" in
967
+ --help | -h) usage_module; return 0 ;;
968
+ new)
969
+ shift
970
+ _module_name=
971
+ _module_exports=
972
+ _module_into=
973
+ _module_wire=1
974
+ for _module_arg do
975
+ case "$_module_arg" in
976
+ --exports=*) _module_exports=${_module_arg#--exports=} ;;
977
+ --into=*) _module_into=${_module_arg#--into=}; [ -n "$_module_into" ] || die "--into needs a module name" ;;
978
+ --no-wire) _module_wire=0 ;;
979
+ --help | -h) usage_module; return 0 ;;
980
+ --*) die "unknown module new option: $_module_arg" ;;
981
+ *) [ -z "$_module_name" ] || die "usage: valise module new <name> [--exports=<list>] [--into=<module>[,...] | --no-wire]"; _module_name=$_module_arg ;;
982
+ esac
983
+ done
984
+ [ -n "$_module_name" ] || die "module new requires a module name"
985
+ [ "$_module_wire" -eq 1 ] || [ -z "$_module_into" ] || die "--into and --no-wire are mutually exclusive"
986
+ case "$_module_name" in
987
+ [abcdefghijklmnopqrstuvwxyz_]*) ;;
988
+ *) die "invalid module name: $_module_name" ;;
989
+ esac
990
+ case "$_module_name" in
991
+ *[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*) die "invalid module name: $_module_name" ;;
992
+ valise_*) die "the valise_ prefix is reserved for engine modules: $_module_name" ;;
993
+ esac
994
+ project_config_load
995
+ _module_targets=
996
+ _module_target_metas=
997
+ if [ "$_module_wire" -eq 1 ]; then
998
+ if [ -n "$_module_into" ]; then
999
+ _module_targets=$(printf '%s\n' "$_module_into" | tr ',' ' ')
1000
+ else
1001
+ _module_targets=$(project_default_wire_targets "$_project_root" "$_project_bundle") || exit 1
1002
+ fi
1003
+ for _module_target in $_module_targets; do
1004
+ _module_target_meta=$(project_meta_path "$_project_root" "$_module_target") || exit 1
1005
+ [ -w "$_module_target_meta" ] || die "metadata file is not writable: $_module_target_meta"
1006
+ _module_target_metas="$_module_target_metas $_module_target_meta"
1007
+ done
1008
+ fi
1009
+ [ -d "$_project_root/modules" ] || die "module body directory is missing: $_project_root/modules"
1010
+ [ -w "$_project_root/module.d" ] && [ -w "$_project_root/modules" ] ||
1011
+ die "module directories are not writable: $_project_root"
1012
+ _module_meta="$_project_root/module.d/$_module_name.meta"
1013
+ _module_body="$_project_root/modules/$_module_name.sh"
1014
+ [ ! -e "$_module_meta" ] && [ ! -L "$_module_meta" ] || die "module metadata already exists: $_module_name"
1015
+ [ ! -e "$_module_body" ] && [ ! -L "$_module_body" ] || die "module body already exists: $_module_name"
1016
+ _module_export_words=
1017
+ if [ -n "$_module_exports" ]; then
1018
+ for _module_export in $(printf '%s\n' "$_module_exports" | tr ',' ' '); do
1019
+ case "$_module_export" in
1020
+ [abcdefghijklmnopqrstuvwxyz_]*) ;;
1021
+ *) die "invalid export name: $_module_export" ;;
1022
+ esac
1023
+ case "$_module_export" in
1024
+ *[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*) die "invalid export name: $_module_export" ;;
1025
+ case | do | done | elif | else | esac | fi | for | if | in | then | until | while | time)
1026
+ die "invalid export name (reserved word): $_module_export"
1027
+ ;;
1028
+ esac
1029
+ _module_export_words="${_module_export_words:+$_module_export_words }$_module_export"
1030
+ done
1031
+ fi
1032
+ _module_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
1033
+ for _module_export in $_module_export_words; do
1034
+ for _module_candidate in "$_project_root"/module.d/*.meta "$_project_root"/layers/*/module.d/*.meta; do
1035
+ [ -f "$_module_candidate" ] || continue
1036
+ if "$_module_awk" -F= -v wanted="$_module_export" '
1037
+ $1 == "exports" {
1038
+ count = split(substr($0, length($1) + 2), words, " ")
1039
+ for (i = 1; i <= count; i++) if (words[i] == wanted) found = 1
1040
+ }
1041
+ END { exit found ? 0 : 1 }
1042
+ ' "$_module_candidate" >/dev/null 2>&1; then
1043
+ die "export already exists in another module: $_module_export"
1044
+ fi
1045
+ done
1046
+ done
1047
+ printf '%s\n' \
1048
+ "name=$_module_name" \
1049
+ 'version=' \
1050
+ 'side_effect=read-only' \
1051
+ 'requires=' \
1052
+ 'optional=' \
1053
+ 'shells=posix' \
1054
+ 'platforms=any' \
1055
+ "exports=$_module_export_words" \
1056
+ "load=modules/$_module_name.sh" \
1057
+ >"$_module_meta"
1058
+ {
1059
+ printf '%s\n' "# Edit this module body; valise apply reloads it."
1060
+ if [ -n "$_module_export_words" ]; then
1061
+ for _module_export in $_module_export_words; do
1062
+ printf '%s() {\n printf "%%s\\n" "hello from %s"\n}\n\n' "$_module_export" "$_module_name"
1063
+ done
1064
+ else
1065
+ printf '%s\n' "# This module has no declared exports."
1066
+ fi
1067
+ } >"$_module_body"
1068
+ if [ "$_module_wire" -eq 1 ]; then
1069
+ for _module_target_meta in $_module_target_metas; do
1070
+ project_meta_add_require "$_module_target_meta" "$_module_name"
1071
+ done
1072
+ info "created module $_module_name and wired it into $_module_targets"
1073
+ else
1074
+ info "created module $_module_name (not wired; add it to a module's requires= or a bundle root)"
1075
+ fi
1076
+ ;;
1077
+ list)
1078
+ shift
1079
+ [ "$#" -eq 0 ] || die "usage: valise module list"
1080
+ project_config_load
1081
+ _module_list_tmp=$(trusted_mktemp file "${TMPDIR:-/tmp}/valise-module-list.XXXXXX") ||
1082
+ die "cannot create module list staging file"
1083
+ registry_generate "$_project_root" "$_module_list_tmp"
1084
+ _module_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
1085
+ "$_module_awk" -F '\t' 'NR > 1 && $13 != "valise" { print $1 }' "$_module_list_tmp"
1086
+ _module_rm=$(trusted_utility_path rm) || die "no trusted rm utility available"
1087
+ "$_module_rm" -f "$_module_list_tmp" || die "cannot remove module list staging file"
1088
+ _module_list_tmp=""
1089
+ ;;
1090
+ remove)
1091
+ shift
1092
+ _module_name=${1:-}
1093
+ [ -n "$_module_name" ] || die "module remove requires a module name"
1094
+ project_config_load
1095
+ _module_meta=$(project_meta_path "$_project_root" "$_module_name") || exit 1
1096
+ _module_name=${_module_name%@*}
1097
+ _module_source=$(path_parent "$_module_meta")
1098
+ # Everything that would make the removal fail is checked here, before any
1099
+ # file changes: a half-applied removal leaves a project that cannot build.
1100
+ _module_registry_source=${_module_source#"$_project_root"/}
1101
+ _module_bundles=$_project_root/bundles.tsv
1102
+ _module_variants=$_project_root/variants.tsv
1103
+ for _module_candidate in "$_project_root"/module.d/*.meta "$_project_root"/layers/*/module.d/*.meta; do
1104
+ [ -f "$_module_candidate" ] || continue
1105
+ [ "$_module_candidate" != "$_module_meta" ] || continue
1106
+ if project_meta_loses_all_targets "$_module_candidate" "$_module_name"; then
1107
+ _module_candidate_name=${_module_candidate##*/}
1108
+ die "cannot remove $_module_name: ${_module_candidate_name%.meta} requires only this module and would have nothing to load; add another dependency or remove ${_module_candidate_name%.meta} first"
1109
+ fi
1110
+ done
1111
+ _module_status=0
1112
+ project_release_filter "$_module_bundles" 3 "$_module_name" "$_module_registry_source" >/dev/null || _module_status=$?
1113
+ case "$_module_status" in
1114
+ 0) ;;
1115
+ 3) die "cannot remove $_module_name: it is the only root of a bundle in bundles.tsv; add another root first" ;;
1116
+ *) die "cannot read $_module_bundles" ;;
1117
+ esac
1118
+ _module_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
1119
+ _module_load=$("$_module_awk" -F= '$1 == "load" { print substr($0, length($1) + 2); exit }' "$_module_meta")
1120
+ _module_body_path=
1121
+ _module_body_shared=0
1122
+ if [ -n "$_module_load" ]; then
1123
+ case "$_module_load" in
1124
+ '' | /* | . | .. | ./* | ../* | */. | */.. | */./* | */../* | *//*)
1125
+ die "invalid module load path: $_module_load"
1126
+ ;;
1127
+ esac
1128
+ _module_root=$(path_parent "$_module_source")
1129
+ source_path_is_physical "$_module_root" "$_module_load" ||
1130
+ die "module load path contains a symlink: $_module_load"
1131
+ _module_body_path="$_module_root/$_module_load"
1132
+ [ ! -d "$_module_body_path" ] || die "module load path is a directory: $_module_load"
1133
+ [ -f "$_module_body_path" ] || die "module body is missing: $_module_load"
1134
+ # Another module in the same source may load the same body. Keep it then:
1135
+ # removing the metadata is enough, and deleting the body would break the
1136
+ # module that still loads it.
1137
+ for _module_candidate in "$_module_source"/*.meta; do
1138
+ [ -f "$_module_candidate" ] || continue
1139
+ [ "$_module_candidate" != "$_module_meta" ] || continue
1140
+ _module_candidate_load=$("$_module_awk" -F= '$1 == "load" { print substr($0, length($1) + 2); exit }' "$_module_candidate")
1141
+ if [ "$_module_candidate_load" = "$_module_load" ]; then
1142
+ _module_body_shared=1
1143
+ break
1144
+ fi
1145
+ done
1146
+ [ -w "$(path_parent "$_module_body_path")" ] ||
1147
+ die "module body directory is not writable: $(path_parent "$_module_body_path")"
1148
+ fi
1149
+ [ -w "$_module_source" ] || die "module metadata directory is not writable: $_module_source"
1150
+ for _module_candidate in "$_project_root"/module.d/*.meta "$_project_root"/layers/*/module.d/*.meta; do
1151
+ [ -f "$_module_candidate" ] || continue
1152
+ _module_candidate_parent=$(path_parent "$_module_candidate")
1153
+ [ -w "$_module_candidate_parent" ] || die "metadata directory is not writable: $_module_candidate_parent"
1154
+ done
1155
+ for _module_candidate in "$_module_bundles" "$_module_variants"; do
1156
+ if [ -f "$_module_candidate" ]; then
1157
+ [ -w "$_module_candidate" ] || die "project file is not writable: $_module_candidate"
1158
+ fi
1159
+ done
1160
+ _module_rm=$(trusted_utility_path rm) || die "no trusted rm utility available"
1161
+ if [ -n "$_module_body_path" ] && [ "$_module_body_shared" -eq 0 ]; then
1162
+ "$_module_rm" -f "$_module_body_path" || die "cannot remove module body: $_module_load"
1163
+ fi
1164
+ "$_module_rm" -f "$_module_meta" || die "cannot remove module metadata: $_module_name"
1165
+ for _module_candidate in "$_project_root"/module.d/*.meta "$_project_root"/layers/*/module.d/*.meta; do
1166
+ [ -f "$_module_candidate" ] || continue
1167
+ project_meta_remove_module "$_module_candidate" "$_module_name"
1168
+ done
1169
+ project_release_strip "$_module_bundles" 3 "$_module_name" "$_module_registry_source"
1170
+ project_release_strip "$_module_variants" 3 "$_module_name" "$_module_registry_source"
1171
+ info "removed module $_module_name"
1172
+ ;;
1173
+ *)
1174
+ die "unknown module subcommand: $_module_subcmd (use new, list, or remove)"
1175
+ ;;
1176
+ esac
1177
+ }
1178
+
1179
+ cmd_build() {
1180
+ _build_out=
1181
+ _build_variant=
1182
+ _build_bundle=
1183
+ for _build_arg do
1184
+ case "$_build_arg" in
1185
+ --variant=*) _build_variant=${_build_arg#--variant=}; [ -n "$_build_variant" ] || die "variant name must not be empty" ;;
1186
+ --bundle=*) _build_bundle=${_build_arg#--bundle=}; [ -n "$_build_bundle" ] || die "bundle name must not be empty" ;;
1187
+ --help | -h) usage_build; return 0 ;;
1188
+ --*) die "unknown build option: $_build_arg" ;;
1189
+ *) [ -z "$_build_out" ] || die "usage: valise build [--variant=<name>] [--bundle=<name>] [out]"; _build_out=$_build_arg ;;
1190
+ esac
1191
+ done
1192
+ project_config_load
1193
+ [ -n "$_build_variant" ] || _build_variant=$_project_variant
1194
+ [ -n "$_build_bundle" ] || _build_bundle=$_project_bundle
1195
+ [ -n "$_build_out" ] || _build_out=$_project_root/build/release
1196
+ _build_out=$(path_normalize "$_build_out") || die "cannot resolve build output path"
1197
+ # The default output is replaced; any other output must be new or empty.
1198
+ _build_replace=0
1199
+ [ "$_build_out" != "$_project_root/build/release" ] || _build_replace=1
1200
+ project_build "$_build_variant" "$_build_bundle" "$_build_out" "$_build_replace"
1201
+ unset _build_out _build_arg _build_variant _build_bundle _build_replace
1202
+ }
1203
+
1204
+ # project_command_class <shells>
1205
+ # Print the POSIX shell class a module's commands run under: sh when the
1206
+ # module supports every POSIX shell, else the first of bash, zsh, ksh it
1207
+ # names. Fail for a Fish-only module.
1208
+ project_command_class() {
1209
+ for _pcc_class in sh bash zsh ksh; do
1210
+ case " $1 " in
1211
+ *" $_pcc_class "*) printf '%s\n' "$_pcc_class"; unset _pcc_class; return 0 ;;
1212
+ *" posix "*) printf 'sh\n'; unset _pcc_class; return 0 ;;
1213
+ esac
1214
+ done
1215
+ unset _pcc_class
1216
+ return 1
1217
+ }
1218
+
1219
+ # project_write_launcher <file> <class> <command> <module> <plan>
1220
+ # Write the bin/ launcher for one published command: it loads the module's
1221
+ # command plan from the release that contains it, in strict mode, then runs
1222
+ # the function with the caller's arguments.
1223
+ project_write_launcher() {
1224
+ case "$2" in
1225
+ sh) _pwl_interpreter='#!/bin/sh' ;;
1226
+ *) _pwl_interpreter="#!/usr/bin/env $2" ;;
1227
+ esac
1228
+ {
1229
+ printf '%s\n' \
1230
+ "$_pwl_interpreter" \
1231
+ "# valise command $3 from module $4; generated by valise build; do not edit." \
1232
+ 'case "$0" in' \
1233
+ '*/*) _valise_launcher=$0 ;;' \
1234
+ '*)' \
1235
+ ' if [ -f "$0" ]; then _valise_launcher=./$0; else _valise_launcher=$(command -v -- "$0") || _valise_launcher=; fi' \
1236
+ ' ;;' \
1237
+ 'esac' \
1238
+ 'case "$_valise_launcher" in' \
1239
+ '*/*) ;;' \
1240
+ "*) printf '%s\\n' 'valise: $3: cannot locate the command launcher' >&2; exit 127 ;;" \
1241
+ 'esac' \
1242
+ 'VALISE_MODULE_ROOT=$(cd -P -- "${_valise_launcher%/*}/.." 2>/dev/null && pwd -P) || {' \
1243
+ " printf '%s\\n' 'valise: $3: cannot locate the release' >&2" \
1244
+ ' exit 127' \
1245
+ '}' \
1246
+ 'unset _valise_launcher' \
1247
+ '_valise_command_mode_set=${VALISE_MODE+x}' \
1248
+ '_valise_command_mode=${VALISE_MODE-}' \
1249
+ 'VALISE_MODE=strict' \
1250
+ ". \"\$VALISE_MODULE_ROOT/$5\" || {" \
1251
+ " printf '%s\\n' 'valise: $3: cannot load module $4' >&2" \
1252
+ ' exit 1' \
1253
+ '}' \
1254
+ 'if [ -n "$_valise_command_mode_set" ]; then VALISE_MODE=$_valise_command_mode; else unset VALISE_MODE; fi' \
1255
+ 'unset VALISE_MODULE_ROOT _valise_command_mode _valise_command_mode_set' \
1256
+ "$3 \"\$@\""
1257
+ } >"$1" || die "cannot write command launcher: $3"
1258
+ unset _pwl_interpreter
1259
+ }
1260
+
1261
+ # project_build_commands <registry> <excludes> <stage> <scratch>
1262
+ # Publish the commands and executables of every module in the closure
1263
+ # recorded in <scratch>/closure. Each module with commands gets a plan for its
1264
+ # command class at plans/commands/<module>.sh, whose closure joins the release; that
1265
+ # can pull in more modules, so the scan repeats until no module is new. Sets
1266
+ # _pb_commands to the published names.
1267
+ project_build_commands() {
1268
+ _pbc_registry=$1
1269
+ _pbc_excludes=$2
1270
+ _pbc_stage=$3
1271
+ _pbc_scratch=$4
1272
+ _pbc_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
1273
+ _pbc_sort=$(trusted_utility_path sort) || die "no trusted sort utility available"
1274
+ _pbc_mkdir=$(trusted_utility_path mkdir) || die "no trusted mkdir utility available"
1275
+ _pbc_cp=$(trusted_utility_path cp) || die "no trusted cp utility available"
1276
+ _pbc_done=
1277
+ _pb_commands=
1278
+ while :; do
1279
+ LC_ALL=C "$_pbc_sort" -u "$_pbc_scratch/closure" >"$_pbc_scratch/closure.scan" || die "cannot order the closure"
1280
+ _pbc_new=0
1281
+ while IFS= read -r _pbc_id; do
1282
+ [ -n "$_pbc_id" ] || continue
1283
+ case " $_pbc_done " in *" $_pbc_id "*) continue ;; esac
1284
+ _pbc_done="$_pbc_done $_pbc_id"
1285
+ _pbc_name=${_pbc_id%%@*}
1286
+ _pbc_source=${_pbc_id#*@}
1287
+ _pbc_row=$("$_pbc_awk" -F '\t' -v n="$_pbc_name" -v s="$_pbc_source" \
1288
+ 'NR > 1 && $1 == n && $13 == s { print $6 "\t" $10 "\t" $11; exit }' "$_pbc_registry")
1289
+ _pbc_shells=${_pbc_row%%" "*}
1290
+ _pbc_rest=${_pbc_row#*" "}
1291
+ _pbc_cmds=${_pbc_rest%%" "*}
1292
+ _pbc_exes=${_pbc_rest#*" "}
1293
+ [ -n "$_pbc_cmds$_pbc_exes" ] || continue
1294
+ "$_pbc_mkdir" -p "$_pbc_stage/bin" || die "cannot create the release bin directory"
1295
+ if [ -n "$_pbc_cmds" ]; then
1296
+ _pbc_class=$(project_command_class "$_pbc_shells") ||
1297
+ die "module $_pbc_name declares commands but supports no POSIX shell (Fish-only modules cannot publish commands)"
1298
+ _pbc_plan="plans/commands/$_pbc_name.sh"
1299
+ [ ! -e "$_pbc_stage/$_pbc_plan" ] || die "two modules named $_pbc_name publish commands; rename one"
1300
+ "$_pbc_mkdir" -p "$_pbc_stage/plans/commands" || die "cannot create the command plan directory"
1301
+ project_compile "$_pbc_registry" "$_pbc_class" "$_pbc_id" "$_pbc_excludes" "$_pbc_scratch/closure.cmd" closure
1302
+ "$_pbc_awk" '{ print }' "$_pbc_scratch/closure.cmd" >>"$_pbc_scratch/closure" ||
1303
+ die "cannot record the command closure of $_pbc_name"
1304
+ _pbc_new=1
1305
+ project_compile "$_pbc_registry" "$_pbc_class" "$_pbc_id" "$_pbc_excludes" "$_pbc_stage/$_pbc_plan"
1306
+ for _pbc_cmd in $_pbc_cmds; do
1307
+ case " $_pb_commands " in *" $_pbc_cmd "*) die "command $_pbc_cmd is published by more than one module" ;; esac
1308
+ _pb_commands="${_pb_commands:+$_pb_commands }$_pbc_cmd"
1309
+ project_write_launcher "$_pbc_stage/bin/$_pbc_cmd" "$_pbc_class" "$_pbc_cmd" "$_pbc_name" "$_pbc_plan"
1310
+ done
1311
+ fi
1312
+ if [ -n "$_pbc_exes" ]; then
1313
+ _pbc_dir=$(project_source_dir "$_project_root" "$_pbc_source") || die "unsupported registry source: $_pbc_source"
1314
+ for _pbc_exe in $_pbc_exes; do
1315
+ _pbc_cmd=${_pbc_exe##*/}
1316
+ case "$_pbc_cmd" in
1317
+ [A-Za-z0-9_]*) ;;
1318
+ *) die "module $_pbc_name: invalid executable name: $_pbc_cmd" ;;
1319
+ esac
1320
+ case "$_pbc_cmd" in
1321
+ *[!A-Za-z0-9_.-]*) die "module $_pbc_name: invalid executable name: $_pbc_cmd" ;;
1322
+ esac
1323
+ case " $_pb_commands " in *" $_pbc_cmd "*) die "command $_pbc_cmd is published by more than one module" ;; esac
1324
+ _pb_commands="${_pb_commands:+$_pb_commands }$_pbc_cmd"
1325
+ [ -f "$_pbc_dir/$_pbc_exe" ] && [ ! -L "$_pbc_dir/$_pbc_exe" ] ||
1326
+ die "executable missing or a symlink: $_pbc_dir/$_pbc_exe"
1327
+ "$_pbc_cp" "$_pbc_dir/$_pbc_exe" "$_pbc_stage/bin/$_pbc_cmd" || die "cannot copy executable $_pbc_exe"
1328
+ done
1329
+ fi
1330
+ done <"$_pbc_scratch/closure.scan"
1331
+ [ "$_pbc_new" -eq 1 ] || break
1332
+ done
1333
+ unset _pbc_registry _pbc_excludes _pbc_stage _pbc_scratch _pbc_awk _pbc_sort _pbc_mkdir _pbc_cp _pbc_done \
1334
+ _pbc_new _pbc_id _pbc_name _pbc_source _pbc_row _pbc_shells _pbc_rest _pbc_cmds _pbc_exes _pbc_class \
1335
+ _pbc_plan _pbc_cmd _pbc_dir _pbc_exe
1336
+ }
1337
+
1338
+ # project_build <variant> <bundle> <out> <replace>
1339
+ # Build a release tree from the loaded project. The tree holds only the
1340
+ # modules some shell's closure loads, under the paths their plans source:
1341
+ #
1342
+ # valise-release.conf build selection and closure
1343
+ # manifest.tsv release manifest
1344
+ # plans/<shell>.sh static plans for sh bash zsh ksh
1345
+ # plans/commands/<module>.sh plans for modules that publish commands
1346
+ # bin/<command> command launchers and executables
1347
+ # runtime/capabilities.sh the capability prelude
1348
+ # runtime/fish/{metadata,loader,profile}.fish the Fish runtime, when the
1349
+ # bundle has Fish roots
1350
+ # module.d/, modules/... primary modules in the closure
1351
+ # layers/<p>/module.d, layers/<p>/... layer modules in the closure
1352
+ # valise/module.d, valise/modules/... engine modules in the closure
1353
+ project_build() {
1354
+ _pb_variant=$1
1355
+ _pb_bundle=$2
1356
+ _pb_out=$3
1357
+ [ "$_pb_out" != "/" ] || die "output directory must not be /"
1358
+ case "$_pb_out" in
1359
+ "$_project_root") die "output directory must not be the project root" ;;
1360
+ "$_project_root"/*)
1361
+ source_path_is_physical "$_project_root" "${_pb_out#"$_project_root"/}" ||
1362
+ die "output path contains a symlink: $_pb_out"
1363
+ ;;
1364
+ esac
1365
+ project_output_check "$_pb_out" "$4"
1366
+ # The developer commands build throwaway releases from explicit roots:
1367
+ # with _pb_override=1 the roots for each shell come from _pbo_<shell> and
1368
+ # no variant exclusions apply.
1369
+ if [ "${_pb_override:-0}" -eq 1 ]; then
1370
+ _pb_excludes=
1371
+ else
1372
+ _pb_excludes=$(project_variant_field "$_project_root" "$_pb_variant" excluded) || die "cannot read variant $_pb_variant"
1373
+ fi
1374
+ _pb_engine=$(engine_root) || die "cannot locate the valise engine"
1375
+ _pb_parent=$(path_parent "$_pb_out")
1376
+ _pb_mkdir=$(trusted_utility_path mkdir) || die "no trusted mkdir utility available"
1377
+ _pb_cp=$(trusted_utility_path cp) || die "no trusted cp utility available"
1378
+ _pb_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
1379
+ _pb_sort=$(trusted_utility_path sort) || die "no trusted sort utility available"
1380
+ [ -d "$_pb_parent" ] || "$_pb_mkdir" -p "$_pb_parent" || die "cannot create output parent: $_pb_parent"
1381
+ # Build beside the destination and publish only after the complete tree and
1382
+ # manifest exist, so a failed build never leaves a partial release.
1383
+ _stage=$(trusted_mktemp_at "$_pb_parent" dir "$_pb_parent/.valise-build.XXXXXX") ||
1384
+ die "cannot create build staging directory"
1385
+ _pb_scratch="$_stage/.scratch"
1386
+ "$_pb_mkdir" -p "$_pb_scratch" "$_stage/plans" "$_stage/runtime" || die "cannot create release tree"
1387
+ _pb_registry="$_pb_scratch/registry"
1388
+ registry_generate "$_project_root" "$_pb_registry"
1389
+ : >"$_pb_scratch/closure"
1390
+ _pb_conf_roots=
1391
+ _pb_any=0
1392
+ _pb_fish=0
1393
+ for _pb_shell in $VALISE_PROJECT_SHELLS fish; do
1394
+ if [ "${_pb_override:-0}" -eq 1 ]; then
1395
+ # _pb_shell is one of the fixed shell names, so the expansion is safe.
1396
+ eval "_pb_roots=\${_pbo_$_pb_shell:-}"
1397
+ else
1398
+ _pb_roots=$(project_bundle_roots "$_project_root" "$_pb_shell" "$_pb_bundle") ||
1399
+ die "cannot read bundle $_pb_bundle"
1400
+ fi
1401
+ if [ -z "$_pb_roots" ]; then
1402
+ _pb_conf_roots="$_pb_conf_roots${_pb_conf_roots:+
1403
+ }roots_$_pb_shell="
1404
+ continue
1405
+ fi
1406
+ # The release records the roots that survive the variant, qualified, so
1407
+ # the Fish adapter seeds exactly what the closure contains.
1408
+ _pb_effective=$(project_effective_roots "$_pb_registry" "$_pb_shell" "$_pb_roots" "$_pb_excludes") || exit 1
1409
+ _pb_conf_roots="$_pb_conf_roots${_pb_conf_roots:+
1410
+ }roots_$_pb_shell=$_pb_effective"
1411
+ _pb_any=1
1412
+ project_compile "$_pb_registry" "$_pb_shell" "$_pb_roots" "$_pb_excludes" "$_pb_scratch/closure.$_pb_shell" closure
1413
+ "$_pb_awk" '{ print }' "$_pb_scratch/closure.$_pb_shell" >>"$_pb_scratch/closure" ||
1414
+ die "cannot record the $_pb_shell closure"
1415
+ if [ "$_pb_shell" = fish ]; then
1416
+ _pb_fish=1
1417
+ else
1418
+ project_compile "$_pb_registry" "$_pb_shell" "$_pb_roots" "$_pb_excludes" "$_stage/plans/$_pb_shell.sh"
1419
+ info "compiled plan for $_pb_shell"
1420
+ fi
1421
+ done
1422
+ [ "$_pb_any" -eq 1 ] || die "bundle $_pb_bundle has no roots for any shell"
1423
+ project_build_commands "$_pb_registry" "$_pb_excludes" "$_stage" "$_pb_scratch"
1424
+ "$_pb_cp" "$_pb_engine/lib/capabilities.sh" "$_stage/runtime/capabilities.sh" || die "cannot copy the capability prelude"
1425
+ # A bundle with Fish roots gets the native Fish runtime: the metadata
1426
+ # reader, the loader, and the release adapter the Fish hook sources. The
1427
+ # loader resolves the closure copied below at Fish startup.
1428
+ if [ "$_pb_fish" -eq 1 ]; then
1429
+ "$_pb_mkdir" -p "$_stage/runtime/fish" || die "cannot create the Fish runtime directory"
1430
+ for _pb_fish_file in metadata loader profile; do
1431
+ "$_pb_cp" "$_pb_engine/lib/fish/$_pb_fish_file.fish" "$_stage/runtime/fish/$_pb_fish_file.fish" ||
1432
+ die "cannot copy the Fish runtime: lib/fish/$_pb_fish_file.fish"
1433
+ done
1434
+ fi
1435
+
1436
+ # Copy every module in the union closure: its metadata and its body, under
1437
+ # the release prefix of its source.
1438
+ LC_ALL=C "$_pb_sort" -u "$_pb_scratch/closure" >"$_pb_scratch/closure.sorted" || die "cannot order the closure"
1439
+ _pb_modules=
1440
+ while IFS= read -r _pb_id; do
1441
+ [ -n "$_pb_id" ] || continue
1442
+ _pb_modules="${_pb_modules:+$_pb_modules }$_pb_id"
1443
+ _pb_name=${_pb_id%%@*}
1444
+ _pb_source=${_pb_id#*@}
1445
+ _pb_load=$("$_pb_awk" -F '\t' -v n="$_pb_name" -v s="$_pb_source" 'NR > 1 && $1 == n && $13 == s { print $9; exit }' "$_pb_registry")
1446
+ _pb_dir=$(project_source_dir "$_project_root" "$_pb_source") || die "unsupported registry source: $_pb_source"
1447
+ _pb_prefix=$(project_release_prefix "$_pb_source") || die "unsupported registry source: $_pb_source"
1448
+ _pb_pairs="$_pb_dir/module.d/$_pb_name.meta:${_pb_prefix}module.d/$_pb_name.meta"
1449
+ [ -z "$_pb_load" ] || _pb_pairs="$_pb_pairs $_pb_dir/$_pb_load:$_pb_prefix$_pb_load"
1450
+ for _pb_pair in $_pb_pairs; do
1451
+ _pb_from=${_pb_pair%%:*}
1452
+ _pb_to="$_stage/${_pb_pair#*:}"
1453
+ [ -f "$_pb_from" ] && [ ! -L "$_pb_from" ] || die "module file missing or a symlink: $_pb_from"
1454
+ # Several modules of one source may share a body.
1455
+ [ ! -e "$_pb_to" ] && [ ! -L "$_pb_to" ] || continue
1456
+ "$_pb_mkdir" -p "$(path_parent "$_pb_to")" && "$_pb_cp" "$_pb_from" "$_pb_to" ||
1457
+ die "cannot copy $_pb_from"
1458
+ done
1459
+ done <"$_pb_scratch/closure.sorted"
1460
+
1461
+ {
1462
+ printf '%s\n' \
1463
+ '# valise release; generated by valise build; do not edit.' \
1464
+ 'format=2' \
1465
+ "valise_version=$VALISE_VERSION" \
1466
+ "bundle=$_pb_bundle" \
1467
+ "variant=$_pb_variant" \
1468
+ "$_pb_conf_roots"
1469
+ printf 'modules=%s\n' "$_pb_modules"
1470
+ printf 'commands=%s\n' "$_pb_commands"
1471
+ } >"$_stage/valise-release.conf" || die "cannot write valise-release.conf"
1472
+ remove_tree "$_pb_scratch" || die "cannot remove build scratch files"
1473
+ _pb_find=$(trusted_utility_path find) || die "no trusted find utility available"
1474
+ _pb_chmod=$(trusted_utility_path chmod) || die "no trusted chmod utility available"
1475
+ "$_pb_find" "$_stage" -type d -exec "$_pb_chmod" 0755 {} + || die "cannot normalise directory modes"
1476
+ "$_pb_find" "$_stage" -type f -exec "$_pb_chmod" 0644 {} + || die "cannot normalise file modes"
1477
+ if [ -d "$_stage/bin" ]; then
1478
+ "$_pb_find" "$_stage/bin" -type f -exec "$_pb_chmod" 0755 {} + || die "cannot make release commands executable"
1479
+ fi
1480
+ cmd_make_manifest "$_stage" >/dev/null
1481
+ project_publish "$_stage" "$_pb_out"
1482
+ _stage=""
1483
+ info "built release: $_pb_out (bundle $_pb_bundle, variant $_pb_variant)"
1484
+ unset _pb_variant _pb_bundle _pb_out _pb_excludes _pb_engine _pb_parent _pb_scratch _pb_registry \
1485
+ _pb_conf_roots _pb_effective _pb_any _pb_fish _pb_fish_file _pb_shell _pb_roots _pb_modules _pb_id _pb_name _pb_source _pb_load \
1486
+ _pb_dir _pb_prefix _pb_pairs _pb_pair _pb_from _pb_to _pb_commands
1487
+ }
1488
+
1489
+ cmd_apply() {
1490
+ _apply_variant=
1491
+ _apply_bundle=
1492
+ _apply_install_arg=
1493
+ _apply_dry_run=0
1494
+ for _apply_arg do
1495
+ case "$_apply_arg" in
1496
+ --variant=*) _apply_variant=${_apply_arg#--variant=}; [ -n "$_apply_variant" ] || die "variant name must not be empty" ;;
1497
+ --bundle=*) _apply_bundle=${_apply_arg#--bundle=}; [ -n "$_apply_bundle" ] || die "bundle name must not be empty" ;;
1498
+ --no-integration) _apply_install_arg=--no-integration ;;
1499
+ --dry-run) _apply_dry_run=1 ;;
1500
+ --help | -h) usage_apply; return 0 ;;
1501
+ *) die "unknown apply option: $_apply_arg" ;;
1502
+ esac
1503
+ done
1504
+ project_config_load
1505
+ [ -n "$_apply_variant" ] || _apply_variant=$_project_variant
1506
+ [ -n "$_apply_bundle" ] || _apply_bundle=$_project_bundle
1507
+ _apply_build_out=$_project_root/build/release
1508
+ project_build "$_apply_variant" "$_apply_bundle" "$_apply_build_out" 1
1509
+ _apply_old_pwd=$(pwd -P) || die "cannot resolve current directory"
1510
+ cd "$_apply_build_out" || die "cannot enter build output: $_apply_build_out"
1511
+ if [ "$_apply_dry_run" -eq 1 ]; then
1512
+ # Only build/ has changed; describe the install without performing it.
1513
+ _apply_integration=1
1514
+ [ -z "$_apply_install_arg" ] || _apply_integration=0
1515
+ install_preview "$_apply_integration"
1516
+ cd "$_apply_old_pwd" || die "cannot restore project directory: $_apply_old_pwd"
1517
+ unset _apply_arg _apply_variant _apply_bundle _apply_install_arg _apply_build_out _apply_old_pwd \
1518
+ _apply_dry_run _apply_integration
1519
+ return 0
1520
+ fi
1521
+ cmd_install $_apply_install_arg || die "valise install failed"
1522
+ cd "$_apply_old_pwd" || die "cannot restore project directory: $_apply_old_pwd"
1523
+ cmd_verify_active
1524
+ info "applied project: $_project_root"
1525
+ unset _apply_arg _apply_variant _apply_bundle _apply_install_arg _apply_build_out _apply_old_pwd _apply_dry_run
1526
+ }
1527
+
1528
+ # release_conf_value <release-dir> <key>
1529
+ # Print one key of a release's valise-release.conf, or fail.
1530
+ release_conf_value() {
1531
+ [ -f "$1/valise-release.conf" ] || return 1
1532
+ _rcv_awk=$(trusted_utility_path awk) || return 1
1533
+ "$_rcv_awk" -v key="$2" '
1534
+ index($0, key "=") == 1 { print substr($0, length(key) + 2); found = 1; exit }
1535
+ END { exit found ? 0 : 1 }
1536
+ ' "$1/valise-release.conf"
1537
+ }
1538
+
1539
+ cmd_status() {
1540
+ for _status_arg do
1541
+ case "$_status_arg" in
1542
+ --help | -h) usage_status; return 0 ;;
1543
+ *) die "unknown status option: $_status_arg" ;;
1544
+ esac
1545
+ done
1546
+ project_config_load
1547
+ printf 'project: %s\n' "$_project_root"
1548
+ printf 'project default bundle: %s\n' "$_project_bundle"
1549
+ printf 'project default variant: %s\n' "$_project_variant"
1550
+ _status_home=$(path_normalize "$HOME") || die "cannot resolve HOME"
1551
+ _status_targets=$(posix_startup_targets "$_status_home")
1552
+ while IFS= read -r _status_target; do
1553
+ [ -n "$_status_target" ] || continue
1554
+ if [ -L "$_status_target" ]; then
1555
+ printf 'integration: %s: symlink, not managed by valise\n' "$_status_target"
1556
+ elif [ ! -f "$_status_target" ]; then
1557
+ printf 'integration: %s: missing\n' "$_status_target"
1558
+ elif integration_has_managed_block "$_status_target"; then
1559
+ printf 'integration: %s: managed block present\n' "$_status_target"
1560
+ else
1561
+ printf 'integration: %s: managed block missing\n' "$_status_target"
1562
+ fi
1563
+ done <<EOF
1564
+ $_status_targets
1565
+ EOF
1566
+ _status_active_target=
1567
+ if [ -L "$VALISE_ACTIVE_LINK" ]; then
1568
+ _status_active_target=$(active_release_dir 2>/dev/null || :)
1569
+ if [ -n "$_status_active_target" ]; then
1570
+ printf 'active release: %s\n' "${_status_active_target##*/}"
1571
+ printf 'active bundle: %s\n' "$(release_conf_value "$_status_active_target" bundle 2>/dev/null || printf unknown)"
1572
+ printf 'active variant: %s\n' "$(release_conf_value "$_status_active_target" variant 2>/dev/null || printf unknown)"
1573
+ printf 'active built by valise: %s\n' "$(release_conf_value "$_status_active_target" valise_version 2>/dev/null || printf unknown)"
1574
+ else
1575
+ printf 'active release: broken symlink\n'
1576
+ fi
1577
+ else
1578
+ printf 'active release: none\n'
1579
+ fi
1580
+ _status_build="$_project_root/build/release"
1581
+ if [ -f "$_status_build/manifest.tsv" ]; then
1582
+ printf 'build output: %s (bundle %s, variant %s)\n' "$_status_build" \
1583
+ "$(release_conf_value "$_status_build" bundle 2>/dev/null || printf unknown)" \
1584
+ "$(release_conf_value "$_status_build" variant 2>/dev/null || printf unknown)"
1585
+ if [ -n "$_status_active_target" ] && [ -f "$_status_active_target/.valise-manifest" ]; then
1586
+ _status_build_hash=$(file_sha256 "$_status_build/manifest.tsv") || die "cannot hash build manifest"
1587
+ _status_active_hash=$(file_sha256 "$_status_active_target/.valise-manifest") || die "cannot hash active manifest"
1588
+ if [ "$_status_build_hash" = "$_status_active_hash" ]; then
1589
+ printf 'drift: active release matches build output\n'
1590
+ else
1591
+ printf 'drift: active release differs from build output\n'
1592
+ fi
1593
+ else
1594
+ printf 'drift: no active release to compare\n'
1595
+ fi
1596
+ else
1597
+ printf 'build output: not built\n'
1598
+ fi
1599
+ unset _status_home _status_targets _status_target _status_active_target \
1600
+ _status_build _status_build_hash _status_active_hash
1601
+ }
1602
+
1603
+ cmd_explain_variant() {
1604
+ _ev_variant=
1605
+ _ev_bundle=
1606
+ for _ev_arg do
1607
+ case "$_ev_arg" in
1608
+ --help | -h) usage_explain_variant; return 0 ;;
1609
+ --bundle=*) _ev_bundle=${_ev_arg#--bundle=}; [ -n "$_ev_bundle" ] || die "bundle name must not be empty" ;;
1610
+ --*) die "unknown explain-variant option: $_ev_arg" ;;
1611
+ *) [ -z "$_ev_variant" ] || die "usage: valise explain-variant [--bundle=<name>] [variant]"; _ev_variant=$_ev_arg ;;
1612
+ esac
1613
+ done
1614
+ project_config_load
1615
+ [ -n "$_ev_variant" ] || _ev_variant=$_project_variant
1616
+ [ -n "$_ev_bundle" ] || _ev_bundle=$_project_bundle
1617
+ _ev_desc=$(project_variant_field "$_project_root" "$_ev_variant" description) || exit 1
1618
+ _ev_excludes=$(project_variant_field "$_project_root" "$_ev_variant" excluded) || exit 1
1619
+ printf 'Variant: %s\n' "$_ev_variant"
1620
+ printf 'Description: %s\n' "${_ev_desc:-none}"
1621
+ printf 'Excluded modules: %s\n' "${_ev_excludes:-none}"
1622
+ printf '\nPer-shell impact (bundle %s):\n' "$_ev_bundle"
1623
+ # Resolve through the compiler, so a qualified exclusion matches a bare root
1624
+ # and the module counts are the closures a build would load.
1625
+ _stage=$(trusted_mktemp dir "${TMPDIR:-/tmp}/valise-explain.XXXXXX") || die "cannot create scratch directory"
1626
+ registry_generate "$_project_root" "$_stage/registry"
1627
+ _ev_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
1628
+ for _ev_shell in $VALISE_PROJECT_SHELLS fish; do
1629
+ _ev_roots=$(project_bundle_roots "$_project_root" "$_ev_shell" "$_ev_bundle") || exit 1
1630
+ [ -n "$_ev_roots" ] || continue
1631
+ _ev_total=0
1632
+ for _ev_root in $_ev_roots; do _ev_total=$((_ev_total + 1)); done
1633
+ _ev_kept=$(project_effective_roots "$_stage/registry" "$_ev_shell" "$_ev_roots" "$_ev_excludes") || exit 1
1634
+ _ev_count=0
1635
+ for _ev_root in $_ev_kept; do _ev_count=$((_ev_count + 1)); done
1636
+ project_compile "$_stage/registry" "$_ev_shell" "$_ev_roots" "" "$_stage/full" closure
1637
+ project_compile "$_stage/registry" "$_ev_shell" "$_ev_roots" "$_ev_excludes" "$_stage/kept" closure
1638
+ _ev_full=$("$_ev_awk" 'END { print NR }' "$_stage/full")
1639
+ _ev_mods=$("$_ev_awk" 'END { print NR }' "$_stage/kept")
1640
+ printf ' %s: %d of %d roots, %d of %d modules\n' "$_ev_shell" "$_ev_count" "$_ev_total" "$_ev_mods" "$_ev_full"
1641
+ done
1642
+ remove_tree "$_stage" || die "cannot remove scratch directory"
1643
+ _stage=""
1644
+ }
1645
+
1646
+ cmd_explain_bundles() {
1647
+ _eb_bundle=
1648
+ _eb_variant=
1649
+ for _eb_arg do
1650
+ case "$_eb_arg" in
1651
+ --bundle=*) _eb_bundle=${_eb_arg#--bundle=}; [ -n "$_eb_bundle" ] || die "bundle name must not be empty" ;;
1652
+ --variant=*) _eb_variant=${_eb_arg#--variant=}; [ -n "$_eb_variant" ] || die "variant name must not be empty" ;;
1653
+ --help | -h) usage_explain_bundles; return 0 ;;
1654
+ *) die "unknown explain-bundles option: $_eb_arg" ;;
1655
+ esac
1656
+ done
1657
+ project_config_load
1658
+ [ -n "$_eb_variant" ] || _eb_variant=$_project_variant
1659
+ _eb_excludes=$(project_variant_field "$_project_root" "$_eb_variant" excluded) || exit 1
1660
+ if [ -n "$_eb_bundle" ]; then
1661
+ project_bundle_roots "$_project_root" sh "$_eb_bundle" >/dev/null || exit 1
1662
+ _eb_bundles=$_eb_bundle
1663
+ else
1664
+ _eb_bundles=$(project_bundle_names "$_project_root") || die "cannot read bundles.tsv"
1665
+ fi
1666
+ _stage=$(trusted_mktemp dir "${TMPDIR:-/tmp}/valise-explain.XXXXXX") || die "cannot create scratch directory"
1667
+ registry_generate "$_project_root" "$_stage/registry"
1668
+ _eb_awk=$(trusted_utility_path awk) || die "no trusted awk utility available"
1669
+ printf 'Bundle resolution (variant %s):\n\n' "$_eb_variant"
1670
+ for _eb_b in $_eb_bundles; do
1671
+ printf 'Bundle: %s\n' "$_eb_b"
1672
+ for _eb_shell in $VALISE_PROJECT_SHELLS fish; do
1673
+ _eb_roots=$(project_bundle_roots "$_project_root" "$_eb_shell" "$_eb_b") || exit 1
1674
+ [ -n "$_eb_roots" ] || continue
1675
+ project_compile "$_stage/registry" "$_eb_shell" "$_eb_roots" "$_eb_excludes" "$_stage/closure" closure
1676
+ _eb_count=$("$_eb_awk" 'END { print NR }' "$_stage/closure")
1677
+ _eb_root_count=0
1678
+ for _eb_root in $_eb_roots; do _eb_root_count=$((_eb_root_count + 1)); done
1679
+ printf ' %s: %d root(s), %d module(s)\n' "$_eb_shell" "$_eb_root_count" "$_eb_count"
1680
+ "$_eb_awk" '{ at = index($0, "@"); printf " module %s (%s)\n", substr($0, 1, at - 1), substr($0, at + 1) }' "$_stage/closure"
1681
+ done
1682
+ printf '\n'
1683
+ done
1684
+ remove_tree "$_stage" || die "cannot remove scratch directory"
1685
+ _stage=""
1686
+ }
1687
+
1688
+ cmd_layer_lint() {
1689
+ _ll_name="${1:-}"
1690
+ if [ "$#" -gt 0 ]; then shift; fi
1691
+ case "$_ll_name" in
1692
+ --help | -h) usage_layer_lint; return 0 ;;
1693
+ esac
1694
+ for _ll_arg do
1695
+ case "$_ll_arg" in
1696
+ --help | -h) usage_layer_lint; return 0 ;;
1697
+ *) die "unknown layer-lint option: $_ll_arg" ;;
1698
+ esac
1699
+ done
1700
+ [ -n "$_ll_name" ] || die "usage: valise layer-lint <layer-name|layer-path>"
1701
+ # A layer is addressed by its directory name in the current project, or by a
1702
+ # path to a layer tree that contains module.d/.
1703
+ _ll_root=$(project_find_root 2>/dev/null || :)
1704
+ if [ -n "$_ll_root" ] && [ -d "$_ll_root/layers/$_ll_name/module.d" ]; then
1705
+ _ll_dir="$_ll_root/layers/$_ll_name"
1706
+ elif [ -d "$_ll_name/module.d" ]; then
1707
+ _ll_dir=$(cd "$_ll_name" && pwd -P) || die "cannot resolve layer path: $_ll_name"
1708
+ else
1709
+ die "layer not found: $_ll_name (expected layers/$_ll_name in a valise project, or a path containing module.d/)"
1710
+ fi
1711
+ layer_lint_dir "$_ll_dir" "$_ll_name" || die "layer $_ll_name failed validation"
1712
+ }
1713
+
1714
+ # layer_lint_dir <layer-dir> <label>
1715
+ # Validate a contribution layer: contrib_ names, versions, bodies, safe modes,
1716
+ # and dependencies that stay inside the layer or the engine. Returns nonzero
1717
+ # after reporting every problem.
1718
+ layer_lint_dir() {
1719
+ _ll_layer=$1
1720
+ _ll_label=$2
1721
+ _ll_engine=$(engine_root) || die "cannot locate the valise engine"
1722
+ _ll_metadata="$_ll_engine/lib/metadata.sh"
1723
+ [ -r "$_ll_metadata" ] || die "metadata library not found: $_ll_metadata"
1724
+ _ll_status=0
1725
+ # The name lists are space-delimited with a leading and a trailing space, so
1726
+ # " $name " matches every entry including the first and the last.
1727
+ _ll_known=" "
1728
+ for _ll_meta in "$_ll_engine"/module.d/*.meta "$_ll_layer"/module.d/*.meta; do
1729
+ [ -f "$_ll_meta" ] || continue
1730
+ _ll_file=${_ll_meta##*/}
1731
+ _ll_known="$_ll_known${_ll_file%.meta} "
1732
+ done
1733
+ _ll_count=0
1734
+ for _ll_meta in "$_ll_layer"/module.d/*.meta; do
1735
+ [ -e "$_ll_meta" ] || [ -L "$_ll_meta" ] || continue
1736
+ _ll_count=$((_ll_count + 1))
1737
+ _ll_file=${_ll_meta##*/}
1738
+ _ll_mod_name=${_ll_file%.meta}
1739
+ if [ -L "$_ll_meta" ]; then
1740
+ printf ' FAIL: %s: metadata file must not be a symlink\n' "$_ll_file" >&2
1741
+ _ll_status=1
1742
+ continue
1743
+ fi
1744
+ _ll_mode=$(file_mode "$_ll_meta") || _ll_mode=""
1745
+ case "$_ll_mode" in
1746
+ 0644 | 0444) ;;
1747
+ *) printf ' FAIL: %s: metadata mode must be 0644 or 0444, got %s\n' "$_ll_file" "${_ll_mode:-unknown}" >&2; _ll_status=1 ;;
1748
+ esac
1749
+ case "$_ll_mod_name" in
1750
+ contrib_*) ;;
1751
+ *) printf ' FAIL: %s: layer module name must use the contrib_ prefix\n' "$_ll_file" >&2; _ll_status=1 ;;
1752
+ esac
1753
+ _ll_record=$(. "$_ll_metadata"; sp_metadata_read "$_ll_meta" 2>&1) || {
1754
+ printf ' FAIL: %s: invalid metadata: %s\n' "$_ll_file" "$_ll_record" >&2
1755
+ _ll_status=1
1756
+ continue
1757
+ }
1758
+ _ll_m_name= _ll_m_version= _ll_m_requires= _ll_m_optional= _ll_m_load=
1759
+ while IFS= read -r _ll_line; do
1760
+ case "$_ll_line" in
1761
+ name=*) _ll_m_name=${_ll_line#name=} ;;
1762
+ version=*) _ll_m_version=${_ll_line#version=} ;;
1763
+ requires=*) _ll_m_requires=${_ll_line#requires=} ;;
1764
+ optional=*) _ll_m_optional=${_ll_line#optional=} ;;
1765
+ load=*) _ll_m_load=${_ll_line#load=} ;;
1766
+ esac
1767
+ done <<EOF
1768
+ $_ll_record
1769
+ EOF
1770
+ if [ "$_ll_m_name" != "$_ll_mod_name" ]; then
1771
+ printf ' FAIL: %s: metadata name %s does not match the file name %s\n' "$_ll_file" "$_ll_m_name" "$_ll_mod_name" >&2
1772
+ _ll_status=1
1773
+ fi
1774
+ if [ -z "$_ll_m_version" ]; then
1775
+ printf ' FAIL: %s: layer modules must declare a version\n' "$_ll_file" >&2
1776
+ _ll_status=1
1777
+ fi
1778
+ if [ -n "$_ll_m_load" ]; then
1779
+ _ll_body="$_ll_layer/$_ll_m_load"
1780
+ if [ -L "$_ll_body" ]; then
1781
+ printf ' FAIL: %s: load target must not be a symlink: %s\n' "$_ll_file" "$_ll_m_load" >&2
1782
+ _ll_status=1
1783
+ elif [ ! -f "$_ll_body" ]; then
1784
+ printf ' FAIL: %s: load target does not exist: %s\n' "$_ll_file" "$_ll_m_load" >&2
1785
+ _ll_status=1
1786
+ else
1787
+ _ll_body_mode=$(file_mode "$_ll_body") || _ll_body_mode=""
1788
+ case "$_ll_body_mode" in
1789
+ 0644 | 0444 | 0755 | 0555) ;;
1790
+ *) printf ' FAIL: %s: load target mode must be 0644, 0444, 0755 or 0555, got %s\n' "$_ll_file" "${_ll_body_mode:-unknown}" >&2; _ll_status=1 ;;
1791
+ esac
1792
+ fi
1793
+ fi
1794
+ for _ll_dep in $_ll_m_requires $_ll_m_optional; do
1795
+ case "$_ll_known" in
1796
+ *" $_ll_dep "*) ;;
1797
+ *) printf ' FAIL: %s: dependency %s is neither in this layer nor an engine module\n' "$_ll_file" "$_ll_dep" >&2; _ll_status=1 ;;
1798
+ esac
1799
+ done
1800
+ done
1801
+ if [ "$_ll_count" -eq 0 ]; then
1802
+ printf ' FAIL: %s: layer contains no modules\n' "$_ll_layer" >&2
1803
+ _ll_status=1
1804
+ fi
1805
+ [ "$_ll_status" -eq 0 ] || return 1
1806
+ printf 'layer %s: %d module(s) validated\n' "$_ll_label" "$_ll_count"
1807
+ }