Current section
Files
Jump to
Current section
Files
src/rebar3_tailwind_prv.erl
-module(rebar3_tailwind_prv).
-export([init/1, do/1, format_error/1]).
-define(PROVIDER, compile).
-define(NAMESPACE, tailwind).
-define(DEPS, [default, app_discovery]).
%% ===================================================================
%% Public API
%% ===================================================================
-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
Provider = providers:create([
{name, ?PROVIDER}, % The 'user friendly' name of the task
{namespace, ?NAMESPACE},
{module, ?MODULE}, % The module implementation of the task
{bare, false}, % The task can be run by the user, always true
{deps, ?DEPS}, % The list of dependencies
{example, "rebar3 tailwind compile"}, % How to use the plugin
{opts, []}, % list of options understood by the plugin
{short_desc, "Compile tailwind css"},
{desc, "Compile tailwind css"}
]),
{ok, rebar_state:add_provider(State, Provider)}.
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
rebar_api:info("Running tailwind...", []),
Apps = case rebar_state:current_app(State) of
undefined ->
rebar_state:project_apps(State);
AppInfo ->
[AppInfo]
end,
[begin
Opts = rebar_app_info:opts(AppInfo),
TailwindOpts = rebar_opts:get(Opts, tailwind_opts, []),
CompileFun = fun(_Source, _Opts) ->
tailwind_compile(TailwindOpts)
end,
rebar_base_compiler:run(Opts, [], [<<"tailwind">>], CompileFun)
end || AppInfo <- Apps],
{ok, State}.
-spec format_error(any()) -> iolist().
format_error(Reason) ->
io_lib:format("~p", [Reason]).
tailwind_compile(Opts) ->
InputSrc =
case proplists:lookup(input, Opts) of
{input, Input} -> Input;
none -> "priv/input.css"
end,
OutputDst =
case proplists:lookup(output, Opts) of
{output, Output} -> Output;
none -> "priv/output.css"
end,
Cmd = io_lib:format("npx tailwindcss -i ~s -o ~s", [InputSrc, OutputDst]),
os:cmd(Cmd),
ok.