Browse Source

increase speed 10 times. merge apps and deps handling. multiple apps.

Maxim Sokhatsky 11 years ago
parent
commit
941a81c0c4
5 changed files with 34 additions and 75 deletions
  1. BIN
      mad
  2. 9 14
      src/mad.erl
  3. 15 57
      src/mad_compile.erl
  4. 3 2
      src/mad_deps.erl
  5. 7 2
      src/mad_utils.erl

BIN
mad


+ 9 - 14
src/mad.erl

@@ -1,6 +1,6 @@
 -module(mad).
 -copyright('Sina Samavati').
--export([main/1,'get-deps'/3,compile/3,'compile-app'/3,'compile-deps'/3]).
+-compile(export_all).
 
 main([]) -> help();
 main(Args) ->
@@ -48,17 +48,12 @@ main(Args) ->
 
 %% compile dependencies and the app
 compile(Cwd, ConfigFile, Conf) ->
-    %% compile dependencies
     'compile-deps'(Cwd, ConfigFile, Conf),
+    'compile-apps'(Cwd, ConfigFile, Conf).
 
-    %% compile the app
-    'compile-app'(Cwd, ConfigFile, Conf).
-
-%% compile a project according to the conventions
-'compile-app'(Cwd, ConfigFile, Conf) ->
-    %% check sub_dirs if they have something to be compiled
+'compile-apps'(Cwd, ConfigFile, Conf) ->
     Dirs = mad_utils:sub_dirs(Cwd, ConfigFile, Conf),
-    mad_compile:foreach(fun mad_compile:app/3, Dirs, Conf, ConfigFile).
+    mad_compile:deps(Cwd, Conf, ConfigFile, Dirs).
 
 'compile-deps'(Cwd, ConfigFile, Conf) ->
     mad_compile:deps(Cwd, Conf, ConfigFile, get_value(deps, Conf, [])).
@@ -105,13 +100,13 @@ help(Msg) ->
     help().
 
 help() ->
-    io:format("Erlang dependency manager~n"),
+    io:format("Manage Deps~n"),
     Params = [
               {"", ""},
-              {"get-deps", "Fetches dependencies"},
-              {"compile-deps", "Compiles dependencies"},
-              {"compile-app", "Compiles application"},
-              {"compile", "Compiles dependencies and application"}
+              {"get-deps", "Fetch dependencies"},
+              {"compile-deps", "Compile dependencies"},
+              {"compile-apps", "Compile applications"},
+              {"compile", "Compile dependencies and applications"}
              ],
     getopt:usage(option_spec_list(), escript:script_name(), "", Params),
     halt().

+ 15 - 57
src/mad_compile.erl

@@ -1,7 +1,6 @@
 -module(mad_compile).
 -copyright('Sina Samavati').
--export([deps/4,app/3,foreach/4]).
--export([erl_files/1,app_src_files/1,app_src_to_app/1,erl_to_beam/2,is_compiled/2]).
+-compile(export_all).
 -define(COMPILE_OPTS(Inc, Ebin, Opts), [report, {i, Inc}, {outdir, Ebin}] ++ Opts).
 
 -type directory() :: string().
@@ -12,7 +11,7 @@
 deps(_, _, _, []) -> ok;
 deps(Cwd, Conf, ConfigFile, [H|T]) ->
     {Name, _} = mad_deps:name_and_repo(H),
-    case get(Name) of
+    case get(mad_utils:to_atom(Name)) of
         compiled -> ok;
         _ -> dep(Cwd, Conf, ConfigFile, Name) end,
     deps(Cwd, Conf, ConfigFile, T).
@@ -20,6 +19,7 @@ deps(Cwd, Conf, ConfigFile, [H|T]) ->
 %% compile a dependency
 -spec dep(directory(), any(), filename(), string()) -> ok.
 dep(Cwd, _Conf, ConfigFile, Name) ->
+    io:format("==> ~p~n",[Name]),
     %% check dependencies of the dependency
     DepsDir = filename:join([mad_utils:get_value(deps_dir, _Conf, ["deps"])]),
     DepPath = filename:join([Cwd, DepsDir, Name]),
@@ -32,12 +32,8 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
     LibDirs = mad_utils:lib_dirs(DepPath, Conf1),
     code:add_paths(LibDirs),
 
-    %% compile sub_dirs and add them to path
-    SubDirs = mad_utils:sub_dirs(DepPath, ConfigFile, Conf),
-    foreach(fun app/3, SubDirs, Conf, ConfigFile),
-
     SrcDir = filename:join([mad_utils:src(DepPath)]),
-    Files = yrl_files(SrcDir) ++ sort(erl_files(SrcDir)) ++ app_src_files(SrcDir),
+    Files = yrl_files(SrcDir) ++ erl_files(SrcDir) ++ app_src_files(SrcDir),
 
     case Files of
         [] -> ok;
@@ -54,33 +50,10 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
 
             dtl(DepPath,Conf1),
 
-            put(Name, compiled),
+            put(mad_utils:to_atom(Name), compiled),
             ok
     end.
 
--spec app(directory(), any(), filename()) -> ok.
-app(Dir, Conf, ConfigFile) ->
-    ConfigFile1 = filename:join(Dir, ConfigFile),
-    Conf1 = mad_utils:consult(ConfigFile1),
-    SrcDir = mad_utils:src(Dir),
-    Files = sort(erl_files(SrcDir)) ++ app_src_files(SrcDir),
-
-    case Files of
-        [] -> ok;
-        Files ->
-            IncDir = mad_utils:include(Dir),
-            EbinDir = mad_utils:ebin(Dir),
-
-            %% create EbinDir and add it to code path
-            file:make_dir(EbinDir),
-            code:add_path(EbinDir),
-
-            Opts = mad_utils:get_value(erl_opts, Conf1, []),
-            lists:foreach(compile_fun(IncDir, EbinDir, Opts), Files),
-            ok
-    end,
-    dtl(Dir,Conf1),
-    ok.
 
 dtl(Dir,Config) ->
     case mad_utils:get_value(erlydtl_opts, Config, []) of
@@ -99,8 +72,11 @@ compile_fun(Inc,Bin,Opt) -> fun(File) -> compile(File,Inc,Bin,Opt,filetype(File)
 filetype(File) -> L=length(hd(string:tokens(File,"."))), string:substr(File,L+1,length(File)).
 
 compile(File,Inc,Bin,Opt,".yrl") ->
-    yecc:file(File),
-    compile(yrl_to_erl(File),Inc,Bin,Opt,".erl");
+    ErlFile = yrl_to_erl(File),
+    Compiled = is_compiled(ErlFile,File),
+    if Compiled == false ->
+        yecc:file(File),
+        compile(ErlFile,Inc,Bin,Opt,".erl"); true -> ok end;
 compile(File,Inc,Bin,Opt,".erl") ->
     BeamFile = erl_to_beam(Bin, File),
     Compiled = is_compiled(BeamFile, File),
@@ -112,6 +88,8 @@ compile(File,Inc,Bin,Opt,".erl") ->
     true -> ok end;
 compile(File,_Inc,Bin,_Opt,".app.src") ->
     AppFile = filename:join(Bin, app_src_to_app(File)),
+    Compiled = is_compiled(AppFile, File),
+    if  Compiled =:= false ->
     io:format("Writing ~s~n", [AppFile]),
     BeamFiles = filelib:wildcard("*.beam", Bin),
     Modules = [list_to_atom(filename:basename(X, ".beam")) || X <- BeamFiles],
@@ -122,6 +100,7 @@ compile(File,_Inc,Bin,_Opt,".app.src") ->
     Struct1 = {application, AppName, Props2},
     file:write_file(AppFile, io_lib:format("~p.~n", [Struct1])),
     ok;
+    true -> ok end;
 compile(File,_Inc,_Bin,_Opt,_) ->
     io:format("Unknown file type: ~p~n",[File]).
 
@@ -134,10 +113,10 @@ compile(File,_Inc,_Bin,_Opt,_) ->
 -spec sort([file:name()]) -> [file:name()].
 -spec sort_by_priority([file:name()], [file:name()], [file:name()], [file:name()]) -> [file:name()].
 
-sort(Files) -> sort_by_priority(Files, [], [], []).
 erl_files(Dir) -> filelib:fold_files(Dir, ".erl", true, fun(F, Acc) -> [F|Acc] end, []).
 yrl_files(Dir) -> filelib:fold_files(Dir, ".yrl", true, fun(F, Acc) -> [F|Acc] end, []).
-app_src_files(Dir) -> filelib:fold_files(Dir, ".app.src", true, fun(F, Acc) -> [F|Acc] end, []).
+app_src_files(Dir) -> filelib:fold_files(Dir, ".app.src", false, fun(F, Acc) -> [F|Acc] end, []).
+
 app_src_to_app(Filename) -> filename:basename(Filename, ".app.src") ++ ".app".
 yrl_to_erl(Filename) -> filename:join(filename:dirname(Filename),filename:basename(Filename, ".yrl")) ++ ".erl".
 erl_to_beam(Bin, Filename) -> filename:join(Bin, filename:basename(Filename, ".erl") ++ ".beam").
@@ -147,33 +126,12 @@ add_modules_property(Properties) ->
         {modules, _} -> Properties;
         _ -> Properties ++ [{modules, []}] end.
 
-
-sort_by_priority([], High, Medium, Low) -> (High ++ Medium) ++ Low;
-sort_by_priority([H|T], High, Medium, Low) ->
-    {High1, Medium1, Low1} =
-        case is_behaviour(H) of
-            true -> {[H|High], Medium, Low};
-            false -> {High, [H|Medium], Low} end,
-    {High2, Medium2, Low2} =
-        case mad_utils:exec("sed", ["-n", "'/-compile/p'", H]) of
-               [] -> {High1, Medium1, Low1};
-               _ -> {High1 -- [H], Medium1 -- [H], [H|Low1]} end,
-    sort_by_priority(T, High2, Medium2, Low2).
-
 -spec foreach(fun((directory(), filename()) -> ok), [filename()], any(), filename()) -> ok.
 foreach(_, [], _, _) -> ok;
 foreach(Fun, [Dir|T], Config, ConfigFile) ->
     Fun(Dir, Config, ConfigFile),
     foreach(Fun, T, Config, ConfigFile).
 
--spec is_behaviour(file:name()) -> boolean().
-is_behaviour(File) ->
-    case filelib:is_file(File) of
-        true ->
-            [] =/= mad_utils:exec("sed", ["-n", "-e", "'/-callback/p'", "-e",
-                                          "'/behaviour_info\\/1/p'", File]);
-        _ -> false end.
-
 get_kv(K, Opts, Default) ->
     V = mad_utils:get_value(K, Opts, Default),
     KV = {K, V},

+ 3 - 2
src/mad_deps.erl

@@ -1,6 +1,6 @@
 -module(mad_deps).
 -copyright('Sina Samavati').
--export([fetch/4,name_and_repo/1,get_publisher/1]).
+-compile(export_all).
 
 -type directory() :: string().
 -type filename() :: string().
@@ -70,7 +70,8 @@ build_dep(Cwd, Conf, _ConfFile, Publisher, Name, _Cmd, _Co, Dir) ->
 %% internal
 -spec name_and_repo(dependency()) -> {string(), repo()}.
 name_and_repo({Name, _, Repo}) -> {atom_to_list(Name), Repo};
-name_and_repo({Name, _, Repo, _}) -> {atom_to_list(Name), Repo}.
+name_and_repo({Name, _, Repo, _}) -> {atom_to_list(Name), Repo};
+name_and_repo(Name) -> {Name,Name}.
 
 -spec get_publisher(uri()) -> string().
 get_publisher(Uri) ->

+ 7 - 2
src/mad_utils.erl

@@ -1,7 +1,6 @@
 -module(mad_utils).
 -copyright('Sina Samavati').
--export([cwd/0,exec/2,home/0,consult/1,src/1,include/1,ebin/1,deps/1,get_value/3,
-         script/2,sub_dirs/3,lib_dirs/2,last_modified/1]).
+-compile(export_all).
 
 -type directory() :: string().
 
@@ -80,3 +79,9 @@ last_modified(File) ->
     case filelib:last_modified(File) of
         0 -> 0;
         Else -> calendar:datetime_to_gregorian_seconds(Else) end.
+
+to_atom(X) when is_atom(X) -> X;
+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) -> X.
+