refactor gomod logic (#47)
* rebase upstream * rebase * trim no need line * trim no need line * trim no need line * refactor gomod module logic Co-authored-by: kingxt <dream4kingxt@163.com>
This commit is contained in:
@@ -4,41 +4,21 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
goformat "go/format"
|
goformat "go/format"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/collection"
|
"github.com/tal-tech/go-zero/core/collection"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
goctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/project"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getParentPackage(dir string) (string, error) {
|
func getParentPackage(dir string) (string, error) {
|
||||||
absDir, err := filepath.Abs(dir)
|
p, err := project.Prepare(dir, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
absDir = strings.ReplaceAll(absDir, `\`, `/`)
|
return p.GoMod.Module, nil
|
||||||
rootPath, hasGoMod := goctlutil.FindGoModPath(dir)
|
|
||||||
if hasGoMod {
|
|
||||||
return rootPath, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
gopath := os.Getenv("GOPATH")
|
|
||||||
parent := path.Join(gopath, "src")
|
|
||||||
pos := strings.Index(absDir, parent)
|
|
||||||
if pos < 0 {
|
|
||||||
fmt.Printf("%s not in go.mod project path, or not in GOPATH of %s directory\n", absDir, gopath)
|
|
||||||
tempPath := filepath.Dir(absDir)
|
|
||||||
rootPath = absDir[len(tempPath)+1:]
|
|
||||||
} else {
|
|
||||||
rootPath = absDir[len(parent)+1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
return rootPath, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeIndent(writer io.Writer, indent int) {
|
func writeIndent(writer io.Writer, indent int) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/logx"
|
"github.com/tal-tech/go-zero/core/logx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/project"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
@@ -34,7 +35,7 @@ type RpcContext struct {
|
|||||||
|
|
||||||
func MustCreateRpcContext(protoSrc, targetDir, serviceName string, idea bool) *RpcContext {
|
func MustCreateRpcContext(protoSrc, targetDir, serviceName string, idea bool) *RpcContext {
|
||||||
log := console.NewConsole(idea)
|
log := console.NewConsole(idea)
|
||||||
info, err := prepare(log)
|
info, err := project.Prepare(targetDir, true)
|
||||||
log.Must(err)
|
log.Must(err)
|
||||||
|
|
||||||
if stringx.From(protoSrc).IsEmptyOrSpace() {
|
if stringx.From(protoSrc).IsEmptyOrSpace() {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package ctx
|
package project
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -10,7 +9,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -33,20 +31,22 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func prepare(log console.Console) (*Project, error) {
|
func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) {
|
||||||
log.Info("checking go env...")
|
|
||||||
_, err := exec.LookPath(constGo)
|
_, err := exec.LookPath(constGo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = exec.LookPath(constProtoC)
|
if checkGrpcEnv {
|
||||||
if err != nil {
|
_, err = exec.LookPath(constProtoC)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
_, err = exec.LookPath(constProtoCGenGo)
|
}
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
_, err = exec.LookPath(constProtoCGenGo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -87,14 +87,16 @@ func prepare(log console.Console) (*Project, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(pwd, src) {
|
if !strings.HasPrefix(pwd, src) {
|
||||||
return nil, fmt.Errorf("%s: project is not in go mod and go path", pwd)
|
name = filepath.Clean(filepath.Base(projectDir))
|
||||||
|
path = projectDir
|
||||||
|
} else {
|
||||||
|
r := strings.TrimPrefix(pwd, src+string(filepath.Separator))
|
||||||
|
name = filepath.Dir(r)
|
||||||
|
if name == "." {
|
||||||
|
name = r
|
||||||
|
}
|
||||||
|
path = filepath.Join(src, name)
|
||||||
}
|
}
|
||||||
r := strings.TrimPrefix(pwd, src+string(filepath.Separator))
|
|
||||||
name = filepath.Dir(r)
|
|
||||||
if name == "." {
|
|
||||||
name = r
|
|
||||||
}
|
|
||||||
path = filepath.Join(src, name)
|
|
||||||
module = name
|
module = name
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user