aboutsummaryrefslogtreecommitdiff
path: root/bpkg/pkg-command.cxx
blob: 60afb08ea8b754b16775585985d5c49bf6dd023c (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
// file      : bpkg/pkg-command.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/pkg-command>

#include <bpkg/types>
#include <bpkg/package>
#include <bpkg/package-odb>
#include <bpkg/utility>
#include <bpkg/database>
#include <bpkg/diagnostics>

using namespace std;
using namespace butl;

namespace bpkg
{
  void
  pkg_command (const string& cmd,
               const dir_path& c,
               const common_options& o,
               const shared_ptr<selected_package>& p)
  {
    tracer trace ("pkg_command");

    level4 ([&]{trace << "command: " << cmd;});

    assert (p->state == package_state::configured);
    assert (p->out_root); // Should be present since configured.

    dir_path out_root (c / *p->out_root); // Always relative.
    level4 ([&]{trace << "out_root: " << out_root;});

    // Form the buildspec.
    //
    string bspec (cmd + "(" + out_root.string () + "/)");
    level4 ([&]{trace << "buildspec: " << bspec;});

    run_b (o, bspec);
  }

  void
  pkg_command (const string& cmd,
               const dir_path& c,
               const common_options& o,
               const vector<shared_ptr<selected_package>>& ps)
  {
    tracer trace ("pkg_command");

    level4 ([&]{trace << "command: " << cmd;});

    // Form the buildspec.
    //
    string bspec (cmd + "(");

    for (const shared_ptr<selected_package>& p: ps)
    {
      assert (p->state == package_state::configured);
      assert (p->out_root); // Should be present since configured.

      dir_path out_root (c / *p->out_root); // Always relative.
      level4 ([&]{trace << p->name << " out_root: " << out_root;});

      if (bspec.back () != '(')
        bspec += ' ';
      bspec += out_root.string ();
      bspec += '/';
    }

    bspec += ')';

    level4 ([&]{trace << "buildspec: " << bspec;});

    run_b (o, bspec);
  }

  int
  pkg_command (const string& cmd,
               const configuration_options& o,
               cli::scanner& args)
  {
    tracer trace ("pkg_command");

    const dir_path& c (o.directory ());
    level4 ([&]{trace << "configuration: " << c;});

    if (!args.more ())
      fail << "package name argument expected" <<
        info << "run 'bpkg help pkg-" << cmd << "' for more information";

    vector<shared_ptr<selected_package>> ps;
    {
      database db (open (c, trace));
      transaction t (db.begin ());

      while (args.more ())
      {
        string n (args.next ());
        shared_ptr<selected_package> p (db.find<selected_package> (n));

        if (p == nullptr)
          fail << "package " << n << " does not exist in configuration " << c;

        if (p->state != package_state::configured)
          fail << "package " << n << " is " << p->state <<
            info << "expected it to be configured";

        level4 ([&]{trace << p->name << " " << p->version;});
        ps.push_back (move (p));
      }

      t.commit ();
    }

    pkg_command (cmd, c, o, ps);

    if (verb)
    {
      for (const shared_ptr<selected_package>& p: ps)
        text << cmd << (cmd.back () != 'e' ? "ed " : "d ")
             << p->name << " " << p->version;
    }

    return 0;
  }
}