Skip to content

Commit

Permalink
Add --boilerplate-file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
avorima committed Dec 8, 2020
1 parent da6bf5e commit 9f995bf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
12 changes: 12 additions & 0 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -89,6 +90,7 @@ func init() {
pFlags.String("srcpkg", "", "source pkg to search for interfaces")
pFlags.BoolP("dry-run", "d", false, "Do a dry run, don't modify any files")
pFlags.Bool("disable-version-string", false, "Do not insert the version string into the generated mock file.")
pFlags.String("boilerplate-file", "", "File to read a boilerplate text from. Text should be a go block comment, i.e. /* ... */")

viper.BindPFlags(pFlags)
}
Expand Down Expand Up @@ -231,10 +233,20 @@ func (r *RootApp) Run() error {
baseDir = filepath.Dir(pkg.GoFiles[0])
}

var boilerplate string
if r.Config.BoilerplateFile != "" {
data, err := ioutil.ReadFile(r.Config.BoilerplateFile)
if err != nil {
log.Fatal().Msgf("Failed to read boilerplate file %s: %v", r.Config.BoilerplateFile, err)
}
boilerplate = string(data)
}

visitor := &pkg.GeneratorVisitor{
Config: r.Config,
InPackage: r.Config.InPackage,
Note: r.Config.Note,
Boilerplate: boilerplate,
Osp: osp,
PackageName: r.Config.Outpkg,
PackageNamePrefix: r.Config.Packageprefix,
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config struct {
Quiet bool
Recursive bool
SrcPkg string
BoilerplateFile string `mapstructure:"boilerplate-file"`
// StructName overrides the name given to the mock struct and should only be nonempty
// when generating for an exact match (non regex expression in -name).
StructName string
Expand Down
15 changes: 8 additions & 7 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,6 @@ func (g *Generator) GeneratePrologueNote(note string) {
}
prologue += ". DO NOT EDIT.\n"

// The note contains a boilerplate block comment that needs to come first.
if strings.HasPrefix(note, "/*") {
g.printf("%s\n", note)
g.printf("\n")
note = ""
}

g.printf(prologue)
if note != "" {
g.printf("\n")
Expand All @@ -282,6 +275,14 @@ func (g *Generator) GeneratePrologueNote(note string) {
g.printf("\n")
}

// GenerateBoilerplate adds a boilerplate text. It should be called
// before any other generator methods to ensure the text is on top.
func (g *Generator) GenerateBoilerplate(boilerplate string) {
if boilerplate != "" {
g.printf("%s\n", boilerplate)
}
}

// ErrNotInterface is returned when the given type is not an interface
// type.
var ErrNotInterface = errors.New("expression not an interface")
Expand Down
6 changes: 2 additions & 4 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,14 @@ func (s *GeneratorSuite) TestGeneratorPrologueNote() {
s.Equal(expected, generator.buf.String())
}

func (s *GeneratorSuite) TestGeneratorPrologueNoteBlockComment() {
func (s *GeneratorSuite) TestGeneratorBoilerplate() {
generator := s.getGenerator(testFile, "Requester", false, "")
generator.GeneratePrologueNote("/*\n BOILERPLATE\n*/")
generator.GenerateBoilerplate("/*\n BOILERPLATE\n*/\n")

expected := `/*
BOILERPLATE
*/
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
`

s.Equal(expected, generator.buf.String())
Expand Down
8 changes: 5 additions & 3 deletions pkg/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ func (w *Walker) doWalk(ctx context.Context, p *Parser, dir string, visitor Walk

type GeneratorVisitor struct {
config.Config
InPackage bool
Note string
Osp OutputStreamProvider
InPackage bool
Note string
Boilerplate string
Osp OutputStreamProvider
// The name of the output package, if InPackage is false (defaults to "mocks")
PackageName string
PackageNamePrefix string
Expand Down Expand Up @@ -149,6 +150,7 @@ func (v *GeneratorVisitor) VisitWalk(ctx context.Context, iface *Interface) erro
defer closer()

gen := NewGenerator(ctx, v.Config, iface, pkg)
gen.GenerateBoilerplate(v.Boilerplate)
gen.GeneratePrologueNote(v.Note)
gen.GeneratePrologue(ctx, pkg)

Expand Down

0 comments on commit 9f995bf

Please sign in to comment.