Skip to content

Commit

Permalink
Merge branch 'master' of github.com:timostamm/protobuf-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm committed Apr 23, 2022
2 parents ad3a725 + fbd1479 commit 356a65f
Show file tree
Hide file tree
Showing 22 changed files with 79 additions and 46 deletions.
5 changes: 3 additions & 2 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ Available plugin options:
- "generate_dependencies"
By default, only the PROTO_FILES passed as input to protoc are generated,
not the files they import. Set this option to generate code for dependencies
too.
not the files they import (with the exception of well-known types which are
always generated when imported).
Set this option to generate code for dependencies too.
- "force_exclude_all_options"
By default, custom options are included in the metadata and can be blacklisted
Expand Down
1 change: 0 additions & 1 deletion packages/bsr-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ plugins:
out: gen/
opt:
- long_type_string
- generate_dependencies
```
```sh
Expand Down
2 changes: 1 addition & 1 deletion packages/example-browser-grpcweb-transport-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
default: generate build pack

generate:
npx protoc -I . --ts_out . --ts_opt generate_dependencies,optimize_code_size service-example.proto
npx protoc -I . --ts_out . --ts_opt optimize_code_size service-example.proto

build:
npx tsc --target ES2015 --module CommonJS --strict --outDir .tsc-out client.ts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// @generated by protobuf-ts 2.0.0-alpha.12 with parameters generate_dependencies,optimize_code_size
// @generated by protobuf-ts 2.4.0 with parameter optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { RpcTransport } from "@protobuf-ts/runtime-rpc";
import { ServiceInfo } from "@protobuf-ts/runtime-rpc";
import type { RpcTransport } from "@protobuf-ts/runtime-rpc";
import type { ServiceInfo } from "@protobuf-ts/runtime-rpc";
import { ExampleService } from "./service-example";
import { DuplexStreamingCall } from "@protobuf-ts/runtime-rpc";
import { ClientStreamingCall } from "@protobuf-ts/runtime-rpc";
import { ServerStreamingCall } from "@protobuf-ts/runtime-rpc";
import type { DuplexStreamingCall } from "@protobuf-ts/runtime-rpc";
import type { ClientStreamingCall } from "@protobuf-ts/runtime-rpc";
import type { ServerStreamingCall } from "@protobuf-ts/runtime-rpc";
import { stackIntercept } from "@protobuf-ts/runtime-rpc";
import { ExampleResponse } from "./service-example";
import { ExampleRequest } from "./service-example";
import { UnaryCall } from "@protobuf-ts/runtime-rpc";
import { RpcOptions } from "@protobuf-ts/runtime-rpc";
import type { ExampleResponse } from "./service-example";
import type { ExampleRequest } from "./service-example";
import type { UnaryCall } from "@protobuf-ts/runtime-rpc";
import type { RpcOptions } from "@protobuf-ts/runtime-rpc";
/**
* @generated from protobuf service spec.ExampleService
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protobuf-ts 2.0.0-alpha.12 with parameters generate_dependencies,optimize_code_size
// @generated by protobuf-ts 2.4.0 with parameter optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { ServiceType } from "@protobuf-ts/runtime-rpc";
Expand Down
1 change: 0 additions & 1 deletion packages/example-buf/proto/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ plugins:
- name: ts
out: ../src-gen
opt:
- generate_dependencies
- long_type_string
path: ../node_modules/.bin/protoc-gen-ts
strategy: all
Expand Down
2 changes: 1 addition & 1 deletion packages/example-node-grpc-backend-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
default: generate run-server

generate:
npx protoc -I . --ts_out . --ts_opt server_generic,client_none,generate_dependencies,optimize_code_size service-example.proto
npx protoc -I . --ts_out . --ts_opt server_generic,client_none,optimize_code_size service-example.proto

run-server:
npx ts-node server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protobuf-ts 2.2.1 with parameter server_generic,client_none,generate_dependencies,optimize_code_size
// @generated by protobuf-ts 2.4.0 with parameter server_generic,client_none,optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { RpcOutputStream } from "@protobuf-ts/runtime-rpc";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protobuf-ts 2.2.1 with parameter server_generic,client_none,generate_dependencies,optimize_code_size
// @generated by protobuf-ts 2.4.0 with parameter server_generic,client_none,optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { ServiceType } from "@protobuf-ts/runtime-rpc";
Expand Down
2 changes: 1 addition & 1 deletion packages/example-node-grpc-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ npm-install:
npm i

generate:
npx protoc -I . --ts_out . --ts_opt client_grpc1,generate_dependencies,optimize_code_size service-example.proto
npx protoc -I . --ts_out . --ts_opt client_grpc1,optimize_code_size service-example.proto

run-client:
npx ts-node client.ts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protobuf-ts 2.0.0-alpha.18 with parameters client_grpc1,generate_dependencies,optimize_code_size
// @generated by protobuf-ts 2.4.0 with parameter client_grpc1,optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { ExampleService } from "./service-example";
Expand Down
2 changes: 1 addition & 1 deletion packages/example-node-grpc-client/service-example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protobuf-ts 2.0.0-alpha.18 with parameters client_grpc1,generate_dependencies,optimize_code_size
// @generated by protobuf-ts 2.4.0 with parameter client_grpc1,optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { ServiceType } from "@protobuf-ts/runtime-rpc";
Expand Down
2 changes: 1 addition & 1 deletion packages/example-node-grpc-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
default: generate run-server

generate:
npx protoc -I . --ts_out . --ts_opt server_grpc1,client_none,generate_dependencies,optimize_code_size service-example.proto
npx protoc -I . --ts_out . --ts_opt server_grpc1,client_none,optimize_code_size service-example.proto

run-server:
npx ts-node server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @generated by protobuf-ts 2.4.0 with parameter server_grpc1,client_none,generate_dependencies,optimize_code_size,// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3),// tslint:disable
// @generated by protobuf-ts 2.4.0 with parameter server_grpc1,client_none,optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { ExampleResponse } from "./service-example";
import { ExampleRequest } from "./service-example";
import type * as grpc from "@grpc/grpc-js";
Expand Down
4 changes: 3 additions & 1 deletion packages/example-node-grpc-server/service-example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @generated by protobuf-ts 2.4.0 with parameter server_grpc1,client_none,generate_dependencies,optimize_code_size,// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3),// tslint:disable
// @generated by protobuf-ts 2.4.0 with parameter server_grpc1,client_none,optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { ServiceType } from "@protobuf-ts/runtime-rpc";
import { MessageType } from "@protobuf-ts/runtime";
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/example-node-grpc-transport-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ npm-install:
npm i

generate:
npx protoc -I . --ts_out . --ts_opt generate_dependencies,optimize_code_size service-example.proto
npx protoc -I . --ts_out . --ts_opt optimize_code_size service-example.proto

run-client:
npx ts-node client.ts
Expand Down
2 changes: 1 addition & 1 deletion packages/example-node-grpcweb-transport-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ npm-install:
npm i

generate:
npx protoc -I . --ts_out . --ts_opt generate_dependencies,optimize_code_size service-example.proto
npx protoc -I . --ts_out . --ts_opt optimize_code_size service-example.proto

run-client:
npx ts-node client.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// @generated by protobuf-ts 2.0.0-alpha.12 with parameters generate_dependencies,optimize_code_size
// @generated by protobuf-ts 2.4.0 with parameter optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { RpcTransport } from "@protobuf-ts/runtime-rpc";
import { ServiceInfo } from "@protobuf-ts/runtime-rpc";
import type { RpcTransport } from "@protobuf-ts/runtime-rpc";
import type { ServiceInfo } from "@protobuf-ts/runtime-rpc";
import { ExampleService } from "./service-example";
import { DuplexStreamingCall } from "@protobuf-ts/runtime-rpc";
import { ClientStreamingCall } from "@protobuf-ts/runtime-rpc";
import { ServerStreamingCall } from "@protobuf-ts/runtime-rpc";
import type { DuplexStreamingCall } from "@protobuf-ts/runtime-rpc";
import type { ClientStreamingCall } from "@protobuf-ts/runtime-rpc";
import type { ServerStreamingCall } from "@protobuf-ts/runtime-rpc";
import { stackIntercept } from "@protobuf-ts/runtime-rpc";
import { ExampleResponse } from "./service-example";
import { ExampleRequest } from "./service-example";
import { UnaryCall } from "@protobuf-ts/runtime-rpc";
import { RpcOptions } from "@protobuf-ts/runtime-rpc";
import type { ExampleResponse } from "./service-example";
import type { ExampleRequest } from "./service-example";
import type { UnaryCall } from "@protobuf-ts/runtime-rpc";
import type { RpcOptions } from "@protobuf-ts/runtime-rpc";
/**
* @generated from protobuf service spec.ExampleService
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protobuf-ts 2.0.0-alpha.12 with parameters generate_dependencies,optimize_code_size
// @generated by protobuf-ts 2.4.0 with parameter optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { ServiceType } from "@protobuf-ts/runtime-rpc";
Expand Down
18 changes: 18 additions & 0 deletions packages/plugin/src/message-type-extensions/well-known-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ import { FieldInfoGenerator } from "../code-gen/field-info-generator";

export class WellKnownTypes implements CustomMethodGenerator {


static readonly protoFilenames = [
"google/protobuf/any.proto",
"google/protobuf/api.proto",
"google/protobuf/descriptor.proto",
"google/protobuf/duration.proto",
"google/protobuf/empty.proto",
"google/protobuf/field_mask.proto",
"google/protobuf/source_context.proto",
"google/protobuf/struct.proto",
"google/protobuf/timestamp.proto",
"google/protobuf/type.proto",
"google/protobuf/wrappers.proto",
"google/protobuf/compiler/plugin.proto",
"google/protobuf/any.proto",
];


constructor(
private readonly typeNameLookup: ITypeNameLookup,
private readonly imports: TypeScriptImports,
Expand Down
24 changes: 20 additions & 4 deletions packages/plugin/src/protobufts-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {FileTable} from "./file-table";
import {ServiceServerGeneratorGeneric} from "./code-gen/service-server-generator-generic";
import {ServiceClientGeneratorGrpc} from "./code-gen/service-client-generator-grpc";
import * as ts from "typescript";
import {assert} from "@protobuf-ts/runtime";
import {WellKnownTypes} from "./message-type-extensions/well-known-types";


export class ProtobuftsPlugin extends PluginBase {
Expand Down Expand Up @@ -56,8 +58,9 @@ export class ProtobuftsPlugin extends PluginBase {
// misc
generate_dependencies: {
description: "By default, only the PROTO_FILES passed as input to protoc are generated, \n" +
"not the files they import. Set this option to generate code for dependencies \n" +
"too.",
"not the files they import (with the exception of well-known types, which are \n" +
"always generated when imported). \n"+
"Set this option to generate code for dependencies too.",
},
force_exclude_all_options: {
description: "By default, custom options are included in the metadata and can be blacklisted \n" +
Expand Down Expand Up @@ -341,9 +344,22 @@ export class ProtobuftsPlugin extends PluginBase {


// plugins should only return files requested to generate
// unless our option "generate_dependencies" is set
// unless our option "generate_dependencies" is set.
// We always return well-known types, because we do not
// maintain them in a package - they are always generated
// on demand.
if (!options.generateDependencies) {
tsFiles = tsFiles.filter(file => request.fileToGenerate.includes(file.fileDescriptor.name!));
tsFiles = tsFiles.filter(file => {
const protoFilename = file.fileDescriptor.name;
assert(protoFilename);
if (request.fileToGenerate.includes(protoFilename)) {
return true;
}
if (WellKnownTypes.protoFilenames.includes(protoFilename)) {
return true;
}
return false;
});
}

// if a proto file is imported to use custom options, or if a proto file declares custom options,
Expand Down
4 changes: 0 additions & 4 deletions packages/test-generated/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ generate-speed: $(PROTOS)
--ts_out ./ts-out/ \
--ts_opt force_optimize_speed \
--ts_opt long_type_string \
--ts_opt generate_dependencies \
--proto_path $(PROTO_PATH) \
--proto_path ../plugin/ \
--experimental_allow_proto3_optional \
Expand All @@ -76,7 +75,6 @@ generate-size: $(PROTOS)
--ts_out ./ts-out/ \
--ts_opt force_optimize_code_size \
--ts_opt long_type_string \
--ts_opt generate_dependencies \
--proto_path $(PROTO_PATH) \
--proto_path ../plugin/ \
--experimental_allow_proto3_optional \
Expand All @@ -88,7 +86,6 @@ generate-size-bigint: $(PROTOS)
@npx protoc \
--ts_out ./ts-out/ \
--ts_opt force_optimize_code_size \
--ts_opt generate_dependencies \
--proto_path $(PROTO_PATH) \
--proto_path ../plugin/ \
--experimental_allow_proto3_optional \
Expand All @@ -100,7 +97,6 @@ generate-speed-bigint: $(PROTOS)
@npx protoc \
--ts_out ./ts-out/ \
--ts_opt force_optimize_speed \
--ts_opt generate_dependencies \
--proto_path $(PROTO_PATH) \
--proto_path ../plugin/ \
--experimental_allow_proto3_optional \
Expand Down

0 comments on commit 356a65f

Please sign in to comment.