diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2018-03-09 15:06:27 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2018-03-09 15:06:27 +0200 |
commit | d304762e4338b4a055e2be018205a00c2ca9ee2f (patch) | |
tree | 252a4aa70118135e589095442fb4df3879c96eb0 /bdep/config.cxx | |
parent | df5e58e6e5eb2727a185bf9a98a462c18fa3a83d (diff) |
Support for resolving and adding configurations
Diffstat (limited to 'bdep/config.cxx')
-rw-r--r-- | bdep/config.cxx | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/bdep/config.cxx b/bdep/config.cxx index 59dcbad..f7d74ed 100644 --- a/bdep/config.cxx +++ b/bdep/config.cxx @@ -4,12 +4,92 @@ #include <bdep/config.hxx> +#include <bdep/database.hxx> +#include <bdep/project-odb.hxx> #include <bdep/diagnostics.hxx> using namespace std; namespace bdep { + shared_ptr<configuration> + cmd_config_add (const dir_path& prj, + database& db, + dir_path path, + optional<string> name, + optional<bool> def, + optional<uint64_t> id) + { + if (!exists (path)) + fail << "configuration directory " << path << " does not exist"; + + transaction t (db.begin ()); + + using count = configuration_count; + using query = bdep::query<count>; + + // By default the first added configuration is the default. + // + if (!def) + def = (db.query_value<count> () == 0); + + // Make sure the configuration path is absolute and normalized. Also + // derive relative to project directory path is possible. + // + path.complete (); + path.normalize (); + + optional<dir_path> rel_path; + try {rel_path = path.relative (prj);} catch (const invalid_path&) {} + + shared_ptr<configuration> r ( + new configuration { + id, + name, + path, + move (rel_path), + *def}); + + try + { + db.persist (r); + } + catch (const odb::exception&) + { + // See if this is id, name, or path conflict. + // + if (id && db.query_value<count> (query::id == *id) != 0) + fail << "configuration with id " << *id << " already exists " + << "in project " << prj; + + if (name && db.query_value<count> (query::name == *name) != 0) + fail << "configuration with name '" << *name << "' already exists " + << "in project " << prj; + + if (db.query_value<count> (query::path == path.string ()) != 0) + fail << "configuration with path " << path << " already exists " + << "in project " << prj; + + // Hm, what could that be? + // + throw; + } + + t.commit (); + + if (verb) + { + diag_record dr (text); + /* */ dr << "added configuration "; + if (r->name) dr << '@' << *r->name << ' '; + /* */ dr << r->path << " (" << *r->id; + if (r->default_) dr << ", default"; + /* */ dr << ')'; + } + + return r; + } + int cmd_config (const cmd_config_options& o, cli::scanner& args) { |