Skip to content

Commit 7d72f3a

Browse files
committed
Introduce _dry-run_ install option
This is useful to skip the hooks installation and debug the output. Needed for testing the install module.
1 parent 37875d4 commit 7d72f3a

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

lib/mix/tasks/git_hooks/install.ex

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ defmodule Mix.Tasks.GitHooks.Install do
1515
```elixir
1616
mix git_hooks.install`
1717
```
18-
1918
"""
2019

2120
use Mix.Task
@@ -28,7 +27,10 @@ defmodule Mix.Tasks.GitHooks.Install do
2827
@spec run(Keyword.t()) :: :ok
2928
def run(args) do
3029
{opts, _other_args, _} =
31-
OptionParser.parse(args, switches: [quiet: :boolean], aliases: [q: :quiet])
30+
OptionParser.parse(args,
31+
switches: [quiet: :boolean, dry_run: :boolean],
32+
aliases: [q: :quiet]
33+
)
3234

3335
install(opts)
3436
end
@@ -49,38 +51,48 @@ defmodule Mix.Tasks.GitHooks.Install do
4951
clean_missing_hooks()
5052
track_configured_hooks()
5153

52-
Config.git_hooks()
53-
|> Enum.each(fn git_hook ->
54-
git_hook_atom_as_string = Atom.to_string(git_hook)
55-
git_hook_atom_as_kebab_string = Recase.to_kebab(git_hook_atom_as_string)
56-
57-
case File.read(template_file) do
58-
{:ok, body} ->
59-
target_file_path = GitPath.git_hooks_path_for(git_hook_atom_as_kebab_string)
60-
61-
target_file_body =
62-
body
63-
|> String.replace("$git_hook", git_hook_atom_as_string)
64-
|> String.replace("$mix_path", mix_path)
65-
|> String.replace("$project_path", project_path)
66-
67-
unless opts[:quiet] || !Config.verbose?() do
68-
Printer.info(
69-
"Writing git hook for `#{git_hook_atom_as_string}` to `#{target_file_path}`"
70-
)
71-
end
72-
73-
backup_current_hook(git_hook_atom_as_kebab_string, opts)
74-
75-
File.write(target_file_path, target_file_body)
76-
File.chmod(target_file_path, 0o755)
77-
78-
{:error, reason} ->
79-
reason |> inspect() |> Printer.error()
80-
end
81-
end)
82-
83-
:ok
54+
git_hooks_configs = Config.git_hooks()
55+
56+
install_result =
57+
Enum.map(git_hooks_configs, fn git_hook ->
58+
git_hook_atom_as_string = Atom.to_string(git_hook)
59+
git_hook_atom_as_kebab_string = Recase.to_kebab(git_hook_atom_as_string)
60+
61+
case File.read(template_file) do
62+
{:ok, body} ->
63+
target_file_path = GitPath.git_hooks_path_for(git_hook_atom_as_kebab_string)
64+
65+
target_file_body =
66+
body
67+
|> String.replace("$git_hook", git_hook_atom_as_string)
68+
|> String.replace("$mix_path", mix_path)
69+
|> String.replace("$project_path", project_path)
70+
71+
unless opts[:quiet] || !Config.verbose?() do
72+
Printer.info(
73+
"Writing git hook for `#{git_hook_atom_as_string}` to `#{target_file_path}`"
74+
)
75+
end
76+
77+
backup_current_hook(git_hook_atom_as_kebab_string, opts)
78+
79+
if opts[:dry_run] do
80+
{git_hook, target_file_body}
81+
else
82+
File.write(target_file_path, target_file_body)
83+
File.chmod(target_file_path, 0o755)
84+
end
85+
86+
{:error, reason} ->
87+
reason |> inspect() |> Printer.error()
88+
end
89+
end)
90+
91+
if opts[:dry_run] do
92+
install_result
93+
else
94+
:ok
95+
end
8496
end
8597

8698
@spec backup_current_hook(String.t(), Keyword.t()) :: {:error, atom} | {:ok, non_neg_integer()}

0 commit comments

Comments
 (0)