|
|
@ -1,53 +1,54 @@ |
|
|
|
package cmd |
|
|
|
package cmd |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"github.com/Unknwon/com" |
|
|
|
"go/build" |
|
|
|
"github.com/gpmgo/gopm/doc" |
|
|
|
|
|
|
|
//"go/build"
|
|
|
|
|
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"os/exec" |
|
|
|
"os/exec" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
//"strings"
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/Unknwon/com" |
|
|
|
|
|
|
|
"github.com/codegangsta/cli" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gpmgo/gopm/doc" |
|
|
|
|
|
|
|
"github.com/gpmgo/gopm/log" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func getGopmPkgs(path string, inludeSys bool) (map[string]*doc.Pkg, error) { |
|
|
|
func getGopmPkgs(dirPath string) (pkgs map[string]*doc.Pkg, err error) { |
|
|
|
// abs, err := filepath.Abs(filepath.Join(path, doc.GopmFileName))
|
|
|
|
absPath, err := filepath.Abs(dirPath) |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// return nil, err
|
|
|
|
log.Error("", "Fail to get absolute path of work directory") |
|
|
|
// }
|
|
|
|
log.Fatal("", err.Error()) |
|
|
|
|
|
|
|
} |
|
|
|
// // load import path
|
|
|
|
|
|
|
|
// gf := doc.NewGopmfile()
|
|
|
|
var builds map[string]string |
|
|
|
// var builds *doc.Section
|
|
|
|
|
|
|
|
// if com.IsExist(abs) {
|
|
|
|
if com.IsFile(absPath + "/" + doc.GopmFileName) { |
|
|
|
// err := gf.Load(abs)
|
|
|
|
gf := doc.NewGopmfile(absPath) |
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// return nil, err
|
|
|
|
if builds, err = gf.GetSection("deps"); err != nil { |
|
|
|
// }
|
|
|
|
builds = nil |
|
|
|
// var ok bool
|
|
|
|
} |
|
|
|
// if builds, ok = gf.Sections["build"]; !ok {
|
|
|
|
} |
|
|
|
// builds = nil
|
|
|
|
|
|
|
|
// }
|
|
|
|
pkg, err := build.ImportDir(dirPath, build.AllowBinary) |
|
|
|
// }
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return map[string]*doc.Pkg{}, err |
|
|
|
// pkg, err := build.ImportDir(path, build.AllowBinary)
|
|
|
|
} |
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// return map[string]*doc.Pkg{}, err
|
|
|
|
pkgs = make(map[string]*doc.Pkg) |
|
|
|
// }
|
|
|
|
for _, name := range pkg.Imports { |
|
|
|
|
|
|
|
if !doc.IsGoRepoPath(name) { |
|
|
|
// pkgs := make(map[string]*doc.Pkg)
|
|
|
|
if builds != nil { |
|
|
|
// for _, name := range pkg.Imports {
|
|
|
|
if dep, ok := builds[name]; ok { |
|
|
|
// if inludeSys || !isStdPkg(name) {
|
|
|
|
pkgs[name] = &doc.Pkg{ImportPath: dep} |
|
|
|
// if builds != nil {
|
|
|
|
continue |
|
|
|
// if dep, ok := builds.Deps[name]; ok {
|
|
|
|
} |
|
|
|
// pkgs[name] = dep.Pkg
|
|
|
|
} |
|
|
|
// continue
|
|
|
|
pkgs[name] = doc.NewDefaultPkg(name) |
|
|
|
// }
|
|
|
|
} |
|
|
|
// }
|
|
|
|
} |
|
|
|
// pkgs[name] = doc.NewDefaultPkg(name)
|
|
|
|
return pkgs, nil |
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
return nil, nil //pkgs, nil
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func pkgInCache(name string, cachePkgs map[string]*doc.Pkg) bool { |
|
|
|
func pkgInCache(name string, cachePkgs map[string]*doc.Pkg) bool { |
|
|
@ -62,42 +63,42 @@ func autoLink(oldPath, newPath string) error { |
|
|
|
return makeLink(oldPath, newPath) |
|
|
|
return makeLink(oldPath, newPath) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getChildPkgs(cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg) error { |
|
|
|
func getChildPkgs(ctx *cli.Context, cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg) error { |
|
|
|
// pkgs, err := getGopmPkgs(cpath, false)
|
|
|
|
pkgs, err := getGopmPkgs(cpath) |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// return err
|
|
|
|
return err |
|
|
|
// }
|
|
|
|
} |
|
|
|
// 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) { |
|
|
|
// newPath = filepath.Join(installRepoPath, pkg.ImportPath)
|
|
|
|
newPath = filepath.Join(installRepoPath, 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:])
|
|
|
|
newPath = filepath.Join(curPath, pkg.ImportPath[len(pkgName)+1:]) |
|
|
|
// } else {
|
|
|
|
} else { |
|
|
|
// if !com.IsExist(newPath) {
|
|
|
|
if !com.IsExist(newPath) { |
|
|
|
// var t, ver string = doc.BRANCH, ""
|
|
|
|
var t, ver string = doc.BRANCH, "" |
|
|
|
// node := doc.NewNode(pkg.ImportPath, pkg.ImportPath, t, ver, true)
|
|
|
|
node := doc.NewNode(pkg.ImportPath, pkg.ImportPath, t, ver, true) |
|
|
|
// nodes := []*doc.Node{node}
|
|
|
|
nodes := []*doc.Node{node} |
|
|
|
// downloadPackages(nodes)
|
|
|
|
downloadPackages(ctx, nodes) |
|
|
|
// // should handler download failed
|
|
|
|
// should handler download failed
|
|
|
|
// }
|
|
|
|
} |
|
|
|
// }
|
|
|
|
} |
|
|
|
// } else {
|
|
|
|
} else { |
|
|
|
// newPath, err = filepath.Abs(name)
|
|
|
|
newPath, err = filepath.Abs(name) |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// return err
|
|
|
|
return err |
|
|
|
// }
|
|
|
|
} |
|
|
|
// }
|
|
|
|
} |
|
|
|
// err = getChildPkgs(newPath, pkg, cachePkgs)
|
|
|
|
err = getChildPkgs(ctx, newPath, pkg, cachePkgs) |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// return err
|
|
|
|
return err |
|
|
|
// }
|
|
|
|
} |
|
|
|
// }
|
|
|
|
} |
|
|
|
// }
|
|
|
|
} |
|
|
|
// if ppkg != nil && !build.IsLocalImport(ppkg.ImportPath) {
|
|
|
|
if ppkg != nil && !build.IsLocalImport(ppkg.ImportPath) { |
|
|
|
// cachePkgs[ppkg.ImportPath] = ppkg
|
|
|
|
cachePkgs[ppkg.ImportPath] = ppkg |
|
|
|
// }
|
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -149,90 +150,86 @@ func execCmd(gopath, curPath string, args ...string) error { |
|
|
|
return cmd.Run() |
|
|
|
return cmd.Run() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func genNewGoPath() { |
|
|
|
func genNewGoPath(ctx *cli.Context) { |
|
|
|
// var err error
|
|
|
|
var err error |
|
|
|
// curPath, err = os.Getwd()
|
|
|
|
curPath, err = os.Getwd() |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// com.ColorLog("[ERRO] %v\n", err)
|
|
|
|
log.Error("", "Fail to get work directory") |
|
|
|
// return
|
|
|
|
log.Fatal("", err.Error()) |
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// hd, err := com.HomeDir()
|
|
|
|
hd, err := com.HomeDir() |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// com.ColorLog("[ERRO] Fail to get current user[ %s ]\n", err)
|
|
|
|
log.Error("", "Fail to get current user") |
|
|
|
// return
|
|
|
|
log.Fatal("", err.Error()) |
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// gf := doc.NewGopmfile()
|
|
|
|
doc.HomeDir = strings.Replace(doc.RawHomeDir, "~", hd, -1) |
|
|
|
// gpmPath := filepath.Join(curPath, doc.GopmFileName)
|
|
|
|
installRepoPath = doc.HomeDir + "/repos" |
|
|
|
// if com.IsExist(gpmPath) {
|
|
|
|
|
|
|
|
// com.ColorLog("[INFO] loading .gopmfile ...\n")
|
|
|
|
if com.IsFile(curPath + "/" + doc.GopmFileName) { |
|
|
|
// err := gf.Load(gpmPath)
|
|
|
|
log.Trace("Loading gopmfile...") |
|
|
|
// if err != nil {
|
|
|
|
gf := doc.NewGopmfile(curPath) |
|
|
|
// com.ColorLog("[ERRO] load .gopmfile failed: %v\n", err)
|
|
|
|
|
|
|
|
// return
|
|
|
|
var err error |
|
|
|
// }
|
|
|
|
pkgName, err = gf.GetValue("target", "path") |
|
|
|
// }
|
|
|
|
if err == nil { |
|
|
|
|
|
|
|
log.Log("Target name: %s", pkgName) |
|
|
|
// installRepoPath = strings.Replace(reposDir, "~", hd, -1)
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
// cachePkgs := make(map[string]*doc.Pkg)
|
|
|
|
|
|
|
|
// if target, ok := gf.Sections["target"]; ok {
|
|
|
|
if len(pkgName) == 0 { |
|
|
|
// pkgName = target.Props["path"]
|
|
|
|
_, pkgName = filepath.Split(curPath) |
|
|
|
// com.ColorLog("[INFO] target name is %v\n", pkgName)
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
cachePkgs := make(map[string]*doc.Pkg) |
|
|
|
// if pkgName == "" {
|
|
|
|
err = getChildPkgs(ctx, curPath, nil, cachePkgs) |
|
|
|
// _, pkgName = filepath.Split(curPath)
|
|
|
|
if err != nil { |
|
|
|
// }
|
|
|
|
log.Error("", "Fail to get child pakcages") |
|
|
|
|
|
|
|
log.Fatal("", err.Error()) |
|
|
|
// err = getChildPkgs(curPath, nil, cachePkgs)
|
|
|
|
} |
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// com.ColorLog("[ERRO] %v\n", err)
|
|
|
|
newGoPath = filepath.Join(curPath, "vendor") |
|
|
|
// return
|
|
|
|
newGoPathSrc := filepath.Join(newGoPath, "src") |
|
|
|
// }
|
|
|
|
os.RemoveAll(newGoPathSrc) |
|
|
|
|
|
|
|
os.MkdirAll(newGoPathSrc, os.ModePerm) |
|
|
|
// newGoPath = filepath.Join(curPath, "vendor")
|
|
|
|
|
|
|
|
// newGoPathSrc := filepath.Join(newGoPath, "src")
|
|
|
|
for name, _ := range cachePkgs { |
|
|
|
// os.RemoveAll(newGoPathSrc)
|
|
|
|
oldPath := filepath.Join(installRepoPath, name) |
|
|
|
// os.MkdirAll(newGoPathSrc, os.ModePerm)
|
|
|
|
newPath := filepath.Join(newGoPathSrc, name) |
|
|
|
|
|
|
|
paths := strings.Split(name, "/") |
|
|
|
// for name, _ := range cachePkgs {
|
|
|
|
var isExistP bool |
|
|
|
// oldPath := filepath.Join(installRepoPath, name)
|
|
|
|
var isCurChild bool |
|
|
|
// newPath := filepath.Join(newGoPathSrc, name)
|
|
|
|
for i := 0; i < len(paths)-1; i++ { |
|
|
|
// paths := strings.Split(name, "/")
|
|
|
|
pName := strings.Join(paths[:len(paths)-1-i], "/") |
|
|
|
// var isExistP bool
|
|
|
|
if _, ok := cachePkgs[pName]; ok { |
|
|
|
// var isCurChild bool
|
|
|
|
isExistP = true |
|
|
|
// for i := 0; i < len(paths)-1; i++ {
|
|
|
|
break |
|
|
|
// pName := strings.Join(paths[:len(paths)-1-i], "/")
|
|
|
|
} |
|
|
|
// if _, ok := cachePkgs[pName]; ok {
|
|
|
|
if pkgName == pName { |
|
|
|
// isExistP = true
|
|
|
|
isCurChild = true |
|
|
|
// break
|
|
|
|
break |
|
|
|
// }
|
|
|
|
} |
|
|
|
// if pkgName == pName {
|
|
|
|
} |
|
|
|
// isCurChild = true
|
|
|
|
if isCurChild { |
|
|
|
// break
|
|
|
|
continue |
|
|
|
// }
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
// if isCurChild {
|
|
|
|
if !isExistP { |
|
|
|
// continue
|
|
|
|
log.Log("Linking %s", name) |
|
|
|
// }
|
|
|
|
err = autoLink(oldPath, newPath) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
// if !isExistP {
|
|
|
|
log.Error("", "Fail to make link") |
|
|
|
// com.ColorLog("[INFO] linked %v\n", name)
|
|
|
|
log.Fatal("", err.Error()) |
|
|
|
// err = autoLink(oldPath, newPath)
|
|
|
|
} |
|
|
|
// if err != nil {
|
|
|
|
} |
|
|
|
// com.ColorLog("[ERRO] make link error %v\n", err)
|
|
|
|
} |
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
newCurPath = filepath.Join(newGoPathSrc, pkgName) |
|
|
|
// }
|
|
|
|
log.Log("Linking %s", pkgName) |
|
|
|
// }
|
|
|
|
err = autoLink(curPath, newCurPath) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
// newCurPath = filepath.Join(newGoPathSrc, pkgName)
|
|
|
|
log.Error("", "Fail to make link") |
|
|
|
// com.ColorLog("[INFO] linked %v\n", pkgName)
|
|
|
|
log.Fatal("", err.Error()) |
|
|
|
// err = autoLink(curPath, newCurPath)
|
|
|
|
} |
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// com.ColorLog("[ERRO] make link error %v\n", err)
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|