diff --git a/cmd/protoc-gen-elixir-grpc/README.md b/cmd/protoc-gen-elixir-grpc/README.md index 0841260..af10c51 100644 --- a/cmd/protoc-gen-elixir-grpc/README.md +++ b/cmd/protoc-gen-elixir-grpc/README.md @@ -35,6 +35,18 @@ plugins: Server modules are generated into the same directory structure as the protobuf definitions, with `.server.pb.ex` suffix. +If you enable Buf's top-level `clean: true`, configure this plugin's `out` as a generated-only directory. Buf deletes each plugin `out` directory before invoking the plugin, so pointing `out` at a directory that also contains hand-written handlers will delete those files before this generator runs. + +```yaml +version: v2 +clean: true +plugins: + - local: protoc-gen-elixir-grpc + out: lib/generated_grpc +``` + +If generated stubs and hand-written handlers share the same output tree, keep `clean: false`. + ### Configuration Options You can configure the plugin using parameters: diff --git a/cmd/protoc-gen-elixir-grpc/main.go b/cmd/protoc-gen-elixir-grpc/main.go index 6778632..abb3994 100644 --- a/cmd/protoc-gen-elixir-grpc/main.go +++ b/cmd/protoc-gen-elixir-grpc/main.go @@ -16,6 +16,10 @@ // - local: protoc-gen-elixir-grpc // out: lib // +// If you use buf's top-level clean: true, set out to a generated-only +// directory. Buf deletes each plugin out directory before invoking the plugin. +// Do not point out at a directory that also contains hand-written handlers. +// // This generates server module definitions for the Protobuf services // defined by file.proto. If file.proto defines the Greeter service, the // invocations above will write output to: @@ -49,15 +53,15 @@ var ( ) const ( - filenameSuffix = ".ex" - serverSuffix = "Server" - defaultPackagePrefix = "" - packagePrefixFlag = "package_prefix" - handlerModulePrefixFlag = "handler_module_prefix" - serverModulePrefixFlag = "server_module_prefix" - httpTranscodeFlag = "http_transcode" - codecsFlag = "codecs" - compressorsFlag = "compressors" + filenameSuffix = ".ex" + serverSuffix = "Server" + defaultPackagePrefix = "" + packagePrefixFlag = "package_prefix" + handlerModulePrefixFlag = "handler_module_prefix" + serverModulePrefixFlag = "server_module_prefix" + httpTranscodeFlag = "http_transcode" + codecsFlag = "codecs" + compressorsFlag = "compressors" usage = "\n\nFlags:\n -h, --help\tPrint this help and exit.\n --version\tPrint the version and exit.\n --handler_module_prefix\tCustom Elixir module prefix for handler modules instead of protobuf package.\n --server_module_prefix\tCustom Elixir module prefix for server modules instead of protobuf package.\n --http_transcode\tEnable HTTP transcoding support (adds http_transcode: true to use GRPC.Server).\n --codecs\tComma-separated list of codec modules (e.g., 'GRPC.Codec.Proto,GRPC.Codec.WebText,GRPC.Codec.JSON').\n --compressors\tComma-separated list of compressor modules (e.g., 'GRPC.Compressor.Gzip')." ) @@ -469,7 +473,9 @@ func generateFilePath(file *descriptorpb.FileDescriptorProto, opts GenerateOptio baseFileName = baseFileName[:idx] } - return strings.Join(pathParts, "/") + "/" + baseFileName + ".server.pb" + filenameSuffix + pathParts = append(pathParts, baseFileName+".server.pb"+filenameSuffix) + + return strings.Join(pathParts, "/") } func toSnakeCase(s string) string { diff --git a/cmd/protoc-gen-elixir-grpc/main_test.go b/cmd/protoc-gen-elixir-grpc/main_test.go index 100c23b..1c5a7fb 100644 --- a/cmd/protoc-gen-elixir-grpc/main_test.go +++ b/cmd/protoc-gen-elixir-grpc/main_test.go @@ -1020,7 +1020,7 @@ end assert.Equal(t, 1, len(rsp.File)) file := rsp.File[0] - assert.Equal(t, "/simple.server.pb.ex", file.GetName()) + assert.Equal(t, "simple.server.pb.ex", file.GetName()) content := file.GetContent() expected := `# Code generated by protoc-gen-elixir-grpc. DO NOT EDIT. @@ -1075,7 +1075,7 @@ end assert.Equal(t, 1, len(rsp.File)) file := rsp.File[0] - assert.Equal(t, "/payment.server.pb.ex", file.GetName()) + assert.Equal(t, "payment.server.pb.ex", file.GetName()) content := file.GetContent() expected := `# Code generated by protoc-gen-elixir-grpc. DO NOT EDIT.