diff --git a/gomock_generator/gomockgenerator/generate.go b/gomock_generator/gomockgenerator/generate.go index b4a01d4e..f03a0e46 100644 --- a/gomock_generator/gomockgenerator/generate.go +++ b/gomock_generator/gomockgenerator/generate.go @@ -28,6 +28,8 @@ type GenerationConfiguration struct { ConcurrentGoroutines int // NoGoMod by default we'll consider go modules is enabled, mockgen will be called with -mod=mod to read interfaces in modules instead of default GOPATH NoGoMod bool + // MocksRootPath override the output root path of the mocks + MocksRootPath string } // MocksConfiguration contains the configuration of the mocks to generate. @@ -67,7 +69,12 @@ func GenerateMocks(ctx context.Context, gcfg GenerationConfiguration, mocksCfg M if mocksCfg.BaseDirectory == "" { mocksCfg.BaseDirectory = mocksCfg.BasePackage } - err = os.Chdir(path.Join(os.Getenv("GOPATH"), "src", mocksCfg.BaseDirectory)) + + mocksPath := path.Join(path.Join(os.Getenv("GOPATH"), "src"), mocksCfg.BaseDirectory) + if gcfg.MocksRootPath != "" { + mocksPath = gcfg.MocksRootPath + } + err = os.Chdir(mocksPath) if err != nil { return errors.Wrap(err, "fail to move to base package directory") } @@ -79,7 +86,10 @@ func GenerateMocks(ctx context.Context, gcfg GenerationConfiguration, mocksCfg M }).Infof("Generating %v mocks", len(mocksCfg.Mocks)) var mockSigs map[string]string - mockSigsPath := path.Join(os.Getenv("GOPATH"), "src", mocksCfg.BaseDirectory, gcfg.SignaturesFilename) + mockSigsPath := path.Join(gcfg.MocksRootPath, mocksCfg.BaseDirectory, gcfg.SignaturesFilename) + if gcfg.MocksRootPath != "" { + mockSigsPath = path.Join(gcfg.MocksRootPath, gcfg.SignaturesFilename) + } sigs, err := os.ReadFile(mockSigsPath) if os.IsNotExist(err) { @@ -168,7 +178,10 @@ func generateMock(ctx context.Context, gcfg GenerationConfiguration, baseDirecto mock.SrcPackage = path.Join(basePackage, mock.SrcPackage) } - mockPath := filepath.Join(os.Getenv("GOPATH"), "src", baseDirectory, mock.MockFile) + mockPath := filepath.Join(gcfg.MocksRootPath, baseDirectory, mock.MockFile) + if gcfg.MocksRootPath != "" { + mockPath = path.Join(gcfg.MocksRootPath, mock.MockFile) + } log = log.WithFields(logrus.Fields{ "mock_file": mock.MockFile, "interface": mock.Interface, @@ -198,7 +211,11 @@ func generateMock(ctx context.Context, gcfg GenerationConfiguration, baseDirecto mockSrcPath := strings.Replace(mock.SrcPackage, basePackage, baseDirectory, -1) hashKey := fmt.Sprintf("%s.%s", mockSrcPath, mock.Interface) - hash, err := interfaceHash(mockSrcPath, mock.Interface) + fullPath := path.Join(os.Getenv("GOPATH"), "src", mockSrcPath) + if gcfg.MocksRootPath != "" { + fullPath = path.Join(gcfg.MocksRootPath, strings.ReplaceAll(mock.SrcPackage, baseDirectory, "")) + } + hash, err := interfaceHash(fullPath, mockSrcPath, mock.Interface) if err != nil { return "", "", errors.Wrapf(err, "fail to get interface hash of %v:%v", mock.SrcPackage, mock.Interface) } diff --git a/gomock_generator/gomockgenerator/package.go b/gomock_generator/gomockgenerator/package.go index b23d79d5..da8b7a21 100644 --- a/gomock_generator/gomockgenerator/package.go +++ b/gomock_generator/gomockgenerator/package.go @@ -7,15 +7,14 @@ import ( "go/parser" "go/token" "os" - "path" "path/filepath" "strings" "github.com/pkg/errors" ) -func interfaceHash(pkg, iName string) (string, error) { - sig, err := interfaceSignature(pkg, iName) +func interfaceHash(fullPath, pkg, iName string) (string, error) { + sig, err := interfaceSignature(fullPath, pkg, iName) if err != nil { return "", errors.Wrapf(err, "fail to get interface signature for %s:%s", pkg, iName) } @@ -26,12 +25,11 @@ func interfaceHash(pkg, iName string) (string, error) { return fmt.Sprintf("% x", hash), nil } -func interfaceSignature(pkg, iName string) (string, error) { +func interfaceSignature(fullPath, pkg, iName string) (string, error) { cwd, err := os.Getwd() if err != nil { return "", errors.Wrap(err, "fail to get current working directory") } - fullPath := path.Join(os.Getenv("GOPATH"), "src", pkg) vendoredPkg := filepath.Join(cwd, "vendor", pkg) if _, err := os.Stat(vendoredPkg); err == nil { fullPath = vendoredPkg diff --git a/gomock_generator/main.go b/gomock_generator/main.go index b81104b1..be5fe50a 100644 --- a/gomock_generator/main.go +++ b/gomock_generator/main.go @@ -42,6 +42,7 @@ Reads the mymocks.json file from the current directory and generates the mocks, app.cli.Flags = []cli.Flag{ cli.StringFlag{Name: "mocks-filepath", Value: "./mocks.json", Usage: "Path to the JSON file containing the MockConfiguration. Location of this file is the base package.", EnvVar: "MOCKS_FILEPATH"}, cli.StringFlag{Name: "signatures-filename", Value: "mocks_sig.json", Usage: "Filename of the signatures cache. Location of this file is the base package.", EnvVar: "SIGNATURES_FILENAME"}, + cli.StringFlag{Name: "mocks-root-path", Value: "", Usage: "Path to the local source code", EnvVar: "MOCKS_ROOT_PATH"}, cli.IntFlag{Name: "concurrent-goroutines", Value: 4, Usage: "Concurrent amount of goroutines to generate mock.", EnvVar: "CONCURRENT_GOROUTINES"}, cli.BoolFlag{Name: "debug", Usage: "Activate debug logs"}, } @@ -66,6 +67,7 @@ VERSION: {{end}} ` app.cli.Before = func(c *cli.Context) error { + app.config.MocksRootPath = c.GlobalString("mocks-root-path") app.config.MocksFilePath = c.GlobalString("mocks-filepath") app.config.SignaturesFilename = c.GlobalString("signatures-filename") app.config.ConcurrentGoroutines = c.GlobalInt("concurrent-goroutines") @@ -87,6 +89,7 @@ VERSION: "mocks_file_path": app.config.MocksFilePath, "signatures_filename": app.config.SignaturesFilename, "concurrent_goroutines": app.config.ConcurrentGoroutines, + "mocks-root-path": app.config.MocksRootPath, }).Info("Configuration for this mocks generation") rawFile, err := os.Open(app.config.MocksFilePath)