diff --git a/lib/bundlex.ex b/lib/bundlex.ex index 60edc65..4bb9344 100644 --- a/lib/bundlex.ex +++ b/lib/bundlex.ex @@ -44,25 +44,16 @@ defmodule Bundlex do } end - {:ok, _} -> + {:ok, _crosscompile} -> target = - Map.new( - [ - architecture: "TARGET_ARCH", - vendor: "TARGET_VENDOR", - os: "TARGET_OS", - abi: "TARGET_ABI" - ], - fn {key, env} -> - value = - case System.fetch_env(env) do - {:ok, value} -> value - :error -> "unknown" - end - - {key, value} - end - ) + [ + architecture: "TARGET_ARCH", + vendor: "TARGET_VENDOR", + os: "TARGET_OS", + abi: "TARGET_ABI" + ] + |> Map.new(fn {key, env} -> {key, System.get_env(env, "unknown")} end) + |> Macro.escape() def get_target() do unquote(target) diff --git a/lib/bundlex/build_script.ex b/lib/bundlex/build_script.ex index 547c94e..aea9cc1 100644 --- a/lib/bundlex/build_script.ex +++ b/lib/bundlex/build_script.ex @@ -33,7 +33,7 @@ defmodule Bundlex.BuildScript do end def run(%__MODULE__{commands: commands}, platform) do - family = platform |> family!() + family = Platform.family(platform) cmd = commands |> join_commands(family) case cmd |> Mix.shell().cmd() do @@ -44,7 +44,7 @@ defmodule Bundlex.BuildScript do @spec store(t, Platform.name_t(), String.t()) :: {:ok, {String.t(), String.t()}} def store(%__MODULE__{commands: commands}, platform, name \\ "bundlex") do - family = platform |> family!() + family = Platform.family(platform) script_name = name <> @script_ext[family] script_prefix = @script_prefix[family] script = script_prefix <> (commands |> join_commands(family)) @@ -55,18 +55,11 @@ defmodule Bundlex.BuildScript do end end - defp join_commands(commands, :unix) do - Enum.map_join(commands, " && \\\n", &"(#{&1})") <> "\n" - end - defp join_commands(commands, :windows) do Enum.join(commands, "\r\n") <> "\r\n" end - defp family!(:windows32), do: :windows - defp family!(:windows64), do: :windows - defp family!(:macosx), do: :unix - defp family!(:linux), do: :unix - defp family!(:freebsd), do: :unix - defp family!(:nerves), do: :unix + defp join_commands(commands, _other) do + Enum.map_join(commands, " && \\\n", &"(#{&1})") <> "\n" + end end diff --git a/lib/bundlex/platform.ex b/lib/bundlex/platform.ex index 46a9a25..1c2b235 100644 --- a/lib/bundlex/platform.ex +++ b/lib/bundlex/platform.ex @@ -44,9 +44,48 @@ defmodule Bundlex.Platform do case System.fetch_env("CROSSCOMPILE") do :error -> - def get_target!(), do: get_host!() + def get_target!() do + case :os.type() do + {:win32, _} -> + {:ok, reg} = :win32reg.open([:read]) + + :ok = + :win32reg.change_key( + reg, + ~c"\\hklm\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" + ) + + {:ok, build} = :win32reg.value(reg, ~c"BuildLabEx") + + platform_name = + if build |> to_string |> String.contains?("amd64") do + :windows64 + else + :windows32 + end + + :ok = :win32reg.close(reg) + + platform_name + + {:unix, :linux} -> + :linux + + {:unix, :freebsd} -> + :freebsd + + {:unix, :darwin} -> + :macosx + + other -> + # TODO add detection for more platforms + Output.raise( + "Unable to detect current platform. Erlang returned #{inspect(other)} which I don't know how to handle." + ) + end + end - {:ok, _} -> + {:ok, _crosscompile} -> def get_target!() do case System.fetch_env("NERVES_APP") do {:ok, _app} -> @@ -62,45 +101,6 @@ defmodule Bundlex.Platform do end end - @spec get_host!() :: name_t - defp get_host!() do - case :os.type() do - {:win32, _} -> - {:ok, reg} = :win32reg.open([:read]) - - :ok = - :win32reg.change_key(reg, ~c"\\hklm\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion") - - {:ok, build} = :win32reg.value(reg, ~c"BuildLabEx") - - platform_name = - if build |> to_string |> String.contains?("amd64") do - :windows64 - else - :windows32 - end - - :ok = :win32reg.close(reg) - - platform_name - - {:unix, :linux} -> - :linux - - {:unix, :freebsd} -> - :freebsd - - {:unix, :darwin} -> - :macosx - - other -> - # TODO add detection for more platforms - Output.raise( - "Unable to detect current platform. Erlang returned #{inspect(other)} which I don't know how to handle." - ) - end - end - @spec family(name_t) :: family_name_t def family(:windows32), do: :windows def family(:windows64), do: :windows