選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

161 行
6.5 KiB

  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. StateAcc1 = merge_deps(AppInfo, StateAcc),
  19. Name = rebar_app_info:name(AppInfo),
  20. OutDir = filename:join(DepsDir, Name),
  21. AppInfo1 = rebar_app_info:out_dir(AppInfo, OutDir),
  22. ProjectDeps1 = lists:delete(Name, ProjectDeps),
  23. rebar_state:project_apps(StateAcc1
  24. ,rebar_app_info:deps(AppInfo1, 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. Profiles = rebar_state:current_profiles(State),
  32. Name = rebar_app_info:name(AppInfo),
  33. C = rebar_config:consult(rebar_app_info:dir(AppInfo)),
  34. AppState = rebar_state:apply_overrides(
  35. rebar_state:apply_profiles(
  36. rebar_state:new(State, C, rebar_app_info:dir(AppInfo)), Profiles), Name),
  37. lists:foldl(fun(Profile, StateAcc) ->
  38. AppProfDeps = rebar_state:get(AppState, {deps, Profile}, []),
  39. TopLevelProfDeps = rebar_state:get(StateAcc, {deps, Profile}, []),
  40. ProfDeps2 = lists:keymerge(1, TopLevelProfDeps, AppProfDeps),
  41. rebar_state:set(StateAcc, {deps, Profile}, ProfDeps2)
  42. end, State, lists:reverse(Profiles)).
  43. -spec all_app_dirs(list(file:name())) -> list(file:name()).
  44. all_app_dirs(LibDirs) ->
  45. lists:flatmap(fun(LibDir) ->
  46. app_dirs(LibDir)
  47. end, LibDirs).
  48. app_dirs(LibDir) ->
  49. Path1 = filename:join([LibDir,
  50. "*",
  51. "src",
  52. "*.app.src"]),
  53. Path2 = filename:join([LibDir,
  54. "src",
  55. "*.app.src"]),
  56. Path3 = filename:join([LibDir,
  57. "*",
  58. "ebin",
  59. "*.app"]),
  60. Path4 = filename:join([LibDir,
  61. "ebin",
  62. "*.app"]),
  63. lists:usort(lists:foldl(fun(Path, Acc) ->
  64. Files = filelib:wildcard(ec_cnv:to_list(Path)),
  65. [app_dir(File) || File <- Files] ++ Acc
  66. end, [], [Path1, Path2, Path3, Path4])).
  67. find_unbuilt_apps(LibDirs) ->
  68. find_apps(LibDirs, invalid).
  69. -spec find_apps([file:filename_all()]) -> [rebar_app_info:t()].
  70. find_apps(LibDirs) ->
  71. find_apps(LibDirs, valid).
  72. -spec find_apps([file:filename_all()], valid | invalid | all) -> [rebar_app_info:t()].
  73. find_apps(LibDirs, Validate) ->
  74. rebar_utils:filtermap(fun(AppDir) ->
  75. find_app(AppDir, Validate)
  76. end, all_app_dirs(LibDirs)).
  77. -spec find_app(file:filename_all(), valid | invalid | all) -> {true, rebar_app_info:t()} | false.
  78. find_app(AppDir, Validate) ->
  79. AppFile = filelib:wildcard(filename:join([AppDir, "ebin", "*.app"])),
  80. AppSrcFile = filelib:wildcard(filename:join([AppDir, "src", "*.app.src"])),
  81. case AppFile of
  82. [File] ->
  83. AppInfo = create_app_info(AppDir, File),
  84. AppInfo1 = rebar_app_info:app_file(AppInfo, File),
  85. AppInfo2 = case AppSrcFile of
  86. [F] ->
  87. rebar_app_info:app_file_src(AppInfo1, F);
  88. [] ->
  89. AppInfo1;
  90. Other when is_list(Other) ->
  91. throw({error, {multiple_app_files, Other}})
  92. end,
  93. case Validate of
  94. valid ->
  95. case rebar_app_utils:validate_application_info(AppInfo2) of
  96. true ->
  97. {true, AppInfo2};
  98. _ ->
  99. false
  100. end;
  101. invalid ->
  102. case rebar_app_utils:validate_application_info(AppInfo2) of
  103. true ->
  104. false;
  105. _ ->
  106. {true, AppInfo2}
  107. end;
  108. all ->
  109. {true, AppInfo2}
  110. end;
  111. [] ->
  112. case AppSrcFile of
  113. [File] ->
  114. case Validate of
  115. V when V =:= invalid ; V =:= all ->
  116. AppInfo = create_app_info(AppDir, File),
  117. {true, rebar_app_info:app_file_src(AppInfo, File)};
  118. valid ->
  119. false
  120. end;
  121. [] ->
  122. false;
  123. Other when is_list(Other) ->
  124. throw({error, {multiple_app_files, Other}})
  125. end;
  126. Other when is_list(Other) ->
  127. throw({error, {multiple_app_files, Other}})
  128. end.
  129. app_dir(AppFile) ->
  130. filename:join(rebar_utils:droplast(filename:split(filename:dirname(AppFile)))).
  131. -spec create_app_info(file:name(), file:name()) -> rebar_app_info:t() | error.
  132. create_app_info(AppDir, AppFile) ->
  133. case file:consult(AppFile) of
  134. {ok, [{application, AppName, AppDetails}]} ->
  135. AppVsn = proplists:get_value(vsn, AppDetails),
  136. Applications = proplists:get_value(applications, AppDetails, []),
  137. IncludedApplications = proplists:get_value(included_applications, AppDetails, []),
  138. {ok, AppInfo} = rebar_app_info:new(AppName, AppVsn, AppDir, []),
  139. AppInfo1 = rebar_app_info:applications(
  140. rebar_app_info:app_details(AppInfo, AppDetails),
  141. IncludedApplications++Applications),
  142. rebar_app_info:dir(AppInfo1, AppDir);
  143. _ ->
  144. error
  145. end.