@@ -7,7 +7,11 @@ defmodule Mix.Tasks.TemplateAssets do
7
7
def run ( _ ) do
8
8
Mix.Task . run ( "app.start" )
9
9
10
+ IO . puts ( "Running mix template_assets in #{ project_dir ( ) } " )
11
+ IO . puts ( "Template path: #{ templates_path ( ) } " )
12
+
10
13
image_paths = Path . wildcard ( Path . join ( templates_path ( ) , "/**/*.{png,jpg,jpeg,gif}" ) )
14
+
11
15
for image_path <- image_paths do
12
16
process_image ( image_path )
13
17
end
@@ -20,23 +24,27 @@ defmodule Mix.Tasks.TemplateAssets do
20
24
Mix . shell ( ) . info ( "Processed #{ count } file(s)." )
21
25
end
22
26
23
- defp project_dir ( ) , do: File . cwd!
24
- defp templates_path ( ) , do: Path . join ( project_dir ( ) , "/apps/components_guide_web/lib/components_guide_web/templates" )
25
- defp static_collected_dir ( ) , do: Path . join ( project_dir ( ) , "/apps/components_guide_web/priv/static/collected" )
27
+ defp project_dir ( ) , do: File . cwd! ( )
28
+
29
+ defp templates_path ( ) ,
30
+ do: Path . join ( project_dir ( ) , "/apps/components_guide_web/lib/components_guide_web/templates" )
31
+
32
+ defp static_collected_dir ( ) ,
33
+ do: Path . join ( project_dir ( ) , "/apps/components_guide_web/priv/static/collected" )
26
34
27
35
defp process_image ( image_path ) do
28
36
data = File . read! ( image_path )
29
37
media_type = MIME . from_path ( image_path )
30
38
hash = :crypto . hash ( :sha256 , data )
31
39
hash_base64 = hash |> Base . url_encode64 ( )
32
-
40
+
33
41
destination_dir = Path . join ( static_collected_dir ( ) , media_type )
34
42
File . mkdir_p! ( destination_dir )
35
-
43
+
36
44
extension = MIME . extensions ( media_type ) |> List . first ( )
37
45
destination_path = Path . join ( destination_dir , "#{ hash_base64 } .#{ extension } " )
38
46
File . copy! ( image_path , destination_path )
39
-
47
+
40
48
Mix . shell ( ) . info ( "Copied #{ image_path } to #{ destination_path } " )
41
49
end
42
50
end
0 commit comments