From a66ce96dbfb44e049878b7a603d36eaedf2db4da Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 25 May 2022 16:43:28 +0200 Subject: [PATCH] Added set-version logic && Fixed CustomScriptService to handle arguments --- cpl-workspace.json | 2 +- src/cpl_cli/command/custom_script_service.py | 10 +++- src/cpl_cli/cpl_cli.json | 2 +- src/cpl_core/cpl_core.json | 2 +- src/cpl_query/cpl_query.json | 2 +- tools/set_version/application.py | 50 +++++++++++++++++++- tools/set_version/git_service.py | 18 +++++++ tools/set_version/set-version.json | 6 +-- tools/set_version/startup.py | 13 +++++ tools/set_version/version_setter_service.py | 22 +++++++++ 10 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 tools/set_version/git_service.py create mode 100644 tools/set_version/version_setter_service.py diff --git a/cpl-workspace.json b/cpl-workspace.json index b92e671b..4f0a2a55 100644 --- a/cpl-workspace.json +++ b/cpl-workspace.json @@ -11,7 +11,7 @@ "hello-world": "echo 'Hello World'", "sv": "cpl set-version", - "set-version": "echo 'Set versions for all projects'; cpl run set-version; echo '';", + "set-version": "echo 'Set versions for all projects'; cpl run set-version $ARGS; echo '';", "docs-build": "echo 'Build Documentation'; cd docs/; sphinx-apidoc -o source/ ../src/cpl_core; sphinx-apidoc -o source/ ../src/cpl_query; make clean; make html; rm source/cpl_query.tests.rst;", "db": "cpl build-docs", diff --git a/src/cpl_cli/command/custom_script_service.py b/src/cpl_cli/command/custom_script_service.py index 3e6bebdf..3ef8b465 100644 --- a/src/cpl_cli/command/custom_script_service.py +++ b/src/cpl_cli/command/custom_script_service.py @@ -35,7 +35,15 @@ class CustomScriptService(CommandABC): if script != cmd: continue - command = self._workspace.scripts[script] + command = '' + external_args = self._config.get_configuration('ARGS') + if external_args is not None: + command += f'ARGS="{external_args}";' + + command += self._workspace.scripts[script] + env_vars = os.environ + env_vars['CPL_ARGS'] = " ".join(args) + try: subprocess.run(command, shell=True if os.name == 'posix' else None) except Exception as e: diff --git a/src/cpl_cli/cpl_cli.json b/src/cpl_cli/cpl_cli.json index 5f3b651e..cf1da4f6 100644 --- a/src/cpl_cli/cpl_cli.json +++ b/src/cpl_cli/cpl_cli.json @@ -4,7 +4,7 @@ "Version": { "Major": "2022", "Minor": "6", - "Micro": "15.dev1" + "Micro": "15" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", diff --git a/src/cpl_core/cpl_core.json b/src/cpl_core/cpl_core.json index 8c8589a5..bf3aac9e 100644 --- a/src/cpl_core/cpl_core.json +++ b/src/cpl_core/cpl_core.json @@ -4,7 +4,7 @@ "Version": { "Major": "2022", "Minor": "6", - "Micro": "15.dev1" + "Micro": "15" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", diff --git a/src/cpl_query/cpl_query.json b/src/cpl_query/cpl_query.json index 85d40f27..024ae55d 100644 --- a/src/cpl_query/cpl_query.json +++ b/src/cpl_query/cpl_query.json @@ -4,7 +4,7 @@ "Version": { "Major": "2022", "Minor": "6", - "Micro": "15.dev1" + "Micro": "15" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", diff --git a/tools/set_version/application.py b/tools/set_version/application.py index 9ce6fc0a..a8ee362d 100644 --- a/tools/set_version/application.py +++ b/tools/set_version/application.py @@ -1,7 +1,12 @@ +import traceback + +from cpl_cli.configuration import WorkspaceSettings from cpl_core.application import ApplicationABC from cpl_core.configuration import ConfigurationABC from cpl_core.console import Console from cpl_core.dependency_injection import ServiceProviderABC +from set_version.git_service import GitService +from set_version.version_setter_service import VersionSetterService class Application(ApplicationABC): @@ -9,8 +14,49 @@ class Application(ApplicationABC): def __init__(self, config: ConfigurationABC, services: ServiceProviderABC): ApplicationABC.__init__(self, config, services) + self._workspace: WorkspaceSettings = config.get_configuration(WorkspaceSettings) + + self._git_service: GitService = self._services.get_service(GitService) + self._version_setter: VersionSetterService = self._services.get_service(VersionSetterService) + def configure(self): - pass + self._configuration.parse_console_arguments(self._services) def main(self): - Console.write_line('Hello World from tools') + Console.write_line('Set versions:') + args = self._configuration.additional_arguments + version = {} + branch = "" + suffix = "" + if len(args) > 1: + Console.error(f'Unexpected argument(s): {", ".join(args[1:])}') + return + + if len(args) == 1: + suffix = f'.{args[0]}' + + try: + branch = self._git_service.get_active_branch_name() + Console.write_line(f'Found branch: {branch}') + except Exception as e: + Console.error('Branch could not be found', traceback.format_exc()) + return + + try: + version['Major'] = branch.split('.')[0] + version['Minor'] = branch.split('.')[1] + version['Micro'] = f'{branch.split(".")[2]}{suffix}' + except Exception as e: + Console.error(f'Branch {branch} does not contain valid version') + return + + try: + for project in self._workspace.projects: + if not project.startswith('cpl'): + continue + + Console.write_line(f'Set version {version["Major"]}.{version["Minor"]}.{version["Micro"]} for {project}') + self._version_setter.set_version(self._workspace.projects[project], version) + except Exception as e: + Console.error('Version could not be set', traceback.format_exc()) + return diff --git a/tools/set_version/git_service.py b/tools/set_version/git_service.py new file mode 100644 index 00000000..263cde5b --- /dev/null +++ b/tools/set_version/git_service.py @@ -0,0 +1,18 @@ +import os + +from cpl_core.environment import ApplicationEnvironmentABC + + +class GitService: + + def __init__(self, env: ApplicationEnvironmentABC): + self._env = env + + def get_active_branch_name(self) -> str: + head_dir = os.path.join(self._env.working_directory, '.git/HEAD') + with open(head_dir, 'r') as f: + content = f.read().splitlines() + + for line in content: + if line[0:4] == "ref:": + return line.partition("refs/heads/")[2] diff --git a/tools/set_version/set-version.json b/tools/set_version/set-version.json index 3039cf07..c04df303 100644 --- a/tools/set_version/set-version.json +++ b/tools/set_version/set-version.json @@ -2,9 +2,9 @@ "ProjectSettings": { "Name": "set-version", "Version": { - "Major": "0", - "Minor": "0", - "Micro": "0" + "Major": "2022", + "Minor": "6", + "Micro": "15" }, "Author": "", "AuthorEmail": "", diff --git a/tools/set_version/startup.py b/tools/set_version/startup.py index 359c03d2..7e4701d5 100644 --- a/tools/set_version/startup.py +++ b/tools/set_version/startup.py @@ -1,7 +1,12 @@ +import os + +from cpl_cli.configuration import WorkspaceSettings from cpl_core.application import StartupABC from cpl_core.configuration import ConfigurationABC from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC from cpl_core.environment import ApplicationEnvironment +from set_version.git_service import GitService +from set_version.version_setter_service import VersionSetterService class Startup(StartupABC): @@ -10,7 +15,15 @@ class Startup(StartupABC): StartupABC.__init__(self) def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: + configuration.add_json_file('cpl-workspace.json', optional=True, output=False) + if configuration.get_configuration(WorkspaceSettings) is None: + environment.set_working_directory(os.path.join(environment.working_directory, '../../')) + configuration.add_json_file('cpl-workspace.json', optional=False, output=False) + return configuration def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: + services.add_transient(GitService) + services.add_transient(VersionSetterService) + return services.build_service_provider() diff --git a/tools/set_version/version_setter_service.py b/tools/set_version/version_setter_service.py new file mode 100644 index 00000000..8d7c17d2 --- /dev/null +++ b/tools/set_version/version_setter_service.py @@ -0,0 +1,22 @@ +import json +import os + +from cpl_core.environment import ApplicationEnvironmentABC + + +class VersionSetterService: + + def __init__(self, env: ApplicationEnvironmentABC): + self._env = env + + def set_version(self, file: str, version: dict): + 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() + + project_json['ProjectSettings']['Version'] = version + 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()