fix: quickstart wrong package when go.mod exists in parent dir (#2048)
* chore: fix typo * fix: quickstart in dir with go.mod * fix: runner failed * chore: refine code * chore: simplify quickstart mono
This commit is contained in:
@@ -3,16 +3,14 @@ package gogen
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
goformat "go/format"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/collection"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/util"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
@@ -54,38 +52,11 @@ func genFile(c fileGenConfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
code := formatCode(buffer.String())
|
||||
code := golang.FormatCode(buffer.String())
|
||||
_, err = fp.WriteString(code)
|
||||
return err
|
||||
}
|
||||
|
||||
func getParentPackage(dir string) (string, error) {
|
||||
abs, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
projectCtx, err := ctx.Prepare(abs)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// fix https://github.com/zeromicro/go-zero/issues/1058
|
||||
wd := projectCtx.WorkDir
|
||||
d := projectCtx.Dir
|
||||
same, err := pathx.SameFile(wd, d)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
trim := strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir)
|
||||
if same {
|
||||
trim = strings.TrimPrefix(strings.ToLower(projectCtx.WorkDir), strings.ToLower(projectCtx.Dir))
|
||||
}
|
||||
|
||||
return filepath.ToSlash(filepath.Join(projectCtx.Path, trim)), nil
|
||||
}
|
||||
|
||||
func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error {
|
||||
util.WriteIndent(writer, indent)
|
||||
var err error
|
||||
@@ -136,15 +107,6 @@ func getMiddleware(api *spec.ApiSpec) []string {
|
||||
return result.KeysStr()
|
||||
}
|
||||
|
||||
func formatCode(code string) string {
|
||||
ret, err := goformat.Source([]byte(code))
|
||||
if err != nil {
|
||||
return code
|
||||
}
|
||||
|
||||
return string(ret)
|
||||
}
|
||||
|
||||
func responseGoTypeName(r spec.Route, pkg ...string) string {
|
||||
if r.ResponseType == nil {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user