Fix file handling and version creation

This commit is contained in:
bom 2023-02-09 12:52:58 +01:00
parent d50b8bd95b
commit fd84063edc
2 changed files with 17 additions and 8 deletions

View file

@ -4,8 +4,12 @@ from file_handlers import FileHandler
def init_project(): def init_project():
# validate_values() # validate_values()
version = Version.from_file('build.gradle') version = Version.from_file('build.gradle')
version.increment(ReleaseLevel.SNAPSHOT) v2 = Version.from_file('package.json')
version.write() print(type(version.file_handler))
print(type(v2.file_handler))
# version.increment(ReleaseLevel.SNAPSHOT)
# version.write()
print(version.get())
def prepare_release(): def prepare_release():
pass pass
@ -24,11 +28,16 @@ class Version():
def __init__(self, version, is_snapshot): def __init__(self, version, is_snapshot):
self.version = version self.version = version
self.is_snapshot = is_snapshot self.is_snapshot = is_snapshot
self.file_handler = None
@classmethod @classmethod
def from_file(cls, config_file_path): def from_file(cls, config_file_path):
file_handler = FileHandler.from_file_path(config_file_path) file_handler = FileHandler.from_file_path(config_file_path)
return cls(file_handler.parse()) version, is_snapshot = file_handler.parse()
inst = cls(version, is_snapshot)
inst.file_handler = file_handler
return inst
def increment(self, level: ReleaseLevel): def increment(self, level: ReleaseLevel):
self.is_snapshot = False self.is_snapshot = False

View file

@ -63,11 +63,14 @@ class GradleFileHandler(FileHandler):
raise Exception("Version not found in gradle file") raise Exception("Version not found in gradle file")
version_string = version_string.group() version_string = version_string.group()
is_snapshot = False
if '-SNAPSHOT' in version_string: if '-SNAPSHOT' in version_string:
self.is_snapshot = True is_snapshot = True
version_string = version_string.replace('-SNAPSHOT', '') version_string = version_string.replace('-SNAPSHOT', '')
self.version = [int(x) for x in version_string.split('.')] version = [int(x) for x in version_string.split('.')]
return version, is_snapshot
def write(self): def write(self):
with open(self.config_file_path, 'r+') as gradle_file: with open(self.config_file_path, 'r+') as gradle_file:
@ -76,6 +79,3 @@ class GradleFileHandler(FileHandler):
gradle_file.seek(0) gradle_file.seek(0)
gradle_file.write(version_substitute) gradle_file.write(version_substitute)
gradle_file.truncate() gradle_file.truncate()
a = FileHandler.from_file_path('build.gradle')
print(type(a))