Browse Source

monadic error handling for compile case

Namdak Tonpa 10 years ago
parent
commit
f08ebb717c

+ 11 - 12
src/compile/mad_app.erl

@@ -15,20 +15,19 @@ compile(File,_Inc,Bin,_Opt,_Deps) ->
     AppFile = filename:join(Bin, app_src_to_app(File)),
     Compiled = mad_compile:is_compiled(AppFile, File),
     if  Compiled =:= false ->
-    io:format("Writing ~s~n\r", [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;
-    true -> ok end.
+        io:format("Writing ~s~n\r", [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])),
+        false;
+        true -> false end.
 
 add_modules_property(Properties) ->
     case lists:keyfind(modules, 1, Properties) of
         {modules, _} -> Properties;
         _ -> Properties ++ [{modules, []}] end.
-

+ 2 - 2
src/compile/mad_dtl.erl

@@ -4,7 +4,7 @@
 
 compile(Dir,Config) ->
     case mad_utils:get_value(erlydtl_opts, Config, []) of
-        [] -> skip;
+        [] -> false;
          X -> compile_erlydtl_files(validate_erlydtl_opts(Dir,X)) end.
 
 get_kv(K, Opts, Default) ->
@@ -52,4 +52,4 @@ compile_erlydtl_files(Opts) ->
         true -> ok end
     end,
 
-    lists:foreach(Compile, Files).
+    lists:any(fun({error,_}) -> true; (ok) -> false end,[Compile(F) || F <- Files]).

+ 7 - 3
src/compile/mad_erl.erl

@@ -12,7 +12,11 @@ compile(File,Inc,Bin,Opt,Deps) ->
         Opts1 = ?COMPILE_OPTS(Inc, Bin, Opt, Deps),
 %        io:format("Compiling ~s~n Opts ~p~n Deps~p~n", [File,Opts1,Deps]),
         io:format("Compiling ~s~n", [File]),
-        compile:file(File, Opts1),
-        ok;
-    true -> ok end.
+        ret(compile:file(File, Opts1));
+    true -> false end.
 
+ret(error) -> true;
+ret({error,_,_}) -> true;
+ret({ok,_}) -> false;
+ret({ok,_,_}) -> false;
+ret({ok,_,_,_}) -> false.

+ 1 - 1
src/compile/mad_leex.erl

@@ -9,5 +9,5 @@ compile(File,Inc,Bin,Opt,Deps) ->
     Compiled = mad_compile:is_compiled(ErlFile,File),
     if Compiled == false ->
         leex:file(File),
-        mad_erl:compile(ErlFile,Inc,Bin,Opt,Deps); true -> ok end.
+        mad_erl:compile(ErlFile,Inc,Bin,Opt,Deps); true -> false end.
 

+ 1 - 1
src/compile/mad_none.erl

@@ -1,4 +1,4 @@
 -module(mad_none).
 -compile(export_all).
 
-compile(_,_,_,_,_) -> ok.
+compile(_,_,_,_,_) -> false.

+ 5 - 5
src/compile/mad_port.erl

@@ -8,7 +8,7 @@ replace_env(String, [{K,V}|Env]) ->
 
 compile(Dir,Config) ->
     case mad_utils:get_value(port_specs, Config, []) of
-        [] -> skip;
+        [] -> [false];
          X -> compile_port(Dir,X,Config) end.
 
 compile_port(Dir,Specs,Config) ->
@@ -16,12 +16,12 @@ compile_port(Dir,Specs,Config) ->
     System = atom_to_list(S),
     filelib:ensure_dir(Dir ++ "/priv/"),
     Env = [ {Var,Val} || {Sys,Var,Val} <- mad_utils:get_value(port_env, Config, []), Sys == System ],
-    [ begin 
+    [ begin
            Template = string:join(filelib:wildcard(Dir ++ "/" ++ Files)," ") 
               ++ " CFLAGS LDFLAGS -o " ++ Dir ++ "/" ++ Out,
        Args = string:strip(replace_env(Template,Env),both,32),
        {_,Status,Report} = sh:run("cc",string:tokens(Args," "),binary,Dir,Env),
-       case Status == 0 of
-          true -> skip;
-          false -> io:format("Port Compilation Error: ~p",[Report]) end
+       case Status of
+          0 -> false;
+          _ -> io:format("Port Compilation Error: ~p",[Report]), true end
       end || {Sys,Out,Files} <- Specs, Sys == System].

+ 1 - 1
src/compile/mad_yecc.erl

@@ -9,5 +9,5 @@ compile(File,Inc,Bin,Opt,Deps) ->
     Compiled = mad_compile:is_compiled(ErlFile,File),
     if Compiled == false ->
         yecc:file(File),
-        mad_erl:compile(ErlFile,Inc,Bin,Opt,Deps); true -> ok end.
+        mad_erl:compile(ErlFile,Inc,Bin,Opt,Deps); true -> false end.
 

+ 5 - 3
src/mad.erl

@@ -42,11 +42,13 @@ deps(Cwd, ConfigFile, Conf, Params) ->
 %% compile dependencies and the app
 compile(Cwd, ConfigFile, Conf, Params) ->
     io:format("Compile Params: ~p~n\r",[Params]),
-    case Params of
+    Res = case Params of
          [] -> mad_compile:'compile-deps'(Cwd, ConfigFile, Conf);
-         __ -> [ mad_compile:dep(Cwd, Conf, ConfigFile, Name) || Name <- Params ]
+         __ -> mad_compile:deps(Cwd, Conf, ConfigFile, Params)
     end,
-    mad_compile:'compile-apps'(Cwd, ConfigFile, Conf), false.
+    case Res of
+         true -> true;
+         false -> mad_compile:'compile-apps'(Cwd, ConfigFile, Conf) end.
 
 %% reltool apps resolving
 plan(_Cwd,_ConfigFileName,_Config,Params) ->

+ 2 - 2
src/mad_bundle.erl

@@ -12,10 +12,10 @@ read_file(File) -> {ok, Bin} = file:read_file(filename:absname(File)), Bin.
 
 static() -> Name = "static.gz",
     {ok,{_,Bin}} = zip:create(Name,
-        [F || F <- mad_repl:wildcards(["{apps,deps}/*/priv/**","priv/**"]), not filelib:is_dir(F) ],
+        [begin io:format("F: ~p~n",[F]), F end  || F <- mad_repl:wildcards(["{apps,deps}/*/priv/**","priv/**"]), not filelib:is_dir(F) ],
         [{compress,all},memory]), [ { Name, Bin } ].
 
 beams() ->
     [ { filename:basename(F), read_file(F) } || F <-
-        lists:concat([filelib:wildcard(X)||X <- 
+        lists:concat([filelib:wildcard(X)||X <-
         [ "ebin/*","{apps,deps}/*/ebin/*","sys.config",".applist"]])].

+ 27 - 16
src/mad_compile.erl

@@ -3,16 +3,19 @@
 -compile(export_all).
 
 %% compile dependencies
-deps(_, _, _, []) -> ok;
+deps(_, _, _, []) -> false;
 deps(Cwd, Conf, ConfigFile, [H|T]) ->
     {Name, _} = mad_deps:name_and_repo(H),
-    case get(Name) == compiled andalso get(mode) /= active  of
-        true -> ok;
-        _ -> dep(Cwd, Conf, ConfigFile, Name) end,
-    deps(Cwd, Conf, ConfigFile, T).
+    Res = case get(Name) == compiled andalso get(mode) /= active  of
+          true -> false;
+          _    -> dep(Cwd, Conf, ConfigFile, Name) end,
+    case Res of
+         true  -> true;
+         false -> deps(Cwd, Conf, ConfigFile, T) end.
 
 %% compile a dependency
 dep(Cwd, _Conf, ConfigFile, Name) ->
+
     %% check dependencies of the dependency
     DepsDir = filename:join([mad_utils:get_value(deps_dir, _Conf, ["deps"])]),
     DepPath = filename:join([Cwd, DepsDir, Name]),
@@ -22,10 +25,11 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
     Conf = mad_utils:consult(DepConfigFile),
     Conf1 = mad_script:script(DepConfigFile, Conf, Name),
     Deps = mad_utils:get_value(deps, Conf1, []),
-    deps(Cwd, Conf, ConfigFile, Deps),
+    DepsRes = deps(Cwd, Conf, ConfigFile, Deps),
+    %io:format("DepsStatus: ~p~n",[DepsRes]),
 
     SrcDir = filename:join([mad_utils:src(DepPath)]),
-%    io:format("DepPath ==> ~p~n\r",[DepPath]),
+    %io:format("DepPath ==> ~p~n\r",[DepPath]),
 
     Files = files(SrcDir,".yrl") ++ 
             files(SrcDir,".xrl") ++ 
@@ -33,7 +37,7 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
             files(SrcDir,".app.src"),
 
     case Files of
-        [] -> ok;
+        [] -> false;
         Files ->
             IncDir = mad_utils:include(DepPath),
             EbinDir = mad_utils:ebin(DepPath),
@@ -41,25 +45,31 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
             Includes = lists:flatten([
                 [{i,filename:join([DepPath,L,D,include])} || D<-mad_utils:raw_deps(Deps) ] % for -include
              ++ [{i,filename:join([DepPath,L])}] || L <- LibDirs ]), % for -include_lib
-%            io:format("DepPath ~p~n Includes: ~p~nLibDirs: ~p~n",[DepPath,Includes,LibDirs]),
+            %io:format("DepPath ~p~n Includes: ~p~nLibDirs: ~p~n",[DepPath,Includes,LibDirs]),
 
-            %% create EbinDir and add it to code path
+            % create EbinDir and add it to code path
             file:make_dir(EbinDir),
             code:replace_path(Name,EbinDir),
 
             %erlc(DepPath), % comment this to build with files/2
 
             Opts = mad_utils:get_value(erl_opts, Conf1, []),
-            lists:foreach(compile_fun(IncDir, EbinDir, Opts,Includes), Files),
-
-            mad_dtl:compile(DepPath,Conf1),
-            mad_port:compile(DepPath,Conf1),
+            FilesStatus = compile_files(Files,IncDir, EbinDir, Opts,Includes),
+            DTLStatus = mad_dtl:compile(DepPath,Conf1),
+            PortStatus = lists:any(fun(X)->X end,mad_port:compile(DepPath,Conf1)),
+            %io:format("DTL Status: ~p~n",[DTLStatus]),
+            %io:format("Port Status: ~p~n",[PortStatus]),
+            %io:format("Files Status: ~p~n",[FilesStatus]),
 
             put(Name, compiled),
-            ok
+            DepsRes orelse FilesStatus orelse DTLStatus orelse PortStatus
     end.
 
-compile_fun(Inc,Bin,Opt,Deps) -> fun(File) -> (module(filetype(File))):compile(File,Inc,Bin,Opt,Deps) end.
+compile_files([],Inc,Bin,Opt,Deps) -> false;
+compile_files([File|Files],Inc,Bin,Opt,Deps) ->
+    case (module(filetype(File))):compile(File,Inc,Bin,Opt,Deps) of
+         true -> true;
+         false -> compile_files(Files,Inc,Bin,Opt,Deps) end.
 
 module("erl") -> mad_erl;
 module("erl.src") -> mad_utils;
@@ -74,6 +84,7 @@ is_compiled(BeamFile, File) -> mad_utils:last_modified(BeamFile) >= mad_utils:la
 
 'compile-apps'(Cwd, ConfigFile, Conf) ->
     Dirs = mad_utils:sub_dirs(Cwd, ConfigFile, Conf),
+    %io:format("Compile Apps: ~p~n",[Dirs]),
     case Dirs of
         [] -> mad_compile:dep(Cwd, Conf, ConfigFile, Cwd);
         Apps -> mad_compile:deps(Cwd, Conf, ConfigFile, Apps) end.