Skip to content

Commit ef32d89

Browse files
committed
track assets in the assets folder as external dependencies of the assets module.
1 parent d0571c9 commit ef32d89

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

lib/scenic/assets/static.ex

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,52 @@ defmodule Scenic.Assets.Static do
163163

164164
# import IEx
165165

166+
# https://hexdocs.pm/mix/1.12/Mix.Tasks.Compile.Elixir.html
167+
166168
# ===========================================================================
167169
# the using macro for scenes adopting this behavior
168170
defmacro __using__(using_opts \\ []) do
169171
quote do
172+
# this section of code is to "watch" the assets folder to look for changes
173+
# it does this by marking the external asset files as objects that the
174+
# module depends on
175+
@sources Keyword.get(unquote(using_opts), :sources, ["assets"])
176+
@paths Enum.reduce(@sources, [], fn
177+
source, acc when is_bitstring(source) ->
178+
[Path.wildcard("#{source}/**/*.{jpg,jpeg,png,ttf}") | acc]
179+
180+
_, acc ->
181+
acc
182+
end)
183+
|> List.flatten()
184+
|> Enum.uniq()
185+
@paths_hash :erlang.md5(@paths)
186+
187+
for path <- @paths do
188+
@external_resource path
189+
end
190+
191+
# called every time compile is run.
192+
# returns a boolean indicating if this module should
193+
# be recompiled
194+
def __mix_recompile__?() do
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+
end
211+
170212
# @library Scenic.Assets.Static.build(__MODULE__, unquote(using_opts))
171213
@library Scenic.Assets.Static.build!(__MODULE__, unquote(using_opts))
172214
def library(), do: @library

0 commit comments

Comments
 (0)