From 3d949cb336c7567eec15ce1fc5666df25b14afa9 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 10 Nov 2013 23:39:13 -0500 Subject: [PATCH] Done run command --- cmd/build.go | 2 +- cmd/gen.go | 2 +- cmd/get.go | 12 +- cmd/gopath.go | 325 ++++++++++++++++++++++++------------------------- cmd/install.go | 2 +- cmd/run.go | 52 ++++---- doc/conf.go | 1 + gopm.go | 4 +- 8 files changed, 200 insertions(+), 200 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 3270197e3..8069f558a 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -35,7 +35,7 @@ func printBuildPrompt(flag string) { } func runBuild(cmd *Command, args []string) { - genNewGoPath() + //genNewGoPath() com.ColorLog("[INFO] building ...\n") diff --git a/cmd/gen.go b/cmd/gen.go index 7d727f094..93710280c 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -73,5 +73,5 @@ func runGen(ctx *cli.Context) { log.Fatal("", err.Error()) } - log.Success("SUCC", "Gen", "Generate gopmfile successful!") + log.Success("SUCC", "Gen", "Generate gopmfile successfully!") } diff --git a/cmd/get.go b/cmd/get.go index 8436c7cab..8d59d8299 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -67,7 +67,7 @@ func runGet(ctx *cli.Context) { log.Fatal("", err.Error()) } - doc.HomeDir = strings.Replace(doc.HomeDir, "~", hd, -1) + doc.HomeDir = strings.Replace(doc.RawHomeDir, "~", hd, -1) doc.LoadPkgNameList(doc.HomeDir + "/data/pkgname.list") installRepoPath = doc.HomeDir + "/repos" @@ -84,14 +84,14 @@ func runGet(ctx *cli.Context) { func getByGopmfile(ctx *cli.Context) { if !com.IsFile(".gopmfile") { - log.Fatal("get", "No argument is supplied and no gopmfile exist") + log.Fatal("Get", "No argument is supplied and no gopmfile exist") } gf := doc.NewGopmfile(".") absPath, err := filepath.Abs(".") if err != nil { - log.Error("", "Fail to get absolute path of work directory") + log.Error("Get", "Fail to get absolute path of work directory") log.Fatal("", err.Error()) } @@ -254,8 +254,8 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) { continue } else { // Invalid import path. - com.ColorLog("[WARN] Skipped invalid package path( %s => %s:%s )\n", - n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) + log.Error("", "Skipped invalid package: "+fmt.Sprintf("%s@%s:%s", + n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))) failConut++ } } @@ -272,7 +272,7 @@ func downloadPackage(nod *doc.Node) (*doc.Node, []string) { if err != nil { log.Error("Get", "Fail to download pakage: "+nod.ImportPath) - log.Fatal("", err.Error()) + log.Error("", err.Error()) failConut++ os.RemoveAll(installRepoPath + "/" + doc.GetProjectPath(nod.ImportPath) + "/") return nil, nil diff --git a/cmd/gopath.go b/cmd/gopath.go index b85a640f5..ed76c69e2 100644 --- a/cmd/gopath.go +++ b/cmd/gopath.go @@ -1,53 +1,54 @@ package cmd import ( - "github.com/Unknwon/com" - "github.com/gpmgo/gopm/doc" - //"go/build" + "go/build" "os" "os/exec" "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) { - // abs, err := filepath.Abs(filepath.Join(path, doc.GopmFileName)) - // if err != nil { - // return nil, err - // } - - // // load import path - // gf := doc.NewGopmfile() - // var builds *doc.Section - // if com.IsExist(abs) { - // err := gf.Load(abs) - // if err != nil { - // return nil, err - // } - // var ok bool - // if builds, ok = gf.Sections["build"]; !ok { - // builds = nil - // } - // } - - // 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 inludeSys || !isStdPkg(name) { - // if builds != nil { - // if dep, ok := builds.Deps[name]; ok { - // pkgs[name] = dep.Pkg - // continue - // } - // } - // pkgs[name] = doc.NewDefaultPkg(name) - // } - // } - return nil, nil //pkgs, nil +func getGopmPkgs(dirPath string) (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()) + } + + var builds map[string]string + + if com.IsFile(absPath + "/" + doc.GopmFileName) { + gf := doc.NewGopmfile(absPath) + + if builds, err = gf.GetSection("deps"); err != nil { + builds = nil + } + } + + pkg, err := build.ImportDir(dirPath, 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) { + if builds != nil { + if dep, ok := builds[name]; ok { + pkgs[name] = &doc.Pkg{ImportPath: dep} + continue + } + } + pkgs[name] = doc.NewDefaultPkg(name) + } + } + return pkgs, nil } func pkgInCache(name string, cachePkgs map[string]*doc.Pkg) bool { @@ -62,42 +63,42 @@ func autoLink(oldPath, newPath string) error { return makeLink(oldPath, newPath) } -func getChildPkgs(cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg) error { - // pkgs, err := getGopmPkgs(cpath, false) - // if err != nil { - // return err - // } - // for name, pkg := range pkgs { - // if !pkgInCache(name, cachePkgs) { - // var newPath string - // if !build.IsLocalImport(name) { - // newPath = filepath.Join(installRepoPath, pkg.ImportPath) - // if pkgName != "" && strings.HasPrefix(pkg.ImportPath, pkgName) { - // newPath = filepath.Join(curPath, pkg.ImportPath[len(pkgName)+1:]) - // } else { - // if !com.IsExist(newPath) { - // var t, ver string = doc.BRANCH, "" - // node := doc.NewNode(pkg.ImportPath, pkg.ImportPath, t, ver, true) - // nodes := []*doc.Node{node} - // downloadPackages(nodes) - // // should handler download failed - // } - // } - // } else { - // newPath, err = filepath.Abs(name) - // if err != nil { - // return err - // } - // } - // err = getChildPkgs(newPath, pkg, cachePkgs) - // if err != nil { - // return err - // } - // } - // } - // if ppkg != nil && !build.IsLocalImport(ppkg.ImportPath) { - // cachePkgs[ppkg.ImportPath] = ppkg - // } +func getChildPkgs(ctx *cli.Context, cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg) error { + pkgs, err := getGopmPkgs(cpath) + if err != nil { + return err + } + for name, pkg := range pkgs { + if !pkgInCache(name, cachePkgs) { + var newPath string + if !build.IsLocalImport(name) { + newPath = filepath.Join(installRepoPath, pkg.ImportPath) + if pkgName != "" && strings.HasPrefix(pkg.ImportPath, pkgName) { + newPath = filepath.Join(curPath, pkg.ImportPath[len(pkgName)+1:]) + } else { + if !com.IsExist(newPath) { + var t, ver string = doc.BRANCH, "" + node := doc.NewNode(pkg.ImportPath, pkg.ImportPath, t, ver, true) + nodes := []*doc.Node{node} + downloadPackages(ctx, nodes) + // should handler download failed + } + } + } else { + newPath, err = filepath.Abs(name) + if err != nil { + return err + } + } + err = getChildPkgs(ctx, newPath, pkg, cachePkgs) + if err != nil { + return err + } + } + } + if ppkg != nil && !build.IsLocalImport(ppkg.ImportPath) { + cachePkgs[ppkg.ImportPath] = ppkg + } return nil } @@ -149,90 +150,86 @@ func execCmd(gopath, curPath string, args ...string) error { return cmd.Run() } -func genNewGoPath() { - // var err error - // curPath, err = os.Getwd() - // if err != nil { - // com.ColorLog("[ERRO] %v\n", err) - // return - // } - - // hd, err := com.HomeDir() - // if err != nil { - // com.ColorLog("[ERRO] Fail to get current user[ %s ]\n", err) - // return - // } - - // gf := doc.NewGopmfile() - // gpmPath := filepath.Join(curPath, doc.GopmFileName) - // if com.IsExist(gpmPath) { - // com.ColorLog("[INFO] loading .gopmfile ...\n") - // err := gf.Load(gpmPath) - // if err != nil { - // com.ColorLog("[ERRO] load .gopmfile failed: %v\n", err) - // return - // } - // } - - // installRepoPath = strings.Replace(reposDir, "~", hd, -1) - - // cachePkgs := make(map[string]*doc.Pkg) - // if target, ok := gf.Sections["target"]; ok { - // pkgName = target.Props["path"] - // 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") - // newGoPathSrc := filepath.Join(newGoPath, "src") - // os.RemoveAll(newGoPathSrc) - // os.MkdirAll(newGoPathSrc, os.ModePerm) - - // for name, _ := range cachePkgs { - // oldPath := filepath.Join(installRepoPath, name) - // newPath := filepath.Join(newGoPathSrc, name) - // paths := strings.Split(name, "/") - // var isExistP bool - // var isCurChild bool - // for i := 0; i < len(paths)-1; i++ { - // pName := strings.Join(paths[:len(paths)-1-i], "/") - // if _, ok := cachePkgs[pName]; ok { - // isExistP = true - // break - // } - // if pkgName == pName { - // isCurChild = true - // break - // } - // } - // if isCurChild { - // continue - // } - - // if !isExistP { - // com.ColorLog("[INFO] linked %v\n", name) - // err = autoLink(oldPath, newPath) - // if err != nil { - // com.ColorLog("[ERRO] make link error %v\n", err) - // return - // } - // } - // } - - // newCurPath = filepath.Join(newGoPathSrc, pkgName) - // com.ColorLog("[INFO] linked %v\n", pkgName) - // err = autoLink(curPath, newCurPath) - // if err != nil { - // com.ColorLog("[ERRO] make link error %v\n", err) - // return - // } +func genNewGoPath(ctx *cli.Context) { + var err error + curPath, err = os.Getwd() + if err != nil { + log.Error("", "Fail to get work directory") + log.Fatal("", err.Error()) + } + + hd, err := com.HomeDir() + if err != nil { + log.Error("", "Fail to get current user") + log.Fatal("", err.Error()) + } + + doc.HomeDir = strings.Replace(doc.RawHomeDir, "~", hd, -1) + installRepoPath = doc.HomeDir + "/repos" + + if com.IsFile(curPath + "/" + doc.GopmFileName) { + log.Trace("Loading gopmfile...") + gf := doc.NewGopmfile(curPath) + + var err error + pkgName, err = gf.GetValue("target", "path") + if err == nil { + log.Log("Target name: %s", pkgName) + } + } + + if len(pkgName) == 0 { + _, pkgName = filepath.Split(curPath) + } + + cachePkgs := make(map[string]*doc.Pkg) + err = getChildPkgs(ctx, curPath, nil, cachePkgs) + if err != nil { + log.Error("", "Fail to get child pakcages") + log.Fatal("", err.Error()) + } + + newGoPath = filepath.Join(curPath, "vendor") + newGoPathSrc := filepath.Join(newGoPath, "src") + os.RemoveAll(newGoPathSrc) + os.MkdirAll(newGoPathSrc, os.ModePerm) + + for name, _ := range cachePkgs { + oldPath := filepath.Join(installRepoPath, name) + newPath := filepath.Join(newGoPathSrc, name) + paths := strings.Split(name, "/") + var isExistP bool + var isCurChild bool + for i := 0; i < len(paths)-1; i++ { + pName := strings.Join(paths[:len(paths)-1-i], "/") + if _, ok := cachePkgs[pName]; ok { + isExistP = true + break + } + if pkgName == pName { + isCurChild = true + break + } + } + if isCurChild { + continue + } + + if !isExistP { + log.Log("Linking %s", name) + err = autoLink(oldPath, newPath) + if err != nil { + log.Error("", "Fail to make link") + log.Fatal("", err.Error()) + } + } + } + + newCurPath = filepath.Join(newGoPathSrc, pkgName) + log.Log("Linking %s", pkgName) + err = autoLink(curPath, newCurPath) + if err != nil { + log.Error("", "Fail to make link") + log.Fatal("", err.Error()) + } } diff --git a/cmd/install.go b/cmd/install.go index 5c5c74037..58ef75a27 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -35,7 +35,7 @@ func printInstallPrompt(flag string) { } func runInstall(cmd *Command, args []string) { - genNewGoPath() + //genNewGoPath() com.ColorLog("[INFO] installing ...\n") diff --git a/cmd/run.go b/cmd/run.go index 7eb03599a..65a04eb47 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -15,52 +15,54 @@ package cmd import ( - "github.com/Unknwon/com" + "fmt" "go/build" "os" "os/exec" -) -var CmdRun = &Command{ - UsageLine: "run", - Short: "run according a gopmfile", - Long: ` -run just like go run -`, -} + "github.com/codegangsta/cli" -func init() { - CmdRun.Run = runRun - CmdRun.Flags = map[string]bool{} -} + "github.com/gpmgo/gopm/log" +) + +var CmdRun = cli.Command{ + Name: "run", + Usage: "link dependencies and go run", + Description: `Command run links dependencies according to gopmfile -func printRunPrompt(flag string) { +gopm run `, + Action: runRun, } -func runRun(cmd *Command, args []string) { +func runRun(ctx *cli.Context) { gopath := build.Default.GOPATH - genNewGoPath() - - com.ColorLog("[INFO] running ...\n") + genNewGoPath(ctx) cmdArgs := []string{"go", "run"} - cmdArgs = append(cmdArgs, args...) + cmdArgs = append(cmdArgs, ctx.Args()...) bCmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) bCmd.Stdout = os.Stdout bCmd.Stderr = os.Stderr + + log.Log("===== application outputs start =====\n") + err := bCmd.Run() + + fmt.Println() + log.Log("====== application outputs end ======") + if err != nil { - com.ColorLog("[ERRO] run failed: %v\n", err) - return + log.Error("Run", "Fail to execute") + log.Fatal("", err.Error()) } - com.ColorLog("[TRAC] set GOPATH=%v\n", gopath) + log.Trace("Set back GOPATH=%s", gopath) err = os.Setenv("GOPATH", gopath) if err != nil { - com.ColorLog("[ERRO] %v\n", err) - return + log.Error("Run", "Fail to set back GOPATH") + log.Fatal("", err.Error()) } - com.ColorLog("[SUCC] run successfully!\n") + log.Success("SUCC", "Run", "Command execute successfully!") } diff --git a/doc/conf.go b/doc/conf.go index bed33cdf0..bc20bf157 100644 --- a/doc/conf.go +++ b/doc/conf.go @@ -26,6 +26,7 @@ import ( const ( GopmFileName = ".gopmfile" + RawHomeDir = "~/.gopm" ) var ( diff --git a/gopm.go b/gopm.go index e9de6bddb..dc1d0ff3e 100644 --- a/gopm.go +++ b/gopm.go @@ -29,10 +29,9 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.5.3.1109" +const APP_VER = "0.5.4.1110" // //cmd.CmdSearch, -// cmd.CmdRun, // cmd.CmdBuild, // cmd.CmdInstall, @@ -58,6 +57,7 @@ func main() { app.Commands = []cli.Command{ cmd.CmdGet, cmd.CmdGen, + cmd.CmdRun, } app.Run(os.Args) }