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 19, 2018
1 parent 3b2d540 commit 73da645
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,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: "// Code generated by GENERATOR_NAME. DO NOT EDIT.",
defaultCommandLineFlags: true,
}
}

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

// If GeneratedByCommentTemplate is set, generate a "Code generated by" comment
// above the package declaration 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 +109,14 @@ 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 != "" {
generatorPath := strings.Split(os.Args[0], "/")
generatorName := generatorPath[len(generatorPath)-1]
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

0 comments on commit 73da645

Please sign in to comment.