~fancycade/rebar3_tailwind

ref: d6403213c2352be1565f40bc7c0b71cc0ea7fb6a rebar3_tailwind/src/rebar3_tailwind_prv.erl -rw-r--r-- 2.1 KiB
d6403213Harley Swick Logging 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-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) ->
    Apps = case rebar_state:current_app(State) of
	       undefined ->
		   rebar_state:project_apps(State);
	       AppInfo ->
		   [AppInfo]
	   end,

    [begin
	 Opts = rebar_app_info:opts(AppInfo),
	 CompileFun = fun(_Source, _Opts) ->
			      tailwind_compile()
		      end,

	 rebar_base_compiler:run(Opts, [], ["default"], CompileFun)
      end || AppInfo <- Apps],
    {ok, State}.

-spec format_error(any()) ->  iolist().
format_error(Reason) ->
    io_lib:format("~p", [Reason]).

tailwind_compile() ->
    InputSrc = 
	case application:get_env(rebar3_tailwind, input) of
	    {ok, I} -> I;
	    _ -> "priv/input.css"
	end,
    io:format("~p~n", [InputSrc]),
    OutputDst =
	case application:get_env(rebar3_tailwind, output) of
	    {ok, O} -> O;
	    _ -> "priv/output.css"
	end,
    io:format("~p~n", [OutputDst]),
    Cmd = io_lib:format("npx tailwindcss -i ~s -o ~s", [InputSrc, OutputDst]),
    os:cmd(Cmd).