From 51e1d64dccf4967eb078858529a0d7f05b8b461e Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 21 Mar 2019 21:56:15 +0300 Subject: Add --post-hook option to bdep-new --- bdep/new.cxx | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) (limited to 'bdep/new.cxx') diff --git a/bdep/new.cxx b/bdep/new.cxx index 7d1815c..55b6f8c 100644 --- a/bdep/new.cxx +++ b/bdep/new.cxx @@ -6,6 +6,7 @@ #include // replace() +#include #include #include @@ -17,11 +18,10 @@ #include using namespace std; +using namespace butl; namespace bdep { - using butl::project_name; - using type = cmd_new_type; using lang = cmd_new_lang; using vcs = cmd_new_vcs; @@ -1596,6 +1596,76 @@ namespace bdep fail << "unable to write " << f << ": " << e; } + // Run post-hooks. + // + optional env; + optional subs; + strings vars; + + if (!o.post_hook ().empty ()) + { + subs = command_substitution_map (); + + auto add_var = [&subs, &vars] (string name, string value) + { + vars.push_back ("BDEP_NEW_" + + ucase (const_cast (name)) + + '=' + + value); + + (*subs)[move (name)] = move (value); + }; + + add_var ("mode", sub ? "subdirectory" : pkg ? "package" : "project"); + add_var ("name", n); + add_var ("base", move (b)); + add_var ("stem", move (s)); + add_var ("type", t.string ()); + add_var ("lang", l.string ()); + add_var ("vcs", vc.string ()); + add_var ("root", prj.string ()); + + env = process_env (process_path (), out, vars); + } + + for (const string& cmd: o.post_hook ()) + { + try + { + // Note: out directory path is absolute and normalized. + // + process_exit e (command_run (cmd, + env, + subs, + '@', + [] (const char* const args[], size_t n) + { + if (verb >= 2) + { + print_process (args, n); + } + })); + + if (!e) + { + if (e.normal ()) + throw failed (); // Assume the command issued diagnostics. + + fail << "post hook '" << cmd << "' " << e; + } + } + catch (const invalid_argument& e) + { + fail << "invalid post hook '" << cmd << "': " << e; + } + // Handle process_error and io_error (both derive from system_error). + // + catch (const system_error& e) + { + fail << "unable to execute post hook '" << cmd << "': " << e; + } + } + if (verb) text << "created new " << t << ' ' << (sub ? "source subdirectory" : pkg ? "package" : "project") -- cgit v1.1