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

Add version flag and version info in generated code #1231

Merged
merged 9 commits into from
Apr 29, 2022
41 changes: 38 additions & 3 deletions javascript/net/grpc/web/generator/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/compiler/plugin.h>
#include <google/protobuf/compiler/plugin.pb.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/printer.h>
Expand All @@ -40,6 +41,7 @@ using google::protobuf::compiler::CodeGenerator;
using google::protobuf::compiler::GeneratorContext;
using google::protobuf::compiler::ParseGeneratorParameter;
using google::protobuf::compiler::PluginMain;
using google::protobuf::compiler::Version;
using google::protobuf::io::Printer;
using google::protobuf::io::ZeroCopyOutputStream;

Expand Down Expand Up @@ -77,6 +79,18 @@ const char* kKeyword[] = {
"volatile", "while", "with",
};

// Edit the version here prior to release
static const std::string GRPC_WEB_VERSION = "1.3.1";

string GetProtocVersion(GeneratorContext* context) {
Version compiler_version;
context->GetCompilerVersion(&compiler_version);
return std::to_string(compiler_version.major()) + "." +
std::to_string(compiler_version.minor()) + "." +
std::to_string(compiler_version.patch()) +
compiler_version.suffix();
}

bool IsReserved(const string& ident) {
for (size_t i = 0; i < sizeof(kKeyword) / sizeof(kKeyword[0]); i++) {
if (ident == kKeyword[i]) {
Expand Down Expand Up @@ -949,7 +963,11 @@ void PrintFileHeader(Printer* printer, const std::map<string, string>& vars) {
" * @enhanceable\n"
" * @public\n"
" */\n\n"
"// GENERATED CODE -- DO NOT EDIT!\n\n\n"
"// Code generated by protoc-gen-grpc-web. DO NOT EDIT.\n"
"// versions:\n"
"// \tprotoc-gen-grpc-web v$version$\n"
"// \tprotoc v$protoc_version$\n"
"// source: $source_file$\n\n\n"
"/* eslint-disable */\n"
"// @ts-nocheck\n\n\n");
}
Expand All @@ -966,7 +984,11 @@ void PrintMethodDescriptorFile(Printer* printer,
printer->Print(
" * @public\n"
" */\n\n"
"// GENERATED CODE -- DO NOT EDIT!\n\n\n"
"// Code generated by protoc-gen-grpc-web. DO NOT EDIT.\n"
"// versions:\n"
"// \tprotoc-gen-grpc-web v$version$\n"
"// \tprotoc v$protoc_version$\n"
"// source: $source_file$\n\n\n"
"/* eslint-disable */\n"
"// @ts-nocheck\n\n\n");

Expand Down Expand Up @@ -1361,7 +1383,11 @@ void PrintGrpcWebClosureES6File(Printer* printer, const FileDescriptor* file) {
string package_dot = file->package().empty() ? "" : file->package() + ".";

printer->Print(
"// GENERATED CODE -- DO NOT EDIT!\n"
"// Code generated by protoc-gen-grpc-web. DO NOT EDIT.\n"
"// versions:\n"
"// \tprotoc-gen-grpc-web v$version$\n"
"// \tprotoc v$protoc_version$\n"
"// source: $source_file$\n"
meling marked this conversation as resolved.
Show resolved Hide resolved
"\n"
"/**\n"
" * @fileoverview gRPC-Web generated client stub for '$file$'\n"
Expand Down Expand Up @@ -1549,6 +1575,10 @@ class GrpcCodeGenerator : public CodeGenerator {
return true;
}

vars["version"] = GRPC_WEB_VERSION;
vars["protoc_version"] = GetProtocVersion(context);
vars["source_file"] = file->name();

string file_name = generator_options.OutputFile(file->name());
if (generator_options.multiple_files() &&
ImportStyle::CLOSURE == generator_options.import_style()) {
Expand Down Expand Up @@ -1712,6 +1742,11 @@ class GrpcCodeGenerator : public CodeGenerator {
} // namespace grpc

int main(int argc, char* argv[]) {
if (argc == 2 && std::string(argv[1]) == "--version") {
std::cout << argv[0] << " " << grpc::web::GRPC_WEB_VERSION << std::endl;
return 0;
}
meling marked this conversation as resolved.
Show resolved Hide resolved

grpc::web::GrpcCodeGenerator generator;
PluginMain(argc, argv, &generator);
return 0;
Expand Down