aboutsummaryrefslogtreecommitdiff
path: root/bdep/init.cxx
blob: a86079f5caa8f1aaae192110196935e29b69f135 (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
// file      : bdep/init.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bdep/init.hxx>

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

#include <bdep/sync.hxx>
#include <bdep/config.hxx>

using namespace std;

namespace bdep
{
  int
  cmd_init (const cmd_init_options& o, cli::scanner&)
  {
    tracer trace ("init");

    project_packages pp (
      find_project_packages (o, o.empty () /* ignore_packages */));

    const dir_path& prj (pp.project);

    text << prj;
    for (const package_location& pl: pp.packages)
      text << "  " << pl.name << " " << (prj / pl.path);

    // Create .bdep/.
    //
    {
      dir_path d (prj / bdep_dir);

      if (!exists (d))
        mk (prj / bdep_dir);
    }

    // Open the database creating it if necessary.
    //
    database db (open (prj, trace, true /* create */));

    // --empty
    //
    bool ca (o.config_add_specified ());
    bool cc (o.config_create_specified ());

    if (o.empty ())
    {
      if (ca) fail << "both --empty and --config-add specified";
      if (cc) fail << "both --empty and --config-create specified";

      //@@ TODO: what should we do if the database already exists?

      return 0;
    }

    // Make sure everyone refers to the same objects across all the
    // transactions.
    //
    session s;

    // --config-add/create
    //
    configurations cfgs;
    if (ca || cc)
    {
      const char* m (!ca ? "--config-create" :
                     !cc ? "--config-add"    :
                     nullptr);

      if (m == nullptr)
        fail << "both --config-add and --config-create specified";

      optional<string> name;
      if (size_t n = o.config_name ().size ())
      {
        if (n > 1)
          fail << "multiple configuration names specified for " << m;

        name = o.config_name ()[0];
      }

      optional<uint64_t> id;
      if (size_t n = o.config_id ().size ())
      {
        if (n > 1)
          fail << "multiple configuration ids specified for " << m;

        id = o.config_id ()[0];
      }

      cfgs.push_back (
        ca
        ? cmd_config_add (prj,
                          db,
                          o.config_add (),
                          move (name),
                          nullopt /* default */, // @@ TODO: --[no]-default
                          move (id))
        : nullptr); // @@ TODO: create

      // Fall through.
    }

    // If this is the default mode, then find the configurations the user
    // wants us to use.
    //
    if (cfgs.empty ())
    {
      transaction t (db.begin ());
      cfgs = find_configurations (prj, t, o);
      t.commit ();
    }

    // Initialize each package in each configuration skipping those that are
    // already initialized. Do each configuration in a separate transaction so
    // that our state reflects the bpkg configuration as closely as possible.
    // Then synchronize each configuration.
    //
    for (const shared_ptr<configuration>& c: cfgs)
    {
      transaction t (db.begin ());

      // Add project repository to the configuration. Note that we don't fetch
      // it since sync is going to do it anyway.
      //
      run_bpkg (o,
                "add",
                "-d", c->path,
                "--type", "dir",
                prj);

      for (const package_location& p: pp.packages)
      {
        if (find_if (c->packages.begin (),
                     c->packages.end (),
                     [&p] (const package_state& s)
                     {
                       return p.name == s.name;
                     }) != c->packages.end ())
        {
          if (verb)
            info << "package " << p.name << " is already initialized "
                 << "in configuration " << *c;

          continue;
        }

        c->packages.push_back (package_state {p.name});
      }

      db.update (c);
      t.commit ();

      //@@ --no-sync for some reason?
      //
      cmd_sync (o, prj, c);
    }

    //@@ TODO: print project/package(s) being initialized.

    return 0;
  }
}