2022-05-25 16:43:28 +02:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2022-05-25 17:04:18 +02:00
|
|
|
from cpl_core.console import Console
|
|
|
|
|
2022-05-25 16:43:28 +02:00
|
|
|
from cpl_core.environment import ApplicationEnvironmentABC
|
|
|
|
|
|
|
|
|
|
|
|
class VersionSetterService:
|
|
|
|
|
|
|
|
def __init__(self, env: ApplicationEnvironmentABC):
|
|
|
|
self._env = env
|
|
|
|
|
2022-05-25 18:54:21 +02:00
|
|
|
def _read_file(self, file: str) -> dict:
|
2022-05-25 16:43:28 +02:00
|
|
|
project_json = {}
|
|
|
|
with open(os.path.join(self._env.working_directory, file), 'r', encoding='utf-8') as f:
|
|
|
|
# load json
|
|
|
|
project_json = json.load(f)
|
|
|
|
f.close()
|
|
|
|
|
2022-05-25 18:54:21 +02:00
|
|
|
return project_json
|
|
|
|
|
|
|
|
def _write_file(self, file: str, project_json: dict):
|
|
|
|
with open(os.path.join(self._env.working_directory, file), 'w', encoding='utf-8') as f:
|
|
|
|
f.write(json.dumps(project_json, indent=2))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def set_version(self, file: str, version: dict):
|
|
|
|
project_json = self._read_file(file)
|
2022-05-25 16:43:28 +02:00
|
|
|
project_json['ProjectSettings']['Version'] = version
|
2022-05-25 18:54:21 +02:00
|
|
|
self._write_file(file, project_json)
|
|
|
|
|
|
|
|
def set_dependencies(self, file: str, version: dict):
|
|
|
|
project_json = self._read_file(file)
|
2022-05-25 17:04:18 +02:00
|
|
|
dependencies = project_json['ProjectSettings']['Dependencies']
|
|
|
|
new_deps = []
|
|
|
|
for dependency in dependencies:
|
|
|
|
if not dependency.startswith('cpl-'):
|
|
|
|
new_deps.append(dependency)
|
|
|
|
continue
|
|
|
|
|
|
|
|
dep_version = dependency.split('=')[1]
|
|
|
|
new_deps.append(dependency.replace(dep_version, f'{version["Major"]}.{version["Minor"]}.{version["Micro"]}'))
|
|
|
|
|
|
|
|
project_json['ProjectSettings']['Dependencies'] = new_deps
|
2022-05-25 18:54:21 +02:00
|
|
|
self._write_file(file, project_json)
|