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

457 lines
21 KiB

10 년 전
9 년 전
9 년 전
10 년 전
10 년 전
  1. %%% @doc utility functions to do the basic discovery of apps
  2. %%% and layout for the project.
  3. -module(rebar_app_discover).
  4. -export([do/2,
  5. format_error/1,
  6. find_unbuilt_apps/1,
  7. find_apps/1,
  8. find_apps/2,
  9. find_apps/3,
  10. find_app/2,
  11. find_app/3]).
  12. -include("rebar.hrl").
  13. -include_lib("providers/include/providers.hrl").
  14. %% @doc from the base directory, find all the applications
  15. %% at the top level and their dependencies based on the configuration
  16. %% and profile information.
  17. -spec do(rebar_state:t(), [file:filename()]) -> rebar_state:t().
  18. do(State, LibDirs) ->
  19. BaseDir = rebar_state:dir(State),
  20. Dirs = [filename:join(BaseDir, LibDir) || LibDir <- LibDirs],
  21. RebarOpts = rebar_state:opts(State),
  22. SrcDirs = rebar_dir:src_dirs(RebarOpts, ["src"]),
  23. Apps = find_apps(Dirs, SrcDirs, all),
  24. ProjectDeps = rebar_state:deps_names(State),
  25. DepsDir = rebar_dir:deps_dir(State),
  26. CurrentProfiles = rebar_state:current_profiles(State),
  27. %% There may be a top level src which is an app and there may not
  28. %% Find it here if there is, otherwise define the deps parent as root
  29. TopLevelApp = define_root_app(Apps, State),
  30. %% Handle top level deps
  31. State1 = lists:foldl(fun(Profile, StateAcc) ->
  32. ProfileDeps = rebar_state:get(StateAcc, {deps, Profile}, []),
  33. ProfileDeps2 = rebar_utils:tup_dedup(ProfileDeps),
  34. StateAcc1 = rebar_state:set(StateAcc, {deps, Profile}, ProfileDeps2),
  35. ParsedDeps = parse_profile_deps(Profile
  36. ,TopLevelApp
  37. ,ProfileDeps2
  38. ,rebar_state:opts(StateAcc1)
  39. ,StateAcc1),
  40. rebar_state:set(StateAcc1, {parsed_deps, Profile}, ParsedDeps)
  41. end, State, lists:reverse(CurrentProfiles)),
  42. %% Handle sub project apps deps
  43. %% Sort apps so we get the same merged deps config everytime
  44. SortedApps = rebar_utils:sort_deps(Apps),
  45. lists:foldl(fun(AppInfo, StateAcc) ->
  46. Name = rebar_app_info:name(AppInfo),
  47. case enable(State, AppInfo) of
  48. true ->
  49. {AppInfo1, StateAcc1} = merge_deps(AppInfo, StateAcc),
  50. OutDir = filename:join(DepsDir, Name),
  51. AppInfo2 = rebar_app_info:out_dir(AppInfo1, OutDir),
  52. ProjectDeps1 = lists:delete(Name, ProjectDeps),
  53. rebar_state:project_apps(StateAcc1
  54. ,rebar_app_info:deps(AppInfo2, ProjectDeps1));
  55. false ->
  56. ?INFO("Ignoring ~ts", [Name]),
  57. StateAcc
  58. end
  59. end, State1, SortedApps).
  60. %% @doc checks whether there is an app at the top level (and returns its
  61. %% name) or the 'root' atom in case we're in an umbrella project.
  62. -spec define_root_app([rebar_app_info:t()], rebar_state:t()) ->
  63. root | binary().
  64. define_root_app(Apps, State) ->
  65. RootDir = rebar_dir:root_dir(State),
  66. case ec_lists:find(fun(X) ->
  67. ec_file:real_dir_path(rebar_app_info:dir(X)) =:=
  68. ec_file:real_dir_path(RootDir)
  69. end, Apps) of
  70. {ok, App} ->
  71. rebar_app_info:name(App);
  72. error ->
  73. root
  74. end.
  75. %% @doc formatting errors from the module.
  76. -spec format_error(term()) -> iodata().
  77. format_error({module_list, File}) ->
  78. io_lib:format("Error reading module list from ~p~n", [File]);
  79. format_error({missing_module, Module}) ->
  80. io_lib:format("Module defined in app file missing: ~p~n", [Module]).
  81. %% @doc handles the merging and application of profiles and overrides
  82. %% for a given application, within its own context.
  83. -spec merge_deps(rebar_app_info:t(), rebar_state:t()) ->
  84. {rebar_app_info:t(), rebar_state:t()}.
  85. merge_deps(AppInfo, State) ->
  86. %% These steps make sure that hooks and artifacts are run in the context of
  87. %% the application they are defined at. If an umbrella structure is used and
  88. %% they are defined at the top level they will instead run in the context of
  89. %% the State and at the top level, not as part of an application.
  90. CurrentProfiles = rebar_state:current_profiles(State),
  91. Default = reset_hooks(rebar_state:default(State), CurrentProfiles),
  92. {C, State1} = project_app_config(AppInfo, State),
  93. AppInfo0 = rebar_app_info:update_opts(AppInfo, Default, C),
  94. Name = rebar_app_info:name(AppInfo0),
  95. %% We reset the opts here to default so no profiles are applied multiple times
  96. AppInfo1 = rebar_app_info:apply_overrides(rebar_state:get(State1, overrides, []), AppInfo0),
  97. AppInfo2 = rebar_app_info:apply_profiles(AppInfo1, CurrentProfiles),
  98. %% Will throw an exception if checks fail
  99. rebar_app_info:verify_otp_vsn(AppInfo2),
  100. State2 = lists:foldl(fun(Profile, StateAcc) ->
  101. handle_profile(Profile, Name, AppInfo2, StateAcc)
  102. end, State1, lists:reverse(CurrentProfiles)),
  103. {AppInfo2, State2}.
  104. %% @doc Applies a given profile for an app, ensuring the deps
  105. %% match the context it will require.
  106. -spec handle_profile(atom(), binary(), rebar_app_info:t(), rebar_state:t()) ->
  107. rebar_state:t().
  108. handle_profile(Profile, Name, AppInfo, State) ->
  109. TopParsedDeps = rebar_state:get(State, {parsed_deps, Profile}, {[], []}),
  110. TopLevelProfileDeps = rebar_state:get(State, {deps, Profile}, []),
  111. AppProfileDeps = rebar_app_info:get(AppInfo, {deps, Profile}, []),
  112. AppProfileDeps2 = rebar_utils:tup_dedup(AppProfileDeps),
  113. ProfileDeps2 = rebar_utils:tup_dedup(rebar_utils:tup_umerge(TopLevelProfileDeps
  114. ,AppProfileDeps2)),
  115. State1 = rebar_state:set(State, {deps, Profile}, ProfileDeps2),
  116. %% Only deps not also specified in the top level config need
  117. %% to be included in the parsed deps
  118. NewDeps = ProfileDeps2 -- TopLevelProfileDeps,
  119. ParsedDeps = parse_profile_deps(Profile, Name, NewDeps, rebar_app_info:opts(AppInfo), State1),
  120. State2 = rebar_state:set(State1, {deps, Profile}, ProfileDeps2),
  121. rebar_state:set(State2, {parsed_deps, Profile}, TopParsedDeps++ParsedDeps).
  122. %% @doc parses all the known dependencies for a given profile
  123. -spec parse_profile_deps(Profile, Name, Deps, Opts, rebar_state:t()) -> [rebar_app_info:t()] when
  124. Profile :: atom(),
  125. Name :: binary(),
  126. Deps :: [term()], % TODO: refine types
  127. Opts :: term(). % TODO: refine types
  128. parse_profile_deps(Profile, Name, Deps, Opts, State) ->
  129. DepsDir = rebar_prv_install_deps:profile_dep_dir(State, Profile),
  130. Locks = rebar_state:get(State, {locks, Profile}, []),
  131. rebar_app_utils:parse_deps(Name
  132. ,DepsDir
  133. ,Deps
  134. ,rebar_state:opts(State, Opts)
  135. ,Locks
  136. ,1).
  137. %% @doc Find the app-level config and return the state updated
  138. %% with the relevant app-level data.
  139. -spec project_app_config(rebar_app_info:t(), rebar_state:t()) ->
  140. {Config, rebar_state:t()} when
  141. Config :: [any()].
  142. project_app_config(AppInfo, State) ->
  143. C = rebar_config:consult(rebar_app_info:dir(AppInfo)),
  144. Dir = rebar_app_info:dir(AppInfo),
  145. Opts = maybe_reset_hooks(Dir, rebar_state:opts(State), State),
  146. {C, rebar_state:opts(State, Opts)}.
  147. %% @private Check if the app is at the root of the project.
  148. %% If it is, then drop the hooks from the config so they aren't run twice
  149. -spec maybe_reset_hooks(file:filename(), Opts, rebar_state:t()) -> Opts when
  150. Opts :: rebar_dict().
  151. maybe_reset_hooks(Dir, Opts, State) ->
  152. case ec_file:real_dir_path(rebar_dir:root_dir(State)) of
  153. Dir ->
  154. CurrentProfiles = rebar_state:current_profiles(State),
  155. reset_hooks(Opts, CurrentProfiles);
  156. _ ->
  157. Opts
  158. end.
  159. %% @doc make the hooks empty for a given set of options
  160. -spec reset_hooks(Opts, Profiles) ->
  161. Opts when
  162. Opts :: rebar_dict(),
  163. Profiles :: [atom()].
  164. reset_hooks(Opts, CurrentProfiles) ->
  165. AllHooks = [post_hooks, pre_hooks, provider_hooks, artifacts],
  166. Opts1 = lists:foldl(fun(Key, OptsAcc) ->
  167. rebar_opts:set(OptsAcc, Key, [])
  168. end, Opts, AllHooks),
  169. Profiles = rebar_opts:get(Opts1, profiles, []),
  170. Profiles1 = lists:map(fun({P, ProfileOpts}) ->
  171. case lists:member(P, CurrentProfiles) of
  172. true ->
  173. {P, [X || X={Key, _} <- ProfileOpts,
  174. not lists:member(Key, AllHooks)]};
  175. false ->
  176. {P, ProfileOpts}
  177. end
  178. end, Profiles),
  179. rebar_opts:set(Opts1, profiles, Profiles1).
  180. %% @private find the directories for all apps, while detecting their source dirs
  181. %% Returns the app dir with the respective src_dirs for them, in that order,
  182. %% for every app found.
  183. -spec all_app_dirs([file:name()]) -> [{file:name(), [file:name()]}].
  184. all_app_dirs(LibDirs) ->
  185. lists:flatmap(fun(LibDir) ->
  186. {_, SrcDirs} = find_config_src(LibDir, ["src"]),
  187. app_dirs(LibDir, SrcDirs)
  188. end, LibDirs).
  189. %% @private find the directories for all apps based on their source dirs
  190. %% Returns the app dir with the respective src_dirs for them, in that order,
  191. %% for every app found.
  192. -spec all_app_dirs([file:name()], [file:name()]) -> [{file:name(), [file:name()]}].
  193. all_app_dirs(LibDirs, SrcDirs) ->
  194. lists:flatmap(fun(LibDir) -> app_dirs(LibDir, SrcDirs) end, LibDirs).
  195. %% @private find the directories based on the library directories.
  196. %% Returns the app dir with the respective src_dirs for them, in that order,
  197. %% for every app found.
  198. %%
  199. %% The function returns the src directories since they might have been
  200. %% detected in a top-level loop and we want to skip further detection
  201. %% starting now.
  202. -spec app_dirs([file:name()], [file:name()]) -> [{file:name(), [file:name()]}].
  203. app_dirs(LibDir, SrcDirs) ->
  204. Paths = lists:append([
  205. [filename:join([LibDir, SrcDir, "*.app.src"]),
  206. filename:join([LibDir, SrcDir, "*.app.src.script"])]
  207. || SrcDir <- SrcDirs
  208. ]),
  209. EbinPath = filename:join([LibDir, "ebin", "*.app"]),
  210. lists:usort(lists:foldl(fun(Path, Acc) ->
  211. Files = filelib:wildcard(rebar_utils:to_list(Path)),
  212. [{app_dir(File), SrcDirs}
  213. || File <- Files] ++ Acc
  214. end, [], [EbinPath | Paths])).
  215. %% @doc find all apps that haven't been built in a list of directories
  216. -spec find_unbuilt_apps([file:filename_all()]) -> [rebar_app_info:t()].
  217. find_unbuilt_apps(LibDirs) ->
  218. find_apps(LibDirs, invalid).
  219. %% @doc for each directory passed, find all apps that are valid.
  220. %% Returns all the related app info records.
  221. -spec find_apps([file:filename_all()]) -> [rebar_app_info:t()].
  222. find_apps(LibDirs) ->
  223. find_apps(LibDirs, valid).
  224. %% @doc for each directory passed, find all apps according
  225. %% to the validity rule passed in. Returns all the related
  226. %% app info records.
  227. -spec find_apps([file:filename_all()], valid | invalid | all) -> [rebar_app_info:t()].
  228. find_apps(LibDirs, Validate) ->
  229. rebar_utils:filtermap(
  230. fun({AppDir, AppSrcDirs}) ->
  231. find_app(rebar_app_info:new(), AppDir, AppSrcDirs, Validate)
  232. end,
  233. all_app_dirs(LibDirs)
  234. ).
  235. %% @doc for each directory passed, with the configured source directories,
  236. %% find all apps according to the validity rule passed in.
  237. %% Returns all the related app info records.
  238. -spec find_apps([file:filename_all()], [file:filename_all()], valid | invalid | all) -> [rebar_app_info:t()].
  239. find_apps(LibDirs, SrcDirs, Validate) ->
  240. rebar_utils:filtermap(
  241. fun({AppDir, AppSrcDirs}) ->
  242. find_app(rebar_app_info:new(), AppDir, AppSrcDirs, Validate)
  243. end,
  244. all_app_dirs(LibDirs, SrcDirs)
  245. ).
  246. %% @doc check that a given app in a directory is there, and whether it's
  247. %% valid or not based on the second argument. Returns the related
  248. %% app info record.
  249. -spec find_app(file:filename_all(), valid | invalid | all) -> {true, rebar_app_info:t()} | false.
  250. find_app(AppDir, Validate) ->
  251. {Config, SrcDirs} = find_config_src(AppDir, ["src"]),
  252. AppInfo = rebar_app_info:update_opts(rebar_app_info:new(), dict:new(), Config),
  253. find_app_(AppInfo, AppDir, SrcDirs, Validate).
  254. %% @doc check that a given app in a directory is there, and whether it's
  255. %% valid or not based on the second argument. Returns the related
  256. %% app info record.
  257. -spec find_app(rebar_app_info:t(), file:filename_all(), valid | invalid | all) ->
  258. {true, rebar_app_info:t()} | false.
  259. find_app(AppInfo, AppDir, Validate) ->
  260. %% if no src dir is passed, figure it out from the app info, with a default
  261. %% of src/
  262. AppOpts = rebar_app_info:opts(AppInfo),
  263. SrcDirs = rebar_dir:src_dirs(AppOpts, ["src"]),
  264. find_app_(AppInfo, AppDir, SrcDirs, Validate).
  265. %% @doc check that a given app in a directory is there, and whether it's
  266. %% valid or not based on the second argument. The third argument includes
  267. %% the directories where source files can be located. Returns the related
  268. %% app info record.
  269. -spec find_app(rebar_app_info:t(), file:filename_all(),
  270. [file:filename_all()], valid | invalid | all) ->
  271. {true, rebar_app_info:t()} | false.
  272. find_app(AppInfo, AppDir, SrcDirs, Validate) ->
  273. Config = rebar_config:consult(AppDir),
  274. AppInfo1 = rebar_app_info:update_opts(AppInfo, rebar_app_info:opts(AppInfo), Config),
  275. find_app_(AppInfo1, AppDir, SrcDirs, Validate).
  276. -spec find_app_(rebar_app_info:t(), file:filename_all(),
  277. [file:filename_all()], valid | invalid | all) ->
  278. {true, rebar_app_info:t()} | false.
  279. find_app_(AppInfo, AppDir, SrcDirs, Validate) ->
  280. AppFile = filelib:wildcard(filename:join([AppDir, "ebin", "*.app"])),
  281. AppSrcFile = lists:append(
  282. [filelib:wildcard(filename:join([AppDir, SrcDir, "*.app.src"]))
  283. || SrcDir <- SrcDirs]
  284. ),
  285. AppSrcScriptFile = lists:append(
  286. [filelib:wildcard(filename:join([AppDir, SrcDir, "*.app.src.script"]))
  287. || SrcDir <- SrcDirs]
  288. ),
  289. try_handle_app_file(AppInfo, AppFile, AppDir, AppSrcFile, AppSrcScriptFile, Validate).
  290. %% @doc find the directory that an appfile has
  291. -spec app_dir(file:filename()) -> file:filename().
  292. app_dir(AppFile) ->
  293. filename:join(rebar_utils:droplast(filename:split(filename:dirname(AppFile)))).
  294. %% @doc populates an app info record based on an app directory and its
  295. %% app file.
  296. -spec create_app_info(rebar_app_info:t(), file:name(), file:name()) -> rebar_app_info:t().
  297. create_app_info(AppInfo, AppDir, AppFile) ->
  298. [{application, AppName, AppDetails}] = rebar_config:consult_app_file(AppFile),
  299. AppVsn = proplists:get_value(vsn, AppDetails),
  300. Applications = proplists:get_value(applications, AppDetails, []),
  301. IncludedApplications = proplists:get_value(included_applications, AppDetails, []),
  302. AppInfo1 = rebar_app_info:name(
  303. rebar_app_info:original_vsn(
  304. rebar_app_info:dir(AppInfo, AppDir), AppVsn), AppName),
  305. AppInfo2 = rebar_app_info:applications(
  306. rebar_app_info:app_details(AppInfo1, AppDetails),
  307. IncludedApplications++Applications),
  308. Valid = case rebar_app_utils:validate_application_info(AppInfo2) =:= true
  309. andalso rebar_app_info:has_all_artifacts(AppInfo2) =:= true of
  310. true ->
  311. true;
  312. _ ->
  313. false
  314. end,
  315. rebar_app_info:dir(rebar_app_info:valid(AppInfo2, Valid), AppDir).
  316. %% @doc Read in and parse the .app file if it is availabe. Do the same for
  317. %% the .app.src file if it exists.
  318. -spec try_handle_app_file(AppInfo, AppFile, AppDir, AppSrcFile, AppSrcScriptFile, valid | invalid | all) ->
  319. {true, AppInfo} | false when
  320. AppInfo :: rebar_app_info:t(),
  321. AppFile :: file:filename(),
  322. AppDir :: file:filename(),
  323. AppSrcFile :: file:filename(),
  324. AppSrcScriptFile :: file:filename().
  325. try_handle_app_file(AppInfo, [], AppDir, [], AppSrcScriptFile, Validate) ->
  326. try_handle_app_src_file(AppInfo, [], AppDir, AppSrcScriptFile, Validate);
  327. try_handle_app_file(AppInfo, [], AppDir, AppSrcFile, _, Validate) ->
  328. try_handle_app_src_file(AppInfo, [], AppDir, AppSrcFile, Validate);
  329. try_handle_app_file(AppInfo0, [File], AppDir, AppSrcFile, _, Validate) ->
  330. try create_app_info(AppInfo0, AppDir, File) of
  331. AppInfo ->
  332. AppInfo1 = rebar_app_info:app_file(AppInfo, File),
  333. AppInfo2 = case AppSrcFile of
  334. [F] ->
  335. rebar_app_info:app_file_src(AppInfo1, F);
  336. [] ->
  337. %% Set to undefined in case AppInfo previous had a .app.src
  338. rebar_app_info:app_file_src(AppInfo1, undefined);
  339. Other when is_list(Other) ->
  340. throw({error, {multiple_app_files, Other}})
  341. end,
  342. case Validate of
  343. valid ->
  344. case rebar_app_utils:validate_application_info(AppInfo2) of
  345. true ->
  346. {true, AppInfo2};
  347. _ ->
  348. false
  349. end;
  350. invalid ->
  351. case rebar_app_utils:validate_application_info(AppInfo2) of
  352. true ->
  353. false;
  354. _ ->
  355. {true, AppInfo2}
  356. end;
  357. all ->
  358. {true, AppInfo2}
  359. end
  360. catch
  361. throw:{error, {Module, Reason}} ->
  362. ?DEBUG("Falling back to app.src file because .app failed: ~ts", [Module:format_error(Reason)]),
  363. try_handle_app_src_file(AppInfo0, File, AppDir, AppSrcFile, Validate)
  364. end;
  365. try_handle_app_file(_AppInfo, Other, _AppDir, _AppSrcFile, _, _Validate) ->
  366. throw({error, {multiple_app_files, Other}}).
  367. %% @doc Read in the .app.src file if we aren't looking for a valid (already
  368. %% built) app.
  369. -spec try_handle_app_src_file(AppInfo, AppFile, AppDir, AppSrcFile, valid | invalid | all) ->
  370. {true, AppInfo} | false when
  371. AppInfo :: rebar_app_info:t(),
  372. AppFile :: file:filename(),
  373. AppDir :: file:filename(),
  374. AppSrcFile :: file:filename().
  375. try_handle_app_src_file(AppInfo, _, _AppDir, [], _Validate) ->
  376. %% if .app and .app.src are not found check for a mix config file
  377. %% it is assumed a plugin will build the application, including
  378. %% a .app after this step
  379. case filelib:is_file(filename:join(rebar_app_info:dir(AppInfo), "mix.exs")) of
  380. true ->
  381. {true, AppInfo};
  382. false ->
  383. false
  384. end;
  385. try_handle_app_src_file(_AppInfo, _, _AppDir, _AppSrcFile, valid) ->
  386. false;
  387. try_handle_app_src_file(AppInfo, _, AppDir, [File], Validate) when Validate =:= invalid
  388. ; Validate =:= all ->
  389. AppInfo1 = rebar_app_info:app_file(AppInfo, undefined),
  390. AppInfo2 = create_app_info(AppInfo1, AppDir, File),
  391. case filename:extension(File) of
  392. ".script" ->
  393. {true, rebar_app_info:app_file_src_script(AppInfo2, File)};
  394. _ ->
  395. {true, rebar_app_info:app_file_src(AppInfo2, File)}
  396. end;
  397. try_handle_app_src_file(_AppInfo, _, _AppDir, Other, _Validate) ->
  398. throw({error, {multiple_app_files, Other}}).
  399. %% @doc checks whether the given app is not blacklisted in the config.
  400. -spec enable(rebar_state:t(), rebar_app_info:t()) -> boolean().
  401. enable(State, AppInfo) ->
  402. not lists:member(to_atom(rebar_app_info:name(AppInfo)),
  403. rebar_state:get(State, excluded_apps, [])).
  404. %% @private convert a binary to an atom.
  405. -spec to_atom(binary()) -> atom().
  406. to_atom(Bin) ->
  407. list_to_atom(binary_to_list(Bin)).
  408. %% @private when looking for unknown apps, it's possible they have a
  409. %% rebar.config file specifying non-standard src_dirs. Check for a
  410. %% possible config file and extract src_dirs from it.
  411. find_config_src(AppDir, Default) ->
  412. case rebar_config:consult(AppDir) of
  413. [] ->
  414. {[], Default};
  415. Terms ->
  416. %% TODO: handle profiles I guess, but we don't have that info
  417. {Terms, proplists:get_value(src_dirs, Terms, Default)}
  418. end.