Browse Source

Add specs for mad_utils

Sina Samavati 11 years ago
parent
commit
23665f69de
3 changed files with 26 additions and 11 deletions
  1. 1 0
      .gitignore
  2. 2 2
      Makefile
  3. 23 9
      src/mad_utils.erl

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@ ebin
 *.beam
 erl_crash.dump
 logs
+.mad.plt

+ 2 - 2
Makefile

@@ -176,9 +176,9 @@ PLT_APPS ?=
 DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
 	-Wunmatched_returns # -Wunderspecs
 
-build-plt: deps app
+build-plt: app
 	@dialyzer --build_plt --output_plt .$(PROJECT).plt \
-		--apps erts kernel stdlib $(PLT_APPS) -r deps/*/ebin
+		--apps erts kernel stdlib $(PLT_APPS)
 
 dialyze:
 	@dialyzer --src src --plt .$(PROJECT).plt --no_native $(DIALYZER_OPTS)

+ 23 - 9
src/mad_utils.erl

@@ -17,23 +17,28 @@
 -export([git_to_https/1]).
 -export([last_modified/1]).
 
+-type directory() :: string().
+
 
 %% get current working directory
+-spec cwd() -> directory().
 cwd() ->
     {ok, Cwd} = file:get_cwd(),
     Cwd.
 
 %% execute a shell command
 exec(Cmd, Opts) ->
-    Opts1 = [concat([" ", X]) || X <- Opts],
-    os:cmd(concat([Cmd, concat(Opts1)])).
+    Opts1 = [" " ++ X || X <- Opts],
+    os:cmd(Cmd ++ lists:concat(Opts1)).
 
 %% return $HOME
+-spec home() -> directory().
 home() ->
     %% ~/
     {ok, [[H|_]]} = init:get_argument(home),
     H.
 
+-spec consult(file:name_all()) -> [term()].
 consult(File) ->
     AbsFile = filename:absname(File),
     case file:consult(AbsFile) of
@@ -43,25 +48,31 @@ consult(File) ->
             []
     end.
 
+-spec rebar_conf(directory()) -> [term()].
 rebar_conf(Dir) ->
     Dir1 = filename:absname(Dir),
     consult(filename:join(Dir1, "rebar.config")).
 
+-spec src(directory()) -> directory().
 src(Dir) ->
     %% Dir/src
     filename:join(Dir, "src").
 
+-spec include(directory()) -> directory().
 include(Dir) ->
     %% Dir/include
     filename:join(Dir, "include").
 
+-spec ebin(directory()) -> directory().
 ebin(Dir) ->
     %% Dir/ebin
     filename:join(Dir, "ebin").
 
+-spec deps(file:name_all()) -> [term()].
 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} ->
@@ -69,6 +80,7 @@ get_value(Key, Opts, Default) ->
         _ -> Default
     end.
 
+-spec script(directory(), [term()]) -> [term()].
 script(Dir, Conf) ->
     File = filename:join(Dir, "rebar.config.script"),
     case file:script(File, [{'CONFIG', Conf}]) of
@@ -78,33 +90,40 @@ script(Dir, Conf) ->
             Conf
     end.
 
+-spec sub_dirs(directory(), [term()]) -> [directory()].
 sub_dirs(Cwd, Conf) ->
     sub_dirs(Cwd, get_value(sub_dirs, Conf, []), []).
 
+-spec sub_dirs(directory(), [term()], [term()]) -> [directory()].
 sub_dirs(_, [], Acc) ->
     Acc;
 sub_dirs(Cwd, [Dir|T], Acc) ->
     SubDir = filename:join(Cwd, Dir),
     Conf = rebar_conf(SubDir),
     Conf1 = script(SubDir, Conf),
-    Acc1 = sub_dirs(SubDir, get_value(sub_dirs, Conf1, []), Acc),
-    sub_dirs(Cwd, T, [SubDir|Acc1]).
+    Acc1 = sub_dirs(SubDir, get_value(sub_dirs, Conf1, []), Acc ++ [SubDir]),
+    sub_dirs(Cwd, T, Acc1).
 
+-spec lib_dirs(directory(), [term()]) -> [directory()].
 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(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 ->
@@ -112,8 +131,3 @@ last_modified(File) ->
         Else ->
             calendar:datetime_to_gregorian_seconds(Else)
     end.
-
-
-%% internal
-concat(L) ->
-    lists:concat(L).