Namdak Tonpa 6 years ago
parent
commit
caa5973f33
8 changed files with 67 additions and 51 deletions
  1. 1 1
      include/mad.hrl
  2. BIN
      mad
  3. 15 13
      src/compile/mad_app.erl
  4. 13 10
      src/compile/mad_compile.erl
  5. 21 11
      src/mad_git.erl
  6. 5 3
      src/mad_hooks.erl
  7. 8 11
      src/mad_utils.erl
  8. 4 2
      src/provide/mad_repl.erl

+ 1 - 1
include/mad.hrl

@@ -1 +1 @@
--define(VERSION,"9c25cd").
+-define(VERSION,"044bd9").

BIN
mad


+ 15 - 13
src/compile/mad_app.erl

@@ -14,19 +14,21 @@ validate_property(Else, _) -> Else.
 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 ->
-        mad:info("Writing ~s~n", [AppFile -- mad_utils:cwd()]),
-        BeamFiles = filelib:wildcard("*.beam", Bin),
-        Modules = [list_to_atom(filename:basename(X, ".beam")) || X <- BeamFiles],
-        [Struct|_] = mad_utils:consult(File),
-        {application, AppName, Props} = Struct,
-        Props0 = add_modules_property(Props),
-        Props1 = generate_deps(AppName,Props0),
-        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.
+    case Compiled of
+        false ->
+           mad:info("Writing ~s~n", [AppFile -- mad_utils:cwd()]),
+           BeamFiles = filelib:wildcard("*.beam", Bin),
+           Modules = [list_to_atom(filename:basename(X, ".beam")) || X <- BeamFiles],
+           case mad_utils:consult(File) of
+                {error,_} -> true;
+                {ok,[Struct|_]} -> {application, AppName, Props} = Struct,
+                                   Props0 = add_modules_property(Props),
+                                   Props1 = generate_deps(AppName,Props0),
+                                   Props2 = [validate_property(X, Modules) || X <- Props1],
+                                   Struct1 = {application, AppName, Props2},
+                                   file:write_file(AppFile, io_lib:format("~p.~n", [Struct1])),
+                                   false end;
+         _ -> false end.
 
 add_modules_property(Properties) ->
     case lists:keyfind(modules, 1, Properties) of

+ 13 - 10
src/compile/mad_compile.erl

@@ -3,14 +3,15 @@
 -compile(export_all).
 
 compile(Params) ->
-    { Cwd, ConfigFile, Conf } = mad_utils:configs(),
-    Res = case Params of
-         [] -> mad_compile:'compile-deps'(Cwd, ConfigFile, Conf);
-         __ -> mad_compile:deps(Cwd, Conf, ConfigFile, [Params])
-    end,
-    case Res of
-         {error,Reason} -> {error,Reason};
-         {ok,_} -> mad_compile:'compile-apps'(Cwd, ConfigFile, Conf) end.
+    case mad_utils:configs() of
+         {error,_} -> {error,"rebar.config error on compile."};
+         {ok,{ Cwd, ConfigFile, Conf }} ->
+           Res = case Params of
+                 [] -> mad_compile:'compile-deps'(Cwd, ConfigFile, Conf);
+                 __ -> mad_compile:deps(Cwd, Conf, ConfigFile, [Params]) end,
+           case Res of
+                {error,Reason} -> {error,Reason};
+                {ok,_} -> mad_compile:'compile-apps'(Cwd, ConfigFile, Conf) end end.
 
 deps(_, _, _, []) -> {ok,deps};
 deps(Cwd, Conf, ConfigFile, [H|T]) ->
@@ -32,7 +33,9 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
     mad:info("==> ~p~n",[Name]),
 
     DepConfig = filename:join(DepPath, ConfigFile),
-    Conf      = mad_utils:consult(DepConfig),
+    case mad_utils:consult(DepConfig) of
+         {error,_} -> {error, <<"rebar.config is missing.">>};
+         {ok,Conf} ->
     Conf1     = mad_script:script(DepConfig, Conf, Name),
     Deps      = mad_utils:get_value(deps, Conf1, []),
     DepsRes   = bool(deps(Cwd, Conf, ConfigFile, Deps)),
@@ -72,7 +75,7 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
             put(Name, compiled),
             case (DepsRes orelse FilesStatus orelse DTLStatus orelse PortStatus) of
                  true  -> {error,Name};
-                 false -> {ok,Name} end end.
+                 false -> {ok,Name} end end end.
 
 compile_files([],_,_,_,_) -> false;
 compile_files([File|Files],Inc,Bin,Opt,Deps) ->

+ 21 - 11
src/mad_git.erl

@@ -2,11 +2,13 @@
 -compile(export_all).
 
 deps(_Params) ->
-    { Cwd, ConfigFile, Conf } = mad_utils:configs(),
+    case mad_utils:configs() of
+      {error, _ } -> {error, <<"rebar.config error">>};
+      {ok,{ Cwd, ConfigFile, Conf }} ->
     case mad_utils:get_value(deps, Conf, []) of
         [] -> {ok,[]};
         Deps -> file:make_dir(mad_utils:get_value(deps_dir, Conf, ["deps"])),
-                (mad:profile()):fetch([Cwd, Conf, ConfigFile, Deps]) end.
+                (mad:profile()):fetch([Cwd, Conf, ConfigFile, Deps]) end end.
 
 fetch([Cwd, Conf, ConfigFile, Deps]) -> fetch(Cwd, Conf, ConfigFile, Deps).
 fetch(_, _Config, _, []) -> false;
@@ -32,7 +34,9 @@ fetch(Cwd, Config, ConfigFile, [H|T]) ->
 
 get_repo([]) -> {error,"Repository unspecified."};
 get_repo([Name|_]) ->
-    { Cwd, File, Conf } = mad_utils:configs(),
+    case mad_utils:configs() of
+      {error,_} -> {error,<<"rebar.config error while retriving deps">>};
+      {ok,{ Cwd, File, Conf }} ->
     Res = case string:tokens(Name,"/") of
          [Org, Rep] -> {ok,Rep,lists:concat(["https://github.com/",Org,"/",Rep])};
          [Rep] -> {ok,Rep,lists:concat(["https://github.com/synrc/",Rep])};
@@ -41,7 +45,7 @@ get_repo([Name|_]) ->
     case Res of
          {error,X} -> {error,X};
          {ok,N,Uri} -> fetch_dep(Cwd,Conf,File,N,"git",Uri,[],deps_fetch,[])
-    end.
+    end end.
 
 git_clone(Uri,Fast,TrunkPath,Rev) when Rev == "head"   orelse Rev == "HEAD"
                                 orelse Rev == "master" orelse Rev == [] ->
@@ -82,7 +86,10 @@ fetch_dep(Cwd, Config, ConfigFile, Name, Cmd, Uri, Co, Cache, Deep) ->
 
                     %% check dependencies of the dependency
                     TrunkConfigFile = filename:join(TrunkPath, ConfigFile),
-                    Conf = mad_utils:consult(TrunkConfigFile),
+                    Conf0 = mad_utils:consult(TrunkConfigFile),
+                    case Conf0 of
+                         {error,_} -> {error, <<"rebar.config error">>};
+                         {ok,Conf} ->
                     Conf1 = mad_utils:script(TrunkConfigFile, Conf, Name),
                     case Deep of
                          deep -> fetch(Cwd, Config, ConfigFile, mad_utils:get_value(deps, Conf1, []));
@@ -92,7 +99,7 @@ fetch_dep(Cwd, Config, ConfigFile, Name, Cmd, Uri, Co, Cache, Deep) ->
                          deps_fetch -> {ok,Name};
                          CacheDir -> build_dep(Cwd, Config, ConfigFile,
                                         get_publisher(Uri), Name, Cmd, Co1, CacheDir)
-                    end;
+                    end end;
     {_,_,FetchError} -> {error,FetchError} end.
 
 %% build dependency based on branch/tag/commit
@@ -136,9 +143,12 @@ upd(Config,[F|T]) ->
                    _ -> upd(Config,T) end end.
 
 up(Params) ->
-  { _Cwd,_ConfigFileName,Config } = mad_utils:configs(),
-  List = case Params of
-                [] -> [ F || F <- mad_repl:wildcards(["deps/*"]), filelib:is_dir(F) ];
-                Apps -> [ "deps/" ++ A || A <- Apps ] end ++ ["."],
-    upd(Config,List).
+  case mad_utils:configs() of
+       {error,_} -> {error,<<"rebar.config error while up.">>};
+       { _Cwd,_ConfigFileName,Config } ->
+          List = case Params of
+                 [] -> [ F || F <- mad_repl:wildcards(["deps/*"]), filelib:is_dir(F) ];
+                 Apps -> [ "deps/" ++ A || A <- Apps ] end ++ ["."],
+          upd(Config,List)
+  end.
 

+ 5 - 3
src/mad_hooks.erl

@@ -4,9 +4,11 @@
 
 -spec run_hooks(pre|post, atom()) -> any().
 run_hooks(Type, Command) ->
-    {_Cwd, _ConfigFile, Config} = mad_utils:configs(),
-    Dir = mad_utils:cwd(),
-    run_hooks(Dir, Type, Command, Config).
+    case mad_utils:configs() of
+         {error,_} -> {error, <<"rebar.config error while hooks.">>};
+         {ok,{_Cwd, _ConfigFile, Config}} ->
+             Dir = mad_utils:cwd(),
+             run_hooks(Dir, Type, Command, Config) end.
 
 run_hooks(Dir, pre, Command, Config) -> run_hooks(Dir, pre_hooks, Command, Config);
 run_hooks(Dir, post, Command, Config) -> run_hooks(Dir, post_hooks, Command, Config);

+ 8 - 11
src/mad_utils.erl

@@ -13,14 +13,10 @@ name_and_repo(Name) -> {Name,Name}.
 
 cwd() -> {ok, Cwd} = file:get_cwd(), Cwd.
 home() -> {ok, [[H|_]]} = init:get_argument(home), H.
-consult(File) ->
-    AbsFile = filename:absname(File),
-    case file:consult(AbsFile) of
-        {ok, V} ->
-            V;
-        _ ->
-            []
-    end.
+consult(File) -> case file:consult(filename:absname(File)) of
+                      {error,enoent} -> {ok,[]};
+                      {error,E} -> {error,E};
+                      {ok,V} -> {ok,V} end.
 
 src(Dir) -> filename:join(Dir, "src").
 include(Dir) -> filename:join(Dir, "include").
@@ -91,6 +87,7 @@ configs() ->
     Cwd            = try fs:path() catch _:_ -> cwd() end,
     ConfigFile     = "rebar.config",
     ConfigFileAbs  = filename:join(Cwd, ConfigFile),
-    Conf           = mad_utils:consult(ConfigFileAbs),
-    Conf1          = mad_script:script(ConfigFileAbs, Conf, ""),
-    {Cwd,ConfigFile,Conf1}.
+    case mad_utils:consult(ConfigFileAbs) of
+         {error,E} -> {error,E};
+         {ok,Conf} -> Conf1 = mad_script:script(ConfigFileAbs, Conf, ""),
+                      {ok,{Cwd,ConfigFile,Conf1}} end.

+ 4 - 2
src/provide/mad_repl.erl

@@ -96,7 +96,9 @@ add_replace(_____,Name,Pos,List,New) -> lists:keyreplace(Name,Pos,List,New).
 cwd() -> case  file:get_cwd() of {ok, Cwd} -> Cwd; _ -> "." end.
 
 sh(Params) ->
-    { _Cwd,_ConfigFileName,_Config } = mad_utils:configs(),
+    case mad_utils:configs() of
+         {error,_} -> {error,<<"rebar.config error while shell.">>};
+    {ok,{ _Cwd,_ConfigFileName,_Config }} ->
     SystemPath = filelib:wildcard(code:root_dir() ++ "/lib/{"
               ++ string:join([atom_to_list(X)||X<-mad_repl:system()],",") ++ "}-*/ebin"),
     UserPath   = wildcards(["{apps,deps}/*/ebin","ebin"]),
@@ -122,7 +124,7 @@ sh(Params) ->
     load_apps(Params,Config,[]),
     case Params of
         ["applist"] -> skip;
-        _ ->  timer:sleep(infinity) end.
+        _ ->  timer:sleep(infinity) end end.
 
 remove(0) -> skip;
 remove(N) -> case gen_event:delete_handler(error_logger, error_logger, []) of