Skip to content

Commit 250848c

Browse files
committed
Make mix zig.get more ergonomic
1 parent 642217f commit 250848c

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

installer/mix.tasks/zig.get.ex

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ defmodule Mix.Tasks.Zig.Get do
3939
4040
- `TAR_COMMAND`: path to a tar executable that is equivalent to gnu tar.
4141
only useful for non-windows architectures.
42-
- `NO_VERIFY`: disable signature verification of the downloaded file.
43-
Not recommended.
42+
- `NO_VERIFY`: disable signature verification of the downloaded file.
43+
Not recommended.
4444
- `ZIG_ARCHIVE_PATH`: path to desired directory to achive the zig compiler toolchain.
4545
"""
4646

47-
defstruct ~w(version path arch os url file verify hash)a
47+
defstruct ~w(version path arch os url file verify hash force)a
4848

4949
def run(app_opts) do
5050
# Elixir 1.14 cannot take a list of applications for this function
@@ -86,6 +86,10 @@ defmodule Mix.Tasks.Zig.Get do
8686
parse_opts(rest, %{so_far | arch: arch})
8787
end
8888

89+
defp parse_opts(["--force" | rest], so_far) do
90+
parse_opts(rest, %{so_far | force: true})
91+
end
92+
8993
defp set_archive_path(opts) do
9094
case System.get_env("ZIG_ARCHIVE_PATH", "") do
9195
"" -> opts
@@ -102,7 +106,8 @@ defmodule Mix.Tasks.Zig.Get do
102106
version: @default_version,
103107
path: :filename.basedir(:user_cache, ~C"zigler"),
104108
os: os,
105-
arch: arch
109+
arch: arch,
110+
force: false
106111
}
107112
end
108113

@@ -127,13 +132,16 @@ defmodule Mix.Tasks.Zig.Get do
127132
defp ensure_destination(opts) do
128133
target_directory = Path.join(opts.path, "zig-#{opts.os}-#{opts.arch}-#{opts.version}")
129134

130-
if File.exists?(target_directory) do
131-
Mix.raise(
132-
"destination directory #{target_directory} already exists. Please remove it and try again."
133-
)
135+
cond do
136+
File.exists?(target_directory) && opts.force ->
137+
File.rm_rf!(target_directory)
138+
File.exists?(target_directory) ->
139+
Mix.shell().info("zig is already installed, rerun with --force to overwrite")
140+
System.halt()
141+
true -> :nothing_to_do
134142
end
135143

136-
File.mkdir_p(opts.path)
144+
File.mkdir_p!(opts.path)
137145

138146
opts
139147
end

0 commit comments

Comments
 (0)