Move regex string to a function

Avoids having duplicates in similar functions
This commit is contained in:
bom 2023-11-17 13:57:11 +01:00
parent f7897a574d
commit bcccfd8b9c

View file

@ -45,9 +45,21 @@ class BuildFile(Validateable):
result = None result = None
return result 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: def get_version(self) -> Version:
try: try:
match self.build_file_type(): build_file_type = self.build_file_type()
match build_file_type:
case BuildFileType.JS: case BuildFileType.JS:
version_str = json.loads(self.content)["version"] version_str = json.loads(self.content)["version"]
case BuildFileType.JAVA_GRADLE: case BuildFileType.JAVA_GRADLE:
@ -55,7 +67,7 @@ class BuildFile(Validateable):
version_line = re.search("\nversion = .*", self.content) version_line = re.search("\nversion = .*", self.content)
version_line_group = version_line.group() version_line_group = version_line.group()
version_string = re.search( 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() version_str = version_string.group()
case BuildFileType.PYTHON: case BuildFileType.PYTHON:
@ -63,7 +75,7 @@ class BuildFile(Validateable):
version_line = re.search("\nversion = .*\n", self.content) version_line = re.search("\nversion = .*\n", self.content)
version_line_group = version_line.group() version_line_group = version_line.group()
version_string = re.search( 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_line_group,
) )
version_str = version_string.group() version_str = version_string.group()
@ -72,7 +84,7 @@ class BuildFile(Validateable):
version_line = re.search("\\(defproject .*\n", self.content) version_line = re.search("\\(defproject .*\n", self.content)
version_line_group = version_line.group() version_line_group = version_line.group()
version_string = re.search( 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() version_str = version_string.group()
except: except:
@ -90,21 +102,22 @@ class BuildFile(Validateable):
new_version.snapshot_suffix = self.get_default_suffix() new_version.snapshot_suffix = self.get_default_suffix()
try: try:
match self.build_file_type(): build_file_type = self.build_file_type()
match build_file_type:
case BuildFileType.JS: case BuildFileType.JS:
json_data = json.loads(self.content) json_data = json.loads(self.content)
json_data["version"] = new_version.to_string() json_data["version"] = new_version.to_string()
self.content = json.dumps(json_data, indent=4) self.content = json.dumps(json_data, indent=4)
case BuildFileType.JAVA_GRADLE: case BuildFileType.JAVA_GRADLE:
substitute = re.sub( 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()}"', f'\nversion = "{new_version.to_string()}"',
self.content, self.content,
) )
self.content = substitute self.content = substitute
case BuildFileType.PYTHON: case BuildFileType.PYTHON:
substitute = re.sub( 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()}"', f'\nversion = "{new_version.to_string()}"',
self.content, self.content,
) )
@ -112,7 +125,7 @@ class BuildFile(Validateable):
case BuildFileType.JAVA_CLOJURE: case BuildFileType.JAVA_CLOJURE:
# TODO: we should stick here on defproject instead of first line! # TODO: we should stick here on defproject instead of first line!
substitute = re.sub( 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()}"', f'"{new_version.to_string()}"',
self.content, self.content,
1, 1,