inject-deps.escript 4.5 KB

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