Skip to content
31 changes: 30 additions & 1 deletion 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,20 @@ const char* kKeyword[] = {
"volatile", "while", "with",
};

// Edit the version here prior to release
const std::string grpc_web_version = "1.3.1";
Comment thread
meling marked this conversation as resolved.
Outdated

string GetVersion() { return grpc_web_version; }
Comment thread
meling marked this conversation as resolved.
Outdated

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 +965,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 Down Expand Up @@ -1549,6 +1569,10 @@ class GrpcCodeGenerator : public CodeGenerator {
return true;
}

vars["version"] = GetVersion();
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 +1736,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::GetVersion() << std::endl;
return 0;
}
Comment thread
meling marked this conversation as resolved.

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