aboutsummaryrefslogtreecommitdiff
path: root/bpkg/system-package-manager.test.hxx
blob: 688eb72c23d2310f8aaf3e9ec9237e83454b1947 (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
// file      : bpkg/system-package-manager.test.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef BPKG_SYSTEM_PACKAGE_MANAGER_TEST_HXX
#define BPKG_SYSTEM_PACKAGE_MANAGER_TEST_HXX

#include <bpkg/system-package-manager.hxx>

#include <algorithm> // sort()

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

#include <libbutl/manifest-parser.hxx>

#include <libbpkg/manifest.hxx>

#include <bpkg/package.hxx>

namespace bpkg
{
  // Parse the manifest as if it comes from a git repository with a single
  // package and make an available package out of it. If the file name is
  // `-` then read fro stdin. If the package name is empty, then take the
  // name from the manifest. Otherwise, assert they match.
  //
  inline
  pair<shared_ptr<available_package>, lazy_shared_ptr<repository_fragment>>
  make_available_from_manifest (const string& pn, const string& f)
  {
    using butl::manifest_parser;
    using butl::manifest_parsing;

    path fp (f);
    path_name fn (fp);

    try
    {
      ifdstream ifds;
      istream& ifs (butl::open_file_or_stdin (fn, ifds));

      manifest_parser mp (ifs, fn.name ? *fn.name : fn.path->string ());

      package_manifest m (mp,
                          false /* ignore_unknown */,
                          true /* complete_values */);

      const string& n (m.name.string ());
      assert (pn.empty () || n == pn);

      m.alt_naming = false;
      m.bootstrap_build = "project = " + n + '\n';

      shared_ptr<available_package> ap (
        make_shared<available_package> (move (m)));

      lazy_shared_ptr<repository_fragment> af (
        make_shared<repository_fragment> (
          repository_location ("https://example.com/" + n,
                               repository_type::git)));

      ap->locations.push_back (package_location {af, current_dir});

      return make_pair (move (ap), move (af));
    }
    catch (const manifest_parsing& e)
    {
      fail (e.name, e.line, e.column) << e.description << endf;
    }
    catch (const io_error& e)
    {
      fail << "unable to read from " << fn << ": " << e << endf;
    }
  }

  // Make an available stub package as if it comes from git repository with
  // a single package.
  //
  inline
  pair<shared_ptr<available_package>, lazy_shared_ptr<repository_fragment>>
  make_available_stub (const string& n)
  {
    shared_ptr<available_package> ap (
      make_shared<available_package> (package_name (n)));

    lazy_shared_ptr<repository_fragment> af (
      make_shared<repository_fragment> (
        repository_location ("https://example.com/" + n,
                             repository_type::git)));

    ap->locations.push_back (package_location {af, current_dir});

    return make_pair (move (ap), move (af));
  }

  // Sort available packages in the version descending order.
  //
  inline void
  sort_available (available_packages& aps)
  {
    using element_type =
      pair<shared_ptr<available_package>, lazy_shared_ptr<repository_fragment>>;

    std::sort (aps.begin (), aps.end (),
               [] (const element_type& x, const element_type& y)
               {
                 return x.first->version > y.first->version;
               });
  }
}

#endif // BPKG_SYSTEM_PACKAGE_MANAGER_TEST_HXX