Update regexes for gradle and python files

This commit is contained in:
bom 2023-11-17 14:58:53 +01:00
parent 36df9f486b
commit 6399a1dfeb

View file

@ -48,11 +48,11 @@ class BuildFile(Validateable):
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)?"
return r'(?P<pre_version>version\s?=\s?)\"(?P<version>\d*\.\d*\.\d*(-SNAPSHOT)?)\"'
case BuildFileType.PYTHON:
return "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?(-dev)?[0-9]*"
return r'(?P<pre_version>version\s?=\s?)\"(?P<version>\d*\.\d*\.\d*(-SNAPSHOT|-dev\d*)?)\"'
case BuildFileType.JAVA_CLOJURE:
return r'(?P<project>\(defproject\s(\S)*\s)\"(?P<version>\d*\.\d*\.\d*(-SNAPSHOT)?)\"'
return r'(?P<pre_version>\(defproject\s(\S)*\s)\"(?P<version>\d*\.\d*\.\d*(-SNAPSHOT)?)\"'
case _:
return ""
@ -63,22 +63,9 @@ class BuildFile(Validateable):
case BuildFileType.JS:
version_str = json.loads(self.content)["version"]
case BuildFileType.JAVA_GRADLE:
# TODO: '\nversion = ' will not parse all ?!
version_line = re.search("\nversion = .*", self.content)
version_line_group = version_line.group()
version_string = re.search(
self.__get_file_type_regex_str(build_file_type), version_line_group
)
version_str = version_string.group()
version_str = re.search(self.__get_file_type_regex_str(build_file_type), self.content).group("version")
case BuildFileType.PYTHON:
# TODO: '\nversion = ' will not parse all ?!
version_line = re.search("\nversion = .*\n", self.content)
version_line_group = version_line.group()
version_string = re.search(
self.__get_file_type_regex_str(build_file_type),
version_line_group,
)
version_str = version_string.group()
version_str = re.search(self.__get_file_type_regex_str(build_file_type), self.content).group("version")
case BuildFileType.JAVA_CLOJURE:
version_str = re.search(self.__get_file_type_regex_str(build_file_type), self.content).group("version")
except:
@ -103,22 +90,24 @@ class BuildFile(Validateable):
self.content = json.dumps(json_data, indent=4)
case BuildFileType.JAVA_GRADLE:
substitute = re.sub(
f'\nversion = "{self.__get_file_type_regex_str(build_file_type)}"',
f'\nversion = "{new_version.to_string()}"',
self.__get_file_type_regex_str(build_file_type),
fr'\g<pre_version>"{new_version.to_string()}"',
self.content,
1,
)
self.content = substitute
case BuildFileType.PYTHON:
substitute = re.sub(
f'\nversion = "{self.__get_file_type_regex_str(build_file_type)}"',
f'\nversion = "{new_version.to_string()}"',
self.__get_file_type_regex_str(build_file_type),
fr'\g<pre_version>"{new_version.to_string()}"',
self.content,
1,
)
self.content = substitute
case BuildFileType.JAVA_CLOJURE:
substitute = re.sub(
self.__get_file_type_regex_str(build_file_type),
fr'\g<project>"{new_version.to_string()}"',
fr'\g<pre_version>"{new_version.to_string()}"',
self.content,
1,
)