Browse Source

Compatable with GOPATH

pull/103/head
Unknown 11 years ago
parent
commit
a16adffbdd
  1. 14
      cmd/build.go
  2. 4
      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" "os"
"path" "path"
"github.com/Unknwon/com"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/gpmgo/gopm/doc" "github.com/gpmgo/gopm/doc"
@ -35,6 +36,19 @@ gopm build <go build commands>`,
} }
func runBuild(ctx *cli.Context) { 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) genNewGoPath(ctx, false)
log.Trace("Building...") log.Trace("Building...")

4
cmd/get.go

@ -76,6 +76,7 @@ func runGet(ctx *cli.Context) {
log.Help("Try 'gopm help get' to get more information") log.Help("Try 'gopm help get' to get more information")
} }
if !ctx.Bool("remote") {
// Get GOPATH. // Get GOPATH.
installGopath = com.GetGOPATHs()[0] installGopath = com.GetGOPATHs()[0]
if !com.IsDir(installGopath) { if !com.IsDir(installGopath) {
@ -86,6 +87,7 @@ func runGet(ctx *cli.Context) {
} }
log.Log("Indicated GOPATH: %s", installGopath) log.Log("Indicated GOPATH: %s", installGopath)
installGopath += "/src" installGopath += "/src"
}
// The gopm local repository. // The gopm local repository.
installRepoPath = doc.HomeDir + "/repos" 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. // Download package.
nod, imports := downloadPackage(ctx, n) nod, imports := downloadPackage(ctx, n)
if len(imports) > 0 { if len(imports) > 0 {

69
cmd/gopath.go

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

21
cmd/install.go

@ -55,7 +55,20 @@ func runInstall(ctx *cli.Context) {
case 1: case 1:
target = ctx.Args()[0] target = ctx.Args()[0]
default: 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) genNewGoPath(ctx, false)
@ -84,10 +97,10 @@ func runInstall(ctx *cli.Context) {
cmdArgs = append(cmdArgs, repo) cmdArgs = append(cmdArgs, repo)
err := execCmd(newGoPath, newCurPath, cmdArgs...) err := execCmd(newGoPath, newCurPath, cmdArgs...)
if err != nil { if err != nil {
log.Error("Install", "Fail to install program") log.Error("install", "Fail to install program:")
log.Fatal("", err.Error()) 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 package cmd
import ( import (
"github.com/Unknwon/com"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/gpmgo/gopm/log" "github.com/gpmgo/gopm/log"
@ -31,6 +32,19 @@ gopm run <go run commands>`,
} }
func runRun(ctx *cli.Context) { 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) genNewGoPath(ctx, false)
log.Trace("Running...") log.Trace("Running...")
@ -39,9 +53,9 @@ func runRun(ctx *cli.Context) {
cmdArgs = append(cmdArgs, ctx.Args()...) cmdArgs = append(cmdArgs, ctx.Args()...)
err := execCmd(newGoPath, newCurPath, cmdArgs...) err := execCmd(newGoPath, newCurPath, cmdArgs...)
if err != nil { if err != nil {
log.Error("Run", "Fail to run program") log.Error("run", "Fail to run program:")
log.Fatal("", err.Error()) log.Fatal("", "\t"+err.Error())
} }
log.Success("SUCC", "Run", "Command execute successfully!") log.Success("SUCC", "run", "Command executed successfully!")
} }

Loading…
Cancel
Save