Просмотр исходного кода

fix n2o build, yrl files, tags and more

Maxim Sokhatsky 11 лет назад
Родитель
Сommit
c47e90eff5
4 измененных файлов с 82 добавлено и 89 удалено
  1. BIN
      mad
  2. 3 3
      src/mad.erl
  3. 75 83
      src/mad_compile.erl
  4. 4 3
      src/mad_deps.erl

+ 3 - 3
src/mad.erl

@@ -1,6 +1,6 @@
 -module(mad).
 -copyright('Sina Samavati').
--export([main/1,'fetch-deps'/3,compile/3,'compile-app'/3,'compile-deps'/3]).
+-export([main/1,'get-deps'/3,compile/3,'compile-app'/3,'compile-deps'/3]).
 
 main([]) -> help();
 main(Args) ->
@@ -33,7 +33,7 @@ main(Args) ->
     lists:foreach(Fun, Params).
 
 %% fetch dependencies
-'fetch-deps'(Cwd, ConfigFile, Conf) ->
+'get-deps'(Cwd, ConfigFile, Conf) ->
     case get_value(deps, Conf, []) of
         [] -> ok;
         Deps ->
@@ -108,7 +108,7 @@ help() ->
     io:format("Erlang dependency manager~n"),
     Params = [
               {"", ""},
-              {"fetch-deps", "Fetches dependencies"},
+              {"get-deps", "Fetches dependencies"},
               {"compile-deps", "Compiles dependencies"},
               {"compile-app", "Compiles application"},
               {"compile", "Compiles dependencies and application"}

+ 75 - 83
src/mad_compile.erl

@@ -1,9 +1,8 @@
 -module(mad_compile).
 -copyright('Sina Samavati').
 -export([deps/4,app/3,foreach/4]).
--export([erl_files/1,app_src_files/1,is_app_src/1,app_src_to_app/1,erl_to_beam/2,is_compiled/2]).
--define(COMPILE_OPTS(Inc, Ebin, Opts),
-        [report, {i, Inc}, {outdir, Ebin}] ++ Opts).
+-export([erl_files/1,app_src_files/1,app_src_to_app/1,erl_to_beam/2,is_compiled/2]).
+-define(COMPILE_OPTS(Inc, Ebin, Opts), [report, {i, Inc}, {outdir, Ebin}] ++ Opts).
 
 -type directory() :: string().
 -type filename() :: string().
@@ -38,7 +37,8 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
     foreach(fun app/3, SubDirs, Conf, ConfigFile),
 
     SrcDir = filename:join([mad_utils:src(DepPath)]),
-    Files = sort(erl_files(SrcDir)) ++ app_src_files(SrcDir),
+    Files = yrl_files(SrcDir) ++ sort(erl_files(SrcDir)) ++ app_src_files(SrcDir),
+
     case Files of
         [] -> ok;
         Files ->
@@ -51,6 +51,9 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
 
             Opts = mad_utils:get_value(erl_opts, Conf1, []),
             lists:foreach(compile_fun(IncDir, EbinDir, Opts), Files),
+
+            dtl(DepPath,Conf1),
+
             put(Name, compiled),
             ok
     end.
@@ -61,6 +64,7 @@ app(Dir, Conf, ConfigFile) ->
     Conf1 = mad_utils:script(ConfigFile1, Conf),
     SrcDir = mad_utils:src(Dir),
     Files = sort(erl_files(SrcDir)) ++ app_src_files(SrcDir),
+
     case Files of
         [] -> ok;
         Files ->
@@ -75,89 +79,77 @@ app(Dir, Conf, ConfigFile) ->
             lists:foreach(compile_fun(IncDir, EbinDir, Opts), Files),
             ok
     end,
-    ErlyDtlOpts = mad_utils:get_value(erlydtl_opts, Conf1, []),
-    ErlyDtlOpts1 = validate_erlydtl_opts(Dir, ErlyDtlOpts),
-    compile_erlydtl_files(ErlyDtlOpts1),
+    dtl(Dir,Conf1),
     ok.
 
+dtl(Dir,Config) ->
+    case mad_utils:get_value(erlydtl_opts, Config, []) of
+        [] -> skip;
+         X -> io:format("Dir: ~p DTL: ~p~n",[Dir,X]),
+              compile_erlydtl_files(validate_erlydtl_opts(Dir,X)) end.
+
+
 -spec validate_property({atom(), term()}, term()) -> {atom(), term()}.
-validate_property({modules, _}, Modules) ->
-    {modules, Modules};
-validate_property(Else, _) ->
-    Else.
+validate_property({modules, _}, Modules) -> {modules, Modules};
+validate_property(Else, _) -> Else.
 
 -spec compile_fun(directory(), directory(), [compile:option()]) ->
-                         fun((file:name()) -> ok).
-compile_fun(IncDir, EbinDir, Opts) ->
-    fun(File) ->
-            case is_app_src(File) of
-                false ->
-                    BeamFile = erl_to_beam(EbinDir, File),
-                    Compiled = is_compiled(BeamFile, File),
-                    if Compiled =:= false ->
-                            io:format("Compiling ~s~n", [File]),
-                            Opts1 = ?COMPILE_OPTS(IncDir, EbinDir, Opts),
-                            compile:file(File, Opts1);
-                       true -> ok end;
-                true ->
-                    %% add {modules, [Modules]} to .app file
-                    AppFile = filename:join(EbinDir, app_src_to_app(File)),
-                    io:format("Writing ~s~n", [AppFile]),
-                    BeamFiles = filelib:wildcard("*.beam", EbinDir),
-                    Modules = [list_to_atom(filename:basename(X, ".beam"))
-                               || X <- BeamFiles],
-                    [Struct|_] = mad_utils:consult(File),
-                    {application, AppName, Props} = Struct,
-                    Props1 = add_modules_property(Props),
-                    Props2 = [validate_property(X, Modules) || X <- Props1],
-                    Struct1 = {application, AppName, Props2},
-                    file:write_file(AppFile, io_lib:format("~p.~n", [Struct1]))
-            end
-    end.
+    fun((file:name(),string(),string(),list(tuple(any(),any())),string()) -> ok).
+compile_fun(Inc,Bin,Opt) -> fun(File) -> compile(File,Inc,Bin,Opt,filetype(File)) end.
+
+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");
+compile(File,Inc,Bin,Opt,".erl") ->
+    BeamFile = erl_to_beam(Bin, File),
+    Compiled = is_compiled(BeamFile, File),
+    if  Compiled =:= false ->
+        io:format("Compiling ~s~n", [File]),
+        Opts1 = ?COMPILE_OPTS(Inc, Bin, Opt),
+        compile:file(File, Opts1),
+        ok;
+    true -> ok end;
+compile(File,_Inc,Bin,_Opt,".app.src") ->
+    AppFile = filename:join(Bin, app_src_to_app(File)),
+    io:format("Writing ~s~n", [AppFile]),
+    BeamFiles = filelib:wildcard("*.beam", Bin),
+    Modules = [list_to_atom(filename:basename(X, ".beam")) || X <- BeamFiles],
+    [Struct|_] = mad_utils:consult(File),
+    {application, AppName, Props} = Struct,
+    Props1 = add_modules_property(Props),
+    Props2 = [validate_property(X, Modules) || X <- Props1],
+    Struct1 = {application, AppName, Props2},
+    file:write_file(AppFile, io_lib:format("~p.~n", [Struct1])),
+    ok;
+compile(File,_Inc,_Bin,_Opt,_) ->
+    io:format("Unknown file type: ~p~n",[File]).
 
-%% find all .erl files in Dir
 -spec erl_files(directory()) -> [file:name()].
-erl_files(Dir) ->
-    filelib:fold_files(Dir, ".erl", true, fun(F, Acc) -> [F|Acc] end, []).
-
-%% find all .app.src files in Dir
 -spec app_src_files(directory()) -> [file:name()].
-app_src_files(Dir) ->
-    filelib:fold_files(Dir, ".app.src", true, fun(F, Acc) -> [F|Acc] end, []).
-
--spec is_app_src(file:name()) -> boolean().
-is_app_src(Filename) ->
-    Filename =/= filename:rootname(Filename, ".app.src").
-
 -spec app_src_to_app(file:name()) -> file:name().
-app_src_to_app(Filename) ->
-    filename:basename(Filename, ".app.src") ++ ".app".
-
 -spec erl_to_beam(directory(), file:name()) -> file:name().
-erl_to_beam(EbinDir, Filename) ->
-    filename:join(EbinDir, filename:basename(Filename, ".erl") ++ ".beam").
-
 -spec is_compiled(directory(), file:name()) -> boolean().
-is_compiled(BeamFile, File) ->
-    mad_utils:last_modified(BeamFile) > mad_utils:last_modified(File).
-
 -spec add_modules_property([{atom(), term()}]) -> [{atom(), term()}].
+-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_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").
+is_compiled(BeamFile, File) -> mad_utils:last_modified(BeamFile) > mad_utils:last_modified(File).
 add_modules_property(Properties) ->
     case lists:keyfind(modules, 1, Properties) of
-        {modules, _} ->
-            Properties;
-        _ ->
-            Properties ++ [{modules, []}]
-    end.
+        {modules, _} -> Properties;
+        _ -> Properties ++ [{modules, []}] end.
 
--spec sort([file:name()]) -> [file:name()].
-sort(Files) ->
-    sort_by_priority(Files, [], [], []).
 
--spec sort_by_priority([file:name()], [file:name()], [file:name()], [file:name()])
-                      -> [file:name()].
-sort_by_priority([], High, Medium, Low) ->
-    (High ++ Medium) ++ Low;
+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
@@ -207,23 +199,23 @@ module_name(File, Ext, NewExt) ->
     list_to_atom(filename:basename(File, Ext) ++ NewExt).
 
 compile_erlydtl_files(Opts) ->
-    {{_, DocRoot}, Opts1} = get_kv(doc_root, Opts, ""),
+
+    {{_, DocRoot},   Opts1} = get_kv(doc_root,   Opts,  ""),
     {{_, SourceExt}, Opts2} = get_kv(source_ext, Opts1, ""),
     {{_, ModuleExt}, Opts3} = get_kv(module_ext, Opts2, ""),
-    {{_, OutDir}, _} = get_kv(out_dir, Opts3, ""),
+    {{_, OutDir},        _} = get_kv(out_dir,    Opts3, ""),
 
     Files = filelib:fold_files(DocRoot, SourceExt, true,
                                fun(F, Acc) -> [F|Acc] end, []),
 
     Compile = fun(F) ->
-                      ModuleName = module_name(F, SourceExt, ModuleExt),
-                      BeamFile = erl_to_beam(OutDir, atom_to_list(ModuleName)),
-                      Compiled = is_compiled(BeamFile, F),
-                      if Compiled =:= false ->
-                              io:format("Compiling ~s~n", [F]),
-                              erlydtl:compile(F, ModuleName, Opts3);
-                         true ->
-                              ok
-                      end
-              end,
+        ModuleName = module_name(F, SourceExt, ModuleExt),
+        BeamFile = erl_to_beam(OutDir, atom_to_list(ModuleName)),
+        Compiled = is_compiled(BeamFile, F),
+        if  Compiled =:= false ->
+            io:format("DTL Compiling ~s~n", [F]),
+            erlydtl:compile(F, ModuleName, Opts3);
+        true -> ok end
+    end,
+
     lists:foreach(Compile, Files).

+ 4 - 3
src/mad_deps.erl

@@ -39,9 +39,11 @@ fetch_dep(Cwd, Config, ConfigFile, Name, Cmd, Uri, Co, Cache) ->
         Dir -> filename:join([Dir,get_publisher(Uri),Name]) end,
 
     Opts = ["clone", Uri, TrunkPath ],
-    io:format("==> dependency: ~p tag: ~p~n", [Uri,Co]),
     %% fetch
     mad_utils:exec(Cmd, Opts),
+    io:format("==> dependency: ~p tag: ~p~n", [Uri,Co]),
+    mad_utils:exec(Cmd, ["checkout", lists:concat([Co]) ] ),
+
     put(Name, fetched),
 
     %% check dependencies of the dependency
@@ -55,12 +57,11 @@ fetch_dep(Cwd, Config, ConfigFile, Name, Cmd, Uri, Co, Cache) ->
 
 %% build dependency based on branch/tag/commit
 -spec build_dep(directory(), any(), string(), string(), string(), string(), string(), string()) -> ok.
-build_dep(Cwd, Conf, _ConfFile, Publisher, Name, Cmd, Co, Dir) ->
+build_dep(Cwd, Conf, _ConfFile, Publisher, Name, _Cmd, _Co, Dir) ->
     TrunkPath = filename:join([Dir, Publisher, Name]),
     DepsDir = filename:join([mad_utils:get_value(deps_dir, Conf, ["deps"]),Name]),
     mad_utils:exec("cp", ["-r", TrunkPath, DepsDir]),
     ok = file:set_cwd(DepsDir),
-    mad_utils:exec(Cmd, ["checkout", lists:concat([Co])]),
     ok = file:set_cwd(Cwd).
 
 %% internal