25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
7.1 KiB

10 년 전
10 년 전
10 년 전
  1. -module(rebar_app_discover).
  2. -export([do/2,
  3. format_error/1,
  4. find_unbuilt_apps/1,
  5. find_apps/1,
  6. find_apps/2,
  7. find_app/2]).
  8. -include_lib("providers/include/providers.hrl").
  9. do(State, LibDirs) ->
  10. BaseDir = rebar_state:dir(State),
  11. Dirs = [filename:join(BaseDir, LibDir) || LibDir <- LibDirs],
  12. Apps = find_apps(Dirs, all),
  13. ProjectDeps = rebar_state:deps_names(State),
  14. DepsDir = rebar_dir:deps_dir(State),
  15. %% Sort apps so we get the same merged deps config everytime
  16. SortedApps = rebar_utils:sort_deps(Apps),
  17. lists:foldl(fun(AppInfo, StateAcc) ->
  18. {AppInfo1, StateAcc1} = merge_deps(AppInfo, StateAcc),
  19. Name = rebar_app_info:name(AppInfo),
  20. OutDir = filename:join(DepsDir, Name),
  21. AppInfo2 = rebar_app_info:out_dir(AppInfo1, OutDir),
  22. ProjectDeps1 = lists:delete(Name, ProjectDeps),
  23. rebar_state:project_apps(StateAcc1
  24. ,rebar_app_info:deps(AppInfo2, ProjectDeps1))
  25. end, State, SortedApps).
  26. format_error({module_list, File}) ->
  27. io_lib:format("Error reading module list from ~p~n", [File]);
  28. format_error({missing_module, Module}) ->
  29. io_lib:format("Module defined in app file missing: ~p~n", [Module]).
  30. merge_deps(AppInfo, State) ->
  31. Default = rebar_state:default(State),
  32. CurrentProfiles = rebar_state:current_profiles(State),
  33. Name = rebar_app_info:name(AppInfo),
  34. C = rebar_config:consult(rebar_app_info:dir(AppInfo)),
  35. %% We reset the opts here to default so no profiles are applied multiple times
  36. AppState = rebar_state:apply_overrides(
  37. rebar_state:apply_profiles(
  38. rebar_state:new(rebar_state:opts(State, Default), C, rebar_app_info:dir(AppInfo)), CurrentProfiles), Name),
  39. AppInfo1 = rebar_app_info:state(AppInfo, AppState),
  40. State1 = lists:foldl(fun(Profile, StateAcc) ->
  41. AppProfDeps = rebar_state:get(AppState, {deps, Profile}, []),
  42. TopLevelProfDeps = rebar_state:get(StateAcc, {deps, Profile}, []),
  43. ProfDeps2 = dedup(lists:keymerge(1,
  44. lists:keysort(1, TopLevelProfDeps),
  45. lists:keysort(1, AppProfDeps))),
  46. rebar_state:set(StateAcc, {deps, Profile}, ProfDeps2)
  47. end, State, lists:reverse(CurrentProfiles)),
  48. {AppInfo1, State1}.
  49. -spec all_app_dirs(list(file:name())) -> list(file:name()).
  50. all_app_dirs(LibDirs) ->
  51. lists:flatmap(fun(LibDir) ->
  52. app_dirs(LibDir)
  53. end, LibDirs).
  54. app_dirs(LibDir) ->
  55. Path1 = filename:join([LibDir,
  56. "*",
  57. "src",
  58. "*.app.src"]),
  59. Path2 = filename:join([LibDir,
  60. "src",
  61. "*.app.src"]),
  62. Path3 = filename:join([LibDir,
  63. "*",
  64. "ebin",
  65. "*.app"]),
  66. Path4 = filename:join([LibDir,
  67. "ebin",
  68. "*.app"]),
  69. lists:usort(lists:foldl(fun(Path, Acc) ->
  70. Files = filelib:wildcard(ec_cnv:to_list(Path)),
  71. [app_dir(File) || File <- Files] ++ Acc
  72. end, [], [Path1, Path2, Path3, Path4])).
  73. find_unbuilt_apps(LibDirs) ->
  74. find_apps(LibDirs, invalid).
  75. -spec find_apps([file:filename_all()]) -> [rebar_app_info:t()].
  76. find_apps(LibDirs) ->
  77. find_apps(LibDirs, valid).
  78. -spec find_apps([file:filename_all()], valid | invalid | all) -> [rebar_app_info:t()].
  79. find_apps(LibDirs, Validate) ->
  80. rebar_utils:filtermap(fun(AppDir) ->
  81. find_app(AppDir, Validate)
  82. end, all_app_dirs(LibDirs)).
  83. -spec find_app(file:filename_all(), valid | invalid | all) -> {true, rebar_app_info:t()} | false.
  84. find_app(AppDir, Validate) ->
  85. AppFile = filelib:wildcard(filename:join([AppDir, "ebin", "*.app"])),
  86. AppSrcFile = filelib:wildcard(filename:join([AppDir, "src", "*.app.src"])),
  87. case AppFile of
  88. [File] ->
  89. AppInfo = create_app_info(AppDir, File),
  90. AppInfo1 = rebar_app_info:app_file(AppInfo, File),
  91. AppInfo2 = case AppSrcFile of
  92. [F] ->
  93. rebar_app_info:app_file_src(AppInfo1, F);
  94. [] ->
  95. AppInfo1;
  96. Other when is_list(Other) ->
  97. throw({error, {multiple_app_files, Other}})
  98. end,
  99. case Validate of
  100. valid ->
  101. case rebar_app_utils:validate_application_info(AppInfo2) of
  102. true ->
  103. {true, AppInfo2};
  104. _ ->
  105. false
  106. end;
  107. invalid ->
  108. case rebar_app_utils:validate_application_info(AppInfo2) of
  109. true ->
  110. false;
  111. _ ->
  112. {true, AppInfo2}
  113. end;
  114. all ->
  115. {true, AppInfo2}
  116. end;
  117. [] ->
  118. case AppSrcFile of
  119. [File] ->
  120. case Validate of
  121. V when V =:= invalid ; V =:= all ->
  122. AppInfo = create_app_info(AppDir, File),
  123. {true, rebar_app_info:app_file_src(AppInfo, File)};
  124. valid ->
  125. false
  126. end;
  127. [] ->
  128. false;
  129. Other when is_list(Other) ->
  130. throw({error, {multiple_app_files, Other}})
  131. end;
  132. Other when is_list(Other) ->
  133. throw({error, {multiple_app_files, Other}})
  134. end.
  135. app_dir(AppFile) ->
  136. filename:join(rebar_utils:droplast(filename:split(filename:dirname(AppFile)))).
  137. -spec create_app_info(file:name(), file:name()) -> rebar_app_info:t() | error.
  138. create_app_info(AppDir, AppFile) ->
  139. case file:consult(AppFile) of
  140. {ok, [{application, AppName, AppDetails}]} ->
  141. AppVsn = proplists:get_value(vsn, AppDetails),
  142. Applications = proplists:get_value(applications, AppDetails, []),
  143. IncludedApplications = proplists:get_value(included_applications, AppDetails, []),
  144. {ok, AppInfo} = rebar_app_info:new(AppName, AppVsn, AppDir, []),
  145. AppInfo1 = rebar_app_info:applications(
  146. rebar_app_info:app_details(AppInfo, AppDetails),
  147. IncludedApplications++Applications),
  148. rebar_app_info:dir(AppInfo1, AppDir);
  149. _ ->
  150. error
  151. end.
  152. dedup([]) -> [];
  153. dedup([A]) -> [A];
  154. dedup([H,H|T]) -> dedup([H|T]);
  155. dedup([H|T]) -> [H|dedup(T)].