inject-deps.escript 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env escript
  2. %% This script injects implicit relup dependencies for emqx applications.
  3. %%
  4. %% By 'implicit', it means that it is not feasible to define application
  5. %% dependencies in .app.src files.
  6. %%
  7. %% For instance, during upgrade/downgrade, emqx_dashboard usually requires
  8. %% a restart after (but not before) all plugins are upgraded (and maybe
  9. %% restarted), however, the dependencies are not resolvable at build time
  10. %% when relup is generated.
  11. %%
  12. %% This script is to be executed after compile, with the profile given as the
  13. %% first argument. For each dependency, it modifies the .app file to
  14. %% have the `relup_deps` list extended to application attributes.
  15. %%
  16. %% The `relup_deps` application attribute is then picked up by (EMQ's fork of)
  17. %% `relx` when top-sorting apps to generate relup instructions
  18. -mode(compile).
  19. usage() ->
  20. "Usage: " ++ escript:script_name() ++ " emqx|emqx-edge".
  21. -type app() :: atom().
  22. -type deps_overlay() :: {re, string()} | app().
  23. %% deps/0 returns the dependency overlays.
  24. %% {re, Pattern} to match application names using regexp pattern
  25. -spec deps(string()) -> [{app(), [deps_overlay()]}].
  26. deps("emqx-edge" ++ _) ->
  27. %% special case for edge
  28. base_deps() ++ [{{re, ".+"}, [{exclude, App} || App <- edge_excludes()]}];
  29. deps(_Profile) ->
  30. base_deps().
  31. edge_excludes() ->
  32. [ emqx_lwm2m
  33. , emqx_auth_ldap
  34. , emqx_auth_pgsql
  35. , emqx_auth_redis
  36. , emqx_auth_mongo
  37. , emqx_lua_hook
  38. , emqx_exhook
  39. , emqx_exproto
  40. , emqx_prometheus
  41. , emqx_psk_file
  42. ].
  43. base_deps() ->
  44. %% make sure emqx_dashboard depends on all other emqx_xxx apps
  45. %% so the appup instructions for emqx_dashboard is always the last
  46. %% to be executed
  47. [ {emqx_dashboard, [{re, "emqx_.*"}]}
  48. , {emqx_management, [{re, "emqx_.*"}, {exclude, emqx_dashboard}, minirest]}
  49. , {{re, "emqx_.*"}, [emqx]}
  50. , {emqx_web_hook, [ehttpc]}
  51. ].
  52. main([Profile | _]) ->
  53. ok = inject(Profile);
  54. main(_Args) ->
  55. io:format(standard_error, "~s", [usage()]),
  56. erlang:halt(1).
  57. expand_names({Name, Deps}, AppNames) ->
  58. Names = match_pattern(Name, AppNames),
  59. [{N, Deps} || N <- Names].
  60. %% merge k-v pairs with v1 ++ v2
  61. merge([], Acc) -> Acc;
  62. merge([{K, V0} | Rest], Acc) ->
  63. V = case lists:keyfind(K, 1, Acc) of
  64. {K, V1} -> V1 ++ V0;
  65. false -> V0
  66. end,
  67. NewAcc = lists:keystore(K, 1, Acc, {K, V}),
  68. merge(Rest, NewAcc).
  69. expand_deps([], _AppNames, Acc) -> Acc;
  70. expand_deps([{exclude, Dep} | Deps], AppNames, Acc) ->
  71. Matches = expand_deps([Dep], AppNames, []),
  72. expand_deps(Deps, AppNames, Acc -- Matches);
  73. expand_deps([Dep | Deps], AppNames, Acc) ->
  74. NewAcc = add_to_list(Acc, match_pattern(Dep, AppNames)),
  75. expand_deps(Deps, AppNames, NewAcc).
  76. inject(Profile) ->
  77. LibDir = lib_dir(Profile),
  78. AppNames = list_apps(LibDir),
  79. Deps0 = lists:flatmap(fun(Dep) -> expand_names(Dep, AppNames) end, deps(Profile)),
  80. Deps1 = merge(Deps0, []),
  81. Deps2 = lists:map(fun({Name, DepsX}) ->
  82. NewDeps = expand_deps(DepsX, AppNames, []),
  83. {Name, NewDeps}
  84. end, Deps1),
  85. lists:foreach(fun({App, Deps}) -> inject(App, Deps, LibDir) end, Deps2).
  86. %% list the profile/lib dir to get all apps
  87. list_apps(LibDir) ->
  88. Apps = filelib:wildcard("*", LibDir),
  89. lists:foldl(fun(App, Acc) -> [App || is_app(LibDir, App)] ++ Acc end, [], Apps).
  90. is_app(_LibDir, "." ++ _) -> false; %% ignore hidden dir
  91. is_app(LibDir, AppName) ->
  92. Path = filename:join([ebin_dir(LibDir, AppName), AppName ++ ".app"]),
  93. filelib:is_regular(Path) orelse error({unknown_app, AppName, Path}). %% wtf
  94. lib_dir(Profile) ->
  95. filename:join(["_build", Profile, lib]).
  96. ebin_dir(LibDir, AppName) -> filename:join([LibDir, AppName, "ebin"]).
  97. inject(App0, DepsToAdd, LibDir) ->
  98. App = str(App0),
  99. AppEbinDir = ebin_dir(LibDir, App),
  100. [AppFile0] = filelib:wildcard("*.app", AppEbinDir),
  101. AppFile = filename:join(AppEbinDir, AppFile0),
  102. {ok, [{application, AppName, Props}]} = file:consult(AppFile),
  103. Deps0 = case lists:keyfind(relup_deps, 1, Props) of
  104. {_, X} -> X;
  105. false -> []
  106. end,
  107. %% merge extra deps, but do not self-include
  108. Deps = add_to_list(Deps0, DepsToAdd) -- [App0],
  109. case Deps =:= [] of
  110. true -> ok;
  111. _ ->
  112. NewProps = lists:keystore(relup_deps, 1, Props, {relup_deps, Deps}),
  113. AppSpec = {application, AppName, NewProps},
  114. AppSpecIoData = io_lib:format("~p.", [AppSpec]),
  115. io:format(user, "updated_relup_deps for ~p~n", [App]),
  116. file:write_file(AppFile, AppSpecIoData)
  117. end.
  118. str(A) when is_atom(A) -> atom_to_list(A).
  119. match_pattern({re, Re}, AppNames) ->
  120. Match = fun(AppName) -> re:run(AppName, Re) =/= nomatch end,
  121. AppNamesToAdd = lists:filter(Match, AppNames),
  122. AppsToAdd = lists:map(fun(N) -> list_to_atom(N) end, AppNamesToAdd),
  123. case AppsToAdd =:= [] of
  124. true -> error({nomatch, Re});
  125. false -> AppsToAdd
  126. end;
  127. match_pattern(NameAtom, AppNames) ->
  128. case lists:member(str(NameAtom), AppNames) of
  129. true -> [NameAtom];
  130. false -> error({notfound, NameAtom})
  131. end.
  132. %% Append elements to list without duplication. No reordering.
  133. add_to_list(List, []) -> List;
  134. add_to_list(List, [H | T]) ->
  135. case lists:member(H, List) of
  136. true -> add_to_list(List, T);
  137. false -> add_to_list(List ++ [H], T)
  138. end.