Skip to content

Commit

Permalink
Add 'Code generated by' comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybuckley committed Jan 23, 2018
1 parent 3b2d540 commit 3a11997
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
25 changes: 21 additions & 4 deletions args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand All @@ -40,10 +41,11 @@ import (
// before calling AddFlags.
func Default() *GeneratorArgs {
return &GeneratorArgs{
OutputBase: DefaultSourceTree(),
GoHeaderFilePath: filepath.Join(DefaultSourceTree(), "k8s.io/gengo/boilerplate/boilerplate.go.txt"),
GeneratedBuildTag: "ignore_autogenerated",
defaultCommandLineFlags: true,
OutputBase: DefaultSourceTree(),
GoHeaderFilePath: filepath.Join(DefaultSourceTree(), "k8s.io/gengo/boilerplate/boilerplate.go.txt"),
GeneratedBuildTag: "ignore_autogenerated",
GeneratedByCommentTemplate: "// This file was autogenerated by GENERATOR_NAME. Do not edit it manually!",
defaultCommandLineFlags: true,
}
}

Expand All @@ -64,6 +66,11 @@ type GeneratorArgs struct {
// Where to get copyright header text.
GoHeaderFilePath string

// If GeneratedByCommentTemplate is set, generate a "Code generated by" comment
// below the bloilerplate, of the format defined by this string.
// Any instances of "GENERATOR_NAME" will be replaced with the name of the code generator.
GeneratedByCommentTemplate string

// If true, only verify, don't write anything.
VerifyOnly bool

Expand Down Expand Up @@ -103,6 +110,16 @@ func (g *GeneratorArgs) LoadGoBoilerplate() ([]byte, error) {
return nil, err
}
b = bytes.Replace(b, []byte("YEAR"), []byte(strconv.Itoa(time.Now().Year())), -1)

if g.GeneratedByCommentTemplate != "" {
if len(b) != 0 {
b = append(b, byte('\n'))
}
generatorName := path.Base(os.Args[0])
generatedByComment := strings.Replace(g.GeneratedByCommentTemplate, "GENERATOR_NAME", generatorName, -1)
s := fmt.Sprintf("%s\n\n", generatedByComment)
b = append(b, []byte(s)...)
}
return b, nil
}

Expand Down
4 changes: 0 additions & 4 deletions examples/deepcopy-gen/generators/deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
inputs := sets.NewString(context.Inputs...)
packages := generator.Packages{}
header := append([]byte(fmt.Sprintf("// +build !%s\n\n", arguments.GeneratedBuildTag)), boilerplate...)
header = append(header, []byte(`
// This file was autogenerated by deepcopy-gen. Do not edit it manually!
`)...)

boundingDirs := []string{}
if customArgs, ok := arguments.CustomArgs.(*CustomArgs); ok {
Expand Down
5 changes: 0 additions & 5 deletions examples/defaulter-gen/generators/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,6 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat

packages := generator.Packages{}
header := append([]byte(fmt.Sprintf("// +build !%s\n\n", arguments.GeneratedBuildTag)), boilerplate...)
header = append(header, []byte(
`
// This file was autogenerated by defaulter-gen. Do not edit it manually!
`)...)

// Accumulate pre-existing default functions.
// TODO: This is too ad-hoc. We need a better way.
Expand Down
6 changes: 1 addition & 5 deletions examples/set-gen/generators/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ func Packages(_ *generator.Context, arguments *args.GeneratorArgs) generator.Pac
return generator.Packages{&generator.DefaultPackage{
PackageName: "sets",
PackagePath: arguments.OutputPackagePath,
HeaderText: append(boilerplate, []byte(
`
// This file was autogenerated by set-gen. Do not edit it manually!
`)...),
HeaderText: boilerplate,
PackageDocumentation: []byte(
`// Package sets has auto-generated set types.
`),
Expand Down

0 comments on commit 3a11997

Please sign in to comment.