2022-05-25 16:43:28 +02:00
|
|
|
import json
|
|
|
|
import os
|
2022-05-27 18:17:07 +02:00
|
|
|
from string import ascii_letters
|
|
|
|
|
2022-05-25 16:43:28 +02:00
|
|
|
from cpl_core.environment import ApplicationEnvironmentABC
|
2022-09-19 22:59:21 +02:00
|
|
|
from cpl_core.utils import String
|
2022-05-25 16:43:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
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 = {}
|
2023-02-20 15:55:20 +01:00
|
|
|
with open(os.path.join(self._env.working_directory, file), "r", encoding="utf-8") as f:
|
2022-05-25 16:43:28 +02:00
|
|
|
# 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):
|
2023-02-20 15:55:20 +01:00
|
|
|
with open(os.path.join(self._env.working_directory, file), "w", encoding="utf-8") as f:
|
2022-05-25 18:54:21 +02:00
|
|
|
f.write(json.dumps(project_json, indent=2))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def set_version(self, file: str, version: dict):
|
|
|
|
project_json = self._read_file(file)
|
2023-02-20 15:55:20 +01:00
|
|
|
project_json["ProjectSettings"]["Version"] = version
|
2022-05-25 18:54:21 +02:00
|
|
|
self._write_file(file, project_json)
|
|
|
|
|
2022-07-10 17:23:50 +02:00
|
|
|
def set_dependencies(self, file: str, version: dict, key: str, skipped=None):
|
2022-05-25 18:54:21 +02:00
|
|
|
project_json = self._read_file(file)
|
2023-02-20 15:55:20 +01:00
|
|
|
if key not in project_json["ProjectSettings"]:
|
|
|
|
project_json["ProjectSettings"][key] = []
|
2022-07-10 17:23:50 +02:00
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
dependencies = project_json["ProjectSettings"][key]
|
2022-05-25 17:04:18 +02:00
|
|
|
new_deps = []
|
|
|
|
for dependency in dependencies:
|
2023-02-20 15:55:20 +01:00
|
|
|
if not dependency.startswith("cpl-"):
|
2022-05-25 17:04:18 +02:00
|
|
|
new_deps.append(dependency)
|
|
|
|
continue
|
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
dep_version = dependency.split("=")[1]
|
|
|
|
dep_name = dependency.split("=")[0]
|
|
|
|
if dep_name[len(dep_name) - 1] not in ascii_letters:
|
|
|
|
dep_name = dep_name[: len(dep_name) - 1]
|
2022-05-27 18:17:07 +02:00
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
if (
|
|
|
|
skipped is not None
|
|
|
|
and (dep_name in skipped or String.convert_to_snake_case(dep_name) in skipped)
|
|
|
|
or dep_version == ""
|
|
|
|
):
|
2022-05-27 18:17:07 +02:00
|
|
|
new_deps.append(dependency)
|
|
|
|
continue
|
2022-07-16 16:07:39 +02:00
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
new_deps.append(
|
|
|
|
dependency.replace(dep_version, f'{version["Major"]}.{version["Minor"]}.{version["Micro"]}')
|
|
|
|
)
|
2022-05-25 17:04:18 +02:00
|
|
|
|
2023-02-20 15:55:20 +01:00
|
|
|
project_json["ProjectSettings"][key] = new_deps
|
2022-05-25 18:54:21 +02:00
|
|
|
self._write_file(file, project_json)
|