Skip to content

Commit

Permalink
allow to compare multiple version with version_compare
Browse files Browse the repository at this point in the history
  • Loading branch information
bruchar1 committed Jan 9, 2025
1 parent a86476c commit bfd7e4b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
8 changes: 8 additions & 0 deletions docs/markdown/snippets/multiple_version_compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## `version_compare` now accept multiple compare strings

Is it now possible to compare version against multiple values, to check for
a range of version for instance.

```meson
'1.5'.version_compare('>=1', '<2')
```
6 changes: 6 additions & 0 deletions docs/yaml/elementary/str.yml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ methods:
It is best to be unambiguous and specify the full revision level to compare.
*Since 1.8.0* multiple version can be compared:
```meson
'3.6'.version_compare('>=3', '<4.0') == true
```
posargs:
compare_string:
type: str
Expand Down
18 changes: 11 additions & 7 deletions mesonbuild/interpreter/primitives/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import typing as T

from ...mesonlib import version_compare
from ...mesonlib import version_compare_many
from ...interpreterbase import (
ObjectHolder,
MesonOperator,
Expand Down Expand Up @@ -169,9 +169,11 @@ def underscorify_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> st
return re.sub(r'[^a-zA-Z0-9]', '_', self.held_object)

@noKwargs
@typed_pos_args('str.version_compare', str)
def version_compare_method(self, args: T.Tuple[str], kwargs: TYPE_kwargs) -> bool:
return version_compare(self.held_object, args[0])
@typed_pos_args('str.version_compare', varargs=str, min_varargs=1)
def version_compare_method(self, args: T.Tuple[T.List[str]], kwargs: TYPE_kwargs) -> bool:
if len(args[0]) > 1:
FeatureNew.single_use('version_compare() with multiple arguments', '1.8.0', self.subproject, location=self.current_node)
return version_compare_many(self.held_object, args[0])[0]

@staticmethod
def _op_div(this: str, other: str) -> str:
Expand Down Expand Up @@ -205,10 +207,12 @@ class MesonVersionString(str):

class MesonVersionStringHolder(StringHolder):
@noKwargs
@typed_pos_args('str.version_compare', str)
def version_compare_method(self, args: T.Tuple[str], kwargs: TYPE_kwargs) -> bool:
@typed_pos_args('str.version_compare', varargs=str, min_varargs=1)
def version_compare_method(self, args: T.Tuple[T.List[str]], kwargs: TYPE_kwargs) -> bool:
if len(args[0]) > 1:
FeatureNew.single_use('version_compare() with multiple arguments', '1.8.0', self.subproject, location=self.current_node)
self.interpreter.tmp_meson_version = args[0]
return version_compare(self.held_object, args[0])
return version_compare_many(self.held_object, args[0])[0]

# These special subclasses of string exist to cover the case where a dependency
# exports a string variable interchangeable with a system dependency. This
Expand Down

0 comments on commit bfd7e4b

Please sign in to comment.