2012-10-14 13:08:15 -04:00
|
|
|
#!/usr/bin/env escript
|
|
|
|
|
2015-03-18 10:13:44 -04:00
|
|
|
main([File]) ->
|
|
|
|
Dir = get_root(filename:dirname(File)),
|
|
|
|
Defs = [strong_validation,
|
|
|
|
warn_export_all,
|
|
|
|
warn_export_vars,
|
|
|
|
warn_shadow_vars,
|
|
|
|
warn_obsolete_guard,
|
|
|
|
warn_unused_import,
|
|
|
|
report,
|
|
|
|
{i, Dir ++ "/include"}],
|
2015-06-29 09:34:00 -04:00
|
|
|
%% `rebar.config` is looked for,
|
|
|
|
%% but it is not necessarily the one in the project root.
|
|
|
|
%% I.e. it may be one deeper in the project file hierarchy.
|
2015-03-18 10:13:44 -04:00
|
|
|
RebarFile = rebar_file(Dir),
|
2015-06-29 09:34:00 -04:00
|
|
|
%% `rebar.config` might contain relative paths.
|
|
|
|
%% They are relative to the file! Not to the project root.
|
2015-03-18 10:13:44 -04:00
|
|
|
RebarOpts = rebar_opts(Dir ++ "/" ++ RebarFile),
|
|
|
|
code:add_patha(filename:absname("ebin")),
|
2015-06-29 09:34:00 -04:00
|
|
|
%% `compile:file/2` requires the `{i, Path}` to be relative
|
|
|
|
%% to CWD - no surprise here.
|
|
|
|
compile:file(File, Defs ++ translate_paths(Dir, RebarOpts));
|
2013-08-06 11:25:29 -04:00
|
|
|
|
2015-03-18 10:13:44 -04:00
|
|
|
main(_) ->
|
|
|
|
io:format("Usage: ~s <file>~n", [escript:script_name()]),
|
|
|
|
halt(1).
|
2013-08-06 11:25:29 -04:00
|
|
|
|
2015-03-18 10:13:44 -04:00
|
|
|
rebar_file(Dir) ->
|
|
|
|
DirList = filename:split(Dir),
|
|
|
|
case lists:last(DirList) of
|
|
|
|
"test" ->
|
|
|
|
"rebar.test.config";
|
|
|
|
_ ->
|
|
|
|
"rebar.config"
|
|
|
|
end.
|
2013-08-06 11:25:29 -04:00
|
|
|
|
2015-03-18 10:13:44 -04:00
|
|
|
rebar_opts(RebarFile) ->
|
|
|
|
Dir = get_root(filename:dirname(RebarFile)),
|
|
|
|
case file:consult(RebarFile) of
|
|
|
|
{ok, Terms} ->
|
|
|
|
RebarLibDirs = proplists:get_value(lib_dirs, Terms, []),
|
|
|
|
lists:foreach(
|
|
|
|
fun(LibDir) ->
|
|
|
|
code:add_pathsa(filelib:wildcard(LibDir ++ "/*/ebin"))
|
|
|
|
end, RebarLibDirs),
|
|
|
|
RebarDepsDir = proplists:get_value(deps_dir, Terms, "deps"),
|
|
|
|
code:add_pathsa(filelib:wildcard(RebarDepsDir ++ "/*/ebin")),
|
|
|
|
IncludeDeps = {i, filename:join(Dir, RebarDepsDir)},
|
|
|
|
proplists:get_value(erl_opts, Terms, []) ++ [IncludeDeps];
|
|
|
|
{error, _} when RebarFile == "rebar.config" ->
|
|
|
|
fallback_opts();
|
|
|
|
{error, _} ->
|
|
|
|
rebar_opts("rebar.config")
|
|
|
|
end.
|
2013-08-06 11:25:29 -04:00
|
|
|
|
2015-03-18 10:13:44 -04:00
|
|
|
fallback_opts() ->
|
|
|
|
code:add_pathsa(filelib:wildcard("deps/*/ebin")),
|
|
|
|
code:add_pathsa(nested_app_ebins()),
|
2015-06-12 06:15:14 -04:00
|
|
|
[
|
|
|
|
{ i, filename:absname("apps") }, { i, filename:absname("deps") } | [ { i, filename:absname(Path) } || Path <- filelib:wildcard("deps/*/apps")]
|
2015-03-18 10:13:44 -04:00
|
|
|
].
|
2013-08-06 11:25:29 -04:00
|
|
|
|
2015-03-18 10:13:44 -04:00
|
|
|
nested_app_ebins() ->
|
|
|
|
DetectedAppSrcFiles = filelib:wildcard("deps/*/apps/**/*.app.src"),
|
|
|
|
[apps_dir_from_src(AppSrcFile)||AppSrcFile<-DetectedAppSrcFiles].
|
2013-04-18 11:07:15 -04:00
|
|
|
|
2015-03-18 10:13:44 -04:00
|
|
|
apps_dir_from_src(SrcFile) ->
|
|
|
|
SrcDir = filename:dirname(SrcFile),
|
|
|
|
filename:join(SrcDir, "../../ebin").
|
2013-04-18 11:07:15 -04:00
|
|
|
|
|
|
|
get_root(Dir) ->
|
|
|
|
Path = filename:split(filename:absname(Dir)),
|
|
|
|
filename:join(get_root(lists:reverse(Path), Path)).
|
|
|
|
|
|
|
|
get_root([], Path) ->
|
|
|
|
Path;
|
|
|
|
get_root(["src" | Tail], _Path) ->
|
|
|
|
lists:reverse(Tail);
|
2013-09-12 02:29:13 -04:00
|
|
|
get_root(["test" | Tail], _Path) ->
|
|
|
|
lists:reverse(Tail);
|
2013-04-18 11:07:15 -04:00
|
|
|
get_root([_ | Tail], Path) ->
|
|
|
|
get_root(Tail, Path).
|
2015-06-29 09:34:00 -04:00
|
|
|
|
|
|
|
translate_paths(Dir, RebarOpts) ->
|
|
|
|
[ translate_path(Dir, Opt) || Opt <- RebarOpts ].
|
|
|
|
|
|
|
|
translate_path(Dir, {i, Path}) ->
|
|
|
|
case Path of
|
|
|
|
%% absolute
|
|
|
|
"/" ++ _ -> {i, Path};
|
|
|
|
%% relative -> make absolute taking rebar.config location into account
|
|
|
|
_ -> {i, filename:join([Dir, Path])}
|
|
|
|
end;
|
|
|
|
translate_path(_, Other) -> Other.
|