@@ -192,21 +192,26 @@ defmodule Scenic.Assets.Static do
192
192
# returns a boolean indicating if this module should
193
193
# be recompiled
194
194
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 ,
196
213
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
+ )
210
215
end
211
216
212
217
# @library Scenic.Assets.Static.build(__MODULE__, unquote(using_opts))
@@ -243,17 +248,64 @@ defmodule Scenic.Assets.Static do
243
248
metas: map ,
244
249
hash_type: :sha3_256 ,
245
250
module: module ,
246
- otp_app: atom
251
+ otp_app: atom ,
252
+ meta_hash: binary
247
253
}
248
254
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: ""
250
261
251
262
# ===========================================================================
252
263
defmodule Error do
253
264
@ moduledoc false
254
265
defexception message: nil , error: nil , id: nil
255
266
end
256
267
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
+
257
309
# ===========================================================================
258
310
259
311
# --------------------------------------------------------
@@ -413,12 +465,21 @@ defmodule Scenic.Assets.Static do
413
465
# add the default aliases
414
466
library = Enum . reduce ( @ default_aliases , library , & add_alias ( & 2 , & 1 ) )
415
467
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 )
422
483
end
423
484
424
485
# --------------------------------------------------------
0 commit comments