Skip to content

Commit

Permalink
Extract Bun binary on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
crbelaus committed Dec 25, 2024
1 parent 704c97b commit 8da52e7
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions lib/bun.ex
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,13 @@ defmodule Bun do
end

def install do
version = configured_version()
tmp_opts = if System.get_env("MIX_XDG"), do: %{os: :linux}, else: %{}

tmp_dir =
freshdir_p(:filename.basedir(:user_cache, "phx-bun", tmp_opts)) ||
freshdir_p(Path.join(System.tmp_dir!(), "phx-bun")) ||
raise "could not install bun. Set MIX_XGD=1 and then set XDG_CACHE_HOME to the path you want to use as cache"

url = "https://github.com/oven-sh/bun/releases/download/bun-v#{version}/bun-#{target()}.zip"

zip = fetch_body!(url)
zip =
fetch_body!(
"https://github.com/oven-sh/bun/releases/download/bun-v#{configured_version()}/bun-#{target()}.zip"
)

# Certain Bun versions contain multiple files in the zip archive, we just want the Bun executable.
download_path =
case :zip.unzip(zip, cwd: to_charlist(tmp_dir), file_list: [~c[bun-#{target()}/bun]]) do
case extract_bun_binary(zip) do
{:ok, [download_path]} -> download_path
other -> raise "couldn't unpack archive: #{inspect(other)}"
end
Expand Down Expand Up @@ -266,6 +258,23 @@ defmodule Bun do
end
end

defp extract_bun_binary(zip) do
tmp_opts = if System.get_env("MIX_XDG"), do: %{os: :linux}, else: %{}

tmp_dir =
freshdir_p(:filename.basedir(:user_cache, "phx-bun", tmp_opts)) ||
freshdir_p(Path.join(System.tmp_dir!(), "phx-bun")) ||
raise "could not install bun. Set MIX_XGD=1 and then set XDG_CACHE_HOME to the path you want to use as cache"

# Certain Bun versions contain multiple files in the zip archive, we just want the Bun executable.
zipped_target =
if String.contains?(target(), "windows"),
do: "bun-#{target()}/bun.exe",
else: "bun-#{target()}/bun"

:zip.unzip(zip, cwd: to_charlist(tmp_dir), file_list: [to_charlist(zipped_target)])
end

defp fetch_body!(url) do
scheme = URI.parse(url).scheme
url = String.to_charlist(url)
Expand Down

0 comments on commit 8da52e7

Please sign in to comment.