inject-deps.escript 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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}]}
  49. , {{re, "emqx_.*"}, [emqx]}
  50. ].
  51. main([Profile | _]) ->
  52. ok = inject(Profile);
  53. main(_Args) ->
  54. io:format(standard_error, "~s", [usage()]),
  55. erlang:halt(1).
  56. expand_names({Name, Deps}, AppNames) ->
  57. Names = match_pattern(Name, AppNames),
  58. [{N, Deps} || N <- Names].
  59. %% merge k-v pairs with v1 ++ v2
  60. merge([], Acc) -> Acc;
  61. merge([{K, V0} | Rest], Acc) ->
  62. V = case lists:keyfind(K, 1, Acc) of
  63. {K, V1} -> V1 ++ V0;
  64. false -> V0
  65. end,
  66. NewAcc = lists:keystore(K, 1, Acc, {K, V}),
  67. merge(Rest, NewAcc).
  68. expand_deps([], _AppNames, Acc) -> Acc;
  69. expand_deps([{exclude, Dep} | Deps], AppNames, Acc) ->
  70. Matches = expand_deps([Dep], AppNames, []),
  71. expand_deps(Deps, AppNames, Acc -- Matches);
  72. expand_deps([Dep | Deps], AppNames, Acc) ->
  73. NewAcc = add_to_list(Acc, match_pattern(Dep, AppNames)),
  74. expand_deps(Deps, AppNames, NewAcc).
  75. inject(Profile) ->
  76. LibDir = lib_dir(Profile),
  77. AppNames = list_apps(LibDir),
  78. Deps0 = lists:flatmap(fun(Dep) -> expand_names(Dep, AppNames) end, deps(Profile)),
  79. Deps1 = merge(Deps0, []),
  80. Deps2 = lists:map(fun({Name, DepsX}) ->
  81. NewDeps = expand_deps(DepsX, AppNames, []),
  82. {Name, NewDeps}
  83. end, Deps1),
  84. lists:foreach(fun({App, Deps}) -> inject(App, Deps, LibDir) end, Deps2).
  85. %% list the profile/lib dir to get all apps
  86. list_apps(LibDir) ->
  87. Apps = filelib:wildcard("*", LibDir),
  88. lists:foldl(fun(App, Acc) -> [App || is_app(LibDir, App)] ++ Acc end, [], Apps).
  89. is_app(_LibDir, "." ++ _) -> false; %% ignore hidden dir
  90. is_app(LibDir, AppName) ->
  91. Path = filename:join([ebin_dir(LibDir, AppName), AppName ++ ".app"]),
  92. filelib:is_regular(Path) orelse error({unknown_app, AppName, Path}). %% wtf
  93. lib_dir(Profile) ->
  94. filename:join(["_build", Profile, lib]).
  95. ebin_dir(LibDir, AppName) -> filename:join([LibDir, AppName, "ebin"]).
  96. inject(App0, DepsToAdd, LibDir) ->
  97. App = str(App0),
  98. AppEbinDir = ebin_dir(LibDir, App),
  99. [AppFile0] = filelib:wildcard("*.app", AppEbinDir),
  100. AppFile = filename:join(AppEbinDir, AppFile0),
  101. {ok, [{application, AppName, Props}]} = file:consult(AppFile),
  102. Deps0 = case lists:keyfind(relup_deps, 1, Props) of
  103. {_, X} -> X;
  104. false -> []
  105. end,
  106. %% merge extra deps, but do not self-include
  107. Deps = add_to_list(Deps0, DepsToAdd) -- [App0],
  108. case Deps =:= [] of
  109. true -> ok;
  110. _ ->
  111. NewProps = lists:keystore(relup_deps, 1, Props, {relup_deps, Deps}),
  112. AppSpec = {application, AppName, NewProps},
  113. AppSpecIoData = io_lib:format("~p.", [AppSpec]),
  114. io:format(user, "updated_relup_deps for ~p~n", [App]),
  115. file:write_file(AppFile, AppSpecIoData)
  116. end.
  117. str(A) when is_atom(A) -> atom_to_list(A).
  118. match_pattern({re, Re}, AppNames) ->
  119. Match = fun(AppName) -> re:run(AppName, Re) =/= nomatch end,
  120. AppNamesToAdd = lists:filter(Match, AppNames),
  121. AppsToAdd = lists:map(fun(N) -> list_to_atom(N) end, AppNamesToAdd),
  122. case AppsToAdd =:= [] of
  123. true -> error({nomatch, Re});
  124. false -> AppsToAdd
  125. end;
  126. match_pattern(NameAtom, AppNames) ->
  127. case lists:member(str(NameAtom), AppNames) of
  128. true -> [NameAtom];
  129. false -> error({notfound, NameAtom})
  130. end.
  131. %% Append elements to list without duplication. No reordering.
  132. add_to_list(List, []) -> List;
  133. add_to_list(List, [H | T]) ->
  134. case lists:member(H, List) of
  135. true -> add_to_list(List, T);
  136. false -> add_to_list(List ++ [H], T)
  137. end.