@@ -10,26 +10,35 @@ defmodule Numscriptex.AssetsManager do
1010 @ retries Application . compile_env ( :numscriptex , :retries , 3 )
1111 @ numscript_checksums_url "https://github.com/PagoPlus/numscript-wasm/releases/download/v#{ @ numscript_wasm_version } /numscript_checksums.txt"
1212 @ numscript_wasm_url "https://github.com/PagoPlus/numscript-wasm/releases/download/v#{ @ numscript_wasm_version } /numscript.wasm"
13- @ binary_path :numscriptex
14- |> :code . priv_dir ( )
15- |> Path . join ( "numscript.wasm" )
16- |> to_charlist ( )
13+
14+ @ default_binary_path :numscriptex
15+ |> :code . priv_dir ( )
16+ |> Path . join ( "numscript.wasm" )
17+ |> to_charlist ( )
1718
1819 defmacro ensure_wasm_binary_is_valid do
1920 quote do
2021 require Logger
2122
2223 File . mkdir_p! ( :code . priv_dir ( :numscriptex ) )
2324
24- if File . exists? ( unquote ( @ binary_path ) ) do
25+ if File . exists? ( unquote ( binary_path ( ) ) ) do
2526 unquote ( maybe_retry_download ( compare_checksums ( ) ) )
2627 else
2728 unquote ( maybe_retry_download ( download_and_compare_binary ( ) ) )
2829 end
2930 end
3031 end
3132
32- def binary_path , do: @ binary_path
33+ def binary_path do
34+ path = Application . get_env ( :numscriptex , :binary_path , @ default_binary_path )
35+
36+ if ! is_nil ( path ) && path != "" do
37+ to_charlist ( path )
38+ else
39+ @ default_binary_path
40+ end
41+ end
3342
3443 def hash_wasm_binary do
3544 # Logic explanation:
@@ -42,7 +51,7 @@ defmodule Numscriptex.AssetsManager do
4251 # 3. Uses `:crypto.hash_final/1` to finalize the streaming hash calculation;
4352 # 4. Encode the hash to the hexadecimal base (also the same as the checksums).
4453 quote do
45- File . stream! ( unquote ( @ binary_path ) , 1024 )
54+ File . stream! ( unquote ( binary_path ( ) ) , 1024 )
4655 |> Enum . reduce ( :crypto . hash_init ( :sha256 ) , fn line , acc ->
4756 :crypto . hash_update ( acc , line )
4857 end )
@@ -124,7 +133,7 @@ defmodule Numscriptex.AssetsManager do
124133 :get ,
125134 { unquote ( @ numscript_wasm_url ) , [ ] } ,
126135 [ ] ,
127- stream: unquote ( @ binary_path )
136+ stream: unquote ( binary_path ( ) )
128137 )
129138
130139 case request do
@@ -138,7 +147,7 @@ defmodule Numscriptex.AssetsManager do
138147
139148 { :error , reason } ->
140149 Logger . error (
141- "Failed to download Numscript-WASM binary. Reason: #{ inspect ( reason ) } . Binary path: #{ unquote ( @ binary_path ) } "
150+ "Failed to download Numscript-WASM binary. Reason: #{ inspect ( reason ) } . Binary path: #{ unquote ( binary_path ( ) ) } "
142151 )
143152
144153 raise CompileError
@@ -148,7 +157,7 @@ defmodule Numscriptex.AssetsManager do
148157
149158 defp maybe_delete_wasm_binary do
150159 quote do
151- case File . rm ( unquote ( @ binary_path ) ) do
160+ case File . rm ( unquote ( binary_path ( ) ) ) do
152161 :ok ->
153162 :ok
154163
0 commit comments