From bcccfd8b9cb4dbfbe1bb34b4dbcef112af8dd653 Mon Sep 17 00:00:00 2001 From: bom Date: Fri, 17 Nov 2023 13:57:11 +0100 Subject: [PATCH] Move regex string to a function Avoids having duplicates in similar functions --- .../python/ddadevops/domain/build_file.py | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/python/ddadevops/domain/build_file.py b/src/main/python/ddadevops/domain/build_file.py index 89feed6..1a65bf2 100644 --- a/src/main/python/ddadevops/domain/build_file.py +++ b/src/main/python/ddadevops/domain/build_file.py @@ -45,9 +45,21 @@ class BuildFile(Validateable): result = None return result + def __get_file_type_regex_str(self, file_type: BuildFileType): + match file_type: + case BuildFileType.JAVA_GRADLE: + return "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?" + case BuildFileType.PYTHON: + return "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?(-dev)?[0-9]*" + case BuildFileType.JAVA_CLOJURE: + return "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?" + case _: + return "" + def get_version(self) -> Version: try: - match self.build_file_type(): + build_file_type = self.build_file_type() + match build_file_type: case BuildFileType.JS: version_str = json.loads(self.content)["version"] case BuildFileType.JAVA_GRADLE: @@ -55,7 +67,7 @@ class BuildFile(Validateable): version_line = re.search("\nversion = .*", self.content) version_line_group = version_line.group() version_string = re.search( - "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?", version_line_group + self.__get_file_type_regex_str(build_file_type), version_line_group ) version_str = version_string.group() case BuildFileType.PYTHON: @@ -63,7 +75,7 @@ class BuildFile(Validateable): version_line = re.search("\nversion = .*\n", self.content) version_line_group = version_line.group() version_string = re.search( - "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?(-dev)?[0-9]*", + self.__get_file_type_regex_str(build_file_type), version_line_group, ) version_str = version_string.group() @@ -72,7 +84,7 @@ class BuildFile(Validateable): version_line = re.search("\\(defproject .*\n", self.content) version_line_group = version_line.group() version_string = re.search( - "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?", version_line_group + self.__get_file_type_regex_str(build_file_type), version_line_group ) version_str = version_string.group() except: @@ -90,21 +102,22 @@ class BuildFile(Validateable): new_version.snapshot_suffix = self.get_default_suffix() try: - match self.build_file_type(): + build_file_type = self.build_file_type() + match build_file_type: case BuildFileType.JS: json_data = json.loads(self.content) json_data["version"] = new_version.to_string() self.content = json.dumps(json_data, indent=4) case BuildFileType.JAVA_GRADLE: substitute = re.sub( - '\nversion = "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"', + f'\nversion = "{self.__get_file_type_regex_str(build_file_type)}"', f'\nversion = "{new_version.to_string()}"', self.content, ) self.content = substitute case BuildFileType.PYTHON: substitute = re.sub( - '\nversion = "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?(-dev)?[0-9]*"', + f'\nversion = "{self.__get_file_type_regex_str(build_file_type)}"', f'\nversion = "{new_version.to_string()}"', self.content, ) @@ -112,7 +125,7 @@ class BuildFile(Validateable): case BuildFileType.JAVA_CLOJURE: # TODO: we should stick here on defproject instead of first line! substitute = re.sub( - '"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"', + f'"{self.__get_file_type_regex_str(build_file_type)}"', f'"{new_version.to_string()}"', self.content, 1,