Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update only generated Code during gRPC Code Re-generation #92

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions grpc-interface-support/data/templates/cpp/ServiceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ class ${{ service_name_camel_case }}Service final : public ${{ package_id }}::${
// <auto-generated>
${{ service_header_code }}
// </auto-generated>

// <user-defined>
${{ service_header_user_code }}
// </user-defined>
};

} // namespace velocitas
Expand Down
2 changes: 1 addition & 1 deletion grpc-interface-support/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
velocitas-lib==0.0.12
velocitas-lib==0.0.13
proto-schema-parser==1.3.4
63 changes: 46 additions & 17 deletions grpc-interface-support/src/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -335,32 +339,30 @@ def __transform_source_stub_code(self, lines: List[str]) -> List[str]:
return result

def __create_or_update_service_header(self) -> None:
header_stub_code = GrpcCodeExtractor(
self.__proto_file_handle, self.__package_directory_path
).get_header_stub_code(self.__get_include_dir())

header_stub_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"{to_camel_case(self.__service_name)}ServiceImpl.h"
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
)

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
self.__update_service_header()
else:
self.__create_service_header()

user_defined_code = capture_area_in_file(
open(service_header_file_path, encoding="utf-8"),
"// <user-defined>",
"// </user-defined>",
)
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())

header_stub_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"{to_camel_case(self.__service_name)}ServiceImpl.h"
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(),
Expand All @@ -369,6 +371,33 @@ def __create_or_update_service_header(self) -> None:
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_generated_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"),
"// <auto-generated>",
"// </auto-generated>",
)

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)
)
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")
service_source_file_name = (
Expand Down
6 changes: 4 additions & 2 deletions grpc-interface-support/src/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down