選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

84 行
3.0 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% -------------------------------------------------------------------
  8. -module(rebar_fetch).
  9. -export([lock_source/2,
  10. download_source/2,
  11. needs_update/2]).
  12. -include("rebar.hrl").
  13. %% map short versions of resources to module names
  14. -define(RESOURCES, [{git, rebar_git_resource}, {pkg, rebar_pkg_resource}]).
  15. -spec lock_source(file:filename_all(), rebar_resource:resource()) ->
  16. rebar_resource:resource() | {error, string()}.
  17. lock_source(AppDir, Source) ->
  18. Module = get_resource_type(Source),
  19. Module:lock(AppDir, Source).
  20. -spec download_source(file:filename_all(), rebar_resource:resource()) -> true | {error, any()}.
  21. download_source(AppDir, Source) ->
  22. try
  23. Module = get_resource_type(Source),
  24. TmpDir = ec_file:insecure_mkdtemp(),
  25. AppDir1 = ec_cnv:to_list(AppDir),
  26. ec_file:mkdir_p(AppDir1),
  27. case Module:download(TmpDir, Source) of
  28. {ok, _} ->
  29. code:del_path(filename:absname(filename:join(AppDir1, "ebin"))),
  30. ec_file:remove(filename:absname(AppDir1), [recursive]),
  31. ok = ec_file:copy(TmpDir, filename:absname(AppDir1), [recursive]),
  32. true;
  33. {tarball, File} ->
  34. ok = erl_tar:extract(File, [{cwd, TmpDir}
  35. ,compressed]),
  36. BaseName = filename:basename(AppDir1),
  37. [FromDir] = filelib:wildcard(filename:join(TmpDir, BaseName++"-*")),
  38. code:del_path(filename:absname(filename:join(AppDir1, "ebin"))),
  39. ec_file:remove(filename:absname(AppDir1), [recursive]),
  40. ok = ec_file:copy(FromDir, filename:absname(AppDir1), [recursive]),
  41. true
  42. end
  43. catch
  44. _:E ->
  45. {error, E}
  46. end.
  47. -spec needs_update(file:filename_all(), rebar_resource:resource()) -> boolean() | {error, string()}.
  48. needs_update(AppDir, Source) ->
  49. Module = get_resource_type(Source),
  50. try
  51. Module:needs_update(AppDir, Source)
  52. catch
  53. _:_ ->
  54. true
  55. end.
  56. get_resource_type({Type, Location}) ->
  57. find_resource_module(Type, Location);
  58. get_resource_type({Type, Location, _}) ->
  59. find_resource_module(Type, Location);
  60. get_resource_type({Type, _, _, Location}) ->
  61. find_resource_module(Type, Location);
  62. get_resource_type(_) ->
  63. rebar_pkg_resource.
  64. find_resource_module(Type, Location) ->
  65. case lists:keyfind(Type, 1, ?RESOURCES) of
  66. false ->
  67. case code:which(Type) of
  68. non_existing ->
  69. {error, io_lib:format("Cannot handle dependency ~s.~n"
  70. " No module for resource type ~p", [Location, Type])};
  71. _ ->
  72. Type
  73. end;
  74. {Type, Module} ->
  75. Module
  76. end.