Skip to content

Commit

Permalink
Only generate moq when interface file is newer than output file
Browse files Browse the repository at this point in the history
  • Loading branch information
djui committed Oct 12, 2022
1 parent 13aa048 commit a030543
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type userFlags struct {
stubImpl bool
skipEnsure bool
remove bool
force bool
args []string
}

Expand All @@ -37,6 +38,7 @@ func main() {
flag.BoolVar(&flags.skipEnsure, "skip-ensure", false,
"suppress mock implementation check, avoid import cycle if mocks generated outside of the tested package")
flag.BoolVar(&flags.remove, "rm", false, "first remove output file, if it exists")
flag.BoolVar(&flags.force, "force", false, "force generation, otherwise check if go generate file is newer than output file")

flag.Usage = func() {
fmt.Println(`moq [flags] source-dir interface [interface2 [interface3 [...]]]`)
Expand Down Expand Up @@ -65,6 +67,19 @@ func run(flags userFlags) error {
return errors.New("not enough arguments")
}

if !flags.force && flags.outFile != "" {
inFile := os.Getenv("GOFILE")
if inStat, err := os.Stat(inFile); err != nil {
fmt.Fprintln(os.Stderr, err)
} else if outStat, err := os.Stat(flags.outFile); err != nil {
if !errors.Is(err, os.ErrNotExist) {
fmt.Fprintln(os.Stderr, err)
}
} else if !inStat.ModTime().After(outStat.ModTime()) {
return nil // Assume no changes thus no need to regenerate.
}
}

if flags.remove && flags.outFile != "" {
if err := os.Remove(flags.outFile); err != nil {
if !errors.Is(err, os.ErrNotExist) {
Expand Down

0 comments on commit a030543

Please sign in to comment.