Procházet zdrojové kódy

move resource modules list to rebar_state, no longer static

pull/319/head
Tristan Sloughter před 10 roky
rodič
revize
38de29ae42
8 změnil soubory, kde provedl 56 přidání a 36 odebrání
  1. +4
    -0
      src/rebar.app.src
  2. +6
    -4
      src/rebar3.erl
  3. +21
    -22
      src/rebar_fetch.erl
  4. +3
    -3
      src/rebar_prv_deps.erl
  5. +3
    -3
      src/rebar_prv_install_deps.erl
  6. +1
    -1
      src/rebar_prv_lock.erl
  7. +12
    -1
      src/rebar_state.erl
  8. +6
    -2
      test/rebar_resource_SUITE.erl

+ 4
- 0
src/rebar.app.src Zobrazit soubor

@ -23,6 +23,10 @@
%% Default log level
{log_level, warn},
{resources, [{git, rebar_git_resource},
{pkg, rebar_pkg_resource},
{hg, rebar_hg_resource}]},
{providers, [rebar_prv_app_discovery,
rebar_prv_as,
rebar_prv_clean,

+ 6
- 4
src/rebar3.erl Zobrazit soubor

@ -124,16 +124,18 @@ run_aux(State, GlobalPluginProviders, RawArgs) ->
filename:join(filename:absname(rebar_state:dir(State2)), BaseDir)),
{ok, Providers} = application:get_env(rebar, providers),
{ok, PluginProviders, State4} = rebar_plugins:install(State3),
{ok, Resources} = application:get_env(rebar, resources),
State4 = rebar_state:resources(State3, Resources),
{ok, PluginProviders, State5} = rebar_plugins:install(State4),
%% Providers can modify profiles stored in opts, so set default after initializing providers
AllProviders = Providers++PluginProviders++GlobalPluginProviders,
State5 = rebar_state:create_logic_providers(AllProviders, State4),
State6 = rebar_state:default(State5, rebar_state:opts(State5)),
State6 = rebar_state:create_logic_providers(AllProviders, State5),
State7 = rebar_state:default(State6, rebar_state:opts(State6)),
{Task, Args} = parse_args(RawArgs),
rebar_core:init_command(rebar_state:command_args(State6, Args), Task).
rebar_core:init_command(rebar_state:command_args(State7, Args), Task).
init_config() ->
%% Initialize logging system

+ 21
- 22
src/rebar_fetch.erl Zobrazit soubor

@ -7,30 +7,28 @@
%% -------------------------------------------------------------------
-module(rebar_fetch).
-export([lock_source/2,
-export([lock_source/3,
download_source/3,
needs_update/2]).
needs_update/3]).
-export([format_error/1]).
-include("rebar.hrl").
-include_lib("providers/include/providers.hrl").
%% map short versions of resources to module names
-define(RESOURCES, [{git, rebar_git_resource}, {pkg, rebar_pkg_resource},
{hg, rebar_hg_resource}]).
-spec lock_source(file:filename_all(), rebar_resource:resource()) ->
-spec lock_source(file:filename_all(), rebar_resource:resource(), rebar_state:t()) ->
rebar_resource:resource() | {error, string()}.
lock_source(AppDir, Source) ->
Module = get_resource_type(Source),
lock_source(AppDir, Source, State) ->
Resources = rebar_state:resources(State),
Module = get_resource_type(Source, Resources),
Module:lock(AppDir, Source).
-spec download_source(file:filename_all(), rebar_resource:resource(), rebar_state:t()) ->
true | {error, any()}.
download_source(AppDir, Source, State) ->
try
Module = get_resource_type(Source),
Resources = rebar_state:resources(State),
Module = get_resource_type(Source, Resources),
TmpDir = ec_file:insecure_mkdtemp(),
AppDir1 = ec_cnv:to_list(AppDir),
case Module:download(TmpDir, Source, State) of
@ -64,9 +62,10 @@ download_source(AppDir, Source, State) ->
throw(?PRV_ERROR({fetch_fail, Source}))
end.
-spec needs_update(file:filename_all(), rebar_resource:resource()) -> boolean() | {error, string()}.
needs_update(AppDir, Source) ->
Module = get_resource_type(Source),
-spec needs_update(file:filename_all(), rebar_resource:resource(), rebar_state:t()) -> boolean() | {error, string()}.
needs_update(AppDir, Source, State) ->
Resources = rebar_state:resources(State),
Module = get_resource_type(Source, Resources),
try
Module:needs_update(AppDir, Source)
catch
@ -77,17 +76,17 @@ needs_update(AppDir, Source) ->
format_error({fetch_fail, Source}) ->
io_lib:format("Failed to fetch and copy dep: ~p", [Source]).
get_resource_type({Type, Location}) ->
find_resource_module(Type, Location);
get_resource_type({Type, Location, _}) ->
find_resource_module(Type, Location);
get_resource_type({Type, _, _, Location}) ->
find_resource_module(Type, Location);
get_resource_type(_) ->
get_resource_type({Type, Location}, Resources) ->
find_resource_module(Type, Location, Resources);
get_resource_type({Type, Location, _}, Resources) ->
find_resource_module(Type, Location, Resources);
get_resource_type({Type, _, _, Location}, Resources) ->
find_resource_module(Type, Location, Resources);
get_resource_type(_, _) ->
rebar_pkg_resource.
find_resource_module(Type, Location) ->
case lists:keyfind(Type, 1, ?RESOURCES) of
find_resource_module(Type, Location, Resources) ->
case lists:keyfind(Type, 1, Resources) of
false ->
case code:which(Type) of
non_existing ->

+ 3
- 3
src/rebar_prv_deps.erl Zobrazit soubor

@ -77,7 +77,7 @@ display_dep(_State, {Name, _Vsn, Source, _Opts}) when is_tuple(Source) ->
display_dep(State, {Name, Source={pkg, _, Vsn}, Level}) when is_integer(Level) ->
DepsDir = rebar_dir:deps_dir(State),
AppDir = filename:join([DepsDir, ec_cnv:to_binary(Name)]),
NeedsUpdate = case rebar_fetch:needs_update(AppDir, Source) of
NeedsUpdate = case rebar_fetch:needs_update(AppDir, Source, State) of
true -> "*";
false -> ""
end,
@ -85,7 +85,7 @@ display_dep(State, {Name, Source={pkg, _, Vsn}, Level}) when is_integer(Level) -
display_dep(State, {Name, Source, Level}) when is_tuple(Source), is_integer(Level), element(1, Source) =:= git ->
DepsDir = rebar_dir:deps_dir(State),
AppDir = filename:join([DepsDir, ec_cnv:to_binary(Name)]),
NeedsUpdate = case rebar_fetch:needs_update(AppDir, Source) of
NeedsUpdate = case rebar_fetch:needs_update(AppDir, Source, State) of
true -> "*";
false -> ""
end,
@ -93,7 +93,7 @@ display_dep(State, {Name, Source, Level}) when is_tuple(Source), is_integer(Leve
display_dep(State, {Name, Source, Level}) when is_tuple(Source), is_integer(Level) ->
DepsDir = rebar_dir:deps_dir(State),
AppDir = filename:join([DepsDir, ec_cnv:to_binary(Name)]),
NeedsUpdate = case rebar_fetch:needs_update(AppDir, Source) of
NeedsUpdate = case rebar_fetch:needs_update(AppDir, Source, State) of
true -> "*";
false -> ""
end,

+ 3
- 3
src/rebar_prv_install_deps.erl Zobrazit soubor

@ -510,12 +510,12 @@ fetch_app(AppInfo, AppDir, State) ->
Result
end.
maybe_upgrade(AppInfo, AppDir, false, _State) ->
maybe_upgrade(AppInfo, AppDir, false, State) ->
Source = rebar_app_info:source(AppInfo),
rebar_fetch:needs_update(AppDir, Source);
rebar_fetch:needs_update(AppDir, Source, State);
maybe_upgrade(AppInfo, AppDir, true, State) ->
Source = rebar_app_info:source(AppInfo),
case rebar_fetch:needs_update(AppDir, Source) of
case rebar_fetch:needs_update(AppDir, Source, State) of
true ->
?INFO("Updating ~s", [rebar_app_info:name(AppInfo)]),
case rebar_fetch:download_source(AppDir, Source, State) of

+ 1
- 1
src/rebar_prv_lock.erl Zobrazit soubor

@ -37,7 +37,7 @@ do(State) ->
%% If source is tuple it is a source dep
%% e.g. {git, "git://github.com/ninenines/cowboy.git", "master"}
{rebar_app_info:name(Dep)
,rebar_fetch:lock_source(Dir, Source)
,rebar_fetch:lock_source(Dir, Source, State)
,rebar_app_info:dep_level(Dep)}
end || Dep <- AllDeps, not(rebar_app_info:is_checkout(Dep))],
Dir = rebar_state:dir(State),

+ 12
- 1
src/rebar_state.erl Zobrazit soubor

@ -30,6 +30,7 @@
overrides/1, overrides/2,
apply_overrides/2,
resources/1, resources/2, add_resource/2,
providers/1, providers/2, add_provider/2]).
-include("rebar.hrl").
@ -51,6 +52,7 @@
all_deps = [] :: [rebar_app_info:t()],
overrides = [],
resources = [],
providers = []}).
-export_type([t/0]).
@ -296,6 +298,16 @@ namespace(#state_t{namespace=Namespace}) ->
namespace(State=#state_t{}, Namespace) ->
State#state_t{namespace=Namespace}.
resources(#state_t{resources=Resources}) ->
Resources.
resources(State, NewResources) ->
State#state_t{resources=NewResources}.
-spec add_resource(t(), rebar_resource:resource()) -> t().
add_resource(State=#state_t{resources=Resources}, Resource) ->
State#state_t{resources=[Resource | Resources]}.
providers(#state_t{providers=Providers}) ->
Providers.
@ -425,4 +437,3 @@ umerge([], Olds, Merged, CmpMerged, Cmp) when CmpMerged == Cmp ->
lists:reverse(Olds, Merged);
umerge([], Olds, Merged, _CmpMerged, Cmp) ->
lists:reverse(Olds, [Cmp | Merged]).

+ 6
- 2
test/rebar_resource_SUITE.erl Zobrazit soubor

@ -12,7 +12,10 @@ groups() ->
{hg, [], [{group, all}]}].
init_per_group(all, Config) ->
Config;
State = rebar_state:resources(rebar_state:new(), [{git, rebar_git_resource},
{pkg, rebar_pkg_resource},
{hg, rebar_hg_resource}]),
[{state, State} | Config];
init_per_group(Name, Config) ->
[{type, Name},
{resource, {Name, "https://example.org/user/app", "vsn"}} | Config].
@ -33,4 +36,5 @@ end_per_testcase(_, Config) ->
change_type_upgrade(Config) ->
?assert(rebar_fetch:needs_update(?config(path, Config),
?config(resource, Config))).
?config(resource, Config),
?config(state, Config))).

Načítá se…
Zrušit
Uložit