From bfd08c129d0e35e51d494309729f19ddb2e76eb7 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Mon, 4 May 2015 18:25:49 -0500 Subject: set resources of state in state initialize functions --- src/rebar3.erl | 12 +++++------- src/rebar_state.erl | 23 +++++++++++++++-------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/rebar3.erl b/src/rebar3.erl index b0cc949..92803c5 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -127,19 +127,17 @@ run_aux(State, RawArgs) -> filename:join(filename:absname(rebar_state:dir(State2)), BaseDir)), {ok, Providers} = application:get_env(rebar, providers), - {ok, Resources} = application:get_env(rebar, resources), - State4 = rebar_state:resources(State3, Resources), - State5 = rebar_plugins:install(State4), + State4 = rebar_plugins:install(State3), %% Providers can modify profiles stored in opts, so set default after initializing providers - State6 = rebar_state:create_logic_providers(Providers, State5), - State7 = rebar_state:default(State6, rebar_state:opts(State6)), + State5 = rebar_state:create_logic_providers(Providers, State4), + State6 = rebar_state:default(State5, rebar_state:opts(State5)), {Task, Args} = parse_args(RawArgs), - State8 = rebar_state:code_paths(State7, default, code:get_path()), + State7 = rebar_state:code_paths(State6, default, code:get_path()), - rebar_core:init_command(rebar_state:command_args(State8, Args), Task). + rebar_core:init_command(rebar_state:command_args(State7, Args), Task). init_config() -> %% Initialize logging system diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 46c870e..9bf90d5 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -69,25 +69,28 @@ -spec new() -> t(). new() -> - #state_t{dir = rebar_dir:get_cwd()}. + BaseState = base_state(), + BaseState#state_t{dir = rebar_dir:get_cwd()}. -spec new(list()) -> t(). new(Config) when is_list(Config) -> + BaseState = base_state(), Deps = proplists:get_value(deps, Config, []), Opts = dict:from_list([{{deps, default}, Deps} | Config]), - #state_t { dir = rebar_dir:get_cwd(), - default = Opts, - opts = Opts }. + BaseState#state_t { dir = rebar_dir:get_cwd(), + default = Opts, + opts = Opts }. -spec new(t() | atom(), list()) -> t(). new(Profile, Config) when is_atom(Profile) , is_list(Config) -> + BaseState = base_state(), Deps = proplists:get_value(deps, Config, []), Opts = dict:from_list([{{deps, default}, Deps} | Config]), - #state_t { dir = rebar_dir:get_cwd(), - current_profiles = [Profile], - default = Opts, - opts = Opts }; + BaseState#state_t { dir = rebar_dir:get_cwd(), + current_profiles = [Profile], + default = Opts, + opts = Opts }; new(ParentState=#state_t{}, Config) -> %% Load terms from rebar.config, if it exists Dir = rebar_dir:get_cwd(), @@ -113,6 +116,10 @@ new(ParentState, Config, Dir) -> ,opts=NewOpts ,default=NewOpts}. +base_state() -> + {ok, Resources} = application:get_env(rebar, resources), + #state_t{resources=Resources}. + get(State, Key) -> {ok, Value} = dict:find(Key, State#state_t.opts), Value. -- cgit v1.1 From aa7a272aaefddeae04b875328971c6e30e4dfade Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Mon, 4 May 2015 19:18:13 -0500 Subject: set resources to empty list if non in app env --- src/rebar_state.erl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 9bf90d5..7a6e60d 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -117,7 +117,12 @@ new(ParentState, Config, Dir) -> ,default=NewOpts}. base_state() -> - {ok, Resources} = application:get_env(rebar, resources), + case application:get_env(rebar, resources) of + undefined -> + Resources = []; + {ok, Resources} -> + Resources + end, #state_t{resources=Resources}. get(State, Key) -> -- cgit v1.1 From 4dd3eb5aefb866ae744d1c0dd30eb8db02ef263c Mon Sep 17 00:00:00 2001 From: Fred Hebert Date: Mon, 4 May 2015 19:29:57 +0000 Subject: Use resource handlers to deal with deps/semver --- src/rebar_otp_app.erl | 7 ++++--- src/rebar_prv_deps.erl | 26 ++++++------------------- src/rebar_utils.erl | 51 +++++++++++++++++++++++++++++++++++++------------- 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/rebar_otp_app.erl b/src/rebar_otp_app.erl index 260343d..0bf27c9 100644 --- a/src/rebar_otp_app.erl +++ b/src/rebar_otp_app.erl @@ -104,7 +104,7 @@ preprocess(State, AppInfo, AppSrcFile) -> A1 = apply_app_vars(AppVars, AppData), %% AppSrcFile may contain instructions for generating a vsn number - Vsn = app_vsn(AppSrcFile), + Vsn = app_vsn(AppSrcFile, State), A2 = lists:keystore(vsn, 1, A1, {vsn, Vsn}), %% systools:make_relup/4 fails with {missing_param, registered} @@ -197,11 +197,12 @@ consult_app_file(Filename) -> end end. -app_vsn(AppFile) -> +app_vsn(AppFile, State) -> case consult_app_file(AppFile) of {ok, [{application, _AppName, AppData}]} -> AppDir = filename:dirname(filename:dirname(AppFile)), - rebar_utils:vcs_vsn(get_value(vsn, AppData, AppFile), AppDir); + Resources = rebar_state:resources(State), + rebar_utils:vcs_vsn(get_value(vsn, AppData, AppFile), AppDir, Resources); {error, Reason} -> ?ABORT("Failed to consult app file ~s: ~p\n", [AppFile, Reason]) end. diff --git a/src/rebar_prv_deps.erl b/src/rebar_prv_deps.erl index cbe440a..be81c31 100644 --- a/src/rebar_prv_deps.erl +++ b/src/rebar_prv_deps.erl @@ -91,20 +91,12 @@ display_dep(_State, {Name, Vsn}) when is_list(Vsn) -> ?CONSOLE("~s* (package ~s)", [ec_cnv:to_binary(Name), ec_cnv:to_binary(Vsn)]); display_dep(_State, Name) when is_binary(Name) -> ?CONSOLE("~s* (package)", [Name]); -%% git source -display_dep(_State, {Name, Source}) when is_tuple(Source), element(1, Source) =:= git -> - ?CONSOLE("~s* (git source)", [ec_cnv:to_binary(Name)]); -display_dep(_State, {Name, _Vsn, Source}) when is_tuple(Source), element(1, Source) =:= git -> - ?CONSOLE("~s* (git source)", [ec_cnv:to_binary(Name)]); -display_dep(_State, {Name, _Vsn, Source, _Opts}) when is_tuple(Source), element(1, Source) =:= git -> - ?CONSOLE("~s* (git soutce)", [ec_cnv:to_binary(Name)]); -%% unknown source display_dep(_State, {Name, Source}) when is_tuple(Source) -> - ?CONSOLE("~s* (source ~p)", [ec_cnv:to_binary(Name), Source]); + ?CONSOLE("~s* (~s source)", [ec_cnv:to_binary(Name), type(Source)]); display_dep(_State, {Name, _Vsn, Source}) when is_tuple(Source) -> - ?CONSOLE("~s* (source ~p)", [ec_cnv:to_binary(Name), Source]); + ?CONSOLE("~s* (~s source)", [ec_cnv:to_binary(Name), type(Source)]); display_dep(_State, {Name, _Vsn, Source, _Opts}) when is_tuple(Source) -> - ?CONSOLE("~s* (source ~p)", [ec_cnv:to_binary(Name), Source]); + ?CONSOLE("~s* (~s source)", [ec_cnv:to_binary(Name), type(Source)]); %% Locked display_dep(State, {Name, Source={pkg, _, Vsn}, Level}) when is_integer(Level) -> DepsDir = rebar_dir:deps_dir(State), @@ -114,14 +106,6 @@ display_dep(State, {Name, Source={pkg, _, Vsn}, Level}) when is_integer(Level) - false -> "" end, ?CONSOLE("~s~s (locked package ~s)", [Name, NeedsUpdate, Vsn]); -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, State) of - true -> "*"; - false -> "" - end, - ?CONSOLE("~s~s (locked git source)", [Name, NeedsUpdate]); 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)]), @@ -129,4 +113,6 @@ display_dep(State, {Name, Source, Level}) when is_tuple(Source), is_integer(Leve true -> "*"; false -> "" end, - ?CONSOLE("~s~s (locked ~p)", [Name, NeedsUpdate, Source]). + ?CONSOLE("~s~s (locked ~s source)", [Name, NeedsUpdate, type(Source)]). + +type(Source) when is_tuple(Source) -> element(1, Source). diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 49f7ad7..b7a9583 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -42,7 +42,7 @@ erl_to_mod/1, beams/1, find_executable/1, - vcs_vsn/2, + vcs_vsn/3, deprecated/3, deprecated/4, erl_opts/1, @@ -471,8 +471,8 @@ escript_foldl(Fun, Acc, File) -> Error end. -vcs_vsn(Vcs, Dir) -> - case vcs_vsn_cmd(Vcs, Dir) of +vcs_vsn(Vcs, Dir, Resources) -> + case vcs_vsn_cmd(Vcs, Dir, Resources) of {plain, VsnString} -> VsnString; {cmd, CmdString} -> @@ -484,23 +484,48 @@ vcs_vsn(Vcs, Dir) -> end. %% Temp work around for repos like relx that use "semver" -vcs_vsn_cmd(VCS, Dir) when VCS =:= semver ; VCS =:= "semver" -> - rebar_git_resource:make_vsn(Dir); -vcs_vsn_cmd(VCS, Dir) when VCS =:= git ; VCS =:= "git" -> - rebar_git_resource:make_vsn(Dir); -vcs_vsn_cmd(VCS, Dir) when VCS =:= pkg ; VCS =:= "pkg" -> - rebar_pkg_resource:make_vsn(Dir); -vcs_vsn_cmd({cmd, _Cmd}=Custom, _) -> +vcs_vsn_cmd(VCS, Dir, Resources) when VCS =:= semver ; VCS =:= "semver" -> + vcs_vsn_cmd(git, Dir, Resources); +vcs_vsn_cmd({cmd, _Cmd}=Custom, _, _) -> Custom; -vcs_vsn_cmd(Version, _) when is_list(Version) -> - {plain, Version}; -vcs_vsn_cmd(_, _) -> +vcs_vsn_cmd(VCS, Dir, Resources) when is_atom(VCS) -> + case find_resource_module(VCS, Resources) of + {ok, Module} -> + Module:make_vsn(Dir); + {error, _} -> + unknown + end; +vcs_vsn_cmd(VCS, Dir, Resources) when is_list(VCS) -> + try list_to_existing_atom(VCS) of + AVCS -> + case vcs_vsn_cmd(AVCS, Dir, Resources) of + unknown -> {plain, VCS}; + Other -> Other + end + catch + error:badarg -> + {plain, VCS} + end; +vcs_vsn_cmd(_, _, _) -> unknown. vcs_vsn_invoke(Cmd, Dir) -> {ok, VsnString} = rebar_utils:sh(Cmd, [{cd, Dir}, {use_stdout, false}]), string:strip(VsnString, right, $\n). +find_resource_module(Type, Resources) -> + case lists:keyfind(Type, 1, Resources) of + false -> + case code:which(Type) of + non_existing -> + {error, unknown}; + _ -> + {ok, Type} + end; + {Type, Module} -> + {ok, Module} + end. + %% %% Filter a list of erl_opts platform_define options such that only %% those which match the provided architecture regex are returned. -- cgit v1.1