From 2f64f0e6b27948b7ace968da049191bdc7292db2 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 25 Dec 2009 23:19:09 -0700 Subject: [PATCH] Refactoring core logic in preparation for dependency mgmt; breaking out sub_dir determination into dedicated module --- ebin/rebar.app | 37 +++++++++++++++++++---------- src/rebar_core.erl | 54 +++++++++++++++++++++++++------------------ src/rebar_subdirs.erl | 44 +++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 35 deletions(-) create mode 100644 src/rebar_subdirs.erl diff --git a/ebin/rebar.app b/ebin/rebar.app index a131746..9598e8e 100644 --- a/ebin/rebar.app +++ b/ebin/rebar.app @@ -3,9 +3,10 @@ {vsn, "2"}, {modules, [ rebar, rebar_app_utils, - rebar_config, + rebar_config, rebar_core, rebar_ct, + rebar_deps, rebar_erlc_compiler, rebar_escripter, rebar_eunit, @@ -19,6 +20,7 @@ rebar_port_compiler, rebar_rel_utils, rebar_reltool, + rebar_subdirs, rebar_utils ]}, {registered, []}, {applications, [kernel, @@ -28,18 +30,29 @@ %% Default log level {log_level, error}, - %% Processing modules + %% any_dir processing modules + {any_dir_modules, [ + rebar_subdirs, + rebar_deps + ]}, + + %% Dir specific processing modules {modules, [ - {app_dir, [ rebar_protobuffs_compiler, - rebar_erlc_compiler, - rebar_lfe_compiler, - rebar_erlydtl_compiler, - rebar_port_compiler, - rebar_otp_app, - rebar_ct, - rebar_eunit, - rebar_escripter]}, - {rel_dir, [ rebar_reltool ]} + {app_dir, [ + rebar_protobuffs_compiler, + rebar_erlc_compiler, + rebar_lfe_compiler, + rebar_erlydtl_compiler, + rebar_port_compiler, + rebar_otp_app, + rebar_ct, + rebar_eunit, + rebar_escripter + ]}, + + {rel_dir, [ + rebar_reltool + ]} ]} ]} ]}. diff --git a/src/rebar_core.erl b/src/rebar_core.erl index 1449b87..06afc7f 100644 --- a/src/rebar_core.erl +++ b/src/rebar_core.erl @@ -30,6 +30,7 @@ -include("rebar.hrl"). + -ifndef(BUILD_TIME). -define(BUILD_TIME, "undefined"). -endif. @@ -92,35 +93,33 @@ filter_flags([Item | Rest], Commands) -> process_dir(Dir, ParentConfig, Commands) -> ok = file:set_cwd(Dir), Config = rebar_config:new(ParentConfig), - + %% Save the current code path and then update it with %% lib_dirs. Children inherit parents code path, but we %% also want to ensure that we restore everything to pristine %% condition after processing this child CurrentCodePath = update_code_path(Config), - %% If there are any subdirs specified, process those first... - case rebar_config:get(Config, sub_dirs, []) of - [] -> - ok; - Subdirs -> - %% Edge case: config is inherited, EXCEPT for sub_dir directives -- filter those out - FilteredConfig = rebar_config:delete(Config, sub_dirs), - [process_dir(filename:join(Dir, Subdir), FilteredConfig, Commands) || - Subdir <- Subdirs], - ok = file:set_cwd(Dir) - end, - %% Get the list of processing modules and check each one against %% CWD to see if it's a fit -- if it is, use that set of modules %% to process this dir. {ok, AvailModuleSets} = application:get_env(rebar, modules), - case choose_module_set(AvailModuleSets, Dir) of - {ok, Modules, ModuleSetFile} -> - apply_commands(Commands, Modules, Config, ModuleSetFile); - none -> - ok - end, + {DirModules, ModuleSetFile} = choose_module_set(AvailModuleSets, Dir), + + %% Get the list of modules for "any dir". This is a catch-all list of modules + %% that are processed in addion to modules associated with this directory + %5 type. These any_dir modules are processed FIRST. + {ok, AnyDirModules} = application:get_env(rebar, any_dir_modules), + Modules = AnyDirModules ++ DirModules, + + %% Give the modules a chance to tweak config and indicate if there + %% are any other dirs that might need processing first. + {UpdatedConfig, Dirs} = acc_modules(select_modules(Modules, preprocess, []), + preprocess, Config, ModuleSetFile, []), + [process_dir(D, UpdatedConfig, Commands) || D <- Dirs], + + %% Finally, process the current working directory + apply_commands(Commands, Modules, UpdatedConfig, ModuleSetFile), %% Once we're all done processing, reset the code path to whatever %% the parent initialized it to @@ -128,15 +127,15 @@ process_dir(Dir, ParentConfig, Commands) -> ok. %% -%% Give a list of module sets from rebar.app and a directory, find +%% Given a list of module sets from rebar.app and a directory, find %% the appropriate subset of modules for this directory %% choose_module_set([], _Dir) -> - none; + {[], undefined}; choose_module_set([{Fn, Modules} | Rest], Dir) -> case ?MODULE:Fn(Dir) of {true, File} -> - {ok, Modules, File}; + {Modules, File}; false -> choose_module_set(Rest, Dir) end. @@ -228,4 +227,13 @@ run_modules([Module | Rest], Command, Config, File) -> {error, Reason} end. - +acc_modules([], _Command, Config, _File, Acc) -> + {Config, Acc}; +acc_modules([Module | Rest], Command, Config, File, Acc) -> + case Module:Command(Config, File) of + {ok, NewConfig, Result} when is_list(Result) -> + List = Result; + {ok, NewConfig, Result} -> + List = [Result] + end, + acc_modules(Rest, Command, NewConfig, File, List ++ Acc). diff --git a/src/rebar_subdirs.erl b/src/rebar_subdirs.erl new file mode 100644 index 0000000..d4563a2 --- /dev/null +++ b/src/rebar_subdirs.erl @@ -0,0 +1,44 @@ +%% ------------------------------------------------------------------- +%% +%% rebar: Erlang Build Tools +%% +%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com) +%% +%% Permission is hereby granted, free of charge, to any person obtaining a copy +%% of this software and associated documentation files (the "Software"), to deal +%% in the Software without restriction, including without limitation the rights +%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +%% copies of the Software, and to permit persons to whom the Software is +%% furnished to do so, subject to the following conditions: +%% +%% The above copyright notice and this permission notice shall be included in +%% all copies or substantial portions of the Software. +%% +%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +%% THE SOFTWARE. +%% ------------------------------------------------------------------- +-module(rebar_subdirs). + +-include("rebar.hrl"). + +-export([preprocess/2]). + +%% =================================================================== +%% Public API +%% =================================================================== + +preprocess(Config, _) -> + %% Get the list of subdirs specified in the config (if any). + Cwd = rebar_utils:get_cwd(), + Subdirs = [filename:join(Cwd, Dir) || Dir <- rebar_config:get(Config, sub_dirs, [])], + + %% Filter out the subdirs from the config so that processing in the + %% subdirs doesn't try to reprocess + Config2 = rebar_config:delete(Config, sub_dirs), + {ok, Config2, Subdirs}. +