Browse Source

code refactoring

pull/103/head
Lunny Xiao 11 years ago
parent
commit
8b176155fc
  1. 21
      cmd/build.go
  2. 2
      cmd/cmd.go
  3. 77
      cmd/gopath.go
  4. 21
      cmd/install.go
  5. 4
      gopm.go

21
cmd/build.go

@ -16,9 +16,6 @@ package cmd
import ( import (
"github.com/Unknwon/com" "github.com/Unknwon/com"
"go/build"
"os"
"os/exec"
) )
var CmdBuild = &Command{ var CmdBuild = &Command{
@ -38,29 +35,17 @@ func printBuildPrompt(flag string) {
} }
func runBuild(cmd *Command, args []string) { func runBuild(cmd *Command, args []string) {
gopath := build.Default.GOPATH
genNewGoPath() genNewGoPath()
com.ColorLog("[INFO] building ...\n") com.ColorLog("[INFO] building ...\n")
cmdArgs := []string{"go", "build"} cmds := []string{"go", "build"}
cmdArgs = append(cmdArgs, args...) cmds = append(cmds, args...)
bCmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) err := execCmd(newGoPath, newCurPath, cmds...)
bCmd.Stdout = os.Stdout
bCmd.Stderr = os.Stderr
err := bCmd.Run()
if err != nil { if err != nil {
com.ColorLog("[ERRO] build failed: %v\n", err) com.ColorLog("[ERRO] build failed: %v\n", err)
return 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") com.ColorLog("[SUCC] build successfully!\n")
} }

2
cmd/cmd.go

@ -20,7 +20,7 @@ import (
"strings" "strings"
) )
const APP_VER = "0.5.1.1105" const APP_VER = "0.5.1.1107"
var ( var (
AppPath string AppPath string

77
cmd/gopath.go

@ -5,6 +5,7 @@ import (
"github.com/gpmgo/gopm/doc" "github.com/gpmgo/gopm/doc"
"go/build" "go/build"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
) )
@ -102,6 +103,51 @@ func getChildPkgs(cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg) er
var pkgName string var pkgName string
var curPath 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() { func genNewGoPath() {
var err error var err error
@ -136,15 +182,19 @@ func genNewGoPath() {
com.ColorLog("[INFO] target name is %v\n", pkgName) com.ColorLog("[INFO] target name is %v\n", pkgName)
} }
if pkgName == "" {
_, pkgName = filepath.Split(curPath)
}
err = getChildPkgs(curPath, nil, cachePkgs) err = getChildPkgs(curPath, nil, cachePkgs)
if err != nil { if err != nil {
com.ColorLog("[ERRO] %v\n", err) com.ColorLog("[ERRO] %v\n", err)
return return
} }
newGoPath := filepath.Join(curPath, "vendor") newGoPath = filepath.Join(curPath, "vendor")
os.RemoveAll(newGoPath)
newGoPathSrc := filepath.Join(newGoPath, "src") newGoPathSrc := filepath.Join(newGoPath, "src")
os.RemoveAll(newGoPathSrc)
os.MkdirAll(newGoPathSrc, os.ModePerm) os.MkdirAll(newGoPathSrc, os.ModePerm)
for name, _ := range cachePkgs { for name, _ := range cachePkgs {
@ -178,26 +228,11 @@ func genNewGoPath() {
} }
} }
if pkgName != "" { newCurPath = filepath.Join(newGoPathSrc, pkgName)
newPath := filepath.Join(newGoPathSrc, pkgName) com.ColorLog("[INFO] linked %v\n", pkgName)
com.ColorLog("[INFO] linked %v\n", pkgName) err = autoLink(curPath, newCurPath)
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)
if err != nil { if err != nil {
com.ColorLog("[ERRO] %v\n", err) com.ColorLog("[ERRO] make link error %v\n", err)
return return
} }
} }

21
cmd/install.go

@ -16,9 +16,6 @@ package cmd
import ( import (
"github.com/Unknwon/com" "github.com/Unknwon/com"
"go/build"
"os"
"os/exec"
) )
var CmdInstall = &Command{ var CmdInstall = &Command{
@ -38,29 +35,17 @@ func printInstallPrompt(flag string) {
} }
func runInstall(cmd *Command, args []string) { func runInstall(cmd *Command, args []string) {
gopath := build.Default.GOPATH
genNewGoPath() genNewGoPath()
com.ColorLog("[INFO] installing ...\n") com.ColorLog("[INFO] installing ...\n")
cmdArgs := []string{"go", "install"} cmds := []string{"go", "install"}
cmdArgs = append(cmdArgs, args...) cmds = append(cmds, args...)
bCmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) err := execCmd(newGoPath, newCurPath, cmds...)
bCmd.Stdout = os.Stdout
bCmd.Stderr = os.Stderr
err := bCmd.Run()
if err != nil { if err != nil {
com.ColorLog("[ERRO] install failed: %v\n", err) com.ColorLog("[ERRO] install failed: %v\n", err)
return 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") com.ColorLog("[SUCC] install successfully!\n")
} }

4
gopm.go

@ -48,9 +48,9 @@ var commands = []*cmd.Command{
cmd.CmdBuild, cmd.CmdBuild,
cmd.CmdRun, cmd.CmdRun,
cmd.CmdVersion, cmd.CmdVersion,
/*cmd.CmdInstall, cmd.CmdInstall,
cmdClean, /*cmdClean,
cmdDoc, cmdDoc,
cmdEnv, cmdEnv,
cmdFix, cmdFix,

Loading…
Cancel
Save