Skip to content

Commit

Permalink
feat: mock support scan files
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed Nov 15, 2024
1 parent a6af1ba commit 4787d4a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
1 change: 0 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 21 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
"program": "./",
"args": [
"mock",
"-s=testdata/projects/mock/service",
"-p=mock",
"-d=testdata/projects/mock/service/mock"
]
},
{
"name": "gonectr mock【scan file】",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "./",
"args": [
"mock",
"-f=testdata/projects/mock/service/i_user.go",
"-p=user_mock",
"-d=testdata/projects/mock/user_mock"
]
},
{
"name": "gonectr generate",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
"program": "./",
"args": [
"generate",
"-s=testdata/projects/generate/internal",
Expand All @@ -34,7 +47,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
"program": "./",
"cwd": "testdata/projects/long_module",
"args": [
"generate",
Expand All @@ -46,7 +59,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
"program": "./",
"cwd": "testdata/projects/single_with_go_generate/test",
"args": [
"run",
Expand All @@ -58,7 +71,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
"program": "./",
"cwd": "testdata/projects/single_with_go_generate/test",
"args": [
"build",
Expand All @@ -70,7 +83,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
"program": "./",
"cwd": "testdata/projects/single/test",
"args": [
"run",
Expand All @@ -82,7 +95,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
"program": "./",
"cwd": "testdata/projects/single/test",
"args": [
"build",
Expand All @@ -94,7 +107,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
"program": "./",
"cwd": "priest/testdata/x",
"args": [
"priest",
Expand Down
61 changes: 39 additions & 22 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@ package mock

import (
"fmt"
"github.com/gone-io/gonectr/utils"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/gone-io/gonectr/utils"

"errors"

"github.com/spf13/cobra"
)

var scanDir string
var scanFile []string
var packageName string
var destinationDir string

func init() {
Command.Flags().StringVarP(&scanDir, "scan-dir", "s", "", "scan dirs")
Command.Flags().StringArrayVarP(&scanFile, "scan-file", "f", []string{}, "scan files")
Command.Flags().StringVarP(&packageName, "package", "p", "", "package name")
Command.Flags().StringVarP(&destinationDir, "destination", "d", "", "destination dir")
}

var Command = &cobra.Command{
Use: "mock",
Short: "generate mock goner code for interface",
RunE: func(cmd *cobra.Command, args []string) error {
if scanDir == "" {
return errors.New("scanDir is required")
if scanDir == "" && scanFile == nil {
return errors.New("scan-dir and scan-file need at least one")
}

if packageName == "" {
Expand All @@ -42,14 +51,8 @@ var Command = &cobra.Command{

var mockedStructs []string

// 使用 filepath.Walk 递归扫描目录
err = filepath.Walk(scanDir, func(theFilepath string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// 检查是否是文件(排除目录)
if !info.IsDir() && !strings.HasSuffix(info.Name(), "gone.go") && strings.HasSuffix(info.Name(), ".go") {
var genCode = func(theFilepath string) error {
if !strings.HasSuffix(theFilepath, "gone.go") && strings.HasSuffix(theFilepath, ".go") {
code, err := GenMockCode(theFilepath, packageName)
if err != nil {
return err
Expand All @@ -68,9 +71,31 @@ var Command = &cobra.Command{
}
}
return nil
})
if err != nil {
return err
}

if scanDir != "" {
// 使用 filepath.Walk 递归扫描目录
err = filepath.Walk(scanDir, func(theFilepath string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// 检查是否是文件(排除目录)
if !info.IsDir() {
return genCode(theFilepath)
}
return nil
})
if err != nil {
return err
}
}

for _, f := range scanFile {
err := genCode(f)
if err != nil {
return err
}
}

priestCode := GenMockPriestCode(mockedStructs, packageName)
Expand All @@ -79,12 +104,6 @@ var Command = &cobra.Command{
},
}

func init() {
Command.Flags().StringVarP(&scanDir, "scan-dir", "s", "", "scan dirs")
Command.Flags().StringVarP(&packageName, "package", "p", "", "package name")
Command.Flags().StringVarP(&destinationDir, "destination", "d", "", "destination dir")
}

func GenMockCode(source string, packageName string) (code string, err error) {
cmd := exec.Command("mockgen",
fmt.Sprintf("-source=%s", source),
Expand All @@ -102,8 +121,6 @@ func GenMockCode(source string, packageName string) (code string, err error) {

const mockGneTpl = "// Code generated by MockGen. DO NOT EDIT."

//const GoneGenTpl = "// Code generated by Goner. DO NOT EDIT."

func AddGoneCode(code string) string {
code = strings.Replace(code, mockGneTpl, utils.GenerateBy, 1)

Expand Down
Binary file removed testdata/projects/single/test/single
Binary file not shown.
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const version = "v0.0.9"
const version = "v0.0.10"

0 comments on commit 4787d4a

Please sign in to comment.