Skip to content

Commit

Permalink
WIP: add support for RPM compatible version compare
Browse files Browse the repository at this point in the history
This uses a library that I'd written for another project that implements
RPM compatible version comparisons.

TODO: needs some tests
TODO: rpm-version cmake support needs to land
  • Loading branch information
dcbaker committed Sep 27, 2024
1 parent 607c449 commit 51e60f9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
13 changes: 13 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ dep_cli11 = dependency('CLI11', version: '>=2.1')
dep_expected = dependency('tl-expected', version : '>= 1.0', modules : ['tl::expected'])
dep_json = dependency('nlohmann_json', version : '>= 3.7')
dep_fmt = dependency('fmt', version : '>= 8')
dep_rpm_version = dependency(
'rpm-version-c++',
version : '>= 0.3.0',
required : false,
allow_fallback : true,
default_options : ['default_library=static'],
)
if dep_rpm_version.found()
dep_rpm_version = declare_dependency(
dependencies : dep_rpm_version,
compile_args : '-DCPS_HAVE_RPM_VERSION',
)
endif

cpp = meson.get_compiler('cpp')

Expand Down
43 changes: 41 additions & 2 deletions src/cps/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "cps/utils.hpp"

#include <fmt/core.h>
#ifdef CPS_HAVE_RPM_VERSION
#include <rpm-version/version.hpp>
#endif

#include <cstdint>

Expand Down Expand Up @@ -97,16 +100,52 @@ namespace cps::version {
return (op == Operator::eq || op == Operator::le || op == Operator::ge);
}

#ifdef CPS_HAVE_RPM_VERSION
RPMVersion::Operator to_rpm_op(const Operator op) {
switch (op) {
case Operator::eq:
return RPMVersion::Operator::eq;
case Operator::ne:
return RPMVersion::Operator::ne;
case Operator::ge:
return RPMVersion::Operator::ge;
case Operator::gt:
return RPMVersion::Operator::gt;
case Operator::le:
return RPMVersion::Operator::le;
case Operator::lt:
return RPMVersion::Operator::lt;
default:
throw std::runtime_error{"This should not be possible"};
}
}
#endif

} // namespace

tl::expected<bool, std::string> compare(std::string_view left, Operator op, std::string_view right, Schema schema) {
std::string msg{};

switch (schema) {
case Schema::simple:
return simple_compare(left, op, right);
case Schema::rpm:
#ifdef CPS_HAVE_RPM_VERSION
return RPMVersion::compare(left, to_rpm_op(op), right);
#else
msg = "RPM Compatible version comparison is not enabled";
break;
#endif
case Schema::dpkg:
msg = "DPKG Compatible version comparison is not implemented";
break;
default:
fmt::print(stderr, "Only the simple schema is implemented");
return "Only the simple schema is implemented.";
msg = "The requested version comparison method is not implemented";
break;
}

fmt::print(stderr, msg);
return tl::unexpected{msg};
}

} // namespace cps::version
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ libcps = static_library(
'cps/utils.cpp',
'cps/version.cpp',
conf_h,
dependencies : [dep_json, dep_expected, dep_fmt],
dependencies : [dep_json, dep_expected, dep_fmt, dep_rpm_version],
cpp_args : warn_args,
include_directories: [cps_include_dir, conf_include_dir],
)
Expand Down
10 changes: 10 additions & 0 deletions subprojects/rpm-version.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[wrap-file]
directory = rpm-version-0.3.0

source_url = https://github.com/dcbaker/rpm-version/archive/refs/tags/0.3.0.tar.gz
source_filename = rpm-version-0.3.0.tar.gz
source_hash = a5b7ae1a1e69fbe3ee529af719ef8a7e46a97a2cfc94c470834b83a346750623

[provide]
rpm-version-c = dep_rpm_ver_c
rpm-version-c++ = dep_rpm_ver_cpp
5 changes: 5 additions & 0 deletions tests/cases/rpm-version.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[case]]
name = "version"
cps = "rpm-version-compare"
args = ["--version"]
expected = "0.0.1"

0 comments on commit 51e60f9

Please sign in to comment.