Add support for gradle files

This commit is contained in:
bom 2023-02-09 11:43:10 +01:00
parent 8a3e77aadf
commit 1995a7b200
2 changed files with 6388 additions and 3 deletions

6352
build.gradle Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,13 +1,13 @@
import json import json
from enum import Enum from enum import Enum
import re
def init_project(): def init_project():
# validate_values() # validate_values()
version = Version('package.json') version = Version('build.gradle')
version.parse() version.parse()
version.increment(ReleaseLevel.MAJOR) version.increment(ReleaseLevel.SNAPSHOT)
version.write() version.write()
print(version.get())
def prepare_release(): def prepare_release():
pass pass
@ -33,6 +33,8 @@ class Version():
match self.config_file_type: match self.config_file_type:
case 'json': case 'json':
self.__parse_json() self.__parse_json()
case 'gradle':
self.__parse_gradle()
def __parse_json(self): def __parse_json(self):
with open(self.config_file_path, 'r') as json_file: with open(self.config_file_path, 'r') as json_file:
@ -42,6 +44,25 @@ class Version():
json_version = json_version.replace('-SNAPSHOT', '') json_version = json_version.replace('-SNAPSHOT', '')
self.version = [int(x) for x in json_version.split('.')] self.version = [int(x) for x in json_version.split('.')]
def __parse_gradle(self):
with open(self.config_file_path, 'r') as gradle_file:
contents = gradle_file.read()
version_line = re.search("\nversion = .*\n", contents)
if version_line is None:
raise Exception("Version not found in gradle file")
version_line = version_line.group()
version_string = re.search('[0-9]*\.[0-9]*\.[0-9]*(-SNAPSHOT)?', version_line)
if version_string is None:
raise Exception("Version not found in gradle file")
version_string = version_string.group()
if '-SNAPSHOT' in version_string:
self.is_snapshot = True
version_string = version_string.replace('-SNAPSHOT', '')
self.version = [int(x) for x in version_string.split('.')]
def increment(self, level: ReleaseLevel): def increment(self, level: ReleaseLevel):
self.is_snapshot = False self.is_snapshot = False
match level: match level:
@ -61,6 +82,10 @@ class Version():
match self.config_file_type: match self.config_file_type:
case 'json': case 'json':
self.__write_json() self.__write_json()
case 'gradle':
self.__write_gradle()
case _:
raise Exception(f'The file type "{self.config_file_type}" is not implemented')
def __write_json(self): def __write_json(self):
with open(self.config_file_path, 'r+') as json_file: with open(self.config_file_path, 'r+') as json_file:
@ -70,6 +95,14 @@ class Version():
json.dump(json_data, json_file, indent=4) json.dump(json_data, json_file, indent=4)
json_file.truncate() json_file.truncate()
def __write_gradle(self):
with open(self.config_file_path, 'r+') as gradle_file:
gradle_contents = gradle_file.read()
version_substitute = re.sub("\nversion = .*\n", f'\nversion = "{self.get()}"\n', gradle_contents)
gradle_file.seek(0)
gradle_file.write(version_substitute)
gradle_file.truncate()
def get(self) -> str: def get(self) -> str:
version_string = ".".join([str(x) for x in self.version]) version_string = ".".join([str(x) for x in self.version])
if self.is_snapshot: if self.is_snapshot: