Skip to content

Commit

Permalink
Resolve assets archive path based on priv path at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Jan 25, 2022
1 parent 67358e5 commit a4606b7
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/kino/js.ex
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,17 @@ defmodule Kino.JS do
assets = Enum.uniq_by(assets, &elem(&1, 0))

dir = dir_for_module(env.module)

File.rm_rf!(dir)
File.mkdir_p!(dir)
File.mkdir!(dir)

{archive_path, hash} = package_assets!(dir, assets)
hash = assets_hash(assets)
archive_path = __assets_archive_path__(env.module, hash)
package_assets!(assets, archive_path)

quote do
def __assets_info__() do
%{
archive_path: unquote(archive_path),
archive_path: Kino.JS.__assets_archive_path__(__MODULE__, unquote(hash)),
js_path: "main.js",
hash: unquote(hash)
}
Expand All @@ -317,16 +318,11 @@ defmodule Kino.JS do
|> Enum.sort()
end

defp package_assets!(dir_path, assets) do
hash = assets_hash(assets)
archive_path = Path.join(dir_path, hash <> ".tar.gz")

defp package_assets!(assets, archive_path) do
archive_content =
for {filename, content} <- assets, do: {String.to_charlist(filename), content}

:ok = :erl_tar.create(archive_path, archive_content, [:compressed])

{archive_path, hash}
end

defp assets_hash(assets) do
Expand All @@ -339,6 +335,11 @@ defmodule Kino.JS do
Base.encode32(md5_hash, case: :lower, padding: false)
end

def __assets_archive_path__(module, hash) do
dir = dir_for_module(module)
Path.join(dir, hash <> ".tar.gz")
end

defp dir_for_module(module) do
priv_dir = :code.priv_dir(:kino)
module_dir = module |> Module.split() |> Enum.join("_")
Expand Down

0 comments on commit a4606b7

Please sign in to comment.