Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RPM compatible version compare #108

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading