@ -2,21 +2,18 @@
% % ex : ts = 4 sw = 4 et
- module ( rebar_pkg_resource ) .
- behaviour ( rebar_resource ) .
- behaviour ( rebar_resource_v2 ) .
- export ( [ init / 1
, lock / 2
, download / 3
, download / 4
, needs_update / 2
, make_vsn / 1 ] ) .
- export ( [ request / 4
, etag / 1 ] ) .
- export ( [ init / 2 ,
lock / 2 ,
download / 4 ,
download / 5 ,
needs_update / 2 ,
make_vsn / 2 ] ) .
- ifdef ( TEST ) .
% % exported for test purposes
- export ( [ store_etag_in_cache / 2 ] ) .
- export ( [ store_etag_in_cache / 2 , etag / 1 , request / 4 ]) .
- endif .
- include ( " rebar.hrl " ) .
@ -34,22 +31,26 @@
% % Public API
% % == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
- spec init ( rebar_state : t ( ) ) - > { ok , term ( ) } .
init ( State ) - >
- spec init ( atom ( ) , rebar_state : t ( ) ) - > { ok , rebar_resource_v2 : resource ( ) } .
init ( Type , State) - >
{ ok , Vsn } = application : get_key ( rebar , vsn ) ,
BaseConfig = #{ http_adapter = > hex_http_httpc ,
http_user_agent_fragment = >
< < " (rebar3/ " , ( list_to_binary ( Vsn ) ) / binary , " ) (httpc) " > > ,
http_adapter_config = > #{ profile = > rebar } } ,
Repos = rebar_hex_repos : from_state ( BaseConfig , State ) ,
{ ok , #{ repos = > Repos ,
base_config = > BaseConfig } } .
Resource = rebar_resource_v2 : new ( Type , ? MODULE , #{ repos = > Repos ,
base_config = > BaseConfig } ) ,
{ ok , Resource } .
- spec lock ( AppDir , Source ) - > Res when
AppDir : : file : name ( ) ,
Source : : tupl e( ) ,
- spec lock ( AppInfo , ResourceStat e ) - > Res when
AppInfo : : rebar_app_info : t ( ) ,
ResourceState : : rebar_resource_v2 : resource_stat e( ) ,
Res : : { atom ( ) , string ( ) , any ( ) , binary ( ) } .
lock ( _ AppDir , { pkg , Name , Vsn , Hash , _ RepoConfig } ) - >
lock ( AppInfo , _ ) - >
{ pkg , Name , Vsn , Hash , _ RepoConfig } = rebar_app_info : source ( AppInfo ) ,
{ pkg , Name , Vsn , Hash } .
% % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
@ -58,12 +59,12 @@ lock(_AppDir, {pkg, Name, Vsn, Hash, _RepoConfig}) ->
% % version .
% % @ end
% % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- spec needs_update ( Dir , Pkg ) - > Res when
Dir : : file : name ( ) ,
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : hex_core : config ( ) } ,
- spec needs_update ( AppInfo , ResourceState ) - > Res when
AppInfo : : rebar_app_info : t ( ) ,
ResourceState : : rebar_resource_v2 : resource_state ( ) ,
Res : : boolean ( ) .
needs_update ( Dir , { pkg , _ Name , Vsn , _ Hash , _ } ) - >
[ AppInfo ] = rebar_app_discover : find_apps ( [ Dir ] , all ) ,
needs_update ( AppInfo , _ ) - >
{ pkg , _ Name , Vsn , _ Hash , _ } = rebar_app_info : source ( AppInfo ) ,
case rebar_app_info : original_vsn ( AppInfo ) =:= rebar_utils : to_binary ( Vsn ) of
true - >
false ;
@ -76,13 +77,14 @@ needs_update(Dir, {pkg, _Name, Vsn, _Hash, _}) ->
% % Download the given pkg .
% % @ end
% % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- spec download ( TmpDir , Pkg , State) - > Res when
- spec download ( TmpDir , AppInfo , State , Resource State) - > Res when
TmpDir : : file : name ( ) ,
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : hex_core : config ( ) } ,
AppInfo : : rebar_app_info : t ( ) ,
ResourceState : : rebar_resource_v2 : resource_state ( ) ,
State : : rebar_state : t ( ) ,
Res : : { 'error' , _ } | { 'ok' , _ } | { 'tarball' , binary ( ) | string ( ) } .
download ( TmpDir , Pkg , State) - >
download ( TmpDir , Pkg , State, true ) .
download ( TmpDir , AppInfo , State , Resource State) - >
download ( TmpDir , rebar_app_info : source ( AppInfo ) , State , Resource State, true ) .
% % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
% % @ doc
@ -91,13 +93,14 @@ download(TmpDir, Pkg, State) ->
% % is different .
% % @ end
% % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- spec download ( TmpDir , Pkg , State , UpdateETag ) - > Res when
- spec download ( TmpDir , Pkg , State , ResourceState , UpdateETag) - > Res when
TmpDir : : file : name ( ) ,
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : hex_core : config ( ) } ,
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : rebar_hex_repos : repo ( ) } ,
State : : rebar_state : t ( ) ,
ResourceState : : rebar_resource_v2 : resource_state ( ) ,
UpdateETag : : boolean ( ) ,
Res : : download_result ( ) .
download ( TmpDir , Pkg = { pkg , Name , Vsn , _ Hash , Repo } , State , UpdateETag ) - >
download ( TmpDir , Pkg = { pkg , Name , Vsn , _ Hash , Repo } , State , _ ResourceState , UpdateETag ) - >
{ ok , PackageDir } = rebar_packages : package_dir ( Repo , State ) ,
Package = binary_to_list ( < < Name / binary , " - " , Vsn / binary , " .tar " > > ) ,
ETagFile = binary_to_list ( < < Name / binary , " - " , Vsn / binary , " .etag " > > ) ,
@ -112,10 +115,11 @@ download(TmpDir, Pkg={pkg, Name, Vsn, _Hash, Repo}, State, UpdateETag) ->
% % Returns { error , string ( ) } as this operation is not supported for pkg sources .
% % @ end
% % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- spec make_vsn ( Vsn ) - > Res when
Vsn : : any ( ) ,
Res : : { 'error' , [ 1 . . 255 , . . . ] } .
make_vsn ( _ ) - >
- spec make_vsn ( AppInfo , ResourceState ) - > Res when
AppInfo : : rebar_app_info : t ( ) ,
ResourceState : : rebar_resource_v2 : resource_state ( ) ,
Res : : { 'error' , string ( ) } .
make_vsn ( _ , _ ) - >
{ error , " Replacing version of type pkg not supported. " } .
% % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
@ -126,7 +130,7 @@ make_vsn(_) ->
% % { ok , Contents , NewEtag } , otherwise if some error occured return error .
% % @ end
% % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- spec request ( hex_core : config ( ) , binary ( ) , binary ( ) , false | binary ( ) )
- spec request ( rebar_hex_repos : repo ( ) , binary ( ) , binary ( ) , false | binary ( ) )
- > { ok , cached } | { ok , binary ( ) , binary ( ) } | error .
request ( Config , Name , Version , ETag ) - >
Config1 = Config #{ http_etag = > ETag } ,
@ -184,7 +188,7 @@ store_etag_in_cache(Path, ETag) ->
UpdateETag ) - > Res when
TmpDir : : file : name ( ) ,
CachePath : : file : name ( ) ,
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : hex_core : config ( ) } ,
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : rebar_hex_repos : repo ( ) } ,
ETag : : binary ( ) ,
State : : rebar_state : t ( ) ,
ETagPath : : file : name ( ) ,
@ -212,7 +216,7 @@ cached_download(TmpDir, CachePath, Pkg={pkg, Name, Vsn, _Hash, RepoConfig}, ETag
- spec serve_from_cache ( TmpDir , CachePath , Pkg , State ) - > Res when
TmpDir : : file : name ( ) ,
CachePath : : file : name ( ) ,
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : hex_core : config ( ) } ,
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : rebar_hex_repos : repo ( ) } ,
State : : rebar_state : t ( ) ,
Res : : cached_result ( ) .
serve_from_cache ( TmpDir , CachePath , Pkg , State ) - >
@ -237,7 +241,7 @@ serve_from_cache(TmpDir, CachePath, Pkg, State) ->
ETagPath ) - > Res when
TmpDir : : file : name ( ) ,
CachePath : : file : name ( ) ,
Package : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : hex_core : config ( ) } ,
Package : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : rebar_hex_repos : repo ( ) } ,
ETag : : binary ( ) ,
Binary : : binary ( ) ,
State : : rebar_state : t ( ) ,
@ -272,7 +276,7 @@ extract(TmpDir, CachePath) ->
{ Files , Contents , Version , Meta } .
- spec checksums ( Pkg , Files , Contents , Version , Meta , State ) - > Res when
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : hex_core : config ( ) } ,
Pkg : : { pkg , Name : : binary ( ) , Vsn : : binary ( ) , Hash : : binary ( ) , RepoConfig : : rebar_hex_repos : repo ( ) } ,
Files : : list ( { file : name ( ) , binary ( ) } ) ,
Contents : : binary ( ) ,
Version : : binary ( ) ,