Browse Source

Merge pull request #11 from artemeff/mad-static

add static files compiler
Namdak Tonpa 10 years ago
parent
commit
e253b8bf4e
4 changed files with 46 additions and 0 deletions
  1. BIN
      mad
  2. 4 0
      src/mad.erl
  3. 41 0
      src/mad_static.erl
  4. 1 0
      src/mad_utils.erl

BIN
mad


+ 4 - 0
src/mad.erl

@@ -85,6 +85,10 @@ release(_Cwd,_ConfigFileName,_Config,Params) ->
     io:format("Release Params: ~p~n",[Params]),
     io:format("Release Params: ~p~n",[Params]),
     mad_release:main(Params).
     mad_release:main(Params).
 
 
+static(_Cwd,_ConfigFileName,Config,Params) ->
+    io:format("Compile Static Params: ~p~n",[Params]),
+    mad_static:main(Config, Params).
+
 help(Reason, Data) -> help(io_lib:format("~s ~p", [Reason, Data])).
 help(Reason, Data) -> help(io_lib:format("~s ~p", [Reason, Data])).
 help(Msg) -> io:format("Error: ~s~n~n", [Msg]), help().
 help(Msg) -> io:format("Error: ~s~n~n", [Msg]), help().
 help() ->
 help() ->

+ 41 - 0
src/mad_static.erl

@@ -0,0 +1,41 @@
+-module(mad_static).
+-copyright('Yuri Artemev').
+-compile(export_all).
+-define(NODE(Bin), "node_modules/.bin/"++Bin).
+
+main(Config, ["watch"]) ->
+    case mad_utils:get_value(static, Config, []) of
+        [] -> skip;
+        SC ->
+            Port = mad_utils:get_value(assets_port, SC, 3000),
+            install_deps(), serve_static(Port)
+    end;
+main(Config, _Params) ->
+    case mad_utils:get_value(static, Config, []) of
+        [] -> skip;
+        SC ->
+            Files = mad_utils:get_value(files, SC, []),
+            install_deps(), compile_static(Files)
+    end.
+
+install_deps() ->
+    case filelib:is_dir("node_modules/mincer-erl") of
+        true -> ok;
+        _ ->
+            case sh:oneliner("npm install mincer-erl") of
+                {_,0,_} -> ok;
+                {_,_,_} -> exit("error while installing mincer-erl")
+            end
+    end.
+
+% FIXME exit
+serve_static(Port) ->
+    PortStr = integer_to_list(Port),
+    sh:oneliner([?NODE("mincer-erl-serve"), "-p " ++ PortStr]).
+
+compile_static(Files) ->
+    Res = sh:oneliner([?NODE("mincer-erl-compile")] ++ Files),
+    case Res of
+        {_,0,_} -> ok;
+        {_,_,_} -> exit("error while compiling assets")
+    end.

+ 1 - 0
src/mad_utils.erl

@@ -64,6 +64,7 @@ to_atom(X) when is_list(X) -> list_to_atom(X);
 to_atom(X) when is_binary(X) -> to_atom(binary_to_list(X));
 to_atom(X) when is_binary(X) -> to_atom(binary_to_list(X));
 to_atom(X) -> X.
 to_atom(X) -> X.
 
 
+atomize("static") -> static;
 atomize("com"++_) -> compile;
 atomize("com"++_) -> compile;
 atomize("rep"++_) -> repl;
 atomize("rep"++_) -> repl;
 atomize("bun"++_) -> bundle;
 atomize("bun"++_) -> bundle;