feat add code generator

This commit is contained in:
tqcq
2024-04-13 10:14:57 +08:00
parent fdbce08c6c
commit ebea797d67
11 changed files with 202 additions and 3 deletions

View File

@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(protoc-gen-sled LANGUAGES C CXX)
add_executable(${PROJECT_NAME} main.cc sled_generator.cc)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE libprotoc sled)
install(TARGETS ${PROJECT_NAME} DESTINATION bin)

View File

@@ -0,0 +1,17 @@
#include "sled_generator.h"
#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/compiler/cpp/cpp_generator.h>
#include <google/protobuf/compiler/plugin.h>
int
main(int argc, char *argv[])
{
google::protobuf::compiler::CommandLineInterface cli;
// google::protobuf::compiler::cpp::CppGenerator cpp_generator;
// cli.RegisterGenerator("--cpp_out", &cpp_generator, "Generate C++ source code.");
sled::SledGenerator sled_generator;
cli.RegisterGenerator("--sled_out", &sled_generator, "sled cpp rpc generator");
return google::protobuf::compiler::PluginMain(argc, argv, &sled_generator);
}

View File

@@ -0,0 +1,54 @@
#include "sled_generator.h"
#include <google/protobuf/compiler/plugin.pb.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <sled/sled.h>
using namespace google::protobuf;
using namespace google::protobuf::compiler;
using namespace google::protobuf::io;
namespace sled {
void
PrintMessage(Printer &printer, const Descriptor *msg)
{
printer.Print("- $name$\n", "name", msg->name());
printer.Indent();
for (int i = 0; i < msg->nested_type_count(); ++i) { PrintMessage(printer, msg->nested_type(i)); }
printer.Outdent();
}
void
PrintService(Printer &printer, const ServiceDescriptor *service)
{
inja::json data;
data["classname"] = service->name();
data["full_name"] = service->full_name();
printer.Print("- $name$\n", "name", service->full_name());
printer.Indent();
for (int i = 0; i < service->method_count(); i++) {
const MethodDescriptor *method = service->method(i);
printer.Print("- $name$\n", "name", method->name());
printer.Indent();
printer.Print("request: $name$ $file$\n", "name", method->input_type()->name(), "file", method->file()->name());
printer
.Print("response: $name$ $file$\n", "name", method->output_type()->name(), "file", method->file()->name());
printer.Outdent();
}
printer.Outdent();
}
bool
SledGenerator::Generate(const FileDescriptor *file,
const std::string &parameter,
GeneratorContext *generator_context,
std::string *error) const
{
auto *out_stream = generator_context->Open(file->name() + ".txt");
Printer printer(out_stream, '$');
for (int i = 0; i < file->message_type_count(); i++) { PrintMessage(printer, file->message_type(i)); }
for (int i = 0; i < file->service_count(); i++) { PrintService(printer, file->service(i)); }
return true;
}
}// namespace sled

View File

@@ -0,0 +1,22 @@
#ifndef SLED_SLED_GENERATOR_SLED_GENERATOR_H
#define SLED_SLED_GENERATOR_SLED_GENERATOR_H
#pragma once
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/descriptor.h>
using namespace google::protobuf;
using namespace google::protobuf::compiler;
namespace sled {
class SledGenerator : public google::protobuf::compiler::CodeGenerator {
public:
~SledGenerator() override = default;
bool Generate(const FileDescriptor *file,
const std::string &parameter,
GeneratorContext *generator_context,
std::string *error) const override;
};
}// namespace sled
#endif// SLED_SLED_GENERATOR_SLED_GENERATOR_H