github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/plugin/builtin/csharp_plugin.go (about)

     1  package builtin
     2  
     3  import (
     4  	"path"
     5  	"strings"
     6  
     7  	"github.com/bazelbuild/bazel-gazelle/label"
     8  	"github.com/stackb/rules_proto/pkg/protoc"
     9  )
    10  
    11  func init() {
    12  	protoc.Plugins().MustRegisterPlugin(&CsharpPlugin{})
    13  }
    14  
    15  // CsharpPlugin implements Plugin for the built-in protoc C# plugin.
    16  type CsharpPlugin struct{}
    17  
    18  // Name implements part of the Plugin interface.
    19  func (p *CsharpPlugin) Name() string {
    20  	return "builtin:csharp"
    21  }
    22  
    23  // Configure implements part of the Plugin interface.
    24  func (p *CsharpPlugin) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration {
    25  	return &protoc.PluginConfiguration{
    26  		Label: label.New("build_stack_rules_proto", "plugin/builtin", "csharp"),
    27  		Outputs: protoc.FlatMapFiles(
    28  			csharpFileName(ctx.Rel, ctx.PluginConfig),
    29  			protoc.Always,
    30  			ctx.ProtoLibrary.Files()...,
    31  		),
    32  		Out:     ctx.Rel,
    33  		Options: ctx.PluginConfig.GetOptions(),
    34  	}
    35  }
    36  
    37  func csharpFileName(rel string, cfg protoc.LanguagePluginConfig) func(*protoc.File) []string {
    38  	return func(f *protoc.File) []string {
    39  		// setup the file extension
    40  		ext := ".cs"
    41  		for k, want := range cfg.Options {
    42  			if strings.HasPrefix(k, "file_extension=") && want {
    43  				ext = k[len("file_extension="):]
    44  				continue
    45  			}
    46  		}
    47  
    48  		return []string{path.Join(rel, protoc.ToPascalCase(f.Name)) + ext}
    49  	}
    50  
    51  }