mad_deps.erl 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. -module(mad_deps).
  2. -copyright('Sina Samavati').
  3. -export([fetch/4,name_and_repo/1,checkout_to/1,get_publisher/1]).
  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. Co1 = checkout_to(Co),
  25. Cache = mad_utils:get_value(cache, Config, deps_fetch),
  26. case get(Name) of
  27. fetched -> ok;
  28. _ -> fetch_dep(Cwd, Config, ConfigFile, Name, Cmd1, Uri, Co1, Cache)
  29. end,
  30. fetch(Cwd, Config, ConfigFile, T).
  31. -spec fetch_dep(directory(), any(), filename(), string(), string(), uri(), any(), atom()) -> ok.
  32. fetch_dep(Cwd, Config, ConfigFile, Name, Cmd, Uri, Co, Cache) ->
  33. TrunkPath = case Cache of
  34. deps_fetch -> filename:join([mad_utils:get_value(deps_dir,Config,"deps"),Name]);
  35. Dir -> filename:join([Dir,get_publisher(Uri),Name]) end,
  36. Opts = ["clone", Uri, TrunkPath ],
  37. io:format("==> dependency: ~p tag: ~p~n", [Uri,Co]),
  38. %% fetch
  39. mad_utils:exec(Cmd, Opts),
  40. put(Name, fetched),
  41. %% check dependencies of the dependency
  42. TrunkConfigFile = filename:join(TrunkPath, ConfigFile),
  43. Conf = mad_utils:consult(TrunkConfigFile),
  44. Conf1 = mad_utils:script(TrunkConfigFile, Conf),
  45. fetch(Cwd, Config, ConfigFile, mad_utils:get_value(deps, Conf1, [])),
  46. case Cache of
  47. deps_fetch -> skip;
  48. CacheDir -> build_dep(Cwd, Config, ConfigFile, get_publisher(Uri), Name, Cmd, Co, CacheDir) end.
  49. %% build dependency based on branch/tag/commit
  50. -spec build_dep(directory(), any(), string(), string(), string(), string(), string(), string()) -> ok.
  51. build_dep(Cwd, Conf, _ConfFile, Publisher, Name, Cmd, Co, Dir) ->
  52. TrunkPath = filename:join([Dir, Publisher, Name]),
  53. DepsDir = filename:join([mad_utils:get_value(deps_dir, Conf, ["deps"]),Name]),
  54. mad_utils:exec("cp", ["-r", TrunkPath, DepsDir]),
  55. ok = file:set_cwd(DepsDir),
  56. mad_utils:exec(Cmd, ["checkout", lists:concat([Co])]),
  57. ok = file:set_cwd(Cwd).
  58. %% internal
  59. -spec name_and_repo(dependency()) -> {string(), repo()}.
  60. name_and_repo({Name, _, Repo}) -> {atom_to_list(Name), Repo};
  61. name_and_repo({Name, _, Repo, _}) -> {atom_to_list(Name), Repo}.
  62. -spec checkout_to(term() | {any(), string}) -> term().
  63. checkout_to({_, V}) -> V;
  64. checkout_to(Else) -> Else.
  65. -spec get_publisher(uri()) -> string().
  66. get_publisher(Uri) ->
  67. case http_uri:parse(Uri, [{scheme_defaults,
  68. [{git, 9418}|http_uri:scheme_defaults()]}]) of
  69. {ok, {_, _, _, _, Path, _}} -> hd(string:tokens(Path,"/"));
  70. _ -> case string:tokens(Uri,":/") of
  71. [_Server,Publisher,_Repo] -> Publisher;
  72. _ -> exit(error) end end.