From 2cf67c90b8d1ed6192327348a0626cd1f471452d Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Wed, 4 Sep 2024 11:39:21 +0200 Subject: [PATCH 1/5] Update only generated Code during gRPC Code Re-generation The "user-defined" area has been removed from the cpp header template. Instead all code is persisted between two "code re-generations" except of the "auto-generated" code. The "auto-generated" code is correctly updated depending on if services where added or removed between two consecutive "code re-generations". --- .../data/templates/cpp/ServiceImpl.h | 4 -- grpc-interface-support/src/cpp.py | 69 +++++++++++++++---- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/grpc-interface-support/data/templates/cpp/ServiceImpl.h b/grpc-interface-support/data/templates/cpp/ServiceImpl.h index 7fb8faf4..018da0e4 100644 --- a/grpc-interface-support/data/templates/cpp/ServiceImpl.h +++ b/grpc-interface-support/data/templates/cpp/ServiceImpl.h @@ -30,10 +30,6 @@ class ${{ service_name_camel_case }}Service final : public ${{ package_id }}::${ // ${{ service_header_code }} // - -// -${{ service_header_user_code }} -// }; } // namespace velocitas diff --git a/grpc-interface-support/src/cpp.py b/grpc-interface-support/src/cpp.py index ac11d2f2..fd36cb0b 100644 --- a/grpc-interface-support/src/cpp.py +++ b/grpc-interface-support/src/cpp.py @@ -335,6 +335,20 @@ def __transform_source_stub_code(self, lines: List[str]) -> List[str]: return result def __create_or_update_service_header(self) -> None: + app_source_dir = os.path.join(get_workspace_dir(), "app", "src") + service_header_file_name = ( + f"{self.__proto_file_handle.get_service_name()}ServiceImpl.h" + ) + service_header_file_path = os.path.join( + app_source_dir, service_header_file_name + ) + + if os.path.exists(service_header_file_path): + self.__update_service_header() + else: + self.__create_service_header() + + def __create_service_header(self) -> None: header_stub_code = GrpcCodeExtractor( self.__proto_file_handle, self.__package_directory_path ).get_header_stub_code(self.__get_include_dir()) @@ -346,21 +360,8 @@ def __create_or_update_service_header(self) -> None: service_header_file_path = os.path.join( app_source_dir, service_header_file_name ) - - user_defined_code: List[str] = [] - if os.path.exists(service_header_file_path): - # there is a previous version of the header - # extract the user-defined code - - user_defined_code = capture_area_in_file( - open(service_header_file_path, encoding="utf-8"), - "// ", - "// ", - ) - variables = self.__get_template_variables() variables["service_header_code"] = "\n".join(header_stub_code) - variables["service_header_user_code"] = "\n".join(user_defined_code) copy_templates( get_template_dir(), @@ -368,6 +369,48 @@ def __create_or_update_service_header(self) -> None: [CopySpec("ServiceImpl.h", service_header_file_name)], variables, ) + + def __update_service_header(self) -> None: + header_generated_code = GrpcCodeExtractor( + self.__proto_file_handle, self.__package_directory_path + ).get_header_stub_code(self.__get_include_dir()) + + header_generated_code = self.__transform_header_stub_code(header_stub_code) + + app_source_dir = os.path.join(get_workspace_dir(), "app", "src") + service_header_file_name = ( + f"{self.__proto_file_handle.get_service_name()}ServiceImpl.h" + ) + service_header_file_path = os.path.join( + app_source_dir, service_header_file_name + ) + + auto_generated_code = capture_area_in_file( + open(service_header_file_path, encoding="utf-8"), + "// ", + "// ", + ) + + header_file_content = self.__read_file_to_string(service_header_file_path) + modified_content = header_file_content.replace('\n'.join(auto_generated_code), '\n'.join(header_generated_code)) + self.__write_string_to_file(service_header_file_path, modified_content) + + def __read_file_to_string(self, filepath): + try: + with open(filepath, 'r') as file: + file_content = file.read() + return file_content + except FileNotFoundError: + print(f"File {filepath} not found") + except Exception as e: + print(f"An error occurred: {e}") + + def __write_string_to_file(self, filepath, content): + try: + with open(filepath, 'w') as file: + file.write(content) + except Exception as e: + print(f"An error occurred: {e}") def __create_service_source(self) -> None: app_source_dir = os.path.join(get_workspace_dir(), "app", "src") From d02076b4a061cfd510bfb95cfe1e50488b220969 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Tue, 10 Sep 2024 07:22:12 +0200 Subject: [PATCH 2/5] Fix Lint Issues --- grpc-interface-support/src/cpp.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/grpc-interface-support/src/cpp.py b/grpc-interface-support/src/cpp.py index fd36cb0b..6ee37069 100644 --- a/grpc-interface-support/src/cpp.py +++ b/grpc-interface-support/src/cpp.py @@ -348,7 +348,7 @@ def __create_or_update_service_header(self) -> None: else: self.__create_service_header() - def __create_service_header(self) -> None: + def __create_service_header(self) -> None: header_stub_code = GrpcCodeExtractor( self.__proto_file_handle, self.__package_directory_path ).get_header_stub_code(self.__get_include_dir()) @@ -375,7 +375,7 @@ def __update_service_header(self) -> None: self.__proto_file_handle, self.__package_directory_path ).get_header_stub_code(self.__get_include_dir()) - header_generated_code = self.__transform_header_stub_code(header_stub_code) + header_generated_code = self.__transform_header_stub_code(header_generated_code) app_source_dir = os.path.join(get_workspace_dir(), "app", "src") service_header_file_name = ( @@ -392,12 +392,14 @@ def __update_service_header(self) -> None: ) header_file_content = self.__read_file_to_string(service_header_file_path) - modified_content = header_file_content.replace('\n'.join(auto_generated_code), '\n'.join(header_generated_code)) + modified_content = header_file_content.replace( + "\n".join(auto_generated_code), "\n".join(header_generated_code) + ) self.__write_string_to_file(service_header_file_path, modified_content) - def __read_file_to_string(self, filepath): + def __read_file_to_string(self, filepath) -> str: try: - with open(filepath, 'r') as file: + with open(filepath, "r") as file: file_content = file.read() return file_content except FileNotFoundError: @@ -405,9 +407,9 @@ def __read_file_to_string(self, filepath): except Exception as e: print(f"An error occurred: {e}") - def __write_string_to_file(self, filepath, content): + def __write_string_to_file(self, filepath, content) -> None: try: - with open(filepath, 'w') as file: + with open(filepath, "w") as file: file.write(content) except Exception as e: print(f"An error occurred: {e}") From 835de17f0f213f9b1c7b3e03bed8ffeeec491101 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Tue, 10 Sep 2024 11:33:01 +0200 Subject: [PATCH 3/5] Update velocitas-lib to 0.0.13 --- grpc-interface-support/requirements.txt | 2 +- grpc-interface-support/src/cpp.py | 29 +++++++------------------ 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/grpc-interface-support/requirements.txt b/grpc-interface-support/requirements.txt index dd10552f..9c1e0a63 100644 --- a/grpc-interface-support/requirements.txt +++ b/grpc-interface-support/requirements.txt @@ -1,2 +1,2 @@ -velocitas-lib==0.0.12 +velocitas-lib==0.0.13 proto-schema-parser==1.3.4 diff --git a/grpc-interface-support/src/cpp.py b/grpc-interface-support/src/cpp.py index 6ee37069..c169c062 100644 --- a/grpc-interface-support/src/cpp.py +++ b/grpc-interface-support/src/cpp.py @@ -32,9 +32,13 @@ export_conan_project, get_required_sdk_version, ) +from velocitas_lib.file_utils import ( + capture_area_in_file, + read_file, + write_file, +) from velocitas_lib.templates import CopySpec, copy_templates from velocitas_lib.text_utils import ( - capture_area_in_file, to_camel_case, ) @@ -369,7 +373,7 @@ def __create_service_header(self) -> None: [CopySpec("ServiceImpl.h", service_header_file_name)], variables, ) - + def __update_service_header(self) -> None: header_generated_code = GrpcCodeExtractor( self.__proto_file_handle, self.__package_directory_path @@ -391,28 +395,11 @@ def __update_service_header(self) -> None: "// ", ) - header_file_content = self.__read_file_to_string(service_header_file_path) + header_file_content = read_file(service_header_file_path) modified_content = header_file_content.replace( "\n".join(auto_generated_code), "\n".join(header_generated_code) ) - self.__write_string_to_file(service_header_file_path, modified_content) - - def __read_file_to_string(self, filepath) -> str: - try: - with open(filepath, "r") as file: - file_content = file.read() - return file_content - except FileNotFoundError: - print(f"File {filepath} not found") - except Exception as e: - print(f"An error occurred: {e}") - - def __write_string_to_file(self, filepath, content) -> None: - try: - with open(filepath, "w") as file: - file.write(content) - except Exception as e: - print(f"An error occurred: {e}") + write_file(service_header_file_path, modified_content) def __create_service_source(self) -> None: app_source_dir = os.path.join(get_workspace_dir(), "app", "src") From 5a65c9aa3c87b054911206ff44be1b261974f493 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Tue, 10 Sep 2024 11:40:31 +0200 Subject: [PATCH 4/5] Adapt Imports of python.py --- grpc-interface-support/src/python.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/grpc-interface-support/src/python.py b/grpc-interface-support/src/python.py index 97f929d7..d38244b7 100644 --- a/grpc-interface-support/src/python.py +++ b/grpc-interface-support/src/python.py @@ -23,12 +23,14 @@ from generator import GrpcServiceSdkGenerator, GrpcServiceSdkGeneratorFactory from proto import ProtoFileHandle from velocitas_lib import get_package_path, get_workspace_dir, templates +from velocitas_lib.file_utils import ( + capture_area_in_file, + replace_text_in_file, +) from velocitas_lib.templates import CopySpec, copy_templates from velocitas_lib.text_utils import ( - capture_area_in_file, replace_item_in_list, replace_text_area, - replace_text_in_file, ) From 1e534312d634e5f0ab84efb957be4631884374b1 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Tue, 10 Sep 2024 11:46:19 +0200 Subject: [PATCH 5/5] Remove unused variable --- grpc-interface-support/src/cpp.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/grpc-interface-support/src/cpp.py b/grpc-interface-support/src/cpp.py index c169c062..7ad9fca5 100644 --- a/grpc-interface-support/src/cpp.py +++ b/grpc-interface-support/src/cpp.py @@ -361,9 +361,6 @@ def __create_service_header(self) -> None: app_source_dir = os.path.join(get_workspace_dir(), "app", "src") service_header_file_name = f"{to_camel_case(self.__service_name)}ServiceImpl.h" - service_header_file_path = os.path.join( - app_source_dir, service_header_file_name - ) variables = self.__get_template_variables() variables["service_header_code"] = "\n".join(header_stub_code)