From 0a4509f5b05ba069a03ddb1acf4e61ad1498afc1 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Mon, 24 Aug 2015 09:00:21 -0500 Subject: simplify package dep parsing and handling --- src/rebar_app_discover.erl | 2 +- src/rebar_app_info.erl | 16 ++++++- src/rebar_app_utils.erl | 90 +++++++++------------------------------ src/rebar_prv_install_deps.erl | 26 +++++------ src/rebar_prv_plugins_upgrade.erl | 2 +- src/rebar_state.erl | 44 +++++++++++++------ 6 files changed, 77 insertions(+), 103 deletions(-) diff --git a/src/rebar_app_discover.erl b/src/rebar_app_discover.erl index 2b1c767..5a25a9e 100644 --- a/src/rebar_app_discover.erl +++ b/src/rebar_app_discover.erl @@ -198,7 +198,7 @@ create_app_info(AppDir, AppFile) -> AppVsn = proplists:get_value(vsn, AppDetails), Applications = proplists:get_value(applications, AppDetails, []), IncludedApplications = proplists:get_value(included_applications, AppDetails, []), - {ok, AppInfo} = rebar_app_info:new(AppName, AppVsn, AppDir, []), + {ok, AppInfo} = rebar_app_info:new(AppName, AppVsn, AppDir), AppInfo1 = rebar_app_info:applications( rebar_app_info:app_details(AppInfo, AppDetails), IncludedApplications++Applications), diff --git a/src/rebar_app_info.erl b/src/rebar_app_info.erl index bb99584..bb5104e 100644 --- a/src/rebar_app_info.erl +++ b/src/rebar_app_info.erl @@ -4,6 +4,7 @@ new/2, new/3, new/4, + new/5, discover/1, name/1, name/2, @@ -58,7 +59,7 @@ app_file :: file:filename_all() | undefined, config :: rebar_state:t() | undefined, original_vsn :: binary() | string() | undefined, - parent :: binary() | root, + parent=root :: binary() | root, app_details=[] :: list(), applications=[] :: list(), deps=[] :: list(), @@ -113,6 +114,17 @@ new(AppName, Vsn, Dir, Deps) -> out_dir=ec_cnv:to_list(Dir), deps=Deps}}. +%% @doc build a complete version of the app info with all fields set. +-spec new(atom() | binary(), atom() | binary() | string(), binary() | string(), file:name(), list()) -> + {ok, t()}. +new(Parent, AppName, Vsn, Dir, Deps) -> + {ok, #app_info_t{name=ec_cnv:to_binary(AppName), + parent=Parent, + original_vsn=Vsn, + dir=ec_cnv:to_list(Dir), + out_dir=ec_cnv:to_list(Dir), + deps=Deps}}. + %% @doc discover a complete version of the app info with all fields set. -spec discover(file:filename_all()) -> {ok, t()} | not_found. discover(Dir) -> @@ -305,7 +317,7 @@ state(#app_info_t{state=State}) -> state_or_new(State, AppInfo=#app_info_t{state=undefined}) -> AppDir = dir(AppInfo), C = rebar_config:consult(AppDir), - rebar_state:new(State, C, AppDir); + rebar_state:new(State, C, AppInfo); state_or_new(_State, #app_info_t{state=State}) -> State. diff --git a/src/rebar_app_utils.erl b/src/rebar_app_utils.erl index 693ad1c..88b75ac 100644 --- a/src/rebar_app_utils.erl +++ b/src/rebar_app_utils.erl @@ -118,59 +118,20 @@ parse_dep(Dep, Parent, DepsDir, State, Locks, Level) -> end. parse_dep(Parent, {Name, Vsn, {pkg, PkgName}}, DepsDir, IsLock, State) -> - %% Versioned Package dependency with different package name from app name - CheckoutsDir = ec_cnv:to_list(rebar_dir:checkouts_dir(State, PkgName)), - case rebar_app_info:discover(CheckoutsDir) of - {ok, _App} -> - dep_to_app(root, DepsDir, Name, [], [], IsLock, State); - not_found -> - {PkgName1, PkgVsn} = parse_goal(ec_cnv:to_binary(PkgName) - ,ec_cnv:to_binary(Vsn)), - %% Verify package actually exists. This will throw a missing_package exception - rebar_packages:deps(PkgName1, PkgVsn, State), - Source = {pkg, PkgName1, PkgVsn}, - rebar_app_info:resource_type(dep_to_app(Parent, DepsDir, Name, PkgVsn, Source, IsLock, State), pkg) - end; + {PkgName1, PkgVsn} = parse_goal(ec_cnv:to_binary(PkgName), ec_cnv:to_binary(Vsn)), + pkg_to_app(Parent, DepsDir, Name, PkgName1, PkgVsn, IsLock, State); parse_dep(Parent, {Name, {pkg, PkgName}}, DepsDir, IsLock, State) -> %% Package dependency with different package name from app name {PkgName1, PkgVsn} = get_package(ec_cnv:to_binary(PkgName), State), - CheckoutsDir = ec_cnv:to_list(rebar_dir:checkouts_dir(State, PkgName1)), - case rebar_app_info:discover(CheckoutsDir) of - {ok, _App} -> - dep_to_app(root, DepsDir, Name, [], [], IsLock, State); - not_found -> - %% Verify package actually exists. This will throw a missing_package exception - rebar_packages:deps(PkgName1, PkgVsn, State), - Source = {pkg, PkgName1, PkgVsn}, - rebar_app_info:resource_type(dep_to_app(Parent, DepsDir, Name, PkgVsn, Source, IsLock, State), pkg) - end; + pkg_to_app(Parent, DepsDir, Name, PkgName1, PkgVsn, IsLock, State); parse_dep(Parent, {Name, Vsn}, DepsDir, IsLock, State) when is_list(Vsn); is_binary(Vsn) -> %% Versioned Package dependency - CheckoutsDir = ec_cnv:to_list(rebar_dir:checkouts_dir(State, Name)), - case rebar_app_info:discover(CheckoutsDir) of - {ok, _App} -> - dep_to_app(root, DepsDir, Name, [], [], IsLock, State); - not_found -> - {PkgName, PkgVsn} = parse_goal(ec_cnv:to_binary(Name) - ,ec_cnv:to_binary(Vsn)), - %% Verify package actually exists. This will throw a missing_package exception - rebar_packages:deps(PkgName, PkgVsn, State), - Source = {pkg, PkgName, PkgVsn}, - rebar_app_info:resource_type(dep_to_app(Parent, DepsDir, PkgName, PkgVsn, Source, IsLock, State), pkg) - end; + {PkgName, PkgVsn} = parse_goal(ec_cnv:to_binary(Name), ec_cnv:to_binary(Vsn)), + pkg_to_app(Parent, DepsDir, PkgName, PkgName, PkgVsn, IsLock, State); parse_dep(Parent, Name, DepsDir, IsLock, State) when is_atom(Name); is_binary(Name) -> %% Unversioned package dependency {PkgName, PkgVsn} = get_package(ec_cnv:to_binary(Name), State), - CheckoutsDir = ec_cnv:to_list(rebar_dir:checkouts_dir(State, Name)), - case rebar_app_info:discover(CheckoutsDir) of - {ok, _App} -> - dep_to_app(root, DepsDir, Name, [], [], IsLock, State); - not_found -> - %% Verify package actually exists. This will throw a missing_package exception - rebar_packages:deps(PkgName, PkgVsn, State), - Source = {pkg, PkgName, PkgVsn}, - rebar_app_info:resource_type(dep_to_app(Parent, DepsDir, PkgName, PkgVsn, Source, IsLock, State), pkg) - end; + pkg_to_app(Parent, DepsDir, PkgName, PkgName, PkgVsn, IsLock, State); parse_dep(Parent, {Name, Source}, DepsDir, IsLock, State) when is_tuple(Source) -> dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State); parse_dep(Parent, {Name, _Vsn, Source}, DepsDir, IsLock, State) when is_tuple(Source) -> @@ -179,26 +140,25 @@ parse_dep(Parent, {Name, _Vsn, Source, Opts}, DepsDir, IsLock, State) when is_tu ?WARN("Dependency option list ~p in ~p is not supported and will be ignored", [Opts, Name]), dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State); parse_dep(Parent, {Name, {pkg, PkgName, Vsn}, Level}, DepsDir, IsLock, State) when is_integer(Level) -> - CheckoutsDir = ec_cnv:to_list(rebar_dir:checkouts_dir(State, PkgName)), - case rebar_app_info:discover(CheckoutsDir) of - {ok, _App} -> - dep_to_app(root, DepsDir, Name, [], [], IsLock, State); - not_found -> - %% Verify package actually exists. This will throw a missing_package exception - rebar_packages:deps(PkgName, Vsn, State), - Source = {pkg, PkgName, Vsn}, - rebar_app_info:resource_type(dep_to_app(Parent, DepsDir, Name, Vsn, Source, IsLock, State), pkg) - end; + pkg_to_app(Parent, DepsDir, Name, PkgName, Vsn, IsLock, State); parse_dep(Parent, {Name, Source, Level}, DepsDir, IsLock, State) when is_tuple(Source) , is_integer(Level) -> dep_to_app(Parent, DepsDir, Name, [], Source, IsLock, State); parse_dep(_, Dep, _, _, _) -> throw(?PRV_ERROR({parse_dep, Dep})). +%% Verify package exists and create the AppInfo record +pkg_to_app(Parent, DepsDir, AppName, PkgName, PkgVsn, IsLock, State) -> + %% Verify package actually exists. This will throw a missing_package exception + Deps = rebar_packages:deps(PkgName, PkgVsn, State), + Source = {pkg, PkgName, PkgVsn}, + AppInfo = dep_to_app(Parent, DepsDir, AppName, PkgVsn, Source, IsLock, State), + rebar_app_info:resource_type(rebar_app_info:deps(AppInfo, Deps), pkg). + dep_to_app(Parent, DepsDir, Name, Vsn, Source, IsLock, State) -> CheckoutsDir = ec_cnv:to_list(rebar_dir:checkouts_dir(State, Name)), BaseDir = rebar_state:get(State, base_dir, []), - {ok, Dep} = case rebar_app_info:discover(CheckoutsDir) of + {ok, App1} = case rebar_app_info:discover(CheckoutsDir) of {ok, App} -> {ok, rebar_app_info:is_checkout(App, true)}; not_found -> @@ -207,24 +167,16 @@ dep_to_app(Parent, DepsDir, Name, Vsn, Source, IsLock, State) -> {ok, App} -> {ok, App}; not_found -> - rebar_app_info:new(Name, Vsn, - ec_cnv:to_list(filename:join(DepsDir, Name))) + rebar_app_info:new(Parent, Name, Vsn, Dir, []) end end, - C = rebar_config:consult(rebar_app_info:dir(Dep)), - S = rebar_state:new(rebar_state:new(), C, rebar_app_info:dir(Dep)), + C = rebar_config:consult(rebar_app_info:dir(App1)), + S = rebar_state:new(rebar_state:new(), C, App1), Overrides = rebar_state:get(State, overrides, []), ParentOverrides = rebar_state:overrides(State), S1 = rebar_state:set(rebar_state:overrides(S, ParentOverrides++Overrides), base_dir, BaseDir), - Dep1 = rebar_app_info:state(Dep, S1), - AppInfo = rebar_app_info:is_lock(rebar_app_info:source(Dep1, Source), IsLock), - ResourceType = case Source of - {pkg, _, _} -> - pkg; - _ -> - src - end, - rebar_app_info:resource_type(rebar_app_info:parent(AppInfo, Parent), ResourceType). + App2 = rebar_app_info:state(App1, S1), + rebar_app_info:is_lock(rebar_app_info:source(App2, Source), IsLock). format_error({missing_package, Package}) -> io_lib:format("Package not found in registry: ~s", [Package]); diff --git a/src/rebar_prv_install_deps.erl b/src/rebar_prv_install_deps.erl index 4dfe1e2..2d725a4 100644 --- a/src/rebar_prv_install_deps.erl +++ b/src/rebar_prv_install_deps.erl @@ -255,7 +255,7 @@ handle_dep(State, Profile, DepsDir, AppInfo, Locks, Level) -> %% Deps may be under a sub project app, find it and use its state if so S = rebar_app_info:state(AppInfo), C = rebar_config:consult(rebar_app_info:dir(AppInfo)), - S1 = rebar_state:new(S, C, rebar_app_info:dir(AppInfo)), + S1 = rebar_state:new(S, C, AppInfo), S2 = rebar_state:apply_overrides(S1, Name), S3 = rebar_state:apply_profiles(S2, Profiles), @@ -270,18 +270,10 @@ handle_dep(State, Profile, DepsDir, AppInfo, Locks, Level) -> AppInfo1 = rebar_app_info:state(AppInfo, S5), %% Upgrade lock level to be the level the dep will have in this dep tree - case rebar_app_info:resource_type(AppInfo1) of - pkg -> - {pkg, PkgName, PkgVsn} = rebar_app_info:source(AppInfo1), - NewDeps = rebar_packages:deps(PkgName, PkgVsn, S5), - NewDeps1 = rebar_app_utils:parse_deps(Name, DepsDir, NewDeps, S5, Locks, Level+1), - {rebar_app_info:deps(AppInfo1, NewDeps), NewDeps1, State}; - _ -> - Deps = rebar_state:get(S5, {deps, default}, []), - AppInfo2 = rebar_app_info:deps(AppInfo1, rebar_state:deps_names(Deps)), - Deps1 = rebar_app_utils:parse_deps(Name, DepsDir, Deps, S5, Locks, Level+1), - {AppInfo2, Deps1, State} - end. + Deps = rebar_state:get(S5, {deps, default}, []), + AppInfo2 = rebar_app_info:deps(AppInfo1, rebar_state:deps_names(Deps)), + Deps1 = rebar_app_utils:parse_deps(Name, DepsDir, Deps, S5, Locks, Level+1), + {AppInfo2, Deps1, State}. -spec maybe_fetch(rebar_app_info:t(), atom(), boolean(), sets:set(binary()), rebar_state:t()) -> {boolean(), rebar_app_info:t()}. @@ -378,13 +370,15 @@ update_app_info(AppDir, AppInfo) -> rebar_app_info:valid(AppInfo2, undefined). copy_app_info(OldAppInfo, NewAppInfo) -> + Deps = rebar_app_info:deps(OldAppInfo), ResourceType = rebar_app_info:resource_type(OldAppInfo), Parent = rebar_app_info:parent(OldAppInfo), Source = rebar_app_info:source(OldAppInfo), - rebar_app_info:resource_type( - rebar_app_info:source( - rebar_app_info:parent(NewAppInfo, Parent), Source), ResourceType). + rebar_app_info:deps( + rebar_app_info:resource_type( + rebar_app_info:source( + rebar_app_info:parent(NewAppInfo, Parent), Source), ResourceType), Deps). maybe_upgrade(AppInfo, AppDir, Upgrade, State) -> Source = rebar_app_info:source(AppInfo), diff --git a/src/rebar_prv_plugins_upgrade.erl b/src/rebar_prv_plugins_upgrade.erl index dfc9990..fabfa5b 100644 --- a/src/rebar_prv_plugins_upgrade.erl +++ b/src/rebar_prv_plugins_upgrade.erl @@ -91,5 +91,5 @@ build_plugin(AppInfo, Apps, State) -> Providers = rebar_state:providers(State), AppDir = rebar_app_info:dir(AppInfo), C = rebar_config:consult(AppDir), - S = rebar_state:new(rebar_state:all_deps(rebar_state:new(), Apps), C, AppDir), + S = rebar_state:new(rebar_state:all_deps(rebar_state:new(), Apps), C, AppInfo), rebar_prv_compile:compile(S, Providers, AppInfo). diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 6feae81..e31b01b 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -97,21 +97,26 @@ new(ParentState=#state_t{}, Config) -> Dir = rebar_dir:get_cwd(), new(ParentState, Config, Dir). --spec new(t(), list(), file:name()) -> t(). -new(ParentState, Config, Dir) -> +-spec new(t(), list(), rebar_app_info:t() | file:filename_all()) -> t(). +new(ParentState, Config, Dir) when is_list(Dir) -> + new(ParentState, Config, deps_from_config(Dir, Config), Dir); +new(ParentState, Config, AppInfo) -> + Dir = rebar_app_info:dir(AppInfo), + DepLocks = case rebar_app_info:resource_type(AppInfo) of + pkg -> + Deps = rebar_app_info:deps(AppInfo), + [{{locks, default}, Deps}, {{deps, default}, Deps}]; + _ -> + deps_from_config(Dir, Config) + end, + new(ParentState, Config, DepLocks, Dir). + +new(ParentState, Config, Deps, Dir) -> Opts = ParentState#state_t.opts, - LocalOpts = case rebar_config:consult_lock_file(filename:join(Dir, ?LOCK_FILE)) of - [D] -> - %% We want the top level deps only from the lock file. - %% This ensures deterministic overrides for configs. - Deps = [X || X <- D, element(3, X) =:= 0], - Plugins = proplists:get_value(plugins, Config, []), - Terms = [{{locks, default}, D}, {{deps, default}, Deps}, {{plugins, default}, Plugins} | Config], - true = rebar_config:verify_config_format(Terms), - dict:from_list(Terms); - _ -> - base_opts(Config) - end, + Plugins = proplists:get_value(plugins, Config, []), + Terms = Deps++[{{plugins, default}, Plugins} | Config], + true = rebar_config:verify_config_format(Terms), + LocalOpts = dict:from_list(Terms), NewOpts = merge_opts(LocalOpts, Opts), @@ -119,6 +124,17 @@ new(ParentState, Config, Dir) -> ,opts=NewOpts ,default=NewOpts}. +deps_from_config(Dir, Config) -> + case rebar_config:consult_lock_file(filename:join(Dir, ?LOCK_FILE)) of + [D] -> + %% We want the top level deps only from the lock file. + %% This ensures deterministic overrides for configs. + Deps = [X || X <- D, element(3, X) =:= 0], + [{{locks, default}, D}, {{deps, default}, Deps}]; + _ -> + [{{deps, default}, proplists:get_value(deps, Config, [])}] + end. + base_state() -> case application:get_env(rebar, resources) of undefined -> -- cgit v1.1