@@ -163,10 +163,52 @@ defmodule Scenic.Assets.Static do
163
163
164
164
# import IEx
165
165
166
+ # https://hexdocs.pm/mix/1.12/Mix.Tasks.Compile.Elixir.html
167
+
166
168
# ===========================================================================
167
169
# the using macro for scenes adopting this behavior
168
170
defmacro __using__ ( using_opts \\ [ ] ) do
169
171
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
+
170
212
# @library Scenic.Assets.Static.build(__MODULE__, unquote(using_opts))
171
213
@ library Scenic.Assets.Static . build! ( __MODULE__ , unquote ( using_opts ) )
172
214
def library ( ) , do: @ library
0 commit comments