fix command run path bug (#52)

* rebase upstream

* rebase

* trim no need line

* trim no need line

* trim no need line

* optimized go path logic

Co-authored-by: kingxt <dream4kingxt@163.com>
master
kingxt 4 years ago committed by GitHub
parent ff7c14c6b6
commit 4408767981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,6 +19,7 @@ func getParentPackage(dir string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if len(p.GoMod.Path) > 0 { if len(p.GoMod.Path) > 0 {
goModePath := filepath.Clean(filepath.Dir(p.GoMod.Path)) goModePath := filepath.Clean(filepath.Dir(p.GoMod.Path))
absPath, err := filepath.Abs(dir) absPath, err := filepath.Abs(dir)
@ -30,7 +31,7 @@ func getParentPackage(dir string) (string, error) {
return parent, nil return parent, nil
} }
return p.GoMod.Module, nil return p.Package, nil
} }
func writeIndent(writer io.Writer, indent int) { func writeIndent(writer io.Writer, indent int) {

@ -8,7 +8,7 @@ import (
"runtime" "runtime"
) )
func Run(arg string) (string, error) { func Run(arg string, dir string) (string, error) {
goos := runtime.GOOS goos := runtime.GOOS
var cmd *exec.Cmd var cmd *exec.Cmd
switch goos { switch goos {
@ -19,7 +19,9 @@ func Run(arg string) (string, error) {
default: default:
return "", fmt.Errorf("unexpected os: %v", goos) return "", fmt.Errorf("unexpected os: %v", goos)
} }
if len(dir) > 0 {
cmd.Dir = dir
}
dtsout := new(bytes.Buffer) dtsout := new(bytes.Buffer)
stderr := new(bytes.Buffer) stderr := new(bytes.Buffer)
cmd.Stdout = dtsout cmd.Stdout = dtsout

@ -169,7 +169,7 @@ func (g *defaultRpcGenerator) genCall() error {
// if mockgen is already installed, it will generate code of gomock for shared files // if mockgen is already installed, it will generate code of gomock for shared files
_, err = exec.LookPath("mockgen") _, err = exec.LookPath("mockgen")
if mockGenInstalled { if mockGenInstalled {
execx.Run(fmt.Sprintf("go generate %s", filename)) execx.Run(fmt.Sprintf("go generate %s", filename), "")
} }
return nil return nil

@ -74,7 +74,7 @@ func (g *defaultRpcGenerator) genPb() error {
func (g *defaultRpcGenerator) protocGenGo(target string) error { func (g *defaultRpcGenerator) protocGenGo(target string) error {
src := filepath.Dir(g.Ctx.ProtoFileSrc) src := filepath.Dir(g.Ctx.ProtoFileSrc)
sh := fmt.Sprintf(`protoc -I=%s --go_out=plugins=grpc:%s %s`, src, target, g.Ctx.ProtoFileSrc) sh := fmt.Sprintf(`protoc -I=%s --go_out=plugins=grpc:%s %s`, src, target, g.Ctx.ProtoFileSrc)
stdout, err := execx.Run(sh) stdout, err := execx.Run(sh, "")
if err != nil { if err != nil {
return err return err
} }

@ -2,7 +2,6 @@ package project
import ( import (
"io/ioutil" "io/ioutil"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -21,14 +20,15 @@ const (
type ( type (
Project struct { Project struct {
Path string Path string // Project path name
Name string Name string // Project name
Package string // The service related package
GoMod GoMod GoMod GoMod
} }
GoMod struct { GoMod struct {
Module string Module string // The gomod module name
Path string Path string // The gomod related path
} }
) )
@ -54,15 +54,16 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) {
goMod, module string goMod, module string
goPath string goPath string
name, path string name, path string
pkg string
) )
ret, err := execx.Run(constGoMod) ret, err := execx.Run(constGoMod, projectDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
goMod = strings.TrimSpace(ret) goMod = strings.TrimSpace(ret)
ret, err = execx.Run(constGoPath) ret, err = execx.Run(constGoPath, "")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -82,10 +83,11 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) {
return nil, err return nil, err
} }
} else { } else {
pwd, err := os.Getwd() pwd, err := execx.Run("pwd", projectDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
pwd = filepath.Clean(strings.TrimSpace(pwd))
if !strings.HasPrefix(pwd, src) { if !strings.HasPrefix(pwd, src) {
absPath, err := filepath.Abs(projectDir) absPath, err := filepath.Abs(projectDir)
@ -95,6 +97,7 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) {
name = filepath.Clean(filepath.Base(absPath)) name = filepath.Clean(filepath.Base(absPath))
path = projectDir path = projectDir
pkg = name
} else { } else {
r := strings.TrimPrefix(pwd, src+string(filepath.Separator)) r := strings.TrimPrefix(pwd, src+string(filepath.Separator))
name = filepath.Dir(r) name = filepath.Dir(r)
@ -102,6 +105,7 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) {
name = r name = r
} }
path = filepath.Join(src, name) path = filepath.Join(src, name)
pkg = r
} }
module = name module = name
} }
@ -109,6 +113,7 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) {
return &Project{ return &Project{
Name: name, Name: name,
Path: path, Path: path,
Package: pkg,
GoMod: GoMod{ GoMod: GoMod{
Module: module, Module: module,
Path: goMod, Path: goMod,

Loading…
Cancel
Save