From f283fbea934a1a2bad3fa1df25a82717e6b11aac Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 18 Jul 2018 20:43:41 +0300 Subject: Implement temp directory facility --- bdep/bdep.cxx | 73 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 23 deletions(-) (limited to 'bdep/bdep.cxx') diff --git a/bdep/bdep.cxx b/bdep/bdep.cxx index 339be05..f77870f 100644 --- a/bdep/bdep.cxx +++ b/bdep/bdep.cxx @@ -12,6 +12,7 @@ #include #include +#include // find_project() #include #include #include @@ -68,9 +69,25 @@ cfg_name (...) return false; } +// Find the project directory if it is possible for the option class O and +// return empty path otherwise. +// +template +static inline auto +prj_dir (const O* o) -> decltype(find_project (*o)) +{ + return find_project (*o); +} + +static inline auto +prj_dir (...) -> const dir_path& {return empty_dir_path;} + template static O -init (const common_options& co, cli::group_scanner& scan, strings& args) +init (const common_options& co, + cli::group_scanner& scan, + strings& args, + bool tmp) { O o; static_cast (o) = co; @@ -124,6 +141,11 @@ init (const common_options& co, cli::group_scanner& scan, strings& args) ? o.verbose () : o.V () ? 3 : o.v () ? 2 : o.quiet () ? 0 : 1; + // Temporary directory. + // + if (tmp) + init_tmp (dir_path (prj_dir (&o))); + return o; } @@ -196,7 +218,7 @@ try const common_options& co (o); if (o.help ()) - return help (init (co, scan, argsv), "", nullptr); + return help (init (co, scan, argsv, false), "", nullptr); // The next argument should be a command. // @@ -211,7 +233,7 @@ try if (h) { - ho = init (co, scan, argsv); + ho = init (co, scan, argsv, false); if (args.more ()) { @@ -255,28 +277,31 @@ try // break; // } // -#define COMMAND_IMPL(ON, FN, SN) \ - if (cmd.ON ()) \ - { \ - if (h) \ - r = help (ho, SN, print_bdep_##FN##_usage); \ - else \ - r = cmd_##FN (init (co, scan, argsv), args); \ - \ - break; \ +#define COMMAND_IMPL(ON, FN, SN, TMP) \ + if (cmd.ON ()) \ + { \ + if (h) \ + r = help (ho, SN, print_bdep_##FN##_usage); \ + else \ + r = cmd_##FN (init (co, scan, argsv, TMP), args); \ + \ + break; \ } - COMMAND_IMPL (new_, new, "new"); - COMMAND_IMPL (init, init, "init"); - COMMAND_IMPL (sync, sync, "sync"); - COMMAND_IMPL (fetch, fetch, "fetch"); - COMMAND_IMPL (status, status, "status"); - COMMAND_IMPL (publish, publish, "publish"); - COMMAND_IMPL (deinit, deinit, "deinit"); - COMMAND_IMPL (config, config, "config"); - COMMAND_IMPL (test, test, "test"); - COMMAND_IMPL (update, update, "update"); - COMMAND_IMPL (clean, clean, "clean"); + // Temp dir is initialized manually for these commands. + // + COMMAND_IMPL (new_, new, "new", false); + COMMAND_IMPL (sync, sync, "sync", false); + + COMMAND_IMPL (init, init, "init", true); + COMMAND_IMPL (fetch, fetch, "fetch", true); + COMMAND_IMPL (status, status, "status", true); + COMMAND_IMPL (publish, publish, "publish", true); + COMMAND_IMPL (deinit, deinit, "deinit", true); + COMMAND_IMPL (config, config, "config", true); + COMMAND_IMPL (test, test, "test", true); + COMMAND_IMPL (update, update, "update", true); + COMMAND_IMPL (clean, clean, "clean", true); assert (false); fail << "unhandled command"; @@ -287,6 +312,8 @@ try break; } + clean_tmp (true /* ignore_error */); + if (r != 0) return r; -- cgit v1.1