erlang_check_file.erl 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env escript
  2. main([File]) ->
  3. Dir = get_root(filename:dirname(File)),
  4. Defs = [strong_validation,
  5. warn_export_all,
  6. warn_export_vars,
  7. warn_shadow_vars,
  8. warn_obsolete_guard,
  9. warn_unused_import,
  10. report,
  11. {i, Dir ++ "/include"}],
  12. %% `rebar.config` is looked for,
  13. %% but it is not necessarily the one in the project root.
  14. %% I.e. it may be one deeper in the project file hierarchy.
  15. RebarFile = rebar_file(Dir),
  16. %% `rebar.config` might contain relative paths.
  17. %% They are relative to the file! Not to the project root.
  18. RebarOpts = rebar_opts(Dir ++ "/" ++ RebarFile),
  19. code:add_patha(filename:absname("ebin")),
  20. %% `compile:file/2` requires the `{i, Path}` to be relative
  21. %% to CWD - no surprise here.
  22. compile:file(File, Defs ++ translate_paths(Dir, RebarOpts));
  23. main(_) ->
  24. io:format("Usage: ~s <file>~n", [escript:script_name()]),
  25. halt(1).
  26. rebar_file(Dir) ->
  27. DirList = filename:split(Dir),
  28. case lists:last(DirList) of
  29. "test" ->
  30. "rebar.test.config";
  31. _ ->
  32. "rebar.config"
  33. end.
  34. rebar_opts(RebarFile) ->
  35. Dir = get_root(filename:dirname(RebarFile)),
  36. case file:consult(RebarFile) of
  37. {ok, Terms} ->
  38. RebarLibDirs = proplists:get_value(lib_dirs, Terms, []),
  39. lists:foreach(
  40. fun(LibDir) ->
  41. code:add_pathsa(filelib:wildcard(LibDir ++ "/*/ebin"))
  42. end, RebarLibDirs),
  43. RebarDepsDir = proplists:get_value(deps_dir, Terms, "deps"),
  44. code:add_pathsa(filelib:wildcard(RebarDepsDir ++ "/*/ebin")),
  45. IncludeDeps = {i, filename:join(Dir, RebarDepsDir)},
  46. proplists:get_value(erl_opts, Terms, []) ++ [IncludeDeps];
  47. {error, _} when RebarFile == "rebar.config" ->
  48. fallback_opts();
  49. {error, _} ->
  50. rebar_opts("rebar.config")
  51. end.
  52. fallback_opts() ->
  53. code:add_pathsa(filelib:wildcard("deps/*/ebin")),
  54. code:add_pathsa(nested_app_ebins()),
  55. [
  56. { i, filename:absname("apps") }, { i, filename:absname("deps") } | [ { i, filename:absname(Path) } || Path <- filelib:wildcard("deps/*/apps")]
  57. ].
  58. nested_app_ebins() ->
  59. DetectedAppSrcFiles = filelib:wildcard("deps/*/apps/**/*.app.src"),
  60. [apps_dir_from_src(AppSrcFile)||AppSrcFile<-DetectedAppSrcFiles].
  61. apps_dir_from_src(SrcFile) ->
  62. SrcDir = filename:dirname(SrcFile),
  63. filename:join(SrcDir, "../../ebin").
  64. get_root(Dir) ->
  65. Path = filename:split(filename:absname(Dir)),
  66. filename:join(get_root(lists:reverse(Path), Path)).
  67. get_root([], Path) ->
  68. Path;
  69. get_root(["src" | Tail], _Path) ->
  70. lists:reverse(Tail);
  71. get_root(["test" | Tail], _Path) ->
  72. lists:reverse(Tail);
  73. get_root([_ | Tail], Path) ->
  74. get_root(Tail, Path).
  75. translate_paths(Dir, RebarOpts) ->
  76. [ translate_path(Dir, Opt) || Opt <- RebarOpts ].
  77. translate_path(Dir, {i, Path}) ->
  78. case Path of
  79. %% absolute
  80. "/" ++ _ -> {i, Path};
  81. %% relative -> make absolute taking rebar.config location into account
  82. _ -> {i, filename:join([Dir, Path])}
  83. end;
  84. translate_path(_, Other) -> Other.