Skip to content

Commit 6f5d38a

Browse files
authored
Merge pull request #17 from PagoPlus/feat/allow-wasm-binary-path-on-config
[FEAT] Allow wasm binary path on config
2 parents c9e7ba9 + 4733145 commit 6f5d38a

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ and you can override the default version.
2828
Available configurations:
2929
- `:version` the binary release version to use (see [numscript-wasm releases](https://github.com/PagoPlus/numscript-wasm/releases/)).
3030
- `:retries` number of times to retry the download in case of a network failure.
31+
- `:binary_path` where the WASM binary file will be downloaded
3132

3233
Ex:
3334
```elixir
3435
config :numscriptex,
36+
binary_path: :numscriptex |> :code.priv_dir() |> Path.join("numscript.wasm")
3537
version: "0.0.2",
3638
retries: 3
3739
```

config/config.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Config
2+
3+
config :numscriptex,
4+
binary_path: System.get_env("NUMSCRIPTEX_BINARY_PATH")

lib/numscriptex/assets_manager.ex

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/numscriptex/assets_manager_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,17 @@ defmodule Numscriptex.AssetsManagerTest do
6161

6262
File.copy!(dest_path, binary_path)
6363
end
64+
65+
test "uses custom binary path when configured" do
66+
new_binary_path = System.tmp_dir() |> Path.join("test_binary.wasm") |> to_charlist()
67+
68+
refute AssetsManager.binary_path() == new_binary_path
69+
70+
Application.put_env(:numscriptex, :binary_path, new_binary_path)
71+
72+
assert AssetsManager.binary_path() == new_binary_path
73+
74+
on_exit(fn -> Application.delete_env(:numscriptex, :binary_path) end)
75+
end
6476
end
6577
end

0 commit comments

Comments
 (0)