From 73b24fd1a0604e413fece30181cc632c1081aa29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 18 Nov 2013 10:28:39 +0100 Subject: Support wildcards in lib dirs Wildcards are supported in both lib_dirs in config files as well via the -l command line option. --- src/rlx_cmd_args.erl | 5 +++-- src/rlx_prv_config.erl | 2 +- src/rlx_util.erl | 20 +++++++++++++++++++- test/rlx_command_SUITE.erl | 15 ++++++++++++++- test/rlx_release_SUITE.erl | 3 ++- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/rlx_cmd_args.erl b/src/rlx_cmd_args.erl index f592474..d2402ce 100644 --- a/src/rlx_cmd_args.erl +++ b/src/rlx_cmd_args.erl @@ -223,11 +223,12 @@ create_output_dir(Opts, Acc) -> create_lib_dirs(Opts, Acc) -> Dirs = proplists:get_all_values(lib_dir, Opts) ++ proplists:get_value(lib_dirs, Opts, []), - case check_lib_dirs(Dirs) of + ExpDirs = rlx_util:wildcard_paths(Dirs), + case check_lib_dirs(ExpDirs) of Error = {error, _} -> Error; ok -> - create_root_dir(Opts, [{lib_dirs, [filename:absname(Dir) || Dir <- Dirs]} | Acc]) + create_root_dir(Opts, [{lib_dirs, ExpDirs} | Acc]) end. -spec create_root_dir([getopt:option()], rlx_state:cmd_args()) -> diff --git a/src/rlx_prv_config.erl b/src/rlx_prv_config.erl index 19aa576..aa2e79c 100644 --- a/src/rlx_prv_config.erl +++ b/src/rlx_prv_config.erl @@ -129,7 +129,7 @@ load_terms({paths, Paths}, {ok, State}) -> load_terms({lib_dirs, Dirs}, {ok, State}) -> State2 = rlx_state:add_lib_dirs(State, - [list_to_binary(filename:absname(Dir)) || Dir <- Dirs]), + [list_to_binary(Dir) || Dir <- rlx_util:wildcard_paths(Dirs)]), {ok, State2}; load_terms({providers, Providers0}, {ok, State0}) -> Providers1 = gen_providers(Providers0, State0), diff --git a/src/rlx_util.erl b/src/rlx_util.erl index 53e9122..48e2ee1 100644 --- a/src/rlx_util.erl +++ b/src/rlx_util.erl @@ -28,7 +28,8 @@ is_error/1, error_reason/1, indent/1, - optional_to_string/1]). + optional_to_string/1, + wildcard_paths/1]). -define(ONE_LEVEL_INDENT, " "). %%============================================================================ @@ -96,6 +97,23 @@ optional_to_string(undefined) -> optional_to_string(Value) when is_list(Value) -> Value. +%% @doc expand wildcards and names in the given paths +-spec wildcard_paths([file:filename_all()]) -> [string()]. +wildcard_paths(Paths) -> + [filename:absname(Expanded) || Path <- Paths, Expanded <- wildcard(Path)]. + +%% In case the given directory does not expand, +%% we return it back in a list so we trigger the +%% proper error reportings. +-spec wildcard(file:filename_all()) -> [string()]. +wildcard(Path) when is_binary(Path) -> + wildcard(binary_to_list(Path)); +wildcard(Path) when is_list(Path) -> + case filelib:wildcard(Path) of + [] -> [Path]; + Paths -> Paths + end. + %%%=================================================================== %%% Test Functions %%%=================================================================== diff --git a/test/rlx_command_SUITE.erl b/test/rlx_command_SUITE.erl index 6f726b4..5ea4769 100644 --- a/test/rlx_command_SUITE.erl +++ b/test/rlx_command_SUITE.erl @@ -24,6 +24,7 @@ end_per_suite/1, all/0, normal_passing_case/1, + lib_expansion_case/1, lib_fail_case/1, spec_parse_fail_case/1, config_fail_case/1]). @@ -41,7 +42,7 @@ end_per_suite(_Config) -> ok. all() -> - [normal_passing_case, lib_fail_case, config_fail_case]. + [normal_passing_case, lib_expansion_case, lib_fail_case, config_fail_case]. normal_passing_case(Config) -> DataDir = proplists:get_value(data_dir, Config), @@ -70,6 +71,18 @@ normal_passing_case(Config) -> {{45,22},{[],[<<"build">>,21]}}, between}], rlx_state:goals(State)). +lib_expansion_case(Config) -> + DataDir = proplists:get_value(data_dir, Config), + Lib1 = filename:join(DataDir, <<"lib1">>), + Lib2 = filename:join(DataDir, <<"lib2">>), + ok = rlx_util:mkdir_p(Lib1), + ok = rlx_util:mkdir_p(Lib2), + + CmdLine = ["-l", filename:join(DataDir, "*")], + {ok, {Opts, Targets}} = getopt:parse(relx:opt_spec_list(), CmdLine), + {ok, State} = rlx_cmd_args:args2state(Opts, Targets), + ?assertMatch([Lib1, Lib2], + rlx_state:lib_dirs(State)). lib_fail_case(Config) -> DataDir = proplists:get_value(data_dir, Config), diff --git a/test/rlx_release_SUITE.erl b/test/rlx_release_SUITE.erl index d16dce9..3087e63 100644 --- a/test/rlx_release_SUITE.erl +++ b/test/rlx_release_SUITE.erl @@ -130,7 +130,8 @@ make_extend_release(Config) -> [goal_app_1, goal_app_2]}, {release, {foo_test, "0.0.1", {extend, foo}}, - [goal_app_2]}]), + [goal_app_2]}, + {lib_dirs, [filename:join(LibDir1, "*")]}]), OutputDir = filename:join([proplists:get_value(data_dir, Config), create_random_name("relx-output")]), {ok, State} = relx:do(foo_test, undefined, [], [LibDir1], 3, -- cgit v1.2.3