aboutsummaryrefslogtreecommitdiff
path: root/bdep/new.cxx
blob: 9ab63f4e0ef98811abde027625afad9e1882fa19 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// file      : bdep/new.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bdep/new.hxx>

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

#include <bdep/init.hxx>

using namespace std;

namespace bdep
{
  using type = cmd_new_type;
  using lang = cmd_new_lang;

  int
  cmd_new (const cmd_new_options& o, cli::scanner& args)
  {
    tracer trace ("new");

    bool ca (o.config_add_specified ());
    bool cc (o.config_create_specified ());

    optional<bool> cd;
    if (o.default_ () || o.no_default ())
    {
      if (!ca && !cc)
        fail << "--[no-]default specified without --config-(add|create)";

      if (o.default_ () && o.no_default ())
        fail << "both --default and --no-default specified";

      cd = o.default_ () && !o.no_default ();
    }

    if (o.no_init ())
    {
      if (ca) fail << "both --no-init and --config-add specified";
      if (cc) fail << "both --no-init and --config-create specified";
    }

    // Validate type options.
    //
    const type& t (o.type ());

    // Validate language options.
    //
    const lang& l (o.lang ());

    switch (l)
    {
    case lang::c:
      {
        break;
      }
    case lang::cxx:
      {
        auto& o (l.cxx_opt);

        if (o.cpp () && o.cxx ())
          fail << "'cxx' and 'cpp' are mutually exclusive c++ options";

        break;
      }
    }

    // Validate argument.
    //
    string n (args.more () ? args.next () : "");
    if (n.empty ())
      fail << "project name argument expected";

    //@@ TODO: verify valid package name (put the helper in libbpkg).

    if (o.type () == type::lib && n.compare (0, 3, "lib") != 0)
      fail << "library name does not start with 'lib'";

    dir_path prj (n);
    prj.complete ();

    // If the directory already exists, make sure it is empty. Otherwise
    // create it.
    //
    if (!exists (prj))
      mk (prj);
    else if (!empty (prj))
      fail << "directory " << prj << " already exists";

    // Initialize the git repository. Do it before writing anything ourselves
    // in case it fails.
    //
    if (!o.no_git ())
      run ("git", "init", "-q", prj);

    path f; // File currently being written.
    try
    {
      ofdstream os;

      // manifest
      //
      os.open (f = prj / "manifest");
      os << ": 1"                                                      << endl
         << "name: " << n                                              << endl
         << "version: 0.1.0-a.0.z"                                     << endl
         << "summary: new " << t << " project"                         << endl
         << "license: proprietary"                                     << endl
         << "url: https://example.org/" << n                           << endl
         << "email: you@example.org"                                   << endl
         << "depends: * build2 >= 0.7.0-"                              << endl
         << "depends: * bpkg >= 0.7.0-"                                << endl;
      os.close ();

      // repositories.manifest
      //
      os.open (f = prj / "repositories.manifest");
      os << ": 1"                                                      << endl
         << "# To add a repository for a dependency, uncomment the"    << endl
         << "# next four lines and specify its location."              << endl
         << "#role: prerequisite"                                      << endl
         << "#location: ..."                                           << endl
         << "#"                                                        << endl
         << "#:"                                                       << endl
         << "role: base"                                               << endl
         << "summary: " << n << " project repository"                  << endl;
      os.close ();

      // build/
      //
      dir_path bd (dir_path (prj) /= "build");
      mk (bd);

      // build/bootstrap.build
      //
      os.open (f = bd / "bootstrap.build");
      os << "project = " << n                                          << endl
         <<                                                               endl
         << "using version"                                            << endl
         << "using config"                                             << endl
         << "using test"                                               << endl
         << "using dist"                                               << endl
         << "using install"                                            << endl;
      os.close ();

      // build/root.build
      //
      os.open (f = bd / "root.build");
      switch (l)
      {
      case lang::c:
        {
          // @@ TODO: 'latest' in c.std.
          //
          os //<< "c.std = latest"                                       << endl
             //<<                                                           endl
             << "using c"                                              << endl
             <<                                                           endl
             << "h{*}: extension = h"                                  << endl
             << "c{*}: extension = c"                                  << endl
             <<                                                           endl
             << "c.poptions =+ \"-I$out_root\" \"-I$src_root\""        << endl;
          break;
        }
      case lang::cxx:
        {
          const char* s (l.cxx_opt.cpp () ? "pp" : "xx");

          os << "cxx.std = latest"                                     << endl
             <<                                                           endl
             << "using cxx"                                            << endl
             <<                                                           endl
             << "hxx{*}: extension = h" << s                           << endl
             << "ixx{*}: extension = i" << s                           << endl
             << "txx{*}: extension = t" << s                           << endl
             << "cxx{*}: extension = c" << s                           << endl
             <<                                                           endl
             << "cxx.poptions =+ \"-I$out_root\" \"-I$src_root\""      << endl;
          break;
        }
      }
      os.close ();

      // build/.gitignore
      //
      if (!o.no_git ())
      {
        os.open (f = bd / ".gitignore");
        os << "config.build"                                           << endl
           << "bootstrap/out-root.build"                               << endl;
        os.close ();
      }

      // buildfile
      //
      os.open (f = prj / "buildfile");
      os << "./: {*/ -build/} file{manifest}"                          << endl;
      os.close ();

      // .gitignore
      //
      if (!o.no_git ())
      {
        // Use POSIX directory separators here.
        //
        os.open (f = prj / ".gitignore");
        os << bdep_dir.string () << '/'                                << endl
           <<                                                             endl
           << "# Compiler/linker output."                              << endl
           << "#"                                                      << endl
           << "*.d"                                                    << endl
           << "*.t"                                                    << endl
           << "*.i"                                                    << endl
           << "*.ii"                                                   << endl
           << "*.o"                                                    << endl
           << "*.obj"                                                  << endl
           << "*.so"                                                   << endl
           << "*.dll"                                                  << endl
           << "*.a"                                                    << endl
           << "*.lib"                                                  << endl
           << "*.exp"                                                  << endl
           << "*.pdb"                                                  << endl
           << "*.ilk"                                                  << endl
           << "*.exe"                                                  << endl
           << "*.exe.dlls/"                                            << endl
           << "*.exe.manifest"                                         << endl
           << "*.pc"                                                   << endl;
        os.close ();
      }

      // <name>/ (source subdirectory).
      //
      dir_path sd (dir_path (prj) /= n);

      if (t != type::bare)
      {
        mk (sd);
        os.open (f = sd / "buildfile");
      }

      switch (t)
      {
      case type::exe:
        {
          switch (l)
          {
          case lang::c:
            {
              // buildfile
              //
              os << "exe{" << n << "}: {h c}{*}" << endl;
              os.close ();

              // <name>.c
              //
              os.open (f = sd / n + ".c");
              os << "#include <stdio.h>"                               << endl
                 <<                                                       endl
                 << "int main ()"                                      << endl
                 << "{"                                                << endl
                 << "  printf (\"Hello, World!\\n\");"                 << endl
                 << "  return 0;"                                      << endl
                 << "}"                                                << endl;
              os.close ();
              break;
            }
          case lang::cxx:
            {
              // buildfile
              //
              os << "exe{" << n << "}: {hxx ixx txx cxx}{*}" << endl;
              os.close ();

              const char* s (l.cxx_opt.cpp () ? "pp" : "xx");

              // <name>.c(xx|pp)
              //
              os.open (f = sd / n + ".c" + s);
              os << "#include <iostream>"                              << endl
                 <<                                                       endl
                 << "int main ()"                                      << endl
                 << "{"                                                << endl
                 << "  std::cout << \"Hello, World!\" << std::endl;"   << endl
                 << "}"                                                << endl;
              os.close ();
              break;
            }
          }
          break;
        }
      case type::lib:
        {
          switch (l)
          {
          case lang::c:
            {
              // buildfile
              //
              os << "lib{" << n << "}: {h c}{*}" << endl;
              os.close ();

              //@@ TODO

              break;
            }
          case lang::cxx:
            {
              // buildfile
              //
              os << "lib{" << n << "}: {hxx ixx txx cxx}{*}" << endl;
              os.close ();

              //@@ TODO

              break;
            }
          }
          break;
        }
      case type::bare:
        {
          break;
        }
      }
    }
    catch (const io_error& e)
    {
      fail << "unable to write " << f << ": " << e;
    }

    if (verb)
      text << "created new " << t << " project " << n << " in " << prj;

    // --no-init
    //
    if (o.no_init ())
      return 0;

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

    // Everything else requires a database.
    //
    database db (open (prj, trace, true /* create */));

    if (ca || cc)
    {
      configurations cfgs {
        cmd_init_config (
          o,
          prj,
          db,
          ca ? o.config_add () : o.config_create (),
          args,
          ca,
          cc,
          cd)};

      package_locations pkgs {{n, dir_path ()}}; // project == package

      cmd_init (o, prj, db, cfgs, pkgs);
    }

    return 0;
  }
}