diff --git a/README.md b/README.md index f0131b9..1e3c719 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,12 @@ and you can override the default version. Available configurations: - `:version` the binary release version to use (see [numscript-wasm releases](https://github.com/PagoPlus/numscript-wasm/releases/)). - `:retries` number of times to retry the download in case of a network failure. +- `:binary_path` where the WASM binary file will be downloaded Ex: ```elixir config :numscriptex, + binary_path: :numscriptex |> :code.priv_dir() |> Path.join("numscript.wasm") version: "0.0.2", retries: 3 ``` diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..c4aba13 --- /dev/null +++ b/config/config.exs @@ -0,0 +1,4 @@ +import Config + +config :numscriptex, + binary_path: System.get_env("NUMSCRIPTEX_BINARY_PATH") diff --git a/lib/numscriptex/assets_manager.ex b/lib/numscriptex/assets_manager.ex index 1db1e43..99fb043 100755 --- a/lib/numscriptex/assets_manager.ex +++ b/lib/numscriptex/assets_manager.ex @@ -10,10 +10,11 @@ defmodule Numscriptex.AssetsManager do @retries Application.compile_env(:numscriptex, :retries, 3) @numscript_checksums_url "https://github.com/PagoPlus/numscript-wasm/releases/download/v#{@numscript_wasm_version}/numscript_checksums.txt" @numscript_wasm_url "https://github.com/PagoPlus/numscript-wasm/releases/download/v#{@numscript_wasm_version}/numscript.wasm" - @binary_path :numscriptex - |> :code.priv_dir() - |> Path.join("numscript.wasm") - |> to_charlist() + + @default_binary_path :numscriptex + |> :code.priv_dir() + |> Path.join("numscript.wasm") + |> to_charlist() defmacro ensure_wasm_binary_is_valid do quote do @@ -21,7 +22,7 @@ defmodule Numscriptex.AssetsManager do File.mkdir_p!(:code.priv_dir(:numscriptex)) - if File.exists?(unquote(@binary_path)) do + if File.exists?(unquote(binary_path())) do unquote(maybe_retry_download(compare_checksums())) else unquote(maybe_retry_download(download_and_compare_binary())) @@ -29,7 +30,15 @@ defmodule Numscriptex.AssetsManager do end end - def binary_path, do: @binary_path + def binary_path do + path = Application.get_env(:numscriptex, :binary_path, @default_binary_path) + + if !is_nil(path) && path != "" do + to_charlist(path) + else + @default_binary_path + end + end def hash_wasm_binary do # Logic explanation: @@ -42,7 +51,7 @@ defmodule Numscriptex.AssetsManager do # 3. Uses `:crypto.hash_final/1` to finalize the streaming hash calculation; # 4. Encode the hash to the hexadecimal base (also the same as the checksums). quote do - File.stream!(unquote(@binary_path), 1024) + File.stream!(unquote(binary_path()), 1024) |> Enum.reduce(:crypto.hash_init(:sha256), fn line, acc -> :crypto.hash_update(acc, line) end) @@ -124,7 +133,7 @@ defmodule Numscriptex.AssetsManager do :get, {unquote(@numscript_wasm_url), []}, [], - stream: unquote(@binary_path) + stream: unquote(binary_path()) ) case request do @@ -138,7 +147,7 @@ defmodule Numscriptex.AssetsManager do {:error, reason} -> Logger.error( - "Failed to download Numscript-WASM binary. Reason: #{inspect(reason)}. Binary path: #{unquote(@binary_path)}" + "Failed to download Numscript-WASM binary. Reason: #{inspect(reason)}. Binary path: #{unquote(binary_path())}" ) raise CompileError @@ -148,7 +157,7 @@ defmodule Numscriptex.AssetsManager do defp maybe_delete_wasm_binary do quote do - case File.rm(unquote(@binary_path)) do + case File.rm(unquote(binary_path())) do :ok -> :ok diff --git a/test/numscriptex/assets_manager_test.exs b/test/numscriptex/assets_manager_test.exs index d4f46a9..3d261ee 100644 --- a/test/numscriptex/assets_manager_test.exs +++ b/test/numscriptex/assets_manager_test.exs @@ -61,5 +61,17 @@ defmodule Numscriptex.AssetsManagerTest do File.copy!(dest_path, binary_path) end + + test "uses custom binary path when configured" do + new_binary_path = System.tmp_dir() |> Path.join("test_binary.wasm") |> to_charlist() + + refute AssetsManager.binary_path() == new_binary_path + + Application.put_env(:numscriptex, :binary_path, new_binary_path) + + assert AssetsManager.binary_path() == new_binary_path + + on_exit(fn -> Application.delete_env(:numscriptex, :binary_path) end) + end end end