diff --git a/CHANGELOG.md b/CHANGELOG.md index f467c63..c2539e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## [Unreleased] -No changes yet. + +- Added `--file-prefix` flag. [#32](https://github.com/derision-test/go-mockgen/pull/32) ## [v1.3.0] - 2022-06-06 diff --git a/README.md b/README.md index 7ec19e0..7214363 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ The following flags are defined by the binary. | disable-formatting | | Do not run goimports over the rendered files (enabled by default). | | goimports | | Path to the goimports binary (uses goimports on your PATH by default). | | for-test | | Append _test suffix to generated package names and file names. | +| file-prefix | | Content that is written at the top of each generated file. | ### Configuration file @@ -52,7 +53,7 @@ mocks: filename: foo/bar/mock_cache_test.go ``` -The top level of the configuration file may also set the keys `exclude`, `prefix`, `constructor-prefix`, `goimports`, `force`, `disable-formatting`, and `for-tests`. Top-level excludes will also be applied to each mock generator entry. The values for prefixes and goimports will apply to each mock generator entry if a value is not set. The remaining boolean values will be true for each mock generator entry if set at the top level (regardless of the setting of each entry). +The top level of the configuration file may also set the keys `exclude`, `prefix`, `constructor-prefix`, `goimports`, `file-prefix`, `force`, `disable-formatting`, and `for-tests`. Top-level excludes will also be applied to each mock generator entry. The values for interface and constructor prefixes, goimports, and file content prefixes will apply to each mock generator entry if a value is not set. The remaining boolean values will be true for each mock generator entry if set at the top level (regardless of the setting of each entry). ## Testing with Mocks diff --git a/cmd/go-mockgen/args.go b/cmd/go-mockgen/args.go index 1fe100c..987947f 100644 --- a/cmd/go-mockgen/args.go +++ b/cmd/go-mockgen/args.go @@ -76,6 +76,7 @@ func parseFlags() (*generation.Options, error) { app.Flag("disable-formatting", "Do not run goimports over the rendered files.").BoolVar(&opts.DisableFormatting) app.Flag("goimports", "Path to the goimports binary.").Default("goimports").StringVar(&opts.GoImportsBinary) app.Flag("for-test", "Append _test suffix to generated package names and file names.").Default("false").BoolVar(&opts.ForTest) + app.Flag("file-prefix", "Content that is written at the top of each generated file.").StringVar(&opts.FilePrefix) if _, err := app.Parse(os.Args[1:]); err != nil { return nil, err @@ -99,6 +100,7 @@ func parseManifest() ([]*generation.Options, error) { DisableFormatting bool `yaml:"disable-formatting"` Goimports string `yaml:"goimports"` ForTest bool `yaml:"for-test"` + FilePrefix string `yaml:"file-prefix"` Mocks []struct { Path string `yaml:"path"` @@ -115,6 +117,7 @@ func parseManifest() ([]*generation.Options, error) { DisableFormatting bool `yaml:"disable-formatting"` Goimports string `yaml:"goimports"` ForTest bool `yaml:"for-test"` + FilePrefix string `yaml:"file-prefix"` } `yaml:"mocks"` } if err := yaml.Unmarshal(contents, &payload); err != nil { @@ -136,6 +139,9 @@ func parseManifest() ([]*generation.Options, error) { if opts.Goimports == "" { opts.Goimports = payload.Goimports } + if opts.FilePrefix == "" { + opts.FilePrefix = payload.FilePrefix + } // Overwrite if payload.Force { @@ -171,6 +177,7 @@ func parseManifest() ([]*generation.Options, error) { DisableFormatting: opts.DisableFormatting, GoImportsBinary: opts.Goimports, ForTest: opts.ForTest, + FilePrefix: opts.FilePrefix, }) } diff --git a/internal/mockgen/generation/generate.go b/internal/mockgen/generation/generate.go index 72aade5..d48e0c8 100644 --- a/internal/mockgen/generation/generate.go +++ b/internal/mockgen/generation/generate.go @@ -30,6 +30,7 @@ type Options struct { DisableFormatting bool GoImportsBinary string ForTest bool + FilePrefix string } func Generate(ifaces []*types.Interface, opts *Options) error { @@ -106,7 +107,7 @@ func generateAndRender(ifaces []*types.Interface, filename string, opts *Options pkgName += "_test" } - content, err := generateContent(ifaces, pkgName, opts.Prefix, opts.ConstructorPrefix, opts.OutputImportPath) + content, err := generateContent(ifaces, pkgName, opts.Prefix, opts.ConstructorPrefix, opts.FilePrefix, opts.OutputImportPath) if err != nil { return err } @@ -132,9 +133,14 @@ func generateAndRender(ifaces []*types.Interface, filename string, opts *Options return nil } -func generateContent(ifaces []*types.Interface, pkgName, prefix, constructorPrefix, outputImportPath string) (string, error) { +func generateContent(ifaces []*types.Interface, pkgName, prefix, constructorPrefix, fileContentPrefix, outputImportPath string) (string, error) { + if fileContentPrefix != "" { + separator := "\n// " + fileContentPrefix = "\n//" + separator + strings.Join(strings.Split(strings.TrimSpace(fileContentPrefix), "\n"), separator) + } + file := jen.NewFile(pkgName) - file.HeaderComment(fmt.Sprintf("Code generated by %s %s; DO NOT EDIT.", consts.Name, consts.Version)) + file.HeaderComment(fmt.Sprintf("// Code generated by %s %s; DO NOT EDIT.%s", consts.Name, consts.Version, fileContentPrefix)) for _, iface := range ifaces { log.Printf("generating code for interface '%s'\n", iface.Name)