您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

278 行
8.4 KiB

  1. -module(rebar_app_info).
  2. -export([new/1,
  3. new/2,
  4. new/3,
  5. new/4,
  6. discover/1,
  7. name/1,
  8. name/2,
  9. config/1,
  10. config/2,
  11. app_file_src/1,
  12. app_file_src/2,
  13. app_file/1,
  14. app_file/2,
  15. app_details/1,
  16. app_details/2,
  17. original_vsn/1,
  18. original_vsn/2,
  19. ebin_dir/1,
  20. applications/1,
  21. applications/2,
  22. profiles/1,
  23. profiles/2,
  24. deps/1,
  25. deps/2,
  26. dep_level/1,
  27. dep_level/2,
  28. dir/1,
  29. dir/2,
  30. out_dir/1,
  31. out_dir/2,
  32. source/1,
  33. source/2,
  34. state/1,
  35. state/2,
  36. is_checkout/1,
  37. is_checkout/2,
  38. valid/1,
  39. valid/2]).
  40. -export_type([t/0]).
  41. -record(app_info_t, {name :: binary(),
  42. app_file_src :: file:filename_all() | undefined,
  43. app_file :: file:filename_all() | undefined,
  44. config :: rebar_state:t() | undefined,
  45. original_vsn :: binary() | string() | undefined,
  46. app_details=[] :: list(),
  47. applications=[] :: list(),
  48. deps=[] :: list(),
  49. profiles=[default] :: [atom()],
  50. dep_level=0 :: integer(),
  51. dir :: file:name(),
  52. out_dir :: file:name(),
  53. source :: string() | tuple() | undefined,
  54. state :: rebar_state:t() | undefined,
  55. is_checkout=false :: boolean(),
  56. valid :: boolean()}).
  57. %%============================================================================
  58. %% types
  59. %%============================================================================
  60. -type t() :: #app_info_t{}.
  61. %%============================================================================
  62. %% API
  63. %% ============================================================================
  64. %% @doc Build a new, empty, app info value. This is not of a lot of use and you
  65. %% probably wont be doing this much.
  66. -spec new(atom() | binary() | string()) ->
  67. {ok, t()}.
  68. new(AppName) ->
  69. {ok, #app_info_t{name=ec_cnv:to_binary(AppName)}}.
  70. -spec new(atom() | binary() | string(), binary() | string()) ->
  71. {ok, t()}.
  72. new(AppName, Vsn) ->
  73. {ok, #app_info_t{name=ec_cnv:to_binary(AppName),
  74. original_vsn=Vsn}}.
  75. %% @doc build a complete version of the app info with all fields set.
  76. -spec new(atom() | binary() | string(), binary() | string(), file:name()) ->
  77. {ok, t()}.
  78. new(AppName, Vsn, Dir) ->
  79. {ok, #app_info_t{name=ec_cnv:to_binary(AppName),
  80. original_vsn=Vsn,
  81. dir=ec_cnv:to_list(Dir),
  82. out_dir=ec_cnv:to_list(Dir)}}.
  83. %% @doc build a complete version of the app info with all fields set.
  84. -spec new(atom() | binary() | string(), binary() | string(), file:name(), list()) ->
  85. {ok, t()}.
  86. new(AppName, Vsn, Dir, Deps) ->
  87. {ok, #app_info_t{name=ec_cnv:to_binary(AppName),
  88. original_vsn=Vsn,
  89. dir=ec_cnv:to_list(Dir),
  90. out_dir=ec_cnv:to_list(Dir),
  91. deps=Deps}}.
  92. %% @doc discover a complete version of the app info with all fields set.
  93. -spec discover(file:filename_all()) -> {ok, t()} | not_found.
  94. discover(Dir) ->
  95. case rebar_app_discover:find_app(Dir, all) of
  96. {true, AppInfo} ->
  97. {ok, AppInfo};
  98. false ->
  99. not_found
  100. end.
  101. -spec name(t()) -> binary().
  102. name(#app_info_t{name=Name}) ->
  103. Name.
  104. -spec name(t(), atom() | binary() | string()) -> t().
  105. name(AppInfo=#app_info_t{}, AppName) ->
  106. AppInfo#app_info_t{name=ec_cnv:to_binary(AppName)}.
  107. -spec config(t()) -> rebar_state:t().
  108. config(#app_info_t{config=Config}) ->
  109. Config.
  110. -spec config(t(), rebar_state:t()) -> t().
  111. config(AppInfo=#app_info_t{}, Config) ->
  112. AppInfo#app_info_t{config=Config}.
  113. -spec app_file_src(t()) -> file:filename_all() | undefined.
  114. app_file_src(#app_info_t{app_file_src=undefined, dir=Dir, name=Name}) ->
  115. AppFileSrc = filename:join([ec_cnv:to_list(Dir), "src", ec_cnv:to_list(Name)++".app.src"]),
  116. case filelib:is_file(AppFileSrc) of
  117. true ->
  118. AppFileSrc;
  119. false ->
  120. undefined
  121. end;
  122. app_file_src(#app_info_t{app_file_src=AppFileSrc}) ->
  123. ec_cnv:to_list(AppFileSrc).
  124. -spec app_file_src(t(), file:filename_all()) -> t().
  125. app_file_src(AppInfo=#app_info_t{}, AppFileSrc) ->
  126. AppInfo#app_info_t{app_file_src=ec_cnv:to_list(AppFileSrc)}.
  127. -spec app_file(t()) -> file:filename_all() | undefined.
  128. app_file(#app_info_t{app_file=undefined, out_dir=Dir, name=Name}) ->
  129. AppFile = filename:join([ec_cnv:to_list(Dir), "ebin", ec_cnv:to_list(Name)++".app"]),
  130. case filelib:is_file(AppFile) of
  131. true ->
  132. AppFile;
  133. false ->
  134. undefined
  135. end;
  136. app_file(#app_info_t{app_file=AppFile}) ->
  137. AppFile.
  138. -spec app_file(t(), file:filename_all()) -> t().
  139. app_file(AppInfo=#app_info_t{}, AppFile) ->
  140. AppInfo#app_info_t{app_file=AppFile}.
  141. -spec app_details(t()) -> list().
  142. app_details(AppInfo=#app_info_t{app_details=[]}) ->
  143. AppFile = case app_file(AppInfo) of
  144. undefined ->
  145. app_file_src(AppInfo);
  146. File ->
  147. File
  148. end,
  149. case file:consult(AppFile) of
  150. {ok, [{application, _, AppDetails}]} ->
  151. AppDetails;
  152. _ ->
  153. []
  154. end;
  155. app_details(#app_info_t{app_details=AppDetails}) ->
  156. AppDetails.
  157. -spec app_details(t(), list()) -> t().
  158. app_details(AppInfo=#app_info_t{}, AppDetails) ->
  159. AppInfo#app_info_t{app_details=AppDetails}.
  160. -spec original_vsn(t()) -> string().
  161. original_vsn(#app_info_t{original_vsn=Vsn}) ->
  162. Vsn.
  163. -spec original_vsn(t(), string()) -> t().
  164. original_vsn(AppInfo=#app_info_t{}, Vsn) ->
  165. AppInfo#app_info_t{original_vsn=Vsn}.
  166. -spec applications(t()) -> list().
  167. applications(#app_info_t{applications=Applications}) ->
  168. Applications.
  169. -spec applications(t(), list()) -> t().
  170. applications(AppInfo=#app_info_t{}, Applications) ->
  171. AppInfo#app_info_t{applications=Applications}.
  172. -spec profiles(t()) -> list().
  173. profiles(#app_info_t{profiles=Profiles}) ->
  174. Profiles.
  175. -spec profiles(t(), list()) -> t().
  176. profiles(AppInfo=#app_info_t{}, Profiles) ->
  177. AppInfo#app_info_t{profiles=Profiles}.
  178. -spec deps(t()) -> list().
  179. deps(#app_info_t{deps=Deps}) ->
  180. Deps.
  181. -spec deps(t(), list()) -> t().
  182. deps(AppInfo=#app_info_t{}, Deps) ->
  183. AppInfo#app_info_t{deps=Deps}.
  184. dep_level(AppInfo=#app_info_t{}, Level) ->
  185. AppInfo#app_info_t{dep_level=Level}.
  186. dep_level(#app_info_t{dep_level=Level}) ->
  187. Level.
  188. -spec dir(t()) -> file:name().
  189. dir(#app_info_t{dir=Dir}) ->
  190. Dir.
  191. -spec dir(t(), file:name()) -> t().
  192. dir(AppInfo=#app_info_t{out_dir=undefined}, Dir) ->
  193. AppInfo#app_info_t{dir=ec_cnv:to_list(Dir),
  194. out_dir=ec_cnv:to_list(Dir)};
  195. dir(AppInfo=#app_info_t{}, Dir) ->
  196. AppInfo#app_info_t{dir=ec_cnv:to_list(Dir)}.
  197. -spec out_dir(t()) -> file:name().
  198. out_dir(#app_info_t{out_dir=OutDir}) ->
  199. OutDir.
  200. -spec out_dir(t(), file:name()) -> t().
  201. out_dir(AppInfo=#app_info_t{}, OutDir) ->
  202. AppInfo#app_info_t{out_dir=ec_cnv:to_list(OutDir)}.
  203. -spec ebin_dir(t()) -> file:name().
  204. ebin_dir(#app_info_t{out_dir=OutDir}) ->
  205. ec_cnv:to_list(filename:join(OutDir, "ebin")).
  206. -spec source(t(), string() | tuple()) -> t().
  207. source(AppInfo=#app_info_t{}, Source) ->
  208. AppInfo#app_info_t{source=Source}.
  209. -spec source(t()) -> string() | tuple().
  210. source(#app_info_t{source=Source}) ->
  211. Source.
  212. -spec state(t(), rebar_state:t() | undefined) -> t().
  213. state(AppInfo=#app_info_t{}, State) ->
  214. AppInfo#app_info_t{state=State}.
  215. -spec state(t()) -> rebar_state:t() | undefined.
  216. state(#app_info_t{state=State}) ->
  217. State.
  218. -spec is_checkout(t(), boolean()) -> t().
  219. is_checkout(AppInfo=#app_info_t{}, IsCheckout) ->
  220. AppInfo#app_info_t{is_checkout=IsCheckout}.
  221. -spec is_checkout(t()) -> boolean().
  222. is_checkout(#app_info_t{is_checkout=IsCheckout}) ->
  223. IsCheckout.
  224. -spec valid(t()) -> boolean().
  225. valid(AppInfo=#app_info_t{valid=undefined}) ->
  226. case rebar_app_utils:validate_application_info(AppInfo) of
  227. true ->
  228. true;
  229. _ ->
  230. false
  231. end;
  232. valid(#app_info_t{valid=Valid}) ->
  233. Valid.
  234. -spec valid(t(), boolean()) -> t().
  235. valid(AppInfo=#app_info_t{}, Valid) ->
  236. AppInfo#app_info_t{valid=Valid}.