diff --git a/lib/default.nix b/lib/default.nix index 0ce2d3f..ae29377 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -22,5 +22,4 @@ in rec { }; mkShellHook = userConfig: (make userConfig).config.shellHook; - mkCLI = userConfig: (make userConfig).config.packages.soonix; } diff --git a/lib/module.nix b/lib/module.nix index 3ea44aa..f1c9942 100644 --- a/lib/module.nix +++ b/lib/module.nix @@ -154,65 +154,57 @@ in { config = let hooks = config.hooks; # allow excluding gitignore since stuff like renovate can't use/commit it anyways - runHooks = concatMapStringsSep "\n" (hookName: let - hook = hooks.${hookName}; - modes = { - link = - # sh - '' - if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then - _soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}" - if [[ "$CHECK_MODE" != "true" ]]; then + getHookNames = {includeGitignored ? true}: + if includeGitignored + then (builtins.attrNames hooks) + else + map + (hook: hook.name) + (builtins.filter (hook: !hook.hook.gitignore) (builtins.attrValues hooks)); + + runHooks = args: + concatMapStringsSep "\n" (hookName: let + hook = hooks.${hookName}; + modes = { + link = + # sh + '' + if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then + _soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}" mkdir -p "$(dirname "${hook.output}")" ln -sf "${hook.generatedDerivation}" "${hook.output}" + _changed=true else - _soonix_log "info" "${hookName}" "Would create symlink: ${hook.output} -> ${hook.generatedDerivation}" + _soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}" fi - _changed=true - else - _soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}" - fi - ''; - copy = - # sh - '' - if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then - _soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}" - if [[ "$CHECK_MODE" != "true" ]]; then + ''; + copy = + # sh + '' + if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then + _soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}" mkdir -p "$(dirname "${hook.output}")" # required since they're read only rm -f "${hook.output}" cp "${hook.generatedDerivation}" "${hook.output}" + _changed=true else - _soonix_log "info" "${hookName}" "Would copy file: ${hook.generatedDerivation} -> ${hook.output}" + _soonix_log "info" "${hookName}" "File up to date: ${hook.output}" fi - _changed=true - else - _soonix_log "info" "${hookName}" "File up to date: ${hook.output}" - fi - ''; - }; + ''; + }; - optionalGitignore = - if hook.hook.gitignore - then '' - _soonix_add_to_gitignore "${hook.output}" + optionalGitignore = + if hook.hook.gitignore + then '' + _soonix_add_to_gitignore "${hook.output}" + '' + else ""; + in + builtins.addErrorContext "[soonix] while generating script for ${hookName}" + # sh '' - else ""; - - isGitignored = - if hook.hook.gitignore - then "true" - else "false"; - in - builtins.addErrorContext "[soonix] while generating script for ${hookName}" - # sh - '' - # Process hook: ${hookName} - # Skip if SKIP_GITIGNORE is set and this hook is gitignored - if [[ "$SKIP_GITIGNORE" == "true" && "${isGitignored}" == "true" ]]; then - : # skip - else + # Process hook: ${hookName} while IFS= read -r line; do case "$line" in UPDATED) _soonix_updated+=("${hookName}") ;; @@ -225,15 +217,13 @@ in { ${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")} - if [[ "$CHECK_MODE" != "true" ]]; then - # Add to gitignore if requested - ${optionalGitignore} + # Add to gitignore if requested + ${optionalGitignore} - # Run extra commands if file changed - if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then - _soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}" - eval "${hook.hook.extra}" - fi + # Run extra commands if file changed + if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then + _soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}" + eval "${hook.hook.extra}" fi if [[ "$_changed" == "true" ]]; then @@ -245,17 +235,13 @@ in { _soonix_log "error" "${hookName}" "Failed to process hook" _soonix_failed+=("${hookName}") } - fi - '') - (builtins.attrNames hooks); + '') + (getHookNames args); - generateShellHook = + generateShellHook = args: builtins.addErrorContext "[soonix] while generating shell hook" # sh '' - CHECK_MODE=''${CHECK_MODE:-false} - SKIP_GITIGNORE=''${SKIP_GITIGNORE:-false} - _soonix_log() { local level="$1" local hook="$2" @@ -289,16 +275,13 @@ in { _soonix_failed=() _soonix_uptodate=() - ${runHooks} + ${runHooks args} local output=$'\E[msoonix:\E[38;5;8m' local status=0 if [[ ''${#_soonix_updated[@]} -gt 0 ]]; then output="$output [updated: ''${_soonix_updated[*]}]" >&2 - if [[ "$CHECK_MODE" == "true" ]]; then - status=2 - fi fi if [[ ''${#_soonix_uptodate[@]} -gt 0 ]]; then output="$output [unchanged: ''${_soonix_uptodate[*]}]" >&2 @@ -310,8 +293,8 @@ in { printf "%s\E[m\n" "$output" >&2 - if [[ $status -ne 0 ]]; then - exit $status + if [[ $status -eq 1 ]]; then + exit 1 fi ''; @@ -325,7 +308,7 @@ in { # nothing to do if no hooks exist shellHook = if (builtins.length (builtins.attrNames config.hooks) > 0) - then generateShellHook + then generateShellHook {} else ""; shellHookFile = pkgs.writeShellScript "shellHook" shellHook; devshellModule = { @@ -342,7 +325,6 @@ in { set -euo pipefail SKIP_GITIGNORE=false - CHECK_MODE=false COMMAND="" show_help() { @@ -354,7 +336,6 @@ in { COMMANDS: update Update all managed files - check Check if all files are up to date (dry-run) list List all managed file targets help Show this help message @@ -371,7 +352,7 @@ in { while [[ $# -gt 0 ]]; do case "$1" in - update|check|list|help) + update|list|help) COMMAND="$1" shift ;; @@ -415,18 +396,17 @@ in { '') (builtins.attrNames hooks)} ;; update) - function _soonix() { - ${generateShellHook} - } - _soonix - ;; - check) - CHECK_MODE=true - echo "Checking files..." - function _soonix() { - ${generateShellHook} - } - _soonix + if [[ "$SKIP_GITIGNORE" == "true" ]]; then + function _soonix() { + ${generateShellHook {includeGitignored = false;}} + } + _soonix + else + function _soonix() { + ${generateShellHook {}} + } + _soonix + fi ;; esac ''; diff --git a/tests/soonix_test.nix b/tests/soonix_test.nix index 39b6bbf..e5d266a 100644 --- a/tests/soonix_test.nix +++ b/tests/soonix_test.nix @@ -87,7 +87,6 @@ in { assert_file_contains "${soonixBin}" "test.json" assert_file_contains "${soonixBin}" "SKIP_GITIGNORE" - assert_file_contains "${soonixBin}" "CHECK_MODE" ''; } ];