summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFred Hebert <mononcqc@ferd.ca>2015-09-06 17:35:16 -0400
committerFred Hebert <mononcqc@ferd.ca>2015-09-06 17:35:16 -0400
commite1cba5a2a940a8301024fbc459fc66c7b9841419 (patch)
tree81066dd8c3d4e32e79945a6f5f4d7fafae2027b4
parent768889ad177cfb10d3cbace36047fba2f16a1315 (diff)
parentcfd4beb99daf5f0686cb2bd6a2ecda9d8e0fe86c (diff)
Merge pull request #779 from tsloughter/hooks_artifacts
hooks/artifacts are always run/resolved from an app unless at top of umbrella
-rw-r--r--src/rebar_app_discover.erl27
-rw-r--r--src/rebar_app_info.erl18
-rw-r--r--src/rebar_dir.erl18
-rw-r--r--src/rebar_hooks.erl2
-rw-r--r--src/rebar_packages.erl2
-rw-r--r--src/rebar_prv_install_deps.erl2
-rw-r--r--src/rebar_templater.erl3
-rw-r--r--test/rebar_hooks_SUITE.erl22
8 files changed, 58 insertions, 36 deletions
diff --git a/src/rebar_app_discover.erl b/src/rebar_app_discover.erl
index 9d7c62a..4f7442b 100644
--- a/src/rebar_app_discover.erl
+++ b/src/rebar_app_discover.erl
@@ -73,25 +73,29 @@ format_error({missing_module, Module}) ->
io_lib:format("Module defined in app file missing: ~p~n", [Module]).
merge_deps(AppInfo, State) ->
+ %% These steps make sure that hooks and artifacts are run in the context of
+ %% the application they are defined at. If an umbrella structure is used and
+ %% they are deifned at the top level they will instead run in the context of
+ %% the State and at the top level, not as part of an application.
Default = reset_hooks(rebar_state:default(State)),
- C = project_app_config(AppInfo, State),
+ {C, State1} = project_app_config(AppInfo, State),
AppInfo0 = rebar_app_info:update_opts(AppInfo, Default, C),
- CurrentProfiles = rebar_state:current_profiles(State),
+ CurrentProfiles = rebar_state:current_profiles(State1),
Name = rebar_app_info:name(AppInfo0),
%% We reset the opts here to default so no profiles are applied multiple times
- AppInfo1 = rebar_app_info:apply_overrides(rebar_state:get(State, overrides, []), AppInfo0),
+ AppInfo1 = rebar_app_info:apply_overrides(rebar_state:get(State1, overrides, []), AppInfo0),
AppInfo2 = rebar_app_info:apply_profiles(AppInfo1, CurrentProfiles),
%% Will throw an exception if checks fail
rebar_app_info:verify_otp_vsn(AppInfo2),
- State1 = lists:foldl(fun(Profile, StateAcc) ->
+ State2 = lists:foldl(fun(Profile, StateAcc) ->
handle_profile(Profile, Name, AppInfo2, StateAcc)
- end, State, lists:reverse(CurrentProfiles)),
+ end, State1, lists:reverse(CurrentProfiles)),
- {AppInfo2, State1}.
+ {AppInfo2, State2}.
handle_profile(Profile, Name, AppInfo, State) ->
TopParsedDeps = rebar_state:get(State, {parsed_deps, Profile}, {[], []}),
@@ -122,18 +126,17 @@ parse_profile_deps(Profile, Name, Deps, Opts, State) ->
project_app_config(AppInfo, State) ->
C = rebar_config:consult(rebar_app_info:dir(AppInfo)),
Dir = rebar_app_info:dir(AppInfo),
- maybe_reset_hooks(C, Dir, State).
+ Opts = maybe_reset_hooks(Dir, rebar_state:opts(State), State),
+ {C, rebar_state:opts(State, Opts)}.
%% Here we check if the app is at the root of the project.
%% If it is, then drop the hooks from the config so they aren't run twice
-maybe_reset_hooks(C, Dir, State) ->
+maybe_reset_hooks(Dir, Opts, State) ->
case ec_file:real_dir_path(rebar_dir:root_dir(State)) of
Dir ->
- C1 = proplists:delete(provider_hooks, C),
- C2 = proplists:delete(artifacts, C1),
- proplists:delete(post_hooks, proplists:delete(pre_hooks, C2));
+ reset_hooks(Opts);
_ ->
- C
+ Opts
end.
reset_hooks(Opts) ->
diff --git a/src/rebar_app_info.erl b/src/rebar_app_info.erl
index 92706f3..b41880e 100644
--- a/src/rebar_app_info.erl
+++ b/src/rebar_app_info.erl
@@ -412,18 +412,22 @@ verify_otp_vsn(AppInfo) ->
-spec has_all_artifacts(#app_info_t{}) -> true | {false, file:filename()}.
has_all_artifacts(AppInfo) ->
Artifacts = rebar_app_info:get(AppInfo, artifacts, []),
- Dir = dir(AppInfo),
- all(Dir, Artifacts).
+ OutDir = out_dir(AppInfo),
+ Context = [{base_dir, rebar_app_info:get(AppInfo, base_dir, ?DEFAULT_BASE_DIR)}
+ ,{profile_dir, rebar_dir:profile_dir(opts(AppInfo), profiles(AppInfo))}
+ ,{out_dir, OutDir}],
+ all(OutDir, Context, Artifacts).
-all(_, []) ->
+all(_, _, []) ->
true;
-all(Dir, [File|Artifacts]) ->
- case filelib:is_regular(filename:join(Dir, File)) of
+all(Dir, Context, [File|Artifacts]) ->
+ FilePath = filename:join(Dir, rebar_templater:render(File, Context)),
+ case filelib:is_regular(FilePath) of
false ->
- ?DEBUG("Missing artifact ~s", [filename:join(Dir, File)]),
+ ?DEBUG("Missing artifact ~s", [FilePath]),
{false, File};
true ->
- all(Dir, Artifacts)
+ all(Dir, Context, Artifacts)
end.
%%%%%
diff --git a/src/rebar_dir.erl b/src/rebar_dir.erl
index 7aeb11e..7220d89 100644
--- a/src/rebar_dir.erl
+++ b/src/rebar_dir.erl
@@ -29,17 +29,17 @@
-spec base_dir(rebar_state:t()) -> file:filename_all().
base_dir(State) ->
- profile_dir(State, rebar_state:current_profiles(State)).
+ profile_dir(rebar_state:opts(State), rebar_state:current_profiles(State)).
--spec profile_dir(rebar_state:t(), [atom()]) -> file:filename_all().
-profile_dir(State, Profiles) ->
+-spec profile_dir(rebar_dict(), [atom()]) -> file:filename_all().
+profile_dir(Opts, Profiles) ->
{BaseDir, ProfilesStrings} = case [ec_cnv:to_list(P) || P <- Profiles] of
- ["global" | _] -> {?MODULE:global_cache_dir(State), [""]};
- ["bootstrap", "default"] -> {rebar_state:get(State, base_dir, ?DEFAULT_BASE_DIR), ["default"]};
- ["default"] -> {rebar_state:get(State, base_dir, ?DEFAULT_BASE_DIR), ["default"]};
+ ["global" | _] -> {?MODULE:global_cache_dir(Opts), [""]};
+ ["bootstrap", "default"] -> {rebar_opts:get(Opts, base_dir, ?DEFAULT_BASE_DIR), ["default"]};
+ ["default"] -> {rebar_opts:get(Opts, base_dir, ?DEFAULT_BASE_DIR), ["default"]};
%% drop `default` from the profile dir if it's implicit and reverse order
%% of profiles to match order passed to `as`
- ["default"|Rest] -> {rebar_state:get(State, base_dir, ?DEFAULT_BASE_DIR), Rest}
+ ["default"|Rest] -> {rebar_opts:get(Opts, base_dir, ?DEFAULT_BASE_DIR), Rest}
end,
ProfilesDir = string:join(ProfilesStrings, "+"),
filename:join(BaseDir, ProfilesDir).
@@ -91,9 +91,9 @@ global_config() ->
Home = home_dir(),
filename:join([Home, ".config", "rebar3", "rebar.config"]).
-global_cache_dir(State) ->
+global_cache_dir(Opts) ->
Home = home_dir(),
- rebar_state:get(State, global_rebar_dir, filename:join([Home, ".cache", "rebar3"])).
+ rebar_opts:get(Opts, global_rebar_dir, filename:join([Home, ".cache", "rebar3"])).
local_cache_dir(Dir) ->
filename:join(Dir, ".rebar3").
diff --git a/src/rebar_hooks.erl b/src/rebar_hooks.erl
index f17e815..c3b17bb 100644
--- a/src/rebar_hooks.erl
+++ b/src/rebar_hooks.erl
@@ -111,7 +111,7 @@ create_env(State, Opts) ->
{"REBAR_CHECKOUTS_DIR", filename:absname(rebar_dir:checkouts_dir(State))},
{"REBAR_PLUGINS_DIR", filename:absname(rebar_dir:plugins_dir(State))},
{"REBAR_GLOBAL_CONFIG_DIR", filename:absname(rebar_dir:global_config_dir(State))},
- {"REBAR_GLOBAL_CACHE_DIR", filename:absname(rebar_dir:global_cache_dir(State))},
+ {"REBAR_GLOBAL_CACHE_DIR", filename:absname(rebar_dir:global_cache_dir(Opts))},
{"REBAR_TEMPLATE_DIR", filename:absname(rebar_dir:template_dir(State))},
{"REBAR_APP_DIRS", join_dirs(BaseDir, rebar_dir:lib_dirs(State))},
{"REBAR_SRC_DIRS", join_dirs(BaseDir, rebar_dir:all_src_dirs(Opts))},
diff --git a/src/rebar_packages.erl b/src/rebar_packages.erl
index dc111de..7be3372 100644
--- a/src/rebar_packages.erl
+++ b/src/rebar_packages.erl
@@ -60,7 +60,7 @@ deps(Name, Vsn, State) ->
end.
registry_dir(State) ->
- CacheDir = rebar_dir:global_cache_dir(State),
+ CacheDir = rebar_dir:global_cache_dir(rebar_state:opts(State)),
case rebar_state:get(State, rebar_packages_cdn, ?DEFAULT_CDN) of
?DEFAULT_CDN ->
RegistryDir = filename:join([CacheDir, "hex", "default"]),
diff --git a/src/rebar_prv_install_deps.erl b/src/rebar_prv_install_deps.erl
index fd1aa49..6c39423 100644
--- a/src/rebar_prv_install_deps.erl
+++ b/src/rebar_prv_install_deps.erl
@@ -225,7 +225,7 @@ update_dep(AppInfo, Profile, Level, Deps, Apps, State, Upgrade, Seen, Locks) ->
profile_dep_dir(State, Profile) ->
case Profile of
- default -> filename:join([rebar_dir:profile_dir(State, [default]), rebar_state:get(State, deps_dir, ?DEFAULT_DEPS_DIR)]);
+ default -> filename:join([rebar_dir:profile_dir(rebar_state:opts(State), [default]), rebar_state:get(State, deps_dir, ?DEFAULT_DEPS_DIR)]);
_ -> rebar_dir:deps_dir(State)
end.
diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl
index 3aa6e90..c7fb2af 100644
--- a/src/rebar_templater.erl
+++ b/src/rebar_templater.erl
@@ -27,7 +27,8 @@
-module(rebar_templater).
-export([new/4,
- list_templates/1]).
+ list_templates/1,
+ render/2]).
-include("rebar.hrl").
diff --git a/test/rebar_hooks_SUITE.erl b/test/rebar_hooks_SUITE.erl
index 85ca0e5..188fb34 100644
--- a/test/rebar_hooks_SUITE.erl
+++ b/test/rebar_hooks_SUITE.erl
@@ -43,7 +43,12 @@ build_and_clean_app(Config) ->
Vsn = rebar_test_utils:create_random_vsn(),
rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name, valid}]}),
- rebar_test_utils:run_and_check(Config, [{provider_hooks, [{post, [{compile, clean}]}]}],
+ RConfFile =
+ rebar_test_utils:create_config(AppDir,
+ [{provider_hooks, [{post, [{compile, clean}]}]}]),
+ {ok, RConf} = file:consult(RConfFile),
+
+ rebar_test_utils:run_and_check(Config, RConf,
["compile"], {ok, [{app, Name, invalid}]}).
escriptize_artifacts(Config) ->
@@ -53,7 +58,7 @@ escriptize_artifacts(Config) ->
Vsn = rebar_test_utils:create_random_vsn(),
rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
- Artifact = "bin/"++Name,
+ Artifact = "{{profile_dir}}/bin/"++Name,
RConfFile =
rebar_test_utils:create_config(AppDir,
[
@@ -69,9 +74,18 @@ escriptize_artifacts(Config) ->
{missing_artifact, Artifact}}} ->
ok
end,
- rebar_test_utils:run_and_check(Config, RConf++[{provider_hooks, [{post, [{compile, escriptize}]}]}],
+ RConfFile1 =
+ rebar_test_utils:create_config(AppDir,
+ [
+ {escript_name, list_to_atom(Name)}
+ ,{artifacts, [Artifact]}
+ ,{provider_hooks, [{post, [{compile, escriptize}]}]}
+ ]),
+ {ok, RConf1} = file:consult(RConfFile1),
+
+ rebar_test_utils:run_and_check(Config, RConf1,
["compile"], {ok, [{app, Name, valid}
- ,{file, filename:join([AppDir, "_build/default/", Artifact])}]}).
+ ,{file, filename:join([AppDir, "_build/default/bin", Name])}]}).
run_hooks_once(Config) ->
AppDir = ?config(apps, Config),