~fancycade/rebar3_tailwind

ref: 66837342d002e3b1ea4eb6be220aa69f38a9d248 rebar3_tailwind/src/rebar3_tailwind_prv.erl -rw-r--r-- 2.1 KiB
66837342Harley Swick Dont log input and output 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
66
-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.