aboutsummaryrefslogtreecommitdiff
path: root/bdep/config.cxx
blob: 8da62111939dc34062bb3bc178c22f6183e18e15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// file      : bdep/config.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bdep/config.hxx>

#include <bdep/database.hxx>
#include <bdep/project-odb.hxx>
#include <bdep/diagnostics.hxx>

using namespace std;

namespace bdep
{
  // Translate the configuration directory that is actually a name (@foo or
  // -@foo) to the real directory (prj-foo) and name (@foo).
  //
  static inline void
  translate_path_name (const dir_path& prj,
                       dir_path& path,
                       optional<string>& name)
  {
    if (name || !path.simple ())
      return;

    size_t n;
    {
      const string& s (path.string ());
      if      (s.size () > 1 && s[0] == '@')                n = 1;
      else if (s.size () > 2 && s[0] == '-' && s[1] == '@') n = 2;
      else return;
    }

    string s (move (path).string ()); // Move out.
    s.erase (0, n);                   // Remove leading '@' or '-@'.

    path  = prj;
    path += '-';
    path += s;

    name = move (s);
  }

  shared_ptr<configuration>
  cmd_config_add (const configuration_add_options& ao,
                  const dir_path&                  prj,
                  database&                        db,
                  dir_path                         path,
                  optional<string>                 name,
                  optional<uint64_t>               id,
                  const char*                      what)
  {
    translate_path_name (prj, path, name);

    if (!exists (path))
      fail << "configuration directory " << path << " does not exist";

    transaction t (db.begin ());

    using count = configuration_count;

    optional<bool> def, fwd;
    {
      using query = bdep::query<configuration>;

      // By default the first added configuration is the default.
      //
      if (ao.default_ () || ao.no_default ())
        def = ao.default_ () && !ao.no_default ();

      if (!def)
        def = (db.query_value<count> () == 0);
      else if (*def)
      {
        if (auto p = db.query_one<configuration> (query::default_))
          fail << "configuration " << *p << " is already the default" <<
            info << "use 'bdep config set --no-default' to clear";
      }

      // By default the default configuration is forwarded unless another
      // is already forwarded.
      //
      if (ao.forward () || ao.no_forward ())
        fwd = ao.forward ()  && !ao.no_forward ();

      if (!fwd)
        fwd = *def && db.query_one<configuration> (query::forward) == nullptr;
      else if (*fwd)
      {
        if (auto p = db.query_one<configuration> (query::forward))
          fail << "configuration " << *p << " is already forwarded" <<
            info << "use 'bdep config set --no-forward' to clear";
      }
    }

    // Make sure the configuration path is absolute and normalized. Also
    // derive relative to project directory path if 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,
        *fwd,
        !ao.no_auto_sync (),
        {} /* packages */});

    try
    {
      db.persist (r);
    }
    catch (const odb::exception&)
    {
      using query = bdep::query<count>;

      // 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 << what << " configuration ";
      if (r->name)       dr << '@' << *r->name << ' ';
      /*              */ dr << r->path << " (" << *r->id;
      if (r->default_)   dr << ", default";
      if (r->forward)    dr << ", forwarded";
      if (r->auto_sync)  dr << ", auto-synchronized";
      /*              */ dr << ')';
    }

    return r;
  }

  static int
  cmd_config_add (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  shared_ptr<configuration>
  cmd_config_create (const common_options&            co,
                     const configuration_add_options& ao,
                     const dir_path&                  prj,
                     database&                        db,
                     dir_path                         path,
                     cli::scanner&                    cfg_args,
                     optional<string>                 name,
                     optional<uint64_t>               id)
  {
    translate_path_name (prj, path, name);

    // Call bpkg to create the configuration.
    //
    {
      strings args;
      while (cfg_args.more ())
        args.push_back (cfg_args.next ());

      run_bpkg (2,
                co,
                "create",
                "-d", path,
                (ao.wipe () ? "--wipe" : nullptr),
                args);
    }

    return cmd_config_add (ao,
                           prj,
                           db,
                           move (path),
                           move (name),
                           id,
                           "created");
  }

  static int
  cmd_config_create (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  static int
  cmd_config_remove (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  static int
  cmd_config_rename (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  static int
  cmd_config_set (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  const char*
  cmd_config_validate_add (const configuration_add_options& o)
  {
    // --[no-]default
    //
    if (o.default_ () && o.no_default ())
      fail << "both --default and --no-default specified";

    // --[no-]forward
    //
    if (o.forward () && o.no_forward ())
      fail << "both --forward and --no-forward specified";

    // --[no-]auto-sync
    //
    if (o.auto_sync () && o.no_auto_sync ())
      fail << "both --auto-sync and --no-auto-sync specified";

    return (o.default_ ()     ? "--default"      :
            o.no_default ()   ? "--no-default"   :
            o.forward ()      ? "--forward"      :
            o.no_forward ()   ? "--no-forward"   :
            o.auto_sync ()    ? "--auto-sync"    :
            o.no_auto_sync () ? "--no-auto-sync" :
            o.wipe ()         ? "--wipe"         : nullptr);
  }

  int
  cmd_config (const cmd_config_options& o, cli::scanner& scan)
  {
    tracer trace ("config");

    cmd_config_subcommands c (
      parse_command<cmd_config_subcommands> (scan,
                                             "config subcommand",
                                             "bdep help config"));

    // Validate options/subcommands.
    //
    if (const char* n = cmd_config_validate_add (o))
    {
      if (!(c.add () || c.create () || c.set ()))
        fail << n << " not valid for this command";

      if (o.wipe () && !c.create ())
        fail << "--wipe is not valid for this command";
    }

    // --all
    //
    if (o.all ())
    {
      if (!c.remove ())
        fail << "--all not valid for this command";
    }

    // Dispatch to subcommand function.
    //
    if (c.add    ()) return cmd_config_add    (o, scan);
    if (c.create ()) return cmd_config_create (o, scan);
    if (c.remove ()) return cmd_config_remove (o, scan);
    if (c.rename ()) return cmd_config_rename (o, scan);
    if (c.set    ()) return cmd_config_set    (o, scan);

    assert (false); // Unhandled (new) subcommand.
    return 1;
  }
}