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

#ifndef BPKG_UTILITY_HXX
#define BPKG_UTILITY_HXX

#include <memory>   // make_shared()
#include <string>   // to_string()
#include <utility>  // move(), forward(), declval(), make_pair()
#include <cassert>  // assert()
#include <iterator> // make_move_iterator()

#include <libbutl/ft/lang.hxx>

#include <libbutl/utility.mxx> // casecmp(), reverse_iterate(), etc

#include <libbutl/filesystem.mxx>

#include <bpkg/types.hxx>
#include <bpkg/version.hxx>

namespace bpkg
{
  using std::move;
  using std::forward;
  using std::declval;

  using std::make_pair;
  using std::make_shared;
  using std::make_move_iterator;
  using std::to_string;

  // <libbutl/utility.mxx>
  //
  using butl::casecmp;
  using butl::reverse_iterate;

  using butl::exception_guard;
  using butl::make_exception_guard;

  // Empty string and path.
  //
  extern const string empty_string;
  extern const path empty_path;
  extern const dir_path empty_dir_path;

  // Widely-used paths.
  //
  extern const dir_path bpkg_dir;  // .bpkg/
  extern const dir_path certs_dir; // .bpkg/certs/

  // Temporary directory.
  //
  // This is normally .bpkg/tmp/ but can also be some system-wide directory
  // (e.g., /tmp/bpkg-XXX/) if there is no bpkg configuration. This directory
  // is automatically created and cleaned up for most commands in main() so
  // you don't need to call init_tmp() explicitly except for certain special
  // commands (like cfg-create).
  //
  extern dir_path tmp_dir;

  void
  init_tmp (const dir_path& cfg);

  void
  clean_tmp (bool ignore_errors);

  // Y/N prompt. The def argument, if specified, should be either 'y'
  // or 'n'. It is used as the default answer, in case the user just
  // hits enter. Issue diagnostics and throw failed if no answer could
  // be extracted from STDOUT (e.g., because it was closed).
  //
  bool
  yn_prompt (const char* prompt, char def = '\0');

  // Filesystem.
  //
  bool
  exists (const path&, bool ignore_error = false);

  bool
  exists (const dir_path&, bool ignore_error = false);

  bool
  empty (const dir_path&);

  void
  mk (const dir_path&);

  void
  mk_p (const dir_path&);

  void
  rm (const path&, uint16_t verbosity = 3);

  void
  rm_r (const dir_path&,
        bool dir_itself = true,
        uint16_t verbosity = 3,
        bool ignore_error = false);

  using auto_rm = butl::auto_rmfile;
  using auto_rm_r = butl::auto_rmdir;

  // Process.
  //
  // By default the process command line is printed for verbosity >= 2
  // (essential command lines).
  //
  // If fallback is specified, then this directory is searched for the
  // executable as a last resort.
  //
  void
  run (const char* args[], const dir_path& fallback = dir_path ());

  inline void
  run (cstrings& args, const dir_path& fallback = dir_path ())
  {
    run (args.data (), fallback);
  }

  // Directory extracted from argv[0] (i.e., this process' recall directory)
  // or empty if there is none. Can be used as a search fallback.
  //
  extern dir_path exec_dir;

  // Run build2, mapping verbosity levels. If quiet is true, then run build2
  // quiet if our verbosity level is 1. Common vars (cvars) are set on the
  // configuration scope.
  //
  class common_options;

  const char*
  name_b (const common_options&);

  void
  run_b (const common_options&,
         const dir_path& configuration,
         const string& buildspec,
         bool quiet = false,
         const strings& pvars = strings (),
         const strings& cvars = strings ());
}

#endif // BPKG_UTILITY_HXX