Browse Source

Compatable with GOPATH

pull/103/head
Unknown 11 years ago
parent
commit
a16adffbdd
  1. 14
      cmd/build.go
  2. 22
      cmd/get.go
  3. 69
      cmd/gopath.go
  4. 21
      cmd/install.go
  5. 20
      cmd/run.go

14
cmd/build.go

@ -18,6 +18,7 @@ import (
"os"
"path"
"github.com/Unknwon/com"
"github.com/codegangsta/cli"
"github.com/gpmgo/gopm/doc"
@ -35,6 +36,19 @@ gopm build <go build commands>`,
}
func runBuild(ctx *cli.Context) {
if !ctx.Bool("remote") {
// Get GOPATH.
installGopath = com.GetGOPATHs()[0]
if !com.IsDir(installGopath) {
log.Error("build", "Invalid GOPATH path")
log.Error("", "GOPATH does not exist or is not a directory:")
log.Error("", "\t"+installGopath)
log.Help("Try 'go help gopath' to get more information")
}
log.Log("Indicated GOPATH: %s", installGopath)
installGopath += "/src"
}
genNewGoPath(ctx, false)
log.Trace("Building...")

22
cmd/get.go

@ -76,16 +76,18 @@ func runGet(ctx *cli.Context) {
log.Help("Try 'gopm help get' to get more information")
}
// Get GOPATH.
installGopath = com.GetGOPATHs()[0]
if !com.IsDir(installGopath) {
log.Error("get", "Invalid GOPATH path")
log.Error("", "GOPATH does not exist or is not a directory:")
log.Error("", "\t"+installGopath)
log.Help("Try 'go help gopath' to get more information")
if !ctx.Bool("remote") {
// Get GOPATH.
installGopath = com.GetGOPATHs()[0]
if !com.IsDir(installGopath) {
log.Error("get", "Invalid GOPATH path")
log.Error("", "GOPATH does not exist or is not a directory:")
log.Error("", "\t"+installGopath)
log.Help("Try 'go help gopath' to get more information")
}
log.Log("Indicated GOPATH: %s", installGopath)
installGopath += "/src"
}
log.Log("Indicated GOPATH: %s", installGopath)
installGopath += "/src"
// The gopm local repository.
installRepoPath = doc.HomeDir + "/repos"
@ -236,7 +238,7 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
}
}
if !downloadCache[n.RootPath] {
if !downloadCache[n.ImportPath] {
// Download package.
nod, imports := downloadPackage(ctx, n)
if len(imports) > 0 {

69
cmd/gopath.go

@ -1,11 +1,13 @@
package cmd
import (
"errors"
"fmt"
"go/build"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/Unknwon/com"
@ -20,8 +22,8 @@ var isWindowsXP = false
func getGopmPkgs(dirPath string, isTest bool) (pkgs map[string]*doc.Pkg, err error) {
absPath, err := filepath.Abs(dirPath)
if err != nil {
log.Error("", "Fail to get absolute path of work directory")
log.Fatal("", err.Error())
log.Error("", "Fail to get absolute path of work directory:")
log.Fatal("", "\t"+err.Error())
}
var builds map[string]string
@ -36,7 +38,7 @@ func getGopmPkgs(dirPath string, isTest bool) (pkgs map[string]*doc.Pkg, err err
pkg, err := build.ImportDir(dirPath, build.AllowBinary)
if err != nil {
return map[string]*doc.Pkg{}, err
return map[string]*doc.Pkg{}, errors.New("Fail to get imports: " + err.Error())
}
pkgs = make(map[string]*doc.Pkg)
@ -82,20 +84,24 @@ func autoLink(oldPath, newPath string) error {
}
func getChildPkgs(ctx *cli.Context, cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg, isTest bool) error {
pkgs, err := getGopmPkgs(cpath, isTest)
var suf string
if ppkg != nil {
suf = versionSuffix(ppkg.Value)
}
pkgs, err := getGopmPkgs(cpath+suf, isTest)
if err != nil {
return err
return errors.New("Fail to get gopmfile deps: " + err.Error())
}
for name, pkg := range pkgs {
if !pkgInCache(name, cachePkgs) {
var newPath string
if !build.IsLocalImport(name) {
suf := "." + pkg.Value
if len(suf) == 1 {
suf = ""
}
suf := versionSuffix(pkg.Value)
newPath = filepath.Join(installRepoPath, pkg.ImportPath)
if len(pkg.Value) == 0 && !ctx.Bool("remote") {
newPath = filepath.Join(installGopath, pkg.ImportPath)
}
if pkgName != "" && strings.HasPrefix(pkg.ImportPath, pkgName) {
newPath = filepath.Join(curPath, pkg.ImportPath[len(pkgName)+1:]+suf)
} else {
@ -133,15 +139,15 @@ var newGoPath string
func execCmd(gopath, curPath string, args ...string) error {
cwd, err := os.Getwd()
if err != nil {
log.Error("", "Fail to get work directory")
log.Fatal("", err.Error())
log.Error("", "Fail to get work directory:")
log.Fatal("", "\t"+err.Error())
}
log.Log("Changing work directory to %s", curPath)
err = os.Chdir(curPath)
if err != nil {
log.Error("", "Fail to change work directory")
log.Fatal("", err.Error())
log.Error("", "Fail to change work directory:")
log.Fatal("", "\t"+err.Error())
}
defer func() {
log.Log("Changing work directory back to %s", cwd)
@ -150,17 +156,21 @@ func execCmd(gopath, curPath string, args ...string) error {
err = os.Chdir(curPath)
if err != nil {
log.Error("", "Fail to change work directory")
log.Fatal("", err.Error())
log.Error("", "Fail to change work directory:")
log.Fatal("", "\t"+err.Error())
}
oldGoPath := os.Getenv("GOPATH")
log.Log("Setting GOPATH to %s", gopath)
err = os.Setenv("GOPATH", gopath)
sep := ":"
if runtime.GOOS == "windos" {
sep = ";"
}
err = os.Setenv("GOPATH", gopath+sep+oldGoPath)
if err != nil {
log.Error("", "Fail to setting GOPATH")
log.Fatal("", err.Error())
log.Error("", "Fail to setting GOPATH:")
log.Fatal("", "\t"+err.Error())
}
defer func() {
log.Log("Setting GOPATH back to %s", oldGoPath)
@ -184,8 +194,8 @@ func genNewGoPath(ctx *cli.Context, isTest bool) {
var err error
curPath, err = os.Getwd()
if err != nil {
log.Error("", "Fail to get work directory")
log.Fatal("", err.Error())
log.Error("", "Fail to get work directory:")
log.Fatal("", "\t"+err.Error())
}
installRepoPath = doc.HomeDir + "/repos"
@ -208,8 +218,8 @@ func genNewGoPath(ctx *cli.Context, isTest bool) {
cachePkgs := make(map[string]*doc.Pkg)
err = getChildPkgs(ctx, curPath, nil, cachePkgs, isTest)
if err != nil {
log.Error("", "Fail to get child pakcages")
log.Fatal("", err.Error())
log.Error("", "Fail to get child pakcages:")
log.Fatal("", "\t"+err.Error())
}
newGoPath = filepath.Join(curPath, doc.VENDOR)
@ -218,10 +228,7 @@ func genNewGoPath(ctx *cli.Context, isTest bool) {
os.MkdirAll(newGoPathSrc, os.ModePerm)
for name, pkg := range cachePkgs {
suf := "." + pkg.Value
if len(suf) == 1 {
suf = ""
}
suf := versionSuffix(pkg.Value)
oldPath := filepath.Join(installRepoPath, name) + suf
newPath := filepath.Join(newGoPathSrc, name)
@ -243,12 +250,12 @@ func genNewGoPath(ctx *cli.Context, isTest bool) {
continue
}
if !isExistP {
if !isExistP && (len(pkg.Value) > 0 || ctx.Bool("remote")) {
log.Log("Linking %s", name+suf)
err = autoLink(oldPath, newPath)
if err != nil {
log.Error("", "Fail to make link")
log.Fatal("", err.Error())
log.Error("", "Fail to make link:")
log.Fatal("", "\t"+err.Error())
}
}
}
@ -257,7 +264,7 @@ func genNewGoPath(ctx *cli.Context, isTest bool) {
log.Log("Linking %s", pkgName)
err = autoLink(curPath, newCurPath)
if err != nil {
log.Error("", "Fail to make link")
log.Fatal("", err.Error())
log.Error("", "Fail to make link:")
log.Fatal("", "\t"+err.Error())
}
}

21
cmd/install.go

@ -55,7 +55,20 @@ func runInstall(ctx *cli.Context) {
case 1:
target = ctx.Args()[0]
default:
log.Fatal("Install", "Too many arguments")
log.Fatal("install", "Too many arguments")
}
if !ctx.Bool("remote") {
// Get GOPATH.
installGopath = com.GetGOPATHs()[0]
if !com.IsDir(installGopath) {
log.Error("install", "Invalid GOPATH path")
log.Error("", "GOPATH does not exist or is not a directory:")
log.Error("", "\t"+installGopath)
log.Help("Try 'go help gopath' to get more information")
}
log.Log("Indicated GOPATH: %s", installGopath)
installGopath += "/src"
}
genNewGoPath(ctx, false)
@ -84,10 +97,10 @@ func runInstall(ctx *cli.Context) {
cmdArgs = append(cmdArgs, repo)
err := execCmd(newGoPath, newCurPath, cmdArgs...)
if err != nil {
log.Error("Install", "Fail to install program")
log.Fatal("", err.Error())
log.Error("install", "Fail to install program:")
log.Fatal("", "\t"+err.Error())
}
}
log.Success("SUCC", "Install", "Command execute successfully!")
log.Success("SUCC", "install", "Command executed successfully!")
}

20
cmd/run.go

@ -15,6 +15,7 @@
package cmd
import (
"github.com/Unknwon/com"
"github.com/codegangsta/cli"
"github.com/gpmgo/gopm/log"
@ -31,6 +32,19 @@ gopm run <go run commands>`,
}
func runRun(ctx *cli.Context) {
if !ctx.Bool("remote") {
// Get GOPATH.
installGopath = com.GetGOPATHs()[0]
if !com.IsDir(installGopath) {
log.Error("run", "Invalid GOPATH path")
log.Error("", "GOPATH does not exist or is not a directory:")
log.Error("", "\t"+installGopath)
log.Help("Try 'go help gopath' to get more information")
}
log.Log("Indicated GOPATH: %s", installGopath)
installGopath += "/src"
}
genNewGoPath(ctx, false)
log.Trace("Running...")
@ -39,9 +53,9 @@ func runRun(ctx *cli.Context) {
cmdArgs = append(cmdArgs, ctx.Args()...)
err := execCmd(newGoPath, newCurPath, cmdArgs...)
if err != nil {
log.Error("Run", "Fail to run program")
log.Fatal("", err.Error())
log.Error("run", "Fail to run program:")
log.Fatal("", "\t"+err.Error())
}
log.Success("SUCC", "Run", "Command execute successfully!")
log.Success("SUCC", "run", "Command executed successfully!")
}

Loading…
Cancel
Save