Skip to content

Commit 1009945

Browse files
committed
check the contents of the output directory to see if the assets module needs to be rebuilt
1 parent dc056f0 commit 1009945

File tree

1 file changed

+83
-22
lines changed

1 file changed

+83
-22
lines changed

lib/scenic/assets/static.ex

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,26 @@ defmodule Scenic.Assets.Static do
192192
# returns a boolean indicating if this module should
193193
# be recompiled
194194
def __mix_recompile__?() do
195-
hash =
195+
# hash =
196+
# unquote(using_opts)
197+
# |> Keyword.get(:sources, ["assets"])
198+
# |> Enum.reduce([], fn
199+
# source, acc when is_bitstring(source) ->
200+
# [Path.wildcard("#{source}/**/*.{jpg,jpeg,png,ttf}") | acc]
201+
202+
# _, acc ->
203+
# acc
204+
# end)
205+
# |> List.flatten()
206+
# |> Enum.uniq()
207+
# |> :erlang.md5()
208+
209+
# hash != @paths_hash
210+
Scenic.Assets.Static.compile_assets?(
211+
library(),
212+
@paths_hash,
196213
unquote(using_opts)
197-
|> Keyword.get(:sources, ["assets"])
198-
|> Enum.reduce([], fn
199-
source, acc when is_bitstring(source) ->
200-
[Path.wildcard("#{source}/**/*.{jpg,jpeg,png,ttf}") | acc]
201-
202-
_, acc ->
203-
acc
204-
end)
205-
|> List.flatten()
206-
|> Enum.uniq()
207-
|> :erlang.md5()
208-
209-
hash != @paths_hash
214+
)
210215
end
211216

212217
# @library Scenic.Assets.Static.build(__MODULE__, unquote(using_opts))
@@ -243,17 +248,64 @@ defmodule Scenic.Assets.Static do
243248
metas: map,
244249
hash_type: :sha3_256,
245250
module: module,
246-
otp_app: atom
251+
otp_app: atom,
252+
meta_hash: binary
247253
}
248254

249-
defstruct aliases: %{}, metas: %{}, hash_type: @hash_type, module: nil, otp_app: nil
255+
defstruct aliases: %{},
256+
metas: %{},
257+
hash_type: @hash_type,
258+
module: nil,
259+
otp_app: nil,
260+
meta_hash: ""
250261

251262
# ===========================================================================
252263
defmodule Error do
253264
@moduledoc false
254265
defexception message: nil, error: nil, id: nil
255266
end
256267

268+
# --------------------------------------------------------
269+
# called during __mix_recompile__?() from the assets module
270+
def compile_assets?(library, paths_hash, opts) do
271+
assets_changed?(paths_hash, opts) || fix_cache?(library, opts)
272+
end
273+
274+
defp assets_changed?(paths_hash, opts) do
275+
hash =
276+
opts
277+
|> Keyword.get(:sources, [@default_src_dir])
278+
|> Enum.reduce([], fn
279+
source, acc when is_bitstring(source) ->
280+
[Path.wildcard("#{source}/**/*.{jpg,jpeg,png,ttf}") | acc]
281+
282+
_, acc ->
283+
acc
284+
end)
285+
|> List.flatten()
286+
|> Enum.uniq()
287+
|> :erlang.md5()
288+
289+
hash != paths_hash
290+
end
291+
292+
defp fix_cache?(library, opts) do
293+
opts[:otp_app]
294+
|> :code.lib_dir()
295+
|> Path.join(dst_dir())
296+
|> File.ls()
297+
|> case do
298+
{:ok, files} -> meta_hash(files) != library.meta_hash
299+
_ -> true
300+
end
301+
end
302+
303+
defp meta_hash(files) when is_list(files) do
304+
files
305+
|> Enum.sort()
306+
|> :erlang.md5()
307+
end
308+
257309
# ===========================================================================
258310

259311
# --------------------------------------------------------
@@ -413,12 +465,21 @@ defmodule Scenic.Assets.Static do
413465
# add the default aliases
414466
library = Enum.reduce(@default_aliases, library, &add_alias(&2, &1))
415467

416-
# add any additional aliases, then return
417-
case opts[:alias] || opts[:aliases] do
418-
nil -> []
419-
aliases -> aliases
420-
end
421-
|> Enum.reduce(library, &add_alias(&2, &1))
468+
# add any additional aliases
469+
library =
470+
case opts[:alias] || opts[:aliases] do
471+
nil -> []
472+
aliases -> aliases
473+
end
474+
|> Enum.reduce(library, &add_alias(&2, &1))
475+
476+
# finally add the meta_hash
477+
meta_hash =
478+
library.metas
479+
|> Enum.map(fn {k, _v} -> k end)
480+
|> meta_hash()
481+
482+
Map.put(library, :meta_hash, meta_hash)
422483
end
423484

424485
# --------------------------------------------------------

0 commit comments

Comments
 (0)