Skip to content

Commit 22a5511

Browse files
authored
Generate individual checksum file for each target (#84)
1 parent e41e23c commit 22a5511

File tree

3 files changed

+73
-27
lines changed

3 files changed

+73
-27
lines changed

lib/elixir_make/artefact.ex

+22-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,33 @@ defmodule ElixirMake.Artefact do
2323
cache_dir
2424
end
2525

26+
@doc """
27+
Returns the checksum algorithm
28+
"""
29+
def checksum_algo do
30+
@checksum_algo
31+
end
32+
2633
@doc """
2734
Computes the checksum and artefact for the given contents.
2835
"""
2936
def checksum(basename, contents) do
30-
hash = :crypto.hash(@checksum_algo, contents)
37+
hash = :crypto.hash(checksum_algo(), contents)
3138
checksum = Base.encode16(hash, case: :lower)
32-
%Artefact{basename: basename, checksum: checksum, checksum_algo: @checksum_algo}
39+
%Artefact{basename: basename, checksum: checksum, checksum_algo: checksum_algo()}
40+
end
41+
42+
@doc """
43+
Writes checksum for the target to disk.
44+
"""
45+
def write_checksum_for_target!(%Artefact{
46+
basename: basename,
47+
checksum: checksum,
48+
checksum_algo: checksum_algo
49+
}) do
50+
cache_dir = Artefact.cache_dir()
51+
file = Path.join(cache_dir, "#{basename}.#{Atom.to_string(checksum_algo)}")
52+
File.write!(file, [checksum, " ", basename, "\n"])
3353
end
3454

3555
@doc """

lib/mix/tasks/elixir_make.checksum.ex

+49-25
Original file line numberDiff line numberDiff line change
@@ -103,38 +103,62 @@ defmodule Mix.Tasks.ElixirMake.Checksum do
103103
tasks =
104104
Task.async_stream(
105105
urls,
106-
fn {{_target, _nif_version}, url} -> {url, Artefact.download(url)} end,
106+
fn {{_target, _nif_version}, url} ->
107+
checksum_algo = Artefact.checksum_algo()
108+
checksum_file_url = "#{url}.#{Atom.to_string(checksum_algo)}"
109+
artifact_checksum = Artefact.download(checksum_file_url)
110+
111+
with {:ok, body} <- artifact_checksum,
112+
[checksum, basename] <- String.split(body, " ", trim: true) do
113+
{:checksum, url,
114+
%Artefact{
115+
basename: String.trim(basename),
116+
checksum: checksum,
117+
checksum_algo: checksum_algo
118+
}}
119+
else
120+
_ -> {:download, url, Artefact.download(url)}
121+
end
122+
end,
107123
timeout: :infinity,
108124
ordered: false
109125
)
110126

111127
cache_dir = Artefact.cache_dir()
112128

113-
Enum.flat_map(tasks, fn {:ok, {url, download}} ->
114-
case download do
115-
{:ok, body} ->
116-
basename = basename_from_url(url)
117-
path = Path.join(cache_dir, basename)
118-
File.write!(path, body)
119-
artefact = Artefact.checksum(basename, body)
120-
121-
Mix.shell().info(
122-
"NIF cached at #{path} with checksum #{artefact.checksum} (#{artefact.checksum_algo})"
123-
)
124-
125-
[artefact]
126-
127-
result ->
128-
if ignore_unavailable? do
129-
msg = "Skipped unavailable NIF artifact. Reason: #{inspect(result)}"
130-
Mix.shell().info(msg)
131-
else
132-
msg = "Could not finish the download of NIF artifacts. Reason: #{inspect(result)}"
133-
Mix.shell().error(msg)
134-
end
129+
Enum.flat_map(tasks, fn
130+
{:ok, {:checksum, _url, artefact}} ->
131+
Mix.shell().info(
132+
"NIF checksum file with checksum #{artefact.checksum} (#{artefact.checksum_algo})"
133+
)
135134

136-
[]
137-
end
135+
[artefact]
136+
137+
{:ok, {:download, url, download}} ->
138+
case download do
139+
{:ok, body} ->
140+
basename = basename_from_url(url)
141+
path = Path.join(cache_dir, basename)
142+
File.write!(path, body)
143+
artefact = Artefact.checksum(basename, body)
144+
145+
Mix.shell().info(
146+
"NIF cached at #{path} with checksum #{artefact.checksum} (#{artefact.checksum_algo})"
147+
)
148+
149+
[artefact]
150+
151+
result ->
152+
if ignore_unavailable? do
153+
msg = "Skipped unavailable NIF artifact. Reason: #{inspect(result)}"
154+
Mix.shell().info(msg)
155+
else
156+
msg = "Could not finish the download of NIF artifacts. Reason: #{inspect(result)}"
157+
Mix.shell().error(msg)
158+
end
159+
160+
[]
161+
end
138162
end)
139163
end
140164

lib/mix/tasks/elixir_make.precompile.ex

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ defmodule Mix.Tasks.ElixirMake.Precompile do
4848
precompiler.post_precompile_target(target)
4949
end
5050

51+
Artefact.write_checksum_for_target!(precompiled_artefacts)
52+
5153
precompiled_artefacts
5254

5355
{:error, msg} ->

0 commit comments

Comments
 (0)