aboutsummaryrefslogtreecommitdiff
path: root/bdep/init.cxx
blob: 44eb37eb3e0b25e7cb6318cac4cbf748f7b3b265 (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
// 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/config.hxx>
#include <bdep/project.hxx>
#include <bdep/database.hxx>
#include <bdep/diagnostics.hxx>

using namespace std;

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

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

    const dir_path& prj (pp.project);

    text << prj;
    for (const dir_path& d: pp.packages)
      text << "  " << (prj / d);

    // 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 (pp.project, 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.
    }

    transaction t (db.begin ());

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

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

    t.commit ();

    return 0;
  }
}