Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: hook not executed when only config is updated #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,26 @@ This command will:

### Hooks

You can define `post_install` and `post_purge` hooks in your module's `init.lua` to run arbitrary commands after the module has been installed or purged.
You can define custom hooks in your module's `init.lua` file to run specific commands at different stages of the module's lifecycle.

#### Available Hooks

- **`post_install`**: Runs after the specified packages have been installed via `brew`.
- **`post_purge`**: Runs after the specified packages have been removed via `brew`.
- **`post_link`**: Runs after configuration files have been linked.
- **`post_unlink`**: Runs after configuration files have been unlinked.

#### Basic Hook Definition
```lua
return {
brew = { "gh" },
post_install = "gh auth login"
}
```

You can also define multi-line hooks:
#### Multi-Line Hook Definition

You can also define multi-line hooks using Lua's multi-line string syntax.

```lua
return {
Expand Down
27 changes: 21 additions & 6 deletions dot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,12 @@ end

local function handle_config_symlink(config, module_dir, options)
if not config.config then
return
return false
end

local configs = type(config.config) == "table" and config.config[1] and config.config or { config.config }
local all_configs_linked = true
local config_changed = false

for _, cfg in ipairs(configs) do
local source = os.getenv "PWD" .. "/" .. module_dir:gsub("^./", "") .. "/" .. cfg.source:gsub("^./", "")
Expand All @@ -387,6 +388,7 @@ local function handle_config_symlink(config, module_dir, options)
local success, err = delete_path(output)
if success then
print_message("success", "config → removed " .. output)
config_changed = true
else
print_message("error", "config → " .. err)
end
Expand All @@ -404,13 +406,14 @@ local function handle_config_symlink(config, module_dir, options)
local success, err = ensure_parent_directory(output)
if not success then
print_message("error", "config → " .. err)
return
return false
end

-- Copy source to output
local success, err = copy_path(source, output)
if success then
print_message("success", "config → copied " .. source .. " to " .. output)
config_changed = true
else
print_message("error", "config → " .. err)
end
Expand All @@ -433,19 +436,19 @@ local function handle_config_symlink(config, module_dir, options)
print_message("warning", "config → existing config backed up to " .. result)
else
print_message("error", "config → " .. result)
return
return false
end
else
print_message("error", "config → file already exists at " .. output .. ". Use -f to force.")
return
return false
end
end

-- Ensure parent directory exists
local success, err = ensure_parent_directory(output)
if not success then
print_message("error", "config → " .. err)
return
return false
end

local cmd = string.format('ln -sf "%s" "%s"', source, output)
Expand All @@ -454,6 +457,7 @@ local function handle_config_symlink(config, module_dir, options)
print_message("error", "config → failed to create symlink: " .. error_output)
else
print_message("success", "config → symlink created for " .. output)
config_changed = true
end
end
end
Expand All @@ -466,6 +470,8 @@ local function handle_config_symlink(config, module_dir, options)
elseif all_configs_linked and options.unlink_mode then
print_message("success", "all configurations are unlinked")
end

return config_changed
end

-- Process each module by installing/uninstalling dependencies and managing symlinks
Expand All @@ -490,7 +496,7 @@ local function process_module(module_name, options)

local dependencies_changed = process_brew_dependencies(config, options.purge_mode)

handle_config_symlink(config, module_dir, options)
local config_changed = handle_config_symlink(config, module_dir, options)

-- Run post_install or post_purge hooks
if dependencies_changed or options.hooks_mode then
Expand All @@ -501,6 +507,15 @@ local function process_module(module_name, options)
end
end

-- Run post_link or post_unlink hooks
if config_changed or options.hooks_mode then
if options.purge_mode and config.post_unlink then
run_hook(config.post_unlink, "post-unlink")
elseif not options.purge_mode and config.post_link then
run_hook(config.post_link, "post-link")
end
end

print "" -- Add a blank line between modules
end

Expand Down
12 changes: 12 additions & 0 deletions spec/dot_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ return {
},
post_install = "touch %s/.hooks_post_install_ran",
post_purge = "touch %s/.hooks_post_purge_ran",
post_link = "touch %s/.hooks_post_link_ran",
post_unlink = "touch %s/.hooks_post_unlink_ran",
}
]],
home_dir,
Expand All @@ -430,13 +432,23 @@ return {
print("Checking for hook_install at:", hook_install)
assert.is_true(path_exists(hook_install), "Post-install hook did not run")

-- Check if post_link hook ran
local hook_link = pl_path.join(home_dir, ".hooks_post_link_ran")
print("Checking for hook_link at:", hook_link)
assert.is_true(path_exists(hook_link), "Post-link hook did not run")

-- Run dot.lua with --purge option for 'dummy_app'
assert.is_true(run_dot "--purge dummy_app")

-- Check if post_purge hook ran
local hook_purge = pl_path.join(home_dir, ".hooks_post_purge_ran")
print("Checking for hook_purge at:", hook_purge)
assert.is_true(path_exists(hook_purge), "Post-purge hook did not run")

-- Check if post_unlink hook ran
local hook_unlink = pl_path.join(home_dir, ".hooks_post_unlink_ran")
print("Checking for hook_unlink at:", hook_unlink)
assert.is_true(path_exists(hook_unlink), "Post-unlink hook did not run")
end)

it("should handle multiple configs in a module", function()
Expand Down
Loading