Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only generate moq when interface file is newer than output file #179

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/matryer/moq
module github.com/djui/moq
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should not be part of the PR.

(see also all other files, where the import paths are changed)


go 1.18

Expand Down
2 changes: 1 addition & 1 deletion internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"text/template"

"github.com/matryer/moq/internal/registry"
"github.com/djui/moq/internal/registry"
)

// Template is the Moq template. It is capable of generating the Moq
Expand Down
2 changes: 1 addition & 1 deletion internal/template/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"go/types"
"strings"

"github.com/matryer/moq/internal/registry"
"github.com/djui/moq/internal/registry"
)

// Data is the template data used to render the Moq template.
Expand Down
2 changes: 1 addition & 1 deletion internal/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"go/types"
"testing"

"github.com/matryer/moq/internal/registry"
"github.com/djui/moq/internal/registry"
)

func TestTemplateFuncs(t *testing.T) {
Expand Down
17 changes: 16 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
"path/filepath"

"github.com/matryer/moq/pkg/moq"
"github.com/djui/moq/pkg/moq"
)

// Version is the command version, injected at build time.
Expand All @@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not backwards compatible. Maybe it is better to preserve the current mode of operation and add a flag to opt-in for the mtime behavior. Maybe the -mtime flag could be used to enable the mtime mode as well as contain the name of the source file as well. If the source file is the empty string, it would fall back to GOFILE env variable.


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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the mtime flag is used instead of force, I think failing to stat the source or the destination file would be an error, which is returned from run.

} 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
4 changes: 2 additions & 2 deletions pkg/moq/moq.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"io"
"strings"

"github.com/matryer/moq/internal/registry"
"github.com/matryer/moq/internal/template"
"github.com/djui/moq/internal/registry"
"github.com/djui/moq/internal/template"
)

// Mocker can generate mock structs.
Expand Down