diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2018-05-13 12:50:36 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2018-05-13 12:50:36 +0200 |
commit | ad1de6c34f5492164d89667c648294363b972c99 (patch) | |
tree | ff95bdc11204b2a3459114b6775bd749f84ef08f | |
parent | e89ecd4d84909d853d9441f1cee01fccbb9b210d (diff) |
Clean up pkg-drop, add --plan
-rw-r--r-- | bpkg/pkg-drop.cli | 13 | ||||
-rw-r--r-- | bpkg/pkg-drop.cxx | 138 | ||||
-rw-r--r-- | bpkg/pkg-drop.hxx | 14 |
3 files changed, 45 insertions, 120 deletions
diff --git a/bpkg/pkg-drop.cli b/bpkg/pkg-drop.cli index cbf2279..dcb9110 100644 --- a/bpkg/pkg-drop.cli +++ b/bpkg/pkg-drop.cli @@ -65,15 +65,22 @@ namespace bpkg "Issue an error if attempting to drop dependent packages." } + bool --disfigure-only + { + "Disfigure all the packages but don't purge." + } + bool --print-only|-p { "Print to \cb{STDOUT} what would be done without actually doing anything." } - bool --disfigure-only + string --plan { - "Disfigure all the packages but don't purge." - } + "<header>", + "Print the plan (even if \cb{--yes} is specified) and start it with the + <header> line (unless it is empty)." + }; }; } diff --git a/bpkg/pkg-drop.cxx b/bpkg/pkg-drop.cxx index 2ba4b72..5c0346b 100644 --- a/bpkg/pkg-drop.cxx +++ b/bpkg/pkg-drop.cxx @@ -292,20 +292,20 @@ namespace bpkg // static int pkg_drop (const dir_path& c, - const common_options& o, + const pkg_drop_options& o, database& db, const drop_packages& pkgs, bool drop_prq, - bool print_only, - bool disfigure_only, - bool yes, - bool no, - bool print_plan) + bool need_prompt) { // Print what we are going to do, then ask for the user's confirmation. // - if (print_only || !(yes || no || !print_plan)) + if (o.print_only () || + o.plan_specified () || + !(o.yes () || o.no () || !need_prompt)) { + bool first (true); // First entry in the plan. + for (const drop_package& dp: pkgs) { // Skip prerequisites if we weren't instructed to drop them. @@ -315,7 +315,22 @@ namespace bpkg const shared_ptr<selected_package>& p (dp.package); - if (print_only) + if (first) + { + // If the plan header is not empty, now is the time to print it. + // + if (!o.plan ().empty ()) + { + if (o.print_only ()) + cout << o.plan () << endl; + else + text << o.plan (); + } + + first = false; + } + + if (o.print_only ()) cout << "drop " << p->name << endl; else if (verb) // Print indented for better visual separation. @@ -323,13 +338,14 @@ namespace bpkg text << " drop " << p->name; } - if (print_only) + if (o.print_only ()) return 0; } // Ask the user if we should continue. // - if (no || !(yes || !print_plan || yn_prompt ("continue? [Y/n]", 'y'))) + if (o.no () || + !(o.yes () || !need_prompt || yn_prompt ("continue? [Y/n]", 'y'))) return 1; // All that's left to do is first disfigure configured packages and @@ -368,7 +384,7 @@ namespace bpkg : "disfigured ") << p->name; } - if (disfigure_only) + if (o.disfigure_only ()) return 0; // Purge. @@ -436,11 +452,12 @@ namespace bpkg drop_packages pkgs; bool drop_prq (false); - // Print the plan and ask for the user's confirmation only if there are - // additional packages (such as dependents or prerequisites of the - // explicitly listed packages) to be dropped. + // We need the plan and to ask for the user's confirmation only if there + // are additional packages (such as dependents or prerequisites of the + // explicitly listed packages) to be dropped. But if the user explicitly + // requested it with --plan, then we print it as long as it is not empty. // - bool print_plan (false); + bool need_prompt (false); { transaction t (db); @@ -495,7 +512,7 @@ namespace bpkg if (o.no () || !yn_prompt ("drop dependent packages? [y/N]", 'n')) return 1; - print_plan = true; + need_prompt = true; } // Collect all the prerequisites that are not held. These will be @@ -552,97 +569,12 @@ namespace bpkg drop_prq = yn_prompt ("drop unused packages? [Y/n]", 'y'); if (drop_prq) - print_plan = true; - } - - t.commit (); - } - - return pkg_drop (c, - o, - db, - pkgs, - drop_prq, - o.print_only (), - o.disfigure_only (), - o.yes (), - o.no (), - print_plan); - } - - set<shared_ptr<selected_package>> - pkg_drop (const dir_path& c, - const common_options& o, - database& db, - const set<shared_ptr<selected_package>>& prqs, - bool prompt) - { - assert (session::has_current ()); - - // Assemble the list of packages we will be dropping. - // - drop_packages pkgs; - { - transaction t (db); - - // First add all the "caller selection" of packages to the list and - // collect their prerequisites (these will be the candidates to drop - // as well). - // - for (const shared_ptr<selected_package>& p: prqs) - { - assert (p->state != package_state::broken); - - if (pkgs.collect (p, drop_reason::prerequisite)) - pkgs.collect_prerequisites (db, p); + need_prompt = true; } - // Now arrange them (and their prerequisites) in the dependency order. - // - for (const shared_ptr<selected_package>& p: prqs) - pkgs.order (p->name); - - // Finally filter out those that we cannot drop. - // - bool r (pkgs.filter_prerequisites (db)); - t.commit (); - - if (!r) - return {}; // Nothing can be dropped. } - if (prompt) - { - { - diag_record dr (text); - - dr << "following dependencies were automatically built but will " - << "no longer be used:"; - - for (const drop_package& dp: pkgs) - dr << text << dp.package->name; - } - - if (!yn_prompt ("drop unused packages? [Y/n]", 'y')) - return {}; - } - - pkg_drop (c, - o, - db, - pkgs, - true, // Drop prerequisites (that's what we are here for). - false, // Print-only (too late for that). - false, // Disfigure-only (could be an option). - true, // Yes (don't print the plan or prompt). - false, // No (we already said yes). - false); // Don't print the plan (just to reiterate). - - set<shared_ptr<selected_package>> r; - for (const drop_package& dp: pkgs) - r.insert (dp.package); - - return r; + return pkg_drop (c, o, db, pkgs, drop_prq, need_prompt); } } diff --git a/bpkg/pkg-drop.hxx b/bpkg/pkg-drop.hxx index b7e0be1..63d6e29 100644 --- a/bpkg/pkg-drop.hxx +++ b/bpkg/pkg-drop.hxx @@ -5,10 +5,7 @@ #ifndef BPKG_PKG_DROP_HXX #define BPKG_PKG_DROP_HXX -#include <set> - #include <bpkg/types.hxx> -#include <bpkg/forward.hxx> // database, selected_package #include <bpkg/utility.hxx> #include <bpkg/pkg-drop-options.hxx> @@ -17,17 +14,6 @@ namespace bpkg { int pkg_drop (const pkg_drop_options&, cli::scanner& args); - - // Examine the list of prerequisite packages and drop those that don't - // have any dependents. Return the set of packages that were actually - // dropped. Note that it should be called in session. - // - std::set<shared_ptr<selected_package>> - pkg_drop (const dir_path& configuration, - const common_options&, - database&, - const std::set<shared_ptr<selected_package>>&, - bool prompt); } #endif // BPKG_PKG_DROP_HXX |