mad_deps.erl 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. -module(mad_deps).
  2. -copyright('Sina Samavati').
  3. -compile(export_all).
  4. -type directory() :: string().
  5. -type filename() :: string().
  6. -type name() :: atom().
  7. -type uri() :: string().
  8. -type version_control() :: git | hg.
  9. -type repo() :: {version_control(), uri(), {branch | tag, string()} | string()}.
  10. -type dependency() :: {name(), string(), repo()}.
  11. -export_type([dependency/0]).
  12. -spec fetch(directory(), any(), filename(), [dependency()]) -> ok.
  13. fetch(_, _Config, _, []) -> ok;
  14. fetch(Cwd, Config, ConfigFile, [H|T]) when is_tuple(H) =:= false -> fetch(Cwd, Config, ConfigFile, T);
  15. fetch(Cwd, Config, ConfigFile, [H|T]) ->
  16. {Name, Repo} = name_and_repo(H),
  17. {Cmd, Uri, Co} = case Repo of
  18. V={_, _, _} ->
  19. V;
  20. {_Cmd, _Url, _Co, _} ->
  21. {_Cmd, _Url, _Co}
  22. end,
  23. Cmd1 = atom_to_list(Cmd),
  24. Cache = mad_utils:get_value(cache, Config, deps_fetch),
  25. case get(Name) of
  26. fetched -> ok;
  27. _ -> fetch_dep(Cwd, Config, ConfigFile, Name, Cmd1, Uri, Co, Cache)
  28. end,
  29. fetch(Cwd, Config, ConfigFile, T).
  30. -spec fetch_dep(directory(), any(), filename(), string(), string(), uri(), any(), atom()) -> ok.
  31. fetch_dep(Cwd, Config, ConfigFile, Name, Cmd, Uri, Co, Cache) ->
  32. TrunkPath = case Cache of
  33. deps_fetch -> filename:join([mad_utils:get_value(deps_dir,Config,"deps"),Name]);
  34. Dir -> filename:join([Dir,get_publisher(Uri),Name]) end,
  35. io:format("==> dependency: ~p tag: ~p~n", [Uri,Co]),
  36. {R,Co1} = case Co of
  37. {_,Rev} ->
  38. {["git clone ",Uri," ",TrunkPath," && cd ",TrunkPath,
  39. " && git checkout \"",Rev,"\"" ],Rev};
  40. Master -> {["git clone ", Uri," ", TrunkPath ],lists:concat([Master])} end,
  41. os:cmd(R),
  42. put(Name, fetched),
  43. %% check dependencies of the dependency
  44. TrunkConfigFile = filename:join(TrunkPath, ConfigFile),
  45. Conf = mad_utils:consult(TrunkConfigFile),
  46. Conf1 = mad_utils:script(TrunkConfigFile, Conf),
  47. fetch(Cwd, Config, ConfigFile, mad_utils:get_value(deps, Conf1, [])),
  48. case Cache of
  49. deps_fetch -> skip;
  50. CacheDir -> build_dep(Cwd, Config, ConfigFile, get_publisher(Uri), Name, Cmd, Co1, CacheDir) end.
  51. %% build dependency based on branch/tag/commit
  52. -spec build_dep(directory(), any(), string(), string(), string(), string(), string(), string()) -> ok.
  53. build_dep(Cwd, Conf, _ConfFile, Publisher, Name, _Cmd, _Co, Dir) ->
  54. TrunkPath = filename:join([Dir, Publisher, Name]),
  55. DepsDir = filename:join([mad_utils:get_value(deps_dir, Conf, ["deps"]),Name]),
  56. os:cmd(["cp -r ", TrunkPath, " ", DepsDir]),
  57. ok = file:set_cwd(DepsDir),
  58. ok = file:set_cwd(Cwd).
  59. %% internal
  60. -spec name_and_repo(dependency()) -> {string(), repo()}.
  61. name_and_repo({Name, _, Repo}) when is_list(Name) -> {Name, Repo};
  62. name_and_repo({Name, _, Repo, _}) when is_list(Name) -> {Name, Repo};
  63. name_and_repo({Name, _, Repo}) -> {atom_to_list(Name), Repo};
  64. name_and_repo({Name, _, Repo, _}) -> {atom_to_list(Name), Repo};
  65. name_and_repo(Name) -> {Name,Name}.
  66. -spec get_publisher(uri()) -> string().
  67. get_publisher(Uri) ->
  68. case http_uri:parse(Uri, [{scheme_defaults,
  69. [{git, 9418}|http_uri:scheme_defaults()]}]) of
  70. {ok, {_, _, _, _, Path, _}} -> hd(string:tokens(Path,"/"));
  71. _ -> case string:tokens(Uri,":/") of
  72. [_Server,Publisher,_Repo] -> Publisher;
  73. _ -> exit(error) end end.