Browse Source

synrc code guidelines

Maxim Sokhatsky 11 years ago
parent
commit
12dbf5f8f8
7 changed files with 49 additions and 116 deletions
  1. BIN
      mad
  2. 2 1
      rebar.config
  3. 3 10
      src/mad.app.src
  4. 4 6
      src/mad.erl
  5. 13 31
      src/mad_compile.erl
  6. 14 24
      src/mad_deps.erl
  7. 13 44
      src/mad_utils.erl

BIN
mad


+ 2 - 1
rebar.config

@@ -1,4 +1,5 @@
+{cache,"~/.mad/repos2"}.
 {deps, [
-        {getopt, ".*",   {git, "git://github.com/jcomellas/getopt.git", {tag, "v0.8.2"}}}
+        {getopt, ".*",   {git, "git@github.com:jcomellas/getopt.git", {tag, "v0.8.2"}}}
 %        {n2o, ".*",   {git, "git@github.com:5HT/n2o.git", master}}
        ]}.

+ 3 - 10
src/mad.app.src

@@ -1,14 +1,7 @@
-%%-*- mode: erlang -*-
-
 {application, mad,
- [
-  {description, "Dependency manager for Erlang"},
+ [{description, "Dependency manager for Erlang"},
   {vsn, ""},
   {registered, []},
-  {applications, [
-                  kernel,
-                  stdlib
-                 ]},
+  {applications, [kernel,stdlib]},
   {mod, { mad_app, []}},
-  {env, []}
- ]}.
+  {env, []}]}.

+ 4 - 6
src/mad.erl

@@ -2,9 +2,9 @@
 -copyright('Sina Samavati').
 -export([main/1,'fetch-deps'/3,compile/3,'compile-app'/3,'compile-deps'/3]).
 
-main([]) ->
-    help();
+main([]) -> help();
 main(Args) ->
+
     {Opts, Params} = case getopt:parse(option_spec_list(), Args) of
                          {ok, {Opts1, Params1}} ->
                              {Opts1, [list_to_atom(E) || E <- Params1]};
@@ -35,8 +35,7 @@ main(Args) ->
 %% fetch dependencies
 'fetch-deps'(Cwd, ConfigFile, Conf) ->
     case get_value(deps, Conf, []) of
-        [] ->
-            ok;
+        [] -> ok;
         Deps ->
             Cache = mad_utils:get_value(deps_dir, Conf, deps_fetch),
             case Cache of
@@ -68,8 +67,7 @@ get_value(Key, Opts, Default) ->
     case lists:keyfind(Key, 1, Opts) of
         {Key, Value} ->
             Value;
-        _ -> Default
-    end.
+        _ -> Default end.
 
 option_spec_list() ->
     [

+ 13 - 31
src/mad_compile.erl

@@ -10,16 +10,12 @@
 
 %% compile dependencies
 -spec deps(directory(), any(), filename(), [mad_deps:dependency()]) -> ok.
-deps(_, _, _, []) ->
-    ok;
+deps(_, _, _, []) -> ok;
 deps(Cwd, Conf, ConfigFile, [H|T]) ->
     {Name, _} = mad_deps:name_and_repo(H),
     case get(Name) of
-        compiled ->
-            ok;
-        _ ->
-            dep(Cwd, Conf, ConfigFile, Name)
-    end,
+        compiled -> ok;
+        _ -> dep(Cwd, Conf, ConfigFile, Name) end,
     deps(Cwd, Conf, ConfigFile, T).
 
 %% compile a dependency
@@ -44,8 +40,7 @@ dep(Cwd, _Conf, ConfigFile, Name) ->
     SrcDir = filename:join([mad_utils:src(DepPath)]),
     Files = sort(erl_files(SrcDir)) ++ app_src_files(SrcDir),
     case Files of
-        [] ->
-            ok;
+        [] -> ok;
         Files ->
             IncDir = mad_utils:include(DepPath),
             EbinDir = mad_utils:ebin(DepPath),
@@ -67,8 +62,7 @@ app(Dir, Conf, ConfigFile) ->
     SrcDir = mad_utils:src(Dir),
     Files = sort(erl_files(SrcDir)) ++ app_src_files(SrcDir),
     case Files of
-        [] ->
-            ok;
+        [] -> ok;
         Files ->
             IncDir = mad_utils:include(Dir),
             EbinDir = mad_utils:ebin(Dir),
@@ -104,9 +98,7 @@ compile_fun(IncDir, EbinDir, Opts) ->
                             io:format("Compiling ~s~n", [File]),
                             Opts1 = ?COMPILE_OPTS(IncDir, EbinDir, Opts),
                             compile:file(File, Opts1);
-                       true ->
-                            ok
-                    end;
+                       true -> ok end;
                 true ->
                     %% add {modules, [Modules]} to .app file
                     AppFile = filename:join(EbinDir, app_src_to_app(File)),
@@ -169,24 +161,16 @@ sort_by_priority([], 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,
+            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,
+               [] -> {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;
+-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).
@@ -197,9 +181,7 @@ is_behaviour(File) ->
         true ->
             [] =/= mad_utils:exec("sed", ["-n", "-e", "'/-callback/p'", "-e",
                                           "'/behaviour_info\\/1/p'", File]);
-        _ ->
-            false
-    end.
+        _ -> false end.
 
 get_kv(K, Opts, Default) ->
     V = mad_utils:get_value(K, Opts, Default),

+ 14 - 24
src/mad_deps.erl

@@ -1,6 +1,6 @@
 -module(mad_deps).
 -copyright('Sina Samavati').
--export([path/3,fetch/4,name_and_repo/1,checkout_to/1,get_publisher/1]).
+-export([fetch/4,name_and_repo/1,checkout_to/1,get_publisher/1]).
 
 -type directory() :: string().
 -type filename() :: string().
@@ -11,16 +11,9 @@
 -type dependency() :: {name(), string(), repo()}.
 -export_type([dependency/0]).
 
--spec path(string(), string(), string()) -> directory().
-path(Dir,Publisher, Repo) ->
-    %% ~/.mad/repos/Publisher/Repo
-    filename:join([Dir, Publisher, Repo]).
-
 -spec fetch(directory(), any(), filename(), [dependency()]) -> ok.
-fetch(_, _Config, _, []) ->
-    ok;
-fetch(Cwd, Config, ConfigFile, [H|T]) when is_tuple(H) =:= false ->
-    fetch(Cwd, Config, ConfigFile, T);
+fetch(_, _Config, _, []) -> ok;
+fetch(Cwd, Config, ConfigFile, [H|T]) when is_tuple(H) =:= false -> fetch(Cwd, Config, ConfigFile, T);
 fetch(Cwd, Config, ConfigFile, [H|T]) ->
     {Name, Repo} = name_and_repo(H),
     {Cmd, Uri, Co} = case Repo of
@@ -43,7 +36,7 @@ fetch_dep(Cwd, Config, ConfigFile, Name, Cmd, Uri, Co, Cache) ->
 
     TrunkPath = case Cache of
         deps_fetch -> filename:join([mad_utils:get_value(deps_dir,Config,"deps"),Name]);
-        Dir -> path(Dir,get_publisher(Uri),Name) end,
+        Dir -> filename:join([Dir,get_publisher(Uri),Name]) end,
 
     Opts = ["clone", Uri, TrunkPath ],
     io:format("dependency: ~s~n", [Name]),
@@ -57,28 +50,23 @@ fetch_dep(Cwd, Config, ConfigFile, Name, Cmd, Uri, Co, Cache) ->
     Conf1 = mad_utils:script(TrunkConfigFile, Conf),
     fetch(Cwd, Config, ConfigFile, mad_utils:get_value(deps, Conf1, [])),
     case Cache of
-       deps_dir -> skip;
+       deps_fetch -> skip;
        CacheDir -> build_dep(Cwd, Config, ConfigFile, get_publisher(Uri), Name, Cmd, Co, CacheDir) end.
 
 %% 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) ->
-    TrunkPath = path(Dir, Publisher, Name),
-%    DepsDir = filename:join([Cwd, "deps", Name]),
+    TrunkPath = filename:join([Dir, Publisher, Name]),
     DepsDir = mad_utils:get_value(deps_dir, Conf, ["deps"]),
-    %% get a copy of dependency from trunk
     mad_utils:exec("cp", ["-r", TrunkPath, DepsDir]),
-    %% change cwd to the copy of trunk and checkout to Co
     ok = file:set_cwd(DepsDir),
     mad_utils:exec(Cmd, ["checkout", lists:concat([Co])]),
     ok = file:set_cwd(Cwd).
 
 %% 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, _, Repo, _}) -> {atom_to_list(Name), Repo}.
 
 -spec checkout_to(term() | {any(), string}) -> term().
 checkout_to({_, V}) -> V;
@@ -86,7 +74,9 @@ checkout_to(Else) -> Else.
 
 -spec get_publisher(uri()) -> string().
 get_publisher(Uri) ->
-    S = [{git, 9418}|http_uri:scheme_defaults()],
-    {ok, {_, _, _, _, Path, _}} = http_uri:parse(Uri, [{scheme_defaults, S}]),
-    [Publisher|_] = string:tokens(Path, "/"),
-    Publisher.
+    case http_uri:parse(Uri, [{scheme_defaults,
+            [{git, 9418}|http_uri:scheme_defaults()]}]) of
+        {ok, {_, _, _, _, Path, _}} -> hd(string:tokens(Path,"/"));
+        _ -> case string:tokens(Uri,":/") of
+                [_Server,Publisher,_Repo] -> Publisher;
+                _ -> exit(error) end end.

+ 13 - 44
src/mad_utils.erl

@@ -1,28 +1,19 @@
 -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,https_to_git/1,git_to_https/1,last_modified/1]).
+         script/2,sub_dirs/3,lib_dirs/2,last_modified/1]).
 
 -type directory() :: string().
 
-
-%% get current working directory
 -spec cwd() -> directory().
-cwd() ->
-    {ok, Cwd} = file:get_cwd(),
-    Cwd.
+cwd() -> {ok, Cwd} = file:get_cwd(), Cwd.
 
-%% execute a shell command
 exec(Cmd, Opts) ->
     Opts1 = [" " ++ X || X <- Opts],
     os:cmd(Cmd ++ lists:concat(Opts1)).
 
-%% return $HOME
 -spec home() -> directory().
-home() ->
-    %% ~/
-    {ok, [[H|_]]} = init:get_argument(home),
-    H.
+home() -> {ok, [[H|_]]} = init:get_argument(home), H.
 
 -spec consult(file:name_all()) -> [term()].
 consult(File) ->
@@ -35,31 +26,23 @@ consult(File) ->
     end.
 
 -spec src(directory()) -> directory().
-src(Dir) ->
-    %% Dir/src
-    filename:join(Dir, "src").
+src(Dir) -> filename:join(Dir, "src").
 
 -spec include(directory()) -> directory().
-include(Dir) ->
-    %% Dir/include
-    filename:join(Dir, "include").
+include(Dir) -> filename:join(Dir, "include").
 
 -spec ebin(directory()) -> directory().
-ebin(Dir) ->
-    %% Dir/ebin
-    filename:join(Dir, "ebin").
+ebin(Dir) -> filename:join(Dir, "ebin").
 
 -spec deps(file:name_all()) -> [term()].
-deps(File) ->
-    get_value(deps, consult(File), []).
+deps(File) -> get_value(deps, consult(File), []).
 
 -spec get_value(term(), [{term(), term()}], Default) -> term() | Default.
 get_value(Key, Opts, Default) ->
     case lists:keyfind(Key, 1, Opts) of
         {Key, Value} ->
             Value;
-        _ -> Default
-    end.
+        _ -> Default end.
 
 -spec script(file:name(), [term()]) -> [term()].
 script(ConfigFile, Conf) ->
@@ -76,8 +59,7 @@ sub_dirs(Cwd, ConfigFile, Conf) ->
     sub_dirs(Cwd, ConfigFile, get_value(sub_dirs, Conf, []), []).
 
 -spec sub_dirs(directory(), file:filename(), [term()], [term()]) -> [directory()].
-sub_dirs(_, _, [], Acc) ->
-    Acc;
+sub_dirs(_, _, [], Acc) -> Acc;
 sub_dirs(Cwd, ConfigFile, [Dir|T], Acc) ->
     SubDir = filename:join(Cwd, Dir),
     ConfigFile1 = filename:join(SubDir, ConfigFile),
@@ -88,29 +70,16 @@ sub_dirs(Cwd, ConfigFile, [Dir|T], Acc) ->
     sub_dirs(Cwd, ConfigFile, T, Acc1).
 
 -spec lib_dirs(directory(), [term()]) -> [directory()].
-lib_dirs(Cwd, Conf) ->
-    lib_dirs(Cwd, get_value(lib_dirs, Conf, []), []).
+lib_dirs(Cwd, Conf) -> lib_dirs(Cwd, get_value(lib_dirs, Conf, []), []).
 
 -spec lib_dirs(directory(), [term()], [term()]) -> [directory()].
-lib_dirs(_, [], Acc) ->
-    Acc;
+lib_dirs(_, [], Acc) -> Acc;
 lib_dirs(Cwd, [H|T], Acc) ->
     Dirs = filelib:wildcard(filename:join([Cwd, H, "*", "ebin"])),
     lib_dirs(Cwd, T, Acc ++ Dirs).
 
--spec https_to_git(string()) -> string().
-https_to_git(X) ->
-    re:replace(X, "https://", "git://", [{return, list}]).
-
--spec git_to_https(string()) -> string().
-git_to_https(X) ->
-    re:replace(X, "git://", "https://", [{return, list}]).
-
 -spec last_modified(file:name_all()) -> Seconds :: non_neg_integer().
 last_modified(File) ->
     case filelib:last_modified(File) of
-        0 ->
-            0;
-        Else ->
-            calendar:datetime_to_gregorian_seconds(Else)
-    end.
+        0 -> 0;
+        Else -> calendar:datetime_to_gregorian_seconds(Else) end.