You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 rivejä
4.0 KiB

10 vuotta sitten
  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. -export([format_error/1]).
  13. -include("rebar.hrl").
  14. -include_lib("providers/include/providers.hrl").
  15. -spec lock_source(rebar_app_info:t(), rebar_state:t())
  16. -> rebar_resource_v2:source() | {error, string()}.
  17. lock_source(AppInfo, State) ->
  18. rebar_resource_v2:lock(AppInfo, State).
  19. -spec download_source(rebar_app_info:t(), rebar_state:t())
  20. -> rebar_app_info:t() | {error, any()}.
  21. download_source(AppInfo, State) ->
  22. AppDir = rebar_app_info:dir(AppInfo),
  23. try download_source_(AppInfo, State) of
  24. ok ->
  25. %% freshly downloaded, update the app info opts to reflect the new config
  26. Config = rebar_config:consult(AppDir),
  27. AppInfo1 = rebar_app_info:update_opts(AppInfo, rebar_app_info:opts(AppInfo), Config),
  28. case rebar_app_discover:find_app(AppInfo1, AppDir, all) of
  29. {true, AppInfo2} ->
  30. rebar_app_info:is_available(AppInfo2, true);
  31. false ->
  32. throw(?PRV_ERROR({dep_app_not_found, rebar_app_info:name(AppInfo1)}))
  33. end;
  34. {error, Reason} ->
  35. throw(?PRV_ERROR(Reason))
  36. catch
  37. throw:{no_resource, Type, Location} ->
  38. throw(?PRV_ERROR({no_resource, Location, Type}));
  39. ?WITH_STACKTRACE(C,T,S)
  40. ?DEBUG("rebar_fetch exception ~p ~p ~p", [C, T, S]),
  41. throw(?PRV_ERROR({fetch_fail, rebar_app_info:source(AppInfo)}))
  42. end.
  43. download_source_(AppInfo, State) ->
  44. AppDir = rebar_app_info:dir(AppInfo),
  45. TmpDir = ec_file:insecure_mkdtemp(),
  46. AppDir1 = rebar_utils:to_list(AppDir),
  47. case rebar_resource_v2:download(TmpDir, AppInfo, State) of
  48. ok ->
  49. ec_file:mkdir_p(AppDir1),
  50. code:del_path(filename:absname(filename:join(AppDir1, "ebin"))),
  51. ok = rebar_file_utils:rm_rf(filename:absname(AppDir1)),
  52. ?DEBUG("Moving checkout ~p to ~p", [TmpDir, filename:absname(AppDir1)]),
  53. rebar_file_utils:mv(TmpDir, filename:absname(AppDir1));
  54. Error ->
  55. Error
  56. end.
  57. -spec needs_update(rebar_app_info:t(), rebar_state:t())
  58. -> boolean() | {error, string()}.
  59. needs_update(AppInfo, State) ->
  60. try
  61. rebar_resource_v2:needs_update(AppInfo, State)
  62. catch
  63. _:_ ->
  64. true
  65. end.
  66. format_error({bad_download, CachePath}) ->
  67. io_lib:format("Download of package does not match md5sum from server: ~ts", [CachePath]);
  68. format_error({unexpected_hash, CachePath, Expected, Found}) ->
  69. io_lib:format("The checksum for package at ~ts (~ts) does not match the "
  70. "checksum previously locked (~ts). Either unlock or "
  71. "upgrade the package, or make sure you fetched it from "
  72. "the same index from which it was initially fetched.",
  73. [CachePath, Found, Expected]);
  74. format_error({failed_extract, CachePath}) ->
  75. io_lib:format("Failed to extract package: ~ts", [CachePath]);
  76. format_error({bad_etag, Source}) ->
  77. io_lib:format("MD5 Checksum comparison failed for: ~ts", [Source]);
  78. format_error({fetch_fail, Name, Vsn}) ->
  79. io_lib:format("Failed to fetch and copy dep: ~ts-~ts", [Name, Vsn]);
  80. format_error({fetch_fail, Source}) ->
  81. io_lib:format("Failed to fetch and copy dep: ~p", [Source]);
  82. format_error({bad_checksum, File}) ->
  83. io_lib:format("Checksum mismatch against tarball in ~ts", [File]);
  84. format_error({bad_registry_checksum, File}) ->
  85. io_lib:format("Checksum mismatch against registry in ~ts", [File]);
  86. format_error({dep_app_not_found, AppName}) ->
  87. io_lib:format("Dependency failure: source for ~ts does not contain a "
  88. "recognizable project and can not be built", [AppName]).