Skip to content

Commit dd1725a

Browse files
authored
Merge pull request #109 from phoenixframework/sd-musl
automatically detect musl, allow overriding target
2 parents df61ebc + d675732 commit dd1725a

File tree

1 file changed

+66
-17
lines changed

1 file changed

+66
-17
lines changed

lib/tailwind.ex

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defmodule Tailwind do
2323
2424
## Tailwind configuration
2525
26-
There are two global configurations for the tailwind application:
26+
There are four global configurations for the tailwind application:
2727
2828
* `:version` - the expected tailwind version
2929
@@ -35,6 +35,10 @@ defmodule Tailwind do
3535
default, it is automatically downloaded and placed inside
3636
the `_build` directory of your current app
3737
38+
* `:target` - the target architecture for the tailwind executable.
39+
For example `"linux-x64-musl"`. By default, it is automatically detected
40+
based on system information.
41+
3842
Overriding the `:path` is not recommended, as we will automatically
3943
download and manage `tailwind` for you. But in case you can't download
4044
it (for example, GitHub behind a proxy), you may want to
@@ -108,6 +112,13 @@ defmodule Tailwind do
108112
Application.get_env(:tailwind, :version, latest_version())
109113
end
110114

115+
@doc """
116+
Returns the configured tailwind target. By default, it is automatically detected.
117+
"""
118+
def configured_target do
119+
Application.get_env(:tailwind, :target, target())
120+
end
121+
111122
@doc """
112123
Returns the configuration for the given profile.
113124
@@ -136,7 +147,7 @@ defmodule Tailwind do
136147
The executable may not be available if it was not yet installed.
137148
"""
138149
def bin_path do
139-
name = "tailwind-#{target()}"
150+
name = "tailwind-#{configured_target()}"
140151

141152
Application.get_env(:tailwind, :path) ||
142153
if Code.ensure_loaded?(Mix.Project) do
@@ -246,19 +257,45 @@ defmodule Tailwind do
246257
# tailwindcss-windows-x64.exe
247258
defp target do
248259
arch_str = :erlang.system_info(:system_architecture)
249-
[arch | _] = arch_str |> List.to_string() |> String.split("-")
250-
251-
case {:os.type(), arch, :erlang.system_info(:wordsize) * 8} do
252-
{{:win32, _}, _arch, 64} -> "windows-x64.exe"
253-
{{:unix, :darwin}, arch, 64} when arch in ~w(arm aarch64) -> "macos-arm64"
254-
{{:unix, :darwin}, "x86_64", 64} -> "macos-x64"
255-
{{:unix, :freebsd}, "aarch64", 64} -> "freebsd-arm64"
256-
{{:unix, :freebsd}, arch, 64} when arch in ~w(x86_64 amd64) -> "freebsd-x64"
257-
{{:unix, :linux}, "aarch64", 64} -> "linux-arm64"
258-
{{:unix, :linux}, "arm", 32} -> "linux-armv7"
259-
{{:unix, :linux}, "armv7" <> _, 32} -> "linux-armv7"
260-
{{:unix, _osname}, arch, 64} when arch in ~w(x86_64 amd64) -> "linux-x64"
261-
{_os, _arch, _wordsize} -> raise "tailwind is not available for architecture: #{arch_str}"
260+
target_triple = arch_str |> List.to_string() |> String.split("-")
261+
262+
{arch, abi} =
263+
case target_triple do
264+
[arch, _vendor, _system, abi] -> {arch, abi}
265+
[arch, _vendor, abi] -> {arch, abi}
266+
[arch | _] -> {arch, nil}
267+
end
268+
269+
case {:os.type(), arch, abi, :erlang.system_info(:wordsize) * 8} do
270+
{{:win32, _}, _arch, _abi, 64} ->
271+
"windows-x64.exe"
272+
273+
{{:unix, :darwin}, arch, _abi, 64} when arch in ~w(arm aarch64) ->
274+
"macos-arm64"
275+
276+
{{:unix, :darwin}, "x86_64", _abi, 64} ->
277+
"macos-x64"
278+
279+
{{:unix, :freebsd}, "aarch64", _abi, 64} ->
280+
"freebsd-arm64"
281+
282+
{{:unix, :freebsd}, arch, _abi, 64} when arch in ~w(x86_64 amd64) ->
283+
"freebsd-x64"
284+
285+
{{:unix, :linux}, "aarch64", "musl", 64} ->
286+
"linux-arm64-musl"
287+
288+
{{:unix, :linux}, "aarch64", _abi, 64} ->
289+
"linux-arm64"
290+
291+
{{:unix, _osname}, arch, "musl", 64} when arch in ~w(x86_64 amd64) ->
292+
"linux-x64-musl"
293+
294+
{{:unix, _osname}, arch, _abi, 64} when arch in ~w(x86_64 amd64) ->
295+
"linux-x64"
296+
297+
{_os, _arch, _abi, _wordsize} ->
298+
raise "tailwind is not available for architecture: #{arch_str}"
262299
end
263300
end
264301

@@ -297,6 +334,18 @@ defmodule Tailwind do
297334
{_, {:ok, {{_, 200, _}, _headers, body}}} ->
298335
body
299336

337+
{_, {:ok, {{_, 404, _}, _headers, _body}}} ->
338+
raise """
339+
The tailwind binary couldn't be found at: #{url}
340+
341+
This could mean that you're trying to install a version that does not support the detected
342+
target architecture.
343+
344+
You can see the available files for the configured version at:
345+
346+
https://github.com/tailwindlabs/tailwindcss/releases/tag/v#{configured_version()}
347+
"""
348+
300349
{true, {:error, {:failed_connect, [{:to_address, _}, {inet, _, reason}]}}}
301350
when inet in [:inet, :inet6] and
302351
reason in [:ehostunreach, :enetunreach, :eprotonosupport, :nxdomain] ->
@@ -314,7 +363,7 @@ defmodule Tailwind do
314363
your certificates are set via OTP ca certfile overide via SSL configuration.
315364
316365
2. Manually download the executable from the URL above and
317-
place it inside "_build/tailwind-#{target()}"
366+
place it inside "_build/tailwind-#{configured_target()}"
318367
319368
3. Install and use Tailwind from npmJS. See our module documentation
320369
to learn more: https://hexdocs.pm/tailwind
@@ -361,6 +410,6 @@ defmodule Tailwind do
361410
defp get_url(base_url) do
362411
base_url
363412
|> String.replace("$version", configured_version())
364-
|> String.replace("$target", target())
413+
|> String.replace("$target", configured_target())
365414
end
366415
end

0 commit comments

Comments
 (0)