From 950161357fe96816d1cd3515d232de70b968b15d Mon Sep 17 00:00:00 2001 From: Carl-Johan Kjellander Date: Fri, 19 Jan 2018 11:43:11 +0100 Subject: export env expansion --- src/rebar_utils.erl | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index b633760..9bbe54e 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -453,6 +453,24 @@ reread_config(ConfigList) -> "and will be ignored.", []) end. +%% @doc Given env. variable `FOO' we want to expand all references to +%% it in `InStr'. References can have two forms: `$FOO' and `${FOO}' +%% The end of form `$FOO' is delimited with whitespace or EOL +-spec expand_env_variable(string(), string(), term()) -> string(). +expand_env_variable(InStr, VarName, RawVarValue) -> + case rebar_string:chr(InStr, $$) of + 0 -> + %% No variables to expand + InStr; + _ -> + ReOpts = [global, unicode, {return, list}], + VarValue = re:replace(RawVarValue, "\\\\", "\\\\\\\\", ReOpts), + %% Use a regex to match/replace: + %% Given variable "FOO": match $FOO\s | $FOOeol | ${FOO} + RegEx = io_lib:format("\\\$(~ts(\\W|$)|{~ts})", [VarName, VarName]), + re:replace(InStr, RegEx, [VarValue, "\\2"], ReOpts) + end. + %% ==================================================================== %% Internal functions %% ==================================================================== @@ -523,24 +541,6 @@ patch_on_windows(Cmd, Env) -> Cmd end. -%% @doc Given env. variable `FOO' we want to expand all references to -%% it in `InStr'. References can have two forms: `$FOO' and `${FOO}' -%% The end of form `$FOO' is delimited with whitespace or EOL --spec expand_env_variable(string(), string(), term()) -> string(). -expand_env_variable(InStr, VarName, RawVarValue) -> - case rebar_string:chr(InStr, $$) of - 0 -> - %% No variables to expand - InStr; - _ -> - ReOpts = [global, unicode, {return, list}], - VarValue = re:replace(RawVarValue, "\\\\", "\\\\\\\\", ReOpts), - %% Use a regex to match/replace: - %% Given variable "FOO": match $FOO\s | $FOOeol | ${FOO} - RegEx = io_lib:format("\\\$(~ts(\\W|$)|{~ts})", [VarName, VarName]), - re:replace(InStr, RegEx, [VarValue, "\\2"], ReOpts) - end. - expand_sh_flag(return_on_error) -> {error_handler, fun(_Command, Err) -> -- cgit v1.1 From dd9680fe42b42774190c2e95e56d052080f2e939 Mon Sep 17 00:00:00 2001 From: Carl-Johan Kjellander Date: Fri, 23 Feb 2018 15:24:55 +0100 Subject: move and export create_env for use by providers --- src/rebar_env.erl | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rebar_hooks.erl | 58 +--------------------------------------------- 2 files changed, 67 insertions(+), 57 deletions(-) create mode 100644 src/rebar_env.erl (limited to 'src') diff --git a/src/rebar_env.erl b/src/rebar_env.erl new file mode 100644 index 0000000..dc12419 --- /dev/null +++ b/src/rebar_env.erl @@ -0,0 +1,66 @@ +-module(rebar_env). + +-export([create_env/2]). + +-include("rebar.hrl"). + +%% @doc The following environment variables are exported when running +%% a hook (absolute paths): +%% +%% REBAR_DEPS_DIR = rebar_dir:deps_dir/1 +%% REBAR_BUILD_DIR = rebar_dir:base_dir/1 +%% REBAR_ROOT_DIR = rebar_dir:root_dir/1 +%% REBAR_CHECKOUTS_DIR = rebar_dir:checkouts_dir/1 +%% REBAR_PLUGINS_DIR = rebar_dir:plugins_dir/1 +%% REBAR_GLOBAL_CONFIG_DIR = rebar_dir:global_config_dir/1 +%% REBAR_GLOBAL_CACHE_DIR = rebar_dir:global_cache_dir/1 +%% REBAR_TEMPLATE_DIR = rebar_dir:template_dir/1 +%% REBAR_APP_DIRS = rebar_dir:lib_dirs/1 +%% REBAR_SRC_DIRS = rebar_dir:src_dirs/1 +%% +%% autoconf compatible variables +%% (see: http://www.gnu.org/software/autoconf/manual/autoconf.html#Erlang-Libraries): +%% ERLANG_ERTS_VER = erlang:system_info(version) +%% ERLANG_ROOT_DIR = code:root_dir/0 +%% ERLANG_LIB_DIR_erl_interface = code:lib_dir(erl_interface) +%% ERLANG_LIB_VER_erl_interface = version part of path returned by code:lib_dir(erl_interface) +%% ERL = ERLANG_ROOT_DIR/bin/erl +%% ERLC = ERLANG_ROOT_DIR/bin/erl +%% +-spec create_env(rebar_state:t(), rebar_dict()) -> proplists:proplist(). +create_env(State, Opts) -> + BaseDir = rebar_dir:base_dir(State), + [ + {"REBAR_DEPS_DIR", filename:absname(rebar_dir:deps_dir(State))}, + {"REBAR_BUILD_DIR", filename:absname(rebar_dir:base_dir(State))}, + {"REBAR_ROOT_DIR", filename:absname(rebar_dir:root_dir(State))}, + {"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(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))}, + {"ERLANG_ERTS_VER", erlang:system_info(version)}, + {"ERLANG_ROOT_DIR", code:root_dir()}, + {"ERLANG_LIB_DIR_erl_interface", code:lib_dir(erl_interface)}, + {"ERLANG_LIB_VER_erl_interface", re_version(code:lib_dir(erl_interface))}, + {"ERL", filename:join([code:root_dir(), "bin", "erl"])}, + {"ERLC", filename:join([code:root_dir(), "bin", "erlc"])}, + {"ERLANG_ARCH" , rebar_api:wordsize()}, + {"ERLANG_TARGET", rebar_api:get_arch()} + + ]. + +%% ==================================================================== +%% Internal functions +%% ==================================================================== + +join_dirs(BaseDir, Dirs) -> + rebar_string:join([filename:join(BaseDir, Dir) || Dir <- Dirs], ":"). + +re_version(Path) -> + case re:run(Path, "^.*-(?[^/-]*)$", [{capture,[1],list}, unicode]) of + nomatch -> ""; + {match, [Ver]} -> Ver + end. diff --git a/src/rebar_hooks.erl b/src/rebar_hooks.erl index 8893f2a..ec6fe31 100644 --- a/src/rebar_hooks.erl +++ b/src/rebar_hooks.erl @@ -61,29 +61,6 @@ format_error({bad_provider, Type, Command, {Name, Namespace}}) -> format_error({bad_provider, Type, Command, Name}) -> io_lib:format("Unable to run ~ts hooks for '~p', command '~p' not found.", [Type, Command, Name]). -%% @doc The following environment variables are exported when running -%% a hook (absolute paths): -%% -%% REBAR_DEPS_DIR = rebar_dir:deps_dir/1 -%% REBAR_BUILD_DIR = rebar_dir:base_dir/1 -%% REBAR_ROOT_DIR = rebar_dir:root_dir/1 -%% REBAR_CHECKOUTS_DIR = rebar_dir:checkouts_dir/1 -%% REBAR_PLUGINS_DIR = rebar_dir:plugins_dir/1 -%% REBAR_GLOBAL_CONFIG_DIR = rebar_dir:global_config_dir/1 -%% REBAR_GLOBAL_CACHE_DIR = rebar_dir:global_cache_dir/1 -%% REBAR_TEMPLATE_DIR = rebar_dir:template_dir/1 -%% REBAR_APP_DIRS = rebar_dir:lib_dirs/1 -%% REBAR_SRC_DIRS = rebar_dir:src_dirs/1 -%% -%% autoconf compatible variables -%% (see: http://www.gnu.org/software/autoconf/manual/autoconf.html#Erlang-Libraries): -%% ERLANG_ERTS_VER = erlang:system_info(version) -%% ERLANG_ROOT_DIR = code:root_dir/0 -%% ERLANG_LIB_DIR_erl_interface = code:lib_dir(erl_interface) -%% ERLANG_LIB_VER_erl_interface = version part of path returned by code:lib_dir(erl_interface) -%% ERL = ERLANG_ROOT_DIR/bin/erl -%% ERLC = ERLANG_ROOT_DIR/bin/erl -%% run_hooks(Dir, pre, Command, Opts, State) -> run_hooks(Dir, pre_hooks, Command, Opts, State); run_hooks(Dir, post, Command, Opts, State) -> @@ -94,7 +71,7 @@ run_hooks(Dir, Type, Command, Opts, State) -> ?DEBUG("run_hooks(~p, ~p, ~p) -> no hooks defined\n", [Dir, Type, Command]), ok; Hooks -> - Env = create_env(State, Opts), + Env = rebar_env:create_env(State, Opts), lists:foreach(fun({_, C, _}=Hook) when C =:= Command -> apply_hook(Dir, Env, Hook); ({C, _}=Hook) when C =:= Command -> @@ -114,36 +91,3 @@ apply_hook(Dir, Env, {Arch, Command, Hook}) -> apply_hook(Dir, Env, {Command, Hook}) -> Msg = lists:flatten(io_lib:format("Hook for ~p failed!~n", [Command])), rebar_utils:sh(Hook, [use_stdout, {cd, Dir}, {env, Env}, {abort_on_error, Msg}]). - -create_env(State, Opts) -> - BaseDir = rebar_dir:base_dir(State), - [ - {"REBAR_DEPS_DIR", filename:absname(rebar_dir:deps_dir(State))}, - {"REBAR_BUILD_DIR", filename:absname(rebar_dir:base_dir(State))}, - {"REBAR_ROOT_DIR", filename:absname(rebar_dir:root_dir(State))}, - {"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(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))}, - {"ERLANG_ERTS_VER", erlang:system_info(version)}, - {"ERLANG_ROOT_DIR", code:root_dir()}, - {"ERLANG_LIB_DIR_erl_interface", code:lib_dir(erl_interface)}, - {"ERLANG_LIB_VER_erl_interface", re_version(code:lib_dir(erl_interface))}, - {"ERL", filename:join([code:root_dir(), "bin", "erl"])}, - {"ERLC", filename:join([code:root_dir(), "bin", "erlc"])}, - {"ERLANG_ARCH" , rebar_api:wordsize()}, - {"ERLANG_TARGET", rebar_api:get_arch()} - - ]. - -join_dirs(BaseDir, Dirs) -> - rebar_string:join([filename:join(BaseDir, Dir) || Dir <- Dirs], ":"). - -re_version(Path) -> - case re:run(Path, "^.*-(?[^/-]*)$", [{capture,[1],list}, unicode]) of - nomatch -> ""; - {match, [Ver]} -> Ver - end. -- cgit v1.1 From 6d29233a16ae94cf4618ddadb381f91e4e5eb882 Mon Sep 17 00:00:00 2001 From: Carl-Johan Kjellander Date: Fri, 23 Feb 2018 15:29:05 +0100 Subject: remove whitespace --- src/rebar_env.erl | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/rebar_env.erl b/src/rebar_env.erl index dc12419..10bc5b8 100644 --- a/src/rebar_env.erl +++ b/src/rebar_env.erl @@ -49,7 +49,6 @@ create_env(State, Opts) -> {"ERLC", filename:join([code:root_dir(), "bin", "erlc"])}, {"ERLANG_ARCH" , rebar_api:wordsize()}, {"ERLANG_TARGET", rebar_api:get_arch()} - ]. %% ==================================================================== -- cgit v1.1 From 57768ba22ed39d2b099ac09c834ed90f422f2336 Mon Sep 17 00:00:00 2001 From: Carl-Johan Kjellander Date: Mon, 26 Feb 2018 16:06:51 +0100 Subject: make it easier to create env --- src/rebar_env.erl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/rebar_env.erl b/src/rebar_env.erl index 10bc5b8..eea47de 100644 --- a/src/rebar_env.erl +++ b/src/rebar_env.erl @@ -1,6 +1,7 @@ -module(rebar_env). --export([create_env/2]). +-export([create_env/1, + create_env/2]). -include("rebar.hrl"). @@ -27,6 +28,12 @@ %% ERL = ERLANG_ROOT_DIR/bin/erl %% ERLC = ERLANG_ROOT_DIR/bin/erl %% + +-spec create_env(rebar_state:t()) -> proplists:proplist(). +create_env(State) -> + Opts = rebar_state:opts(State), + create_env(State, Opts). + -spec create_env(rebar_state:t(), rebar_dict()) -> proplists:proplist(). create_env(State, Opts) -> BaseDir = rebar_dir:base_dir(State), -- cgit v1.1