summaryrefslogtreecommitdiff
path: root/src/rebar_prv_update.erl
diff options
context:
space:
mode:
authorHeinz N. Gies <heinz@licenser.net>2016-02-08 11:15:59 -0500
committerHeinz N. Gies <heinz@licenser.net>2016-02-08 11:15:59 -0500
commit2f563041cb248ba0cac27b92da5dcc3e7be27f80 (patch)
treee27ecaf59cfeb0e6406f1f4ce753842ff52855d5 /src/rebar_prv_update.erl
parent91c47db27a3c63fc04940c7c72433062dbadf042 (diff)
parent7fab47dfa05754242790a748bbd303ffe9703e5c (diff)
Merge master
Diffstat (limited to 'src/rebar_prv_update.erl')
-rw-r--r--src/rebar_prv_update.erl68
1 files changed, 46 insertions, 22 deletions
diff --git a/src/rebar_prv_update.erl b/src/rebar_prv_update.erl
index 6637ebe..0e3b9a0 100644
--- a/src/rebar_prv_update.erl
+++ b/src/rebar_prv_update.erl
@@ -36,26 +36,36 @@ init(State) ->
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
try
- RegistryDir = rebar_packages:registry_dir(State),
- filelib:ensure_dir(filename:join(RegistryDir, "dummy")),
- HexFile = filename:join(RegistryDir, "registry"),
- ?INFO("Updating package registry...", []),
- TmpDir = ec_file:insecure_mkdtemp(),
- TmpFile = filename:join(TmpDir, "packages.gz"),
+ case rebar_packages:registry_dir(State) of
+ {ok, RegistryDir} ->
+ filelib:ensure_dir(filename:join(RegistryDir, "dummy")),
+ HexFile = filename:join(RegistryDir, "registry"),
+ ?INFO("Updating package registry...", []),
+ TmpDir = ec_file:insecure_mkdtemp(),
+ TmpFile = filename:join(TmpDir, "packages.gz"),
- Url = rebar_state:get(State, rebar_packages_cdn, ?DEFAULT_HEX_REGISTRY),
- case httpc:request(get, {Url, []},
- [], [{stream, TmpFile}, {sync, true}],
- rebar) of
- {ok, saved_to_file} ->
- {ok, Data} = file:read_file(TmpFile),
- Unzipped = zlib:gunzip(Data),
- ok = file:write_file(HexFile, Unzipped),
- ?INFO("Writing registry to ~s", [HexFile]),
- hex_to_index(State),
- {ok, State};
- _ ->
- ?PRV_ERROR(package_index_download)
+ CDN = rebar_state:get(State, rebar_packages_cdn, ?DEFAULT_CDN),
+ case rebar_utils:url_append_path(CDN, ?REMOTE_REGISTRY_FILE) of
+ {ok, Url} ->
+ ?DEBUG("Fetching registry from ~p", [Url]),
+ case httpc:request(get, {Url, [{"User-Agent", rebar_utils:user_agent()}]},
+ [], [{stream, TmpFile}, {sync, true}],
+ rebar) of
+ {ok, saved_to_file} ->
+ {ok, Data} = file:read_file(TmpFile),
+ Unzipped = zlib:gunzip(Data),
+ ok = file:write_file(HexFile, Unzipped),
+ ?INFO("Writing registry to ~s", [HexFile]),
+ hex_to_index(State),
+ {ok, State};
+ _ ->
+ ?PRV_ERROR(package_index_download)
+ end;
+ _ ->
+ ?PRV_ERROR({package_parse_cdn, CDN})
+ end;
+ {uri_parse_error, CDN} ->
+ ?PRV_ERROR({package_parse_cdn, CDN})
end
catch
_E:C ->
@@ -64,6 +74,8 @@ do(State) ->
end.
-spec format_error(any()) -> iolist().
+format_error({package_parse_cdn, Uri}) ->
+ io_lib:format("Failed to parse CDN url: ~p", [Uri]);
format_error(package_index_download) ->
"Failed to download package index.";
format_error(package_index_write) ->
@@ -75,7 +87,7 @@ is_supported(<<"rebar3">>) -> true;
is_supported(_) -> false.
hex_to_index(State) ->
- RegistryDir = rebar_packages:registry_dir(State),
+ {ok, RegistryDir} = rebar_packages:registry_dir(State),
HexFile = filename:join(RegistryDir, "registry"),
try ets:file2tab(HexFile) of
{ok, Registry} ->
@@ -92,12 +104,24 @@ hex_to_index(State) ->
false ->
true
end;
- ({Pkg, [Vsns]}, _) when is_binary(Pkg) ->
- ets:insert(?PACKAGE_TABLE, {Pkg, Vsns});
(_, _) ->
true
end, true, Registry),
+ ets:foldl(fun({Pkg, [[]]}, _) when is_binary(Pkg) ->
+ true;
+ ({Pkg, [Vsns=[Vsn | _Rest]]}, _) when is_binary(Pkg) ->
+ %% Verify the package is of the right build tool by checking if the first
+ %% version exists in the table from the foldl above
+ case ets:member(?PACKAGE_TABLE, {Pkg, Vsn}) of
+ true ->
+ ets:insert(?PACKAGE_TABLE, {Pkg, Vsns});
+ false ->
+ true
+ end;
+ (_, _) ->
+ true
+ end, true, Registry),
ets:insert(?PACKAGE_TABLE, {package_index_version, ?PACKAGE_INDEX_VERSION}),
?INFO("Writing index to ~s", [PackageIndex]),
ets:tab2file(?PACKAGE_TABLE, PackageIndex),