diff --git a/home/.chezmoiscripts/darwin/run_onchange_02_install-packages.sh.tmpl b/home/.chezmoiscripts/darwin/run_onchange_02_install-packages.sh.tmpl index af79967..fcd636c 100644 --- a/home/.chezmoiscripts/darwin/run_onchange_02_install-packages.sh.tmpl +++ b/home/.chezmoiscripts/darwin/run_onchange_02_install-packages.sh.tmpl @@ -1,7 +1,5 @@ #!/usr/bin/env bash -{{ $groups := list "shared" }} -{{ if .personal }}{{ $groups = append $groups "personal" }}{{ end }} -{{ if .work }}{{ $groups = append $groups "work" }}{{ end }} +{{ $groups := includeTemplate "lib/chezmoi/active-groups.json.tmpl" . | fromJson -}} # packages.yaml hash: {{ include ".chezmoidata/packages.yaml" | sha256sum }} {{ if eq .chezmoi.os "darwin" }} diff --git a/home/.chezmoiscripts/darwin/run_onchange_03_install-uv-tools.sh.tmpl b/home/.chezmoiscripts/darwin/run_onchange_03_install-uv-tools.sh.tmpl index 65f455a..e69c052 100644 --- a/home/.chezmoiscripts/darwin/run_onchange_03_install-uv-tools.sh.tmpl +++ b/home/.chezmoiscripts/darwin/run_onchange_03_install-uv-tools.sh.tmpl @@ -1,8 +1,6 @@ #!/usr/bin/env bash # uv tools data hash: {{ include ".chezmoidata/uv.yaml" | sha256sum }} -{{- $groups := list "shared" -}} -{{- if .personal }}{{ $groups = append $groups "personal" }}{{ end -}} -{{- if .work }}{{ $groups = append $groups "work" }}{{ end -}} +{{ $groups := includeTemplate "lib/chezmoi/active-groups.json.tmpl" . | fromJson -}} {{- $hasTools := false -}} {{- range $groups -}} {{- $tools := index $.uv.tools . -}} diff --git a/home/.chezmoiscripts/darwin/run_onchange_08_install-graphify-skills.sh.tmpl b/home/.chezmoiscripts/darwin/run_onchange_08_install-graphify-skills.sh.tmpl index aba82d4..89a579e 100644 --- a/home/.chezmoiscripts/darwin/run_onchange_08_install-graphify-skills.sh.tmpl +++ b/home/.chezmoiscripts/darwin/run_onchange_08_install-graphify-skills.sh.tmpl @@ -2,15 +2,11 @@ # @file run_onchange_08_install-graphify-skills.sh # @brief Install Graphify agent skills. # @description APM data hash: {{ include ".chezmoidata/apm.yaml" | sha256sum }} +{{- $activeTargets := includeTemplate "lib/chezmoi/active-group-values.json.tmpl" (dict "ctx" . "valuesByGroup" .apm.targets) | fromJson -}} {{- $platforms := list -}} -{{- if .personal -}} -{{- range (index .apm.targets "shared") -}} +{{- range $activeTargets -}} {{- if eq . "agent-skills" }}{{ $platforms = append $platforms "agents" }}{{- else if eq . "claude" }}{{ $platforms = append $platforms "claude" }}{{- end -}} {{- end -}} -{{- range (index .apm.targets "personal") -}} -{{- if eq . "agent-skills" }}{{ $platforms = append $platforms "agents" }}{{- else if eq . "claude" }}{{ $platforms = append $platforms "claude" }}{{- end -}} -{{- end -}} -{{- end }} {{ if and (eq .chezmoi.os "darwin") (gt (len $platforms) 0) }} {{ template "lib/common/log.sh" . }} diff --git a/home/.chezmoitemplates/lib/chezmoi/active-group-values.json.tmpl b/home/.chezmoitemplates/lib/chezmoi/active-group-values.json.tmpl new file mode 100644 index 0000000..4e3e5fb --- /dev/null +++ b/home/.chezmoitemplates/lib/chezmoi/active-group-values.json.tmpl @@ -0,0 +1,11 @@ +{{- $ctx := .ctx -}} +{{- $valuesByGroup := .valuesByGroup -}} +{{- $groups := includeTemplate "lib/chezmoi/active-groups.json.tmpl" $ctx | fromJson -}} +{{- $values := list -}} +{{- range $groups -}} +{{- $groupValues := index $valuesByGroup . -}} +{{- range $groupValues -}} +{{- $values = append $values . -}} +{{- end -}} +{{- end -}} +{{- $values | toJson -}} diff --git a/home/.chezmoitemplates/lib/chezmoi/active-groups.json.tmpl b/home/.chezmoitemplates/lib/chezmoi/active-groups.json.tmpl new file mode 100644 index 0000000..178c6c0 --- /dev/null +++ b/home/.chezmoitemplates/lib/chezmoi/active-groups.json.tmpl @@ -0,0 +1,4 @@ +{{- $groups := list "shared" -}} +{{- if .personal }}{{ $groups = append $groups "personal" }}{{ end -}} +{{- if .work }}{{ $groups = append $groups "work" }}{{ end -}} +{{- $groups | toJson -}} diff --git a/home/dot_apm/apm.yml.tmpl b/home/dot_apm/apm.yml.tmpl index 9b6d044..d178edc 100644 --- a/home/dot_apm/apm.yml.tmpl +++ b/home/dot_apm/apm.yml.tmpl @@ -1,6 +1,4 @@ -{{- $groups := list "shared" -}} -{{- if .personal }}{{ $groups = append $groups "personal" }}{{ end -}} -{{- if .work }}{{ $groups = append $groups "work" }}{{ end -}} +{{ $groups := includeTemplate "lib/chezmoi/active-groups.json.tmpl" . | fromJson -}} name: development version: 1.0.0 description: APM project for development diff --git a/home/dot_config/mise/config.toml.tmpl b/home/dot_config/mise/config.toml.tmpl index a76b2ff..88b2267 100644 --- a/home/dot_config/mise/config.toml.tmpl +++ b/home/dot_config/mise/config.toml.tmpl @@ -1,7 +1,5 @@ [tools] -{{ $groups := list "shared" -}} -{{ if .personal }}{{ $groups = append $groups "personal" }}{{ end -}} -{{ if .work }}{{ $groups = append $groups "work" }}{{ end -}} +{{ $groups := includeTemplate "lib/chezmoi/active-groups.json.tmpl" . | fromJson -}} {{ range $groups -}} {{ $tools := index $.mise.tools . -}} {{ range $tools -}} diff --git a/tests/template/chezmoi-helpers.bats b/tests/template/chezmoi-helpers.bats new file mode 100644 index 0000000..96759b9 --- /dev/null +++ b/tests/template/chezmoi-helpers.bats @@ -0,0 +1,92 @@ +#!/usr/bin/env bats +# @file tests/template/chezmoi-helpers.bats +# @brief Template rendering tests for chezmoi helper templates. + +load '../test_helpers/load.bash' + +SOURCE_DIR="$DOTFILES_ROOT/home" +ACTIVE_GROUPS_TMPL="$DOTFILES_ROOT/home/.chezmoitemplates/lib/chezmoi/active-groups.json.tmpl" +ACTIVE_GROUP_VALUES_TMPL="$DOTFILES_ROOT/home/.chezmoitemplates/lib/chezmoi/active-group-values.json.tmpl" + +DARWIN_DATA='{"chezmoi":{"os":"darwin"},"personal":true,"work":false}' +WORK_DATA='{"chezmoi":{"os":"darwin"},"personal":false,"work":true}' +SHARED_DATA='{"chezmoi":{"os":"darwin"},"personal":false,"work":false}' +BOTH_DATA='{"chezmoi":{"os":"darwin"},"personal":true,"work":true}' + +APM_VALUES_BY_GROUP='{"shared":["graphify"],"personal":["claude","agent-skills"],"work":["copilot"]}' +APM_VALUES_SPARSE='{"shared":["graphify"]}' + +render_helper() { + local template="$1" + local data="$2" + + mise exec -- chezmoi execute-template --source "$SOURCE_DIR" --override-data "$data" <"$template" +} + +@test "active-groups returns shared and personal for personal context" { + run render_helper "$ACTIVE_GROUPS_TMPL" "$DARWIN_DATA" + + assert_success + assert_output '["shared","personal"]' +} + +@test "active-groups returns shared and work for work context" { + run render_helper "$ACTIVE_GROUPS_TMPL" "$WORK_DATA" + + assert_success + assert_output '["shared","work"]' +} + +@test "active-groups returns only shared when neither personal nor work" { + run render_helper "$ACTIVE_GROUPS_TMPL" "$SHARED_DATA" + + assert_success + assert_output '["shared"]' +} + +@test "active-group-values merges shared and personal apm targets for personal context" { + local data + data=$(printf '{"ctx":{"personal":true,"work":false},"valuesByGroup":%s}' "$APM_VALUES_BY_GROUP") + + run render_helper "$ACTIVE_GROUP_VALUES_TMPL" "$data" + + assert_success + assert_output '["graphify","claude","agent-skills"]' +} + +@test "active-group-values merges shared and work apm targets for work context" { + local data + data=$(printf '{"ctx":{"personal":false,"work":true},"valuesByGroup":%s}' "$APM_VALUES_BY_GROUP") + + run render_helper "$ACTIVE_GROUP_VALUES_TMPL" "$data" + + assert_success + assert_output '["graphify","copilot"]' +} + +@test "active-group-values returns only shared apm targets when neither personal nor work" { + local data + data=$(printf '{"ctx":{"personal":false,"work":false},"valuesByGroup":%s}' "$APM_VALUES_BY_GROUP") + + run render_helper "$ACTIVE_GROUP_VALUES_TMPL" "$data" + + assert_success + assert_output '["graphify"]' +} + +@test "active-groups returns shared, personal, and work when both are true" { + run render_helper "$ACTIVE_GROUPS_TMPL" "$BOTH_DATA" + + assert_success + assert_output '["shared","personal","work"]' +} + +@test "active-group-values silently skips groups absent from valuesByGroup" { + local data + data=$(printf '{"ctx":{"personal":true,"work":false},"valuesByGroup":%s}' "$APM_VALUES_SPARSE") + + run render_helper "$ACTIVE_GROUP_VALUES_TMPL" "$data" + + assert_success + assert_output '["graphify"]' +}