inject-deps.escript 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. base_deps() ->
  24. %% make sure emqx_dashboard depends on all other emqx_xxx apps
  25. %% so the appup instructions for emqx_dashboard is always the last
  26. %% to be executed
  27. [ {emqx_dashboard, [{re, "emqx_.*"}]}
  28. , {emqx_management, [{re, "emqx_.*"}, {exclude, emqx_dashboard}]}
  29. , {{re, "emqx_.*"}, [emqx]}
  30. ].
  31. main([Profile | _]) ->
  32. ok = inject(Profile);
  33. main(_Args) ->
  34. io:format(standard_error, "~s", [usage()]),
  35. erlang:halt(1).
  36. expand_names({Name, Deps}, AppNames) ->
  37. Names = match_pattern(Name, AppNames),
  38. [{N, Deps} || N <- Names].
  39. %% merge k-v pairs with v1 ++ v2
  40. merge([], Acc) -> Acc;
  41. merge([{K, V0} | Rest], Acc) ->
  42. V = case lists:keyfind(K, 1, Acc) of
  43. {K, V1} -> V1 ++ V0;
  44. false -> V0
  45. end,
  46. NewAcc = lists:keystore(K, 1, Acc, {K, V}),
  47. merge(Rest, NewAcc).
  48. expand_deps([], _AppNames, Acc) -> Acc;
  49. expand_deps([{exclude, Dep} | Deps], AppNames, Acc) ->
  50. Matches = expand_deps([Dep], AppNames, []),
  51. expand_deps(Deps, AppNames, Acc -- Matches);
  52. expand_deps([Dep | Deps], AppNames, Acc) ->
  53. NewAcc = add_to_list(Acc, match_pattern(Dep, AppNames)),
  54. expand_deps(Deps, AppNames, NewAcc).
  55. inject(Profile) ->
  56. LibDir = lib_dir(Profile),
  57. AppNames = list_apps(LibDir),
  58. Deps0 = lists:flatmap(fun(Dep) -> expand_names(Dep, AppNames) end, base_deps()),
  59. Deps1 = merge(Deps0, []),
  60. Deps2 = lists:map(fun({Name, DepsX}) ->
  61. NewDeps = expand_deps(DepsX, AppNames, []),
  62. {Name, NewDeps}
  63. end, Deps1),
  64. lists:foreach(fun({App, Deps}) -> inject(App, Deps, LibDir) end, Deps2).
  65. %% list the profile/lib dir to get all apps
  66. list_apps(LibDir) ->
  67. Apps = filelib:wildcard("*", LibDir),
  68. lists:foldl(fun(App, Acc) -> [App || is_app(LibDir, App)] ++ Acc end, [], Apps).
  69. is_app(_LibDir, "." ++ _) -> false; %% ignore hidden dir
  70. is_app(LibDir, AppName) ->
  71. filelib:is_regular(filename:join([ebin_dir(LibDir, AppName), AppName ++ ".app"])) orelse
  72. error({unknown_app, AppName}). %% wtf
  73. lib_dir(Profile) ->
  74. filename:join(["_build", Profile, lib]).
  75. ebin_dir(LibDir, AppName) -> filename:join([LibDir, AppName, "ebin"]).
  76. inject(App0, DepsToAdd, LibDir) ->
  77. App = str(App0),
  78. AppEbinDir = ebin_dir(LibDir, App),
  79. [AppFile0] = filelib:wildcard("*.app", AppEbinDir),
  80. AppFile = filename:join(AppEbinDir, AppFile0),
  81. {ok, [{application, AppName, Props}]} = file:consult(AppFile),
  82. Deps0 = case lists:keyfind(relup_deps, 1, Props) of
  83. {_, X} -> X;
  84. false -> []
  85. end,
  86. %% merge extra deps, but do not self-include
  87. Deps = add_to_list(Deps0, DepsToAdd) -- [App0],
  88. case Deps =:= [] of
  89. true -> ok;
  90. _ ->
  91. NewProps = lists:keystore(relup_deps, 1, Props, {relup_deps, Deps}),
  92. AppSpec = {application, AppName, NewProps},
  93. AppSpecIoData = io_lib:format("~p.", [AppSpec]),
  94. io:format(user, "updated_relup_deps for ~p~n", [App]),
  95. file:write_file(AppFile, AppSpecIoData)
  96. end.
  97. str(A) when is_atom(A) -> atom_to_list(A).
  98. match_pattern({re, Re}, AppNames) ->
  99. Match = fun(AppName) -> re:run(AppName, Re) =/= nomatch end,
  100. AppNamesToAdd = lists:filter(Match, AppNames),
  101. AppsToAdd = lists:map(fun(N) -> list_to_atom(N) end, AppNamesToAdd),
  102. case AppsToAdd =:= [] of
  103. true -> error({nomatch, Re});
  104. false -> AppsToAdd
  105. end;
  106. match_pattern(NameAtom, AppNames) ->
  107. case lists:member(str(NameAtom), AppNames) of
  108. true -> [NameAtom];
  109. false -> error({notfound, NameAtom})
  110. end.
  111. %% Append elements to list without duplication. No reordering.
  112. add_to_list(List, []) -> List;
  113. add_to_list(List, [H | T]) ->
  114. case lists:member(H, List) of
  115. true -> add_to_list(List, T);
  116. false -> add_to_list(List ++ [H], T)
  117. end.