From 6c9bc2c1d5e7041f7e81cc470b6de78220ea9eb1 Mon Sep 17 00:00:00 2001 From: Francisco Rojas Date: Tue, 15 Mar 2016 21:51:39 -0700 Subject: Allows overwrite default cache dir using REBAR_CACHE_DIR Allows overwrite the default cache directory using the environment variable REBAR_CACHE_DIR. --- src/rebar_dir.erl | 10 ++++++++-- test/rebar_dir_SUITE.erl | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/rebar_dir.erl b/src/rebar_dir.erl index 3729704..840d1f9 100644 --- a/src/rebar_dir.erl +++ b/src/rebar_dir.erl @@ -93,8 +93,14 @@ global_config() -> filename:join([Home, ".config", "rebar3", "rebar.config"]). global_cache_dir(Opts) -> - Home = home_dir(), - rebar_opts:get(Opts, global_rebar_dir, filename:join([Home, ".cache", "rebar3"])). + RebarCacheDir = case os:getenv("REBAR_CACHE_DIR") of + false -> + Home = home_dir(), + filename:join([Home, ".cache", "rebar3"]); + ConfigFile -> + ConfigFile + end, + rebar_opts:get(Opts, global_rebar_dir, RebarCacheDir). local_cache_dir(Dir) -> filename:join(Dir, ".rebar3"). diff --git a/test/rebar_dir_SUITE.erl b/test/rebar_dir_SUITE.erl index 1221db7..8194464 100644 --- a/test/rebar_dir_SUITE.erl +++ b/test/rebar_dir_SUITE.erl @@ -6,6 +6,7 @@ -export([src_dirs/1, extra_src_dirs/1, all_src_dirs/1]). -export([profile_src_dirs/1, profile_extra_src_dirs/1, profile_all_src_dirs/1]). -export([retarget_path/1, alt_base_dir_abs/1, alt_base_dir_rel/1]). +-export([global_cache_dir/1, default_global_cache_dir/1, overwrite_default_global_cache_dir/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -15,8 +16,19 @@ all() -> [default_src_dirs, default_extra_src_dirs, default_all_src_dirs, src_dirs, extra_src_dirs, all_src_dirs, profile_src_dirs, profile_extra_src_dirs, profile_all_src_dirs, - retarget_path, alt_base_dir_abs, alt_base_dir_rel]. - + retarget_path, alt_base_dir_abs, alt_base_dir_rel, global_cache_dir, + default_global_cache_dir, overwrite_default_global_cache_dir]. + +init_per_testcase(default_global_cache_dir, Config) -> + [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, _State} | Config] = rebar_test_utils:init_rebar_state(Config), + NewState = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])} + ,{root_dir, AppsDir}]), + [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, NewState} | Config]; +init_per_testcase(overwrite_default_global_cache_dir, Config) -> + [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, _State} | Config] = rebar_test_utils:init_rebar_state(Config), + NewState = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])} + ,{root_dir, AppsDir}]), + [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, NewState} | Config]; init_per_testcase(_, Config) -> C = rebar_test_utils:init_rebar_state(Config), AppDir = ?config(apps, C), @@ -162,3 +174,23 @@ alt_base_dir_rel(Config) -> ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name2, "ebin"]))), ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".app"]))), ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".beam"]))). + +global_cache_dir(Config) -> + RebarConfig = [{erl_opts, []}], + {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return), + DataDir = ?config(priv_dir, Config), + Expected = filename:join([DataDir, "cache"]), + ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))). + +default_global_cache_dir(Config) -> + RebarConfig = [{erl_opts, []}], + {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return), + Expected = filename:join([rebar_dir:home_dir(), ".cache", "rebar3"]), + ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))). + +overwrite_default_global_cache_dir(Config) -> + os:putenv("REBAR_CACHE_DIR", "/tmp/"), + RebarConfig = [{erl_opts, []}], + {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return), + Expected = "/tmp/", + ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))). -- cgit v1.1 From 8b1193e4af7a43abe84a108435b9a2c52e475f8f Mon Sep 17 00:00:00 2001 From: Francisco Rojas Date: Thu, 17 Mar 2016 20:07:36 -0700 Subject: Add spec for global_cache_dir --- src/rebar_dir.erl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rebar_dir.erl b/src/rebar_dir.erl index 840d1f9..df05cdb 100644 --- a/src/rebar_dir.erl +++ b/src/rebar_dir.erl @@ -92,6 +92,7 @@ global_config() -> Home = home_dir(), filename:join([Home, ".config", "rebar3", "rebar.config"]). +-spec global_cache_dir(rebar_dict()) -> file:filename_all(). global_cache_dir(Opts) -> RebarCacheDir = case os:getenv("REBAR_CACHE_DIR") of false -> -- cgit v1.1 From 2d1d251add285c6296e0ce1c12e2a9a961c2209b Mon Sep 17 00:00:00 2001 From: Francisco Rojas Date: Thu, 17 Mar 2016 20:09:44 -0700 Subject: use priv_dir in rebar_dir test --- test/rebar_dir_SUITE.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/rebar_dir_SUITE.erl b/test/rebar_dir_SUITE.erl index 8194464..f287d5f 100644 --- a/test/rebar_dir_SUITE.erl +++ b/test/rebar_dir_SUITE.erl @@ -189,8 +189,8 @@ default_global_cache_dir(Config) -> ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))). overwrite_default_global_cache_dir(Config) -> - os:putenv("REBAR_CACHE_DIR", "/tmp/"), + os:putenv("REBAR_CACHE_DIR", ?config(priv_dir, Config)), RebarConfig = [{erl_opts, []}], {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return), - Expected = "/tmp/", + Expected = ?config(priv_dir, Config), ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))). -- cgit v1.1 From 243e94391a4ac446cfe0c814750abfb014db9ac4 Mon Sep 17 00:00:00 2001 From: Francisco Rojas Date: Wed, 6 Apr 2016 21:16:25 -0700 Subject: Read env var REBAR_CACHE_DIR on rebar3 only once Instead of reading every time that rebar_dir:global_cache_dir/1 is called --- src/rebar3.erl | 13 ++++++++++--- src/rebar_dir.erl | 10 ++-------- test/rebar_dir_SUITE.erl | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/rebar3.erl b/src/rebar3.erl index c1a1ae4..ff0ab6a 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -129,13 +129,20 @@ run_aux(State, RawArgs) -> %% Initializing project_plugins which can override default providers State6 = rebar_plugins:project_plugins_install(State5), State7 = rebar_plugins:top_level_install(State6), - State8 = rebar_state:default(State7, rebar_state:opts(State7)), + State8 = case os:getenv("REBAR_CACHE_DIR") of + false -> + State7; + ConfigFile -> + rebar_state:set(State7, global_rebar_dir, ConfigFile) + end, + + State9 = rebar_state:default(State8, rebar_state:opts(State8)), {Task, Args} = parse_args(RawArgs), - State9 = rebar_state:code_paths(State8, default, code:get_path()), + State10 = rebar_state:code_paths(State9, default, code:get_path()), - rebar_core:init_command(rebar_state:command_args(State9, Args), Task). + rebar_core:init_command(rebar_state:command_args(State10, Args), Task). init_config() -> %% Initialize logging system diff --git a/src/rebar_dir.erl b/src/rebar_dir.erl index df05cdb..1ec58d4 100644 --- a/src/rebar_dir.erl +++ b/src/rebar_dir.erl @@ -94,14 +94,8 @@ global_config() -> -spec global_cache_dir(rebar_dict()) -> file:filename_all(). global_cache_dir(Opts) -> - RebarCacheDir = case os:getenv("REBAR_CACHE_DIR") of - false -> - Home = home_dir(), - filename:join([Home, ".cache", "rebar3"]); - ConfigFile -> - ConfigFile - end, - rebar_opts:get(Opts, global_rebar_dir, RebarCacheDir). + Home = home_dir(), + rebar_opts:get(Opts, global_rebar_dir, filename:join([Home, ".cache", "rebar3"])). local_cache_dir(Dir) -> filename:join(Dir, ".rebar3"). diff --git a/test/rebar_dir_SUITE.erl b/test/rebar_dir_SUITE.erl index f287d5f..9734830 100644 --- a/test/rebar_dir_SUITE.erl +++ b/test/rebar_dir_SUITE.erl @@ -25,6 +25,7 @@ init_per_testcase(default_global_cache_dir, Config) -> ,{root_dir, AppsDir}]), [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, NewState} | Config]; init_per_testcase(overwrite_default_global_cache_dir, Config) -> + os:putenv("REBAR_CACHE_DIR", ?config(priv_dir, Config)), [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, _State} | Config] = rebar_test_utils:init_rebar_state(Config), NewState = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])} ,{root_dir, AppsDir}]), @@ -189,7 +190,6 @@ default_global_cache_dir(Config) -> ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))). overwrite_default_global_cache_dir(Config) -> - os:putenv("REBAR_CACHE_DIR", ?config(priv_dir, Config)), RebarConfig = [{erl_opts, []}], {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return), Expected = ?config(priv_dir, Config), -- cgit v1.1