aboutsummaryrefslogtreecommitdiff
path: root/bpkg/cfg-create.cxx
blob: f125e435a1cd6f8af837bd76f7759506c82f3456 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// file      : bpkg/cfg-create.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <bpkg/cfg-create.hxx>

#include <bpkg/package.hxx>
#include <bpkg/package-odb.hxx>
#include <bpkg/database.hxx>
#include <bpkg/diagnostics.hxx>

#include <bpkg/cfg-link.hxx>

using namespace std;
using namespace butl;

namespace bpkg
{
  shared_ptr<configuration>
  cfg_create (const common_options& o,
              const dir_path& c,
              optional<string> name,
              string type,
              const strings& mods,
              const strings& vars,
              bool existing,
              bool wipe,
              optional<uuid> uid,
              const optional<dir_path>& host_config,
              const optional<dir_path>& build2_config)
  {
    tracer trace ("cfg_create");

    // Stash and restore the current transaction, if any.
    //
    namespace sqlite = odb::sqlite;

    sqlite::transaction* ct (nullptr);
    if (sqlite::transaction::has_current ())
    {
      ct = &sqlite::transaction::current ();
      sqlite::transaction::reset_current ();
    }

    auto tg (make_guard ([ct] ()
                         {
                           if (ct != nullptr)
                             sqlite::transaction::current (*ct);
                         }));

    // First, let's verify the host/build2 configurations existence and types
    // and normalize their paths.
    //
    auto norm = [&trace] (const dir_path& d, const string& t)
    {
      dir_path r (normalize (d, string (t + " configuration").c_str ()));
      database db (r, trace, false /* pre_attach */);
      if (db.type != t)
        fail << t << " configuration " << r << " is of '" << db.type
             << "' type";

      return r;
    };

    optional<dir_path> hc (host_config
                           ? norm (*host_config, host_config_type)
                           : optional<dir_path> ());

    optional<dir_path> bc (build2_config
                           ? norm (*build2_config, build2_config_type)
                           : optional<dir_path> ());

    // Verify the existing directory is compatible with our mode.
    //
    if (exists (c))
    {
      if (existing)
      {
        // Bail if the .bpkg/ directory already exists and is not empty.
        //
        // If you are wondering why don't we allow --wipe here, it's the
        // existing packages that may be littering the configuration --
        // cleaning those up will be messy.
        //
        dir_path d (c / bpkg_dir);

        if (exists (d) && !empty (d))
          fail << "directory " << d << " already exists";
      }
      else
      {
        // If the directory already exists, make sure it is empty.
        //
        if (!empty (c))
        {
          if (!wipe)
            fail << "directory " << c << " is not empty" <<
              info << "use --wipe to clean it up but be careful";

          rm_r (c, false);
        }
      }
    }
    else
    {
      // Note that we allow non-existent directory even in the --existing mode
      // in case the user wants to add the build system configuration later.
      //
      mk_p (c);
    }

    // Create and configure.
    //
    if (existing)
    {
      if (!mods.empty ())
        fail << "module '" << mods[0] << "' specified with --existing|-e";

      if (!vars.empty ())
        fail << "variable '" << vars[0] << "' specified with --existing|-e";
    }
    else
    {
      // Assemble the build2 create meta-operation parameters.
      //
      string params ('\'' + c.representation () + '\'');
      if (!mods.empty ())
      {
        params += ',';
        for (const string& m: mods)
        {
          params += ' ';
          params += m;
        }
      }

      // Run quiet. Use path representation to get canonical trailing slash.
      //
      run_b (o, verb_b::quiet, vars, "create(" + params + ')');
    }

    // Create .bpkg/ and its subdirectories.
    //
    {
      dir_path d (c / bpkg_dir);

      mk (d);
      mk (c / certs_dir);
      mk (c / repos_dir);

      // Create the .gitignore file that ignores everything under .bpkg/
      // effectively making git ignore it (this prevents people from
      // accidentally adding this directory to a git repository).
      //
      path f (d / ".gitignore");
      try
      {
        ofdstream os (f);
        os << "# This directory should not be version-controlled." << '\n'
           << "#"                                                  << '\n'
           << "*"                                                  << '\n';
        os.close ();
      }
      catch (const io_error& e)
      {
        fail << "unable to write to " << f << ": " << e;
      }
    }

    // Initialize tmp directory.
    //
    init_tmp (c);

    // Create the database.
    //
    shared_ptr<configuration> r (make_shared<configuration> (move (name),
                                                             move (type),
                                                             uid));

    dir_paths pre_link;

    if (hc)
      pre_link.push_back (*hc);

    if (bc)
      pre_link.push_back (*bc);

    database db (c, r, trace, pre_link);
    transaction t (db);

    // Add the special, root repository object with empty location and
    // containing a single repository fragment having an empty location as
    // well.
    //
    // Note that the root repository serves as a complement for dir and git
    // repositories that have neither prerequisites nor complements. The
    // root repository fragment is used for transient available package
    // locations and as a search starting point for held packages (see
    // pkg-build for details).
    //
    shared_ptr<repository_fragment> fr (
      make_shared<repository_fragment> (repository_location ()));

    db.persist (fr);

    shared_ptr<repository> rep (
      make_shared<repository> (repository_location ()));

    rep->fragments.push_back (
      repository::fragment_type {string () /* friendly_name */, move (fr)});

    db.persist (rep);

    if (hc)
      cfg_link (db, *hc, host_config->relative (), nullopt /* name */);

    if (bc)
      cfg_link (db, *bc, build2_config->relative (), nullopt /* name */);

    t.commit ();

    return r;
  }

  int
  cfg_create (const cfg_create_options& o, cli::scanner& args)
  {
    tracer trace ("cfg_create");

    if (o.name_specified ())
      validate_configuration_name (o.name (), "--name option value");

    if (o.type ().empty ())
      fail << "empty --type option value";

    if (o.existing () && o.wipe ())
      fail << "both --existing|-e and --wipe specified";

    if (o.wipe () && !o.directory_specified ())
      fail << "--wipe requires explicit --directory|-d";

    dir_path c (o.directory ());
    l4 ([&]{trace << "creating configuration in " << c;});

    // Sort arguments into modules and configuration variables.
    //
    strings mods;
    strings vars;
    while (args.more ())
    {
      string a (args.next ());

      if (a.find ('=') != string::npos)
        vars.push_back (move (trim (a)));
      else if (!a.empty ())
        mods.push_back (move (a));
      else
        fail << "empty string as argument";
    }

    // Auto-generate the configuration UUID, unless it is specified
    // explicitly.
    //
    shared_ptr<configuration> cf (
      cfg_create (
        o,
        c,
        o.name_specified () ? o.name () : optional<string> (),
        o.type (),
        mods,
        vars,
        o.existing (),
        o.wipe (),
        o.uuid_specified () ? o.uuid () : optional<uuid> (),
        (o.host_config_specified () && !o.no_host_config ()
         ? o.host_config ()
         : optional<dir_path> ()),
        (o.build2_config_specified () && !o.no_build2_config ()
         ? o.build2_config ()
         : optional<dir_path> ())));

    if (verb && !o.no_result ())
    {
      normalize (c, "configuration");

      diag_record dr (text);

      if (o.existing ())
        dr << "initialized existing configuration in " << c << '\n';
      else
        dr << "created new configuration in " << c << '\n';

      dr << "  uuid: " << cf->uuid << '\n'
         << "  type: " << cf->type;

      if (cf->name)
        dr << "\n  name: " << *cf->name;
    }

    return 0;
  }

  default_options_files
  options_files (const char*, const cfg_create_options& o, const strings&)
  {
    // NOTE: remember to update the documentation if changing anything here.

    // bpkg.options
    // bpkg-cfg-create.options

    // Use the configuration parent directory as a start directory.
    //
    optional<dir_path> start;

    // Let cfg_create() complain later for the root directory used as a
    // configuration directory.
    //
    dir_path d (normalize (o.directory (), "configuration"));
    if (!d.root ())
      start = d.directory ();

    return default_options_files {
      {path ("bpkg.options"), path ("bpkg-cfg-create.options")},
      move (start)};
  }

  cfg_create_options
  merge_options (const default_options<cfg_create_options>& defs,
                 const cfg_create_options& cmd)
  {
    // NOTE: remember to update the documentation if changing anything here.

    return merge_default_options (
      defs,
      cmd,
      [] (const default_options_entry<cfg_create_options>& e,
          const cfg_create_options&)
      {
        const cfg_create_options& o (e.options);

        auto forbid = [&e] (const char* opt, bool specified)
        {
          if (specified)
            fail (e.file) << opt << " in default options file";
        };

        forbid ("--directory|-d", o.directory_specified ());
        forbid ("--wipe",         o.wipe ()); // Dangerous.
      });
  }
}