github.com/jwilson-ts/prototool@v1.3.0/internal/cfginit/cfginit.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package cfginit contains the template for prototool.yaml files, as well
    22  // as a function to generate a prototool.yaml file given a specific protoc
    23  // version, with or without commenting out the remainder of the options.
    24  package cfginit
    25  
    26  import (
    27  	"bytes"
    28  	"html/template"
    29  )
    30  
    31  var tmpl = template.Must(template.New("tmpl").Parse(`# Paths to exclude when searching for Protobuf files.
    32  {{.V}}excludes:
    33  {{.V}}  - path/to/a
    34  {{.V}}  - path/to/b/file.proto
    35  
    36  # Protoc directives.
    37  protoc:
    38    # The Protobuf version to use from https://github.com/protocolbuffers/protobuf/releases.
    39    # By default use {{.ProtocVersion}}.
    40    # You probably want to set this to make your builds completely reproducible.
    41    version: {{.ProtocVersion}}
    42  
    43    # Additional paths to include with -I to protoc.
    44    # By default, the directory of the config file is included,
    45    # or the current directory if there is no config file.
    46    {{.V}}includes:
    47    {{.V}}  - ../../vendor/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
    48  
    49  
    50    # If not set, compile will fail if there are unused imports.
    51    # Setting this will ignore unused imports.
    52    {{.V}}allow_unused_imports: true
    53  
    54  # Create directives.
    55  {{.V}}create:
    56    # List of mappings from relative directory to base package.
    57    # This affects how packages are generated with create.
    58    {{.V}}packages:
    59      # This means that a file created "foo.proto" in the current directory will have package "bar".
    60      # A file created "a/b/foo.proto" will have package "bar.a.b".
    61      {{.V}}- directory: .
    62      {{.V}}  name: bar
    63      # This means that a file created "idl/code.uber/a/b/c.proto" will have package "uber.a.b".
    64      {{.V}}- directory: idl/code.uber
    65      {{.V}}  name: uber
    66  
    67  # Lint directives.
    68  {{.V}}lint:
    69    # Linter files to ignore.
    70  {{.V}}  ignores:
    71  {{.V}}    - id: RPC_NAMES_CAMEL_CASE
    72  {{.V}}      files:
    73  {{.V}}        - path/to/foo.proto
    74  {{.V}}        - path/to/bar.proto
    75  {{.V}}    - id: SYNTAX_PROTO3
    76  {{.V}}      files:
    77  {{.V}}        - path/to/foo.proto
    78  
    79    # Linter rules.
    80    # Run prototool list-all-linters to see all available linters.
    81  {{.V}}  rules:
    82      # Determines whether or not to include the default set of linters.
    83  {{.V}}    no_default: true
    84  
    85      # The specific linters to add.
    86  {{.V}}    add:
    87  {{.V}}      - ENUM_NAMES_CAMEL_CASE
    88  {{.V}}      - ENUM_NAMES_CAPITALIZED
    89  
    90      # The specific linters to remove.
    91  {{.V}}    remove:
    92  {{.V}}      - ENUM_NAMES_CAMEL_CASE
    93  
    94  # Code generation directives.
    95  {{.V}}generate:
    96    # Options that will apply to all plugins of type go and gogo.
    97  {{.V}}  go_options:
    98      # The base import path. This should be the go path of the prototool.yaml file.
    99      # This is required if you have any go plugins.
   100  {{.V}}    import_path: uber/foo/bar.git/idl/uber
   101  
   102      # Extra modifiers to include with Mfile=package.
   103  {{.V}}    extra_modifiers:
   104  {{.V}}      google/api/annotations.proto: google.golang.org/genproto/googleapis/api/annotations
   105  {{.V}}      google/api/http.proto: google.golang.org/genproto/googleapis/api/annotations
   106  
   107    # The list of plugins.
   108  {{.V}}  plugins:
   109        # The plugin name. This will go to protoc with --name_out, so it either needs
   110        # to be a built-in name (like java), or a plugin name with a binary
   111        # protoc-gen-name.
   112  {{.V}}    - name: gogo
   113  
   114        # The type, if any. Valid types are go, gogo.
   115        # Use go if your plugin is a standard Golang plugin
   116        # that uses github.com/golang/protobuf imports, use gogo
   117        # if it uses github.com/gogo/protobuf imports. For protoc-gen-go
   118        # use go, For protoc-gen-gogo, protoc-gen-gogoslick, etc, use gogo.
   119  {{.V}}      type: gogo
   120  
   121        # Extra flags to specify.
   122        # The only flag you will generally set is plugins=grpc for Golang.
   123        # The Mfile=package flags are automatically set.
   124        # ** Otherwise, generally do not set this unless you know what you are doing. **
   125  {{.V}}      flags: plugins=grpc
   126  
   127        # The path to output generated files to.
   128        # If the directory does not exist, it will be created when running generation.
   129        # This needs to be a relative path.
   130  {{.V}}      output: ../../.gen/proto/go
   131  
   132        # Optional override for the plugin path. For example, if you set set path to
   133        # /usr/local/bin/gogo_plugin", prototool will add the
   134        # "--plugin=protoc-gen-gogo=/usr/local/bin/gogo_plugin" flag to protoc calls.
   135  {{.V}}      path: /usr/local/bin/gogo
   136  
   137  {{.V}}    - name: yarpc-go
   138  {{.V}}      type: gogo
   139  {{.V}}      output: ../../.gen/proto/go
   140  
   141  {{.V}}    - name: grpc-gateway
   142  {{.V}}      type: go
   143  {{.V}}      output: ../../.gen/proto/go
   144  
   145  {{.V}}    - name: java
   146  {{.V}}      output: ../../.gen/proto/java`))
   147  
   148  type tmplData struct {
   149  	V             string
   150  	ProtocVersion string
   151  }
   152  
   153  // Generate generates the data.
   154  //
   155  // Set uncomment to true to uncomment the example settings.
   156  func Generate(protocVersion string, uncomment bool) ([]byte, error) {
   157  	tmplData := &tmplData{
   158  		ProtocVersion: protocVersion,
   159  	}
   160  	if !uncomment {
   161  		tmplData.V = "#"
   162  	}
   163  	buffer := bytes.NewBuffer(nil)
   164  	if err := tmpl.Execute(buffer, tmplData); err != nil {
   165  		return nil, err
   166  	}
   167  	return buffer.Bytes(), nil
   168  }