Skip to content

Commit

Permalink
feat(jzero): support workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Nov 24, 2024
1 parent 1d54258 commit 7f58b88
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
9 changes: 7 additions & 2 deletions internal/gen/genapi/patch_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"

"github.com/jzero-io/jzero/pkg/mod"
)

type HandlerFile struct {
Expand Down Expand Up @@ -74,11 +76,15 @@ func (ja *JzeroApi) patchHandler(file HandlerFile) error {
return err
}

if err = mod.UpdateImportedModule(f, fset, ja.Wd, ja.Module); err != nil {
return err
}

// split api types dir
if ja.SplitApiTypesDir {
for _, g := range ja.GenCodeApiSpecMap[file.ApiFilepath].Service.Groups {
if g.GetAnnotation("group") == file.Group {
if err := ja.updateHandlerImportedTypesPath(f, fset, file); err != nil {
if err = ja.updateHandlerImportedTypesPath(f, fset, file); err != nil {
return err
}
}
Expand Down Expand Up @@ -124,7 +130,6 @@ func (ja *JzeroApi) removeHandlerSuffix(f *ast.File) error {
}
return false
}

return true
})
return nil
Expand Down
6 changes: 6 additions & 0 deletions internal/gen/genapi/patch_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/util/console"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"

"github.com/jzero-io/jzero/pkg/mod"
)

type LogicFile struct {
Expand Down Expand Up @@ -92,6 +94,10 @@ func (ja *JzeroApi) patchLogic(file LogicFile) error {
return errors.Errorf("remove suffix meet error: [%v]", err)
}

if err = mod.UpdateImportedModule(f, fset, ja.Wd, ja.Module); err != nil {
return err
}

// change logic types
if ja.ChangeLogicTypes {
if _, ok := ja.GenCodeApiSpecMap[file.DescFilepath]; ok {
Expand Down
54 changes: 54 additions & 0 deletions pkg/mod/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ package mod
import (
"bytes"
"encoding/json"
"go/ast"
"go/token"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"golang.org/x/tools/go/ast/astutil"
)

// GetParentPackage if is submodule project, root package is based on go.mod and add its dir
Expand Down Expand Up @@ -69,10 +75,58 @@ func GetGoMod(workDir string) (*ModuleStruct, error) {
return &ms[0], nil
}

func GetGoMods(workDir string) ([]ModuleStruct, error) {
data, err := execx.Run("go list -json -m", workDir)
if err != nil {
return nil, err
}

var ms []ModuleStruct
decoder := json.NewDecoder(bytes.NewReader([]byte(data)))
for {
var m ModuleStruct
err = decoder.Decode(&m)
if err != nil {
if err.Error() == "EOF" {
break
}
}
ms = append(ms, m)
}
return ms, nil
}

// ModuleStruct contains the relative data of go module,
// which is the result of the command go list
type ModuleStruct struct {
Path string
Dir string
GoVersion string
}

func UpdateImportedModule(f *ast.File, fset *token.FileSet, workDir string, module string) error {
// 当前项目存在 go.mod 项目, 并且 go list -json -m 有多个, 即使用了 go workspace 机制
if pathx.FileExists("go.mod") {
mods, err := GetGoMods(workDir)
if err != nil {
return err
}
if len(mods) > 1 {
rootPkg, err := golang.GetParentPackage(workDir)
if err != nil {
return err
}
imports := astutil.Imports(fset, f)
for _, imp := range imports {
for _, name := range imp {
if strings.HasPrefix(name.Path.Value, "\""+rootPkg) {
unQuote, _ := strconv.Unquote(name.Path.Value)
newImp := strings.Replace(unQuote, rootPkg, module, 1)
astutil.RewriteImport(fset, f, unQuote, newImp)
}
}
}
}
}
return nil
}

0 comments on commit 7f58b88

Please sign in to comment.