diff --git a/cmd/build.go b/cmd/build.go index abff63dce..3270197e3 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -16,9 +16,6 @@ package cmd import ( "github.com/Unknwon/com" - "go/build" - "os" - "os/exec" ) var CmdBuild = &Command{ @@ -38,29 +35,17 @@ func printBuildPrompt(flag string) { } func runBuild(cmd *Command, args []string) { - gopath := build.Default.GOPATH - genNewGoPath() com.ColorLog("[INFO] building ...\n") - cmdArgs := []string{"go", "build"} - cmdArgs = append(cmdArgs, args...) - bCmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) - bCmd.Stdout = os.Stdout - bCmd.Stderr = os.Stderr - err := bCmd.Run() + cmds := []string{"go", "build"} + cmds = append(cmds, args...) + err := execCmd(newGoPath, newCurPath, cmds...) if err != nil { com.ColorLog("[ERRO] build failed: %v\n", err) return } - com.ColorLog("[TRAC] set GOPATH=%v\n", gopath) - err = os.Setenv("GOPATH", gopath) - if err != nil { - com.ColorLog("[ERRO] %v\n", err) - return - } - com.ColorLog("[SUCC] build successfully!\n") } diff --git a/cmd/cmd.go b/cmd/cmd.go index b87b4998e..4c7ea5af9 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -20,7 +20,7 @@ import ( "strings" ) -const APP_VER = "0.5.1.1105" +const APP_VER = "0.5.1.1107" var ( AppPath string diff --git a/cmd/gopath.go b/cmd/gopath.go index a12d7270e..ff93bb1b3 100644 --- a/cmd/gopath.go +++ b/cmd/gopath.go @@ -5,6 +5,7 @@ import ( "github.com/gpmgo/gopm/doc" "go/build" "os" + "os/exec" "path/filepath" "strings" ) @@ -102,6 +103,51 @@ func getChildPkgs(cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg) er var pkgName string var curPath string +var newCurPath string +var newGoPath string + +func execCmd(gopath, curPath string, args ...string) error { + cwd, err := os.Getwd() + if err != nil { + return err + } + + com.ColorLog("[INFO] change current dir from %v to %v\n", cwd, curPath) + err = os.Chdir(filepath.Join(cwd, "vendor")) + if err != nil { + com.ColorLog("[ERRO] change current directory error %v\n", err) + return err + } + err = os.Chdir(curPath) + if err != nil { + com.ColorLog("[ERRO] change current directory error %v\n", err) + return err + } + defer os.Chdir(cwd) + ccmd := exec.Command("cd", curPath) + ccmd.Stdout = os.Stdout + ccmd.Stderr = os.Stderr + err = ccmd.Run() + if err != nil { + com.ColorLog("[ERRO] change current directory error %v\n", err) + return err + } + + oldGoPath := os.Getenv("GOPATH") + com.ColorLog("[TRAC] set GOPATH from %v to %v\n", oldGoPath, gopath) + + err = os.Setenv("GOPATH", gopath) + if err != nil { + com.ColorLog("[ERRO] %v\n", err) + return err + } + defer os.Setenv("GOPATH", oldGoPath) + + cmd := exec.Command(args[0], args[1:]...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} func genNewGoPath() { var err error @@ -136,15 +182,19 @@ func genNewGoPath() { com.ColorLog("[INFO] target name is %v\n", pkgName) } + if pkgName == "" { + _, pkgName = filepath.Split(curPath) + } + err = getChildPkgs(curPath, nil, cachePkgs) if err != nil { com.ColorLog("[ERRO] %v\n", err) return } - newGoPath := filepath.Join(curPath, "vendor") - os.RemoveAll(newGoPath) + newGoPath = filepath.Join(curPath, "vendor") newGoPathSrc := filepath.Join(newGoPath, "src") + os.RemoveAll(newGoPathSrc) os.MkdirAll(newGoPathSrc, os.ModePerm) for name, _ := range cachePkgs { @@ -178,26 +228,11 @@ func genNewGoPath() { } } - if pkgName != "" { - newPath := filepath.Join(newGoPathSrc, pkgName) - com.ColorLog("[INFO] linked %v\n", pkgName) - err = autoLink(curPath, newPath) - if err != nil { - com.ColorLog("[ERRO] make link error %v\n", err) - return - } - com.ColorLog("[INFO] change dir to %v\n", newPath) - err = os.Chdir(newPath) - if err != nil { - com.ColorLog("[ERRO] change current directory error %v\n", err) - return - } - } - - com.ColorLog("[TRAC] set GOPATH=%v\n", newGoPath) - err = os.Setenv("GOPATH", newGoPath) + newCurPath = filepath.Join(newGoPathSrc, pkgName) + com.ColorLog("[INFO] linked %v\n", pkgName) + err = autoLink(curPath, newCurPath) if err != nil { - com.ColorLog("[ERRO] %v\n", err) + com.ColorLog("[ERRO] make link error %v\n", err) return } } diff --git a/cmd/install.go b/cmd/install.go index 77768a4a7..5c5c74037 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -16,9 +16,6 @@ package cmd import ( "github.com/Unknwon/com" - "go/build" - "os" - "os/exec" ) var CmdInstall = &Command{ @@ -38,29 +35,17 @@ func printInstallPrompt(flag string) { } func runInstall(cmd *Command, args []string) { - gopath := build.Default.GOPATH - genNewGoPath() com.ColorLog("[INFO] installing ...\n") - cmdArgs := []string{"go", "install"} - cmdArgs = append(cmdArgs, args...) - bCmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) - bCmd.Stdout = os.Stdout - bCmd.Stderr = os.Stderr - err := bCmd.Run() + cmds := []string{"go", "install"} + cmds = append(cmds, args...) + err := execCmd(newGoPath, newCurPath, cmds...) if err != nil { com.ColorLog("[ERRO] install failed: %v\n", err) return } - com.ColorLog("[TRAC] set GOPATH=%v\n", gopath) - err = os.Setenv("GOPATH", gopath) - if err != nil { - com.ColorLog("[ERRO] %v\n", err) - return - } - com.ColorLog("[SUCC] install successfully!\n") } diff --git a/gopm.go b/gopm.go index 7c614e560..1ac799d87 100644 --- a/gopm.go +++ b/gopm.go @@ -48,9 +48,9 @@ var commands = []*cmd.Command{ cmd.CmdBuild, cmd.CmdRun, cmd.CmdVersion, - /*cmd.CmdInstall, + cmd.CmdInstall, - cmdClean, + /*cmdClean, cmdDoc, cmdEnv, cmdFix,