mad_deps.erl 2.8 KB

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