Packages

Gleam modules and functions for installing and invoking TailwindCSS

Current section

Files

Jump to
glailglind src tailwind.erl
Raw

src/tailwind.erl

-module(tailwind).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/tailwind.gleam").
-export([install/0, get_args/0, run/1, install_and_run/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Contains all the functions necessary to install and execute the TailwindCSS CLI.\n"
" \n"
).
-file("src/tailwind.gleam", 150).
-spec target() -> binary().
target() ->
case {tailwind_erl:os_platform(), tailwind_erl:os_arch()} of
{<<"win32"/utf8>>, <<"x86_64"/utf8>>} ->
<<"windows-x64.exe"/utf8>>;
{<<"win32"/utf8>>, <<"x64"/utf8>>} ->
<<"windows-x64.exe"/utf8>>;
{<<"win32"/utf8>>, <<"arm"/utf8, _/binary>>} ->
<<"windows-arm64.exe"/utf8>>;
{<<"darwin"/utf8>>, <<"aarch64"/utf8>>} ->
<<"macos-arm64"/utf8>>;
{<<"darwin"/utf8>>, <<"arm"/utf8, _/binary>>} ->
<<"macos-arm64"/utf8>>;
{<<"darwin"/utf8>>, <<"x86_64"/utf8>>} ->
<<"macos-x64"/utf8>>;
{<<"darwin"/utf8>>, <<"x64"/utf8>>} ->
<<"macos-x64"/utf8>>;
{<<"linux"/utf8>>, <<"aarch64"/utf8>>} ->
<<"linux-arm64"/utf8>>;
{<<"linux"/utf8>>, <<"arm64"/utf8>>} ->
<<"linux-arm64"/utf8>>;
{<<"linux"/utf8>>, <<"armv7"/utf8, _/binary>>} ->
<<"linux-armv7"/utf8>>;
{<<"linux"/utf8>>, <<"x86_64"/utf8>>} ->
<<"linux-x64"/utf8>>;
{<<"linux"/utf8>>, <<"x64"/utf8>>} ->
<<"linux-x64"/utf8>>;
{<<"linux"/utf8>>, <<"amd64"/utf8>>} ->
<<"linux-x64"/utf8>>;
{Os, Arch} ->
erlang:error(#{gleam_error => panic,
message => gleam@string:join(
[<<"Error: TailwindCSS CLI is not available for"/utf8>>,
Os,
Arch],
<<" "/utf8>>
),
file => <<?FILEPATH/utf8>>,
module => <<"tailwind"/utf8>>,
function => <<"target"/utf8>>,
line => 160})
end.
-file("src/tailwind.gleam", 233).
-spec download_bin(binary()) -> {ok, nil} | {error, binary()}.
download_bin(Path) ->
Request = begin
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, get),
_pipe@2 = gleam@http@request:set_host(_pipe@1, <<"github.com"/utf8>>),
_pipe@3 = gleam@http@request:set_path(_pipe@2, Path),
gleam@http@request:map(_pipe@3, fun gleam_stdlib:identity/1)
end,
Response = begin
_pipe@4 = gleam@httpc:configure(),
_pipe@5 = gleam@httpc:follow_redirects(_pipe@4, true),
gleam@httpc:dispatch_bits(_pipe@5, Request)
end,
_pipe@6 = Response,
_pipe@7 = gleam@result:map_error(
_pipe@6,
fun(Err) ->
<<"Error: Couldn't download tailwind. Reason: "/utf8,
(gleam@string:inspect(Err))/binary>>
end
),
gleam@result:'try'(
_pipe@7,
fun(Resp) ->
_pipe@8 = simplifile_erl:write_bits(
<<"./build/bin/tailwindcss"/utf8>>,
erlang:element(4, Resp)
),
gleam@result:map_error(
_pipe@8,
fun(Err@1) ->
<<"Error: Couldn't write tailwind binary. Reason: "/utf8,
(gleam@string:inspect(Err@1))/binary>>
end
)
end
).
-file("src/tailwind.gleam", 167).
-spec download_tailwind({ok, binary()} | {error, binary()}, binary()) -> {ok,
nil} |
{error, binary()}.
download_tailwind(Version, Target) ->
case simplifile_erl:is_file(<<"./build/bin/tailwindcss"/utf8>>) of
{ok, true} ->
gleam_stdlib:println(<<"TailwindCSS CLI already exists."/utf8>>),
{ok, nil};
_ ->
Url_path = case Version of
{ok, <<"v"/utf8, Version@1/binary>>} ->
erlang:list_to_binary(
[<<"/tailwindlabs/tailwindcss/releases/download/v"/utf8>>,
Version@1,
<<"/tailwindcss-"/utf8>>,
Target]
);
{ok, Version@2} ->
erlang:list_to_binary(
[<<"/tailwindlabs/tailwindcss/releases/download/v"/utf8>>,
Version@2,
<<"/tailwindcss-"/utf8>>,
Target]
);
{error, _} ->
erlang:list_to_binary(
[<<"/tailwindlabs/tailwindcss/releases/latest/download/"/utf8>>,
<<"/tailwindcss-"/utf8>>,
Target]
)
end,
case simplifile:create_directory_all(<<"./build/bin/"/utf8>>) of
{ok, nil} -> nil;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"tailwind"/utf8>>,
function => <<"download_tailwind"/utf8>>,
line => 202,
value => _assert_fail,
start => 5306,
'end' => 5374,
pattern_start => 5317,
pattern_end => 5324})
end,
gleam_stdlib:println(
<<<<"Downloading TailwindCSS "/utf8, Target/binary>>/binary,
"..."/utf8>>
),
_pipe = download_bin(Url_path),
gleam@result:'try'(
_pipe,
fun(_) ->
_pipe@1 = simplifile_erl:set_permissions_octal(
<<"./build/bin/tailwindcss"/utf8>>,
8#755
),
gleam@result:map_error(
_pipe@1,
fun(Err) ->
<<"Error: Can't change tailwindcli permissions. Reason: "/utf8,
(gleam@string:inspect(Err))/binary>>
end
)
end
)
end.
-file("src/tailwind.gleam", 78).
-spec get_config() -> {ok, gleam@dict:dict(binary(), tom:toml())} |
{error, binary()}.
get_config() ->
_pipe = simplifile:read(<<"./gleam.toml"/utf8>>),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(Err) ->
<<"Error: Couldn't read config. Reason: "/utf8,
(gleam@string:inspect(Err))/binary>>
end
),
gleam@result:'try'(_pipe@1, fun(Config) -> _pipe@2 = tom:parse(Config),
gleam@result:replace_error(
_pipe@2,
<<"Error: Couldn't parse config."/utf8>>
) end).
-file("src/tailwind.gleam", 89).
-spec get_config_string(binary()) -> {ok, binary()} | {error, binary()}.
get_config_string(Key) ->
_pipe = get_config(),
gleam@result:'try'(
_pipe,
fun(Parsed) ->
case tom:get_string(
Parsed,
[<<"tools"/utf8>>, <<"tailwind"/utf8>>, Key]
) of
{ok, Value} ->
{ok, Value};
{error, _} ->
_pipe@1 = tom:get_string(Parsed, [<<"tailwind"/utf8>>, Key]),
_pipe@2 = gleam@result:map(
_pipe@1,
fun(Value@1) ->
gleam_stdlib:println(
<<"Warning: [tailwind] configuration is deprecated. Please use [tools.tailwind] instead."/utf8>>
),
Value@1
end
),
gleam@result:replace_error(
_pipe@2,
<<<<"Error: Config key \""/utf8, Key/binary>>/binary,
"\" not found."/utf8>>
)
end
end
).
-file("src/tailwind.gleam", 31).
?DOC(" Downloads the TailwindCSS CLI matching your operating system and architecture.\n").
-spec install() -> {ok, nil} | {error, binary()}.
install() ->
gleam_stdlib:println(<<"Installing TailwindCSS..."/utf8>>),
Output = begin
_pipe = get_config_string(<<"version"/utf8>>),
download_tailwind(_pipe, target())
end,
case Output of
{ok, _} ->
gleam_stdlib:println(<<"TailwindCSS installed!"/utf8>>),
{ok, nil};
{error, Err} ->
gleam_stdlib:println(Err),
{error, Err}
end.
-file("src/tailwind.gleam", 114).
?DOC(false).
-spec get_args() -> {ok, list(binary())} | {error, binary()}.
get_args() ->
_pipe = get_config(),
gleam@result:'try'(
_pipe,
fun(Parsed) ->
Toml_args = case tom:get_array(
Parsed,
[<<"tools"/utf8>>, <<"tailwind"/utf8>>, <<"args"/utf8>>]
) of
{ok, Args} ->
{ok, Args};
{error, _} ->
_pipe@1 = tom:get_array(
Parsed,
[<<"tailwind"/utf8>>, <<"args"/utf8>>]
),
gleam@result:map(
_pipe@1,
fun(Args@1) ->
gleam_stdlib:println(
<<"Warning: [tailwind] configuration is deprecated. Please use [tools.tailwind] instead."/utf8>>
),
Args@1
end
)
end,
_pipe@2 = Toml_args,
_pipe@3 = gleam@result:replace_error(
_pipe@2,
<<"Error: Config arguments not found. Is the \"args\" key set in the \"gleam.toml\"?"/utf8>>
),
gleam@result:map(
_pipe@3,
fun(Args@2) -> gleam@list:map(Args@2, fun(Arg) -> case Arg of
{string, A} ->
A;
_ ->
<<""/utf8>>
end end) end
)
end
).
-file("src/tailwind.gleam", 145).
-spec get_cli_path() -> binary().
get_cli_path() ->
_pipe = get_config_string(<<"path"/utf8>>),
gleam@result:unwrap(_pipe, <<"./build/bin/tailwindcss"/utf8>>).
-file("src/tailwind.gleam", 57).
?DOC(
" Executes the TailwindCSS CLI with the passed arguments.\n"
" # Example\n"
" ```gleam\n"
" > run([\"--input=./test/input.css\", \"--output=./build/css/output.css\"])\n"
" Rebuilding...\n"
" Done in 90ms.\n"
" ```\n"
).
-spec run(list(binary())) -> {ok, binary()} | {error, binary()}.
run(Args) ->
Cli = get_cli_path(),
case simplifile_erl:is_file(Cli) of
{ok, true} ->
_pipe = shellout:command(Cli, Args, <<"."/utf8>>, []),
gleam@result:map_error(
_pipe,
fun(Err) -> gleam@pair:second(Err) end
);
_ ->
{error, <<"Error: TailwindCSS CLI isn't installed."/utf8>>}
end.
-file("src/tailwind.gleam", 73).
?DOC(
" Installs and then executes the TailwindCSS CLI with the passed arguments.\n"
" \n"
" Check `install()` and `run()`.\n"
).
-spec install_and_run(list(binary())) -> {ok, binary()} | {error, binary()}.
install_and_run(Args) ->
_pipe = install(),
gleam@result:'try'(_pipe, fun(_) -> run(Args) end).