Browse Source

Done run command

pull/103/head
Unknown 11 years ago
parent
commit
3d949cb336
  1. 2
      cmd/build.go
  2. 2
      cmd/gen.go
  3. 12
      cmd/get.go
  4. 325
      cmd/gopath.go
  5. 2
      cmd/install.go
  6. 52
      cmd/run.go
  7. 1
      doc/conf.go
  8. 4
      gopm.go

2
cmd/build.go

@ -35,7 +35,7 @@ func printBuildPrompt(flag string) {
} }
func runBuild(cmd *Command, args []string) { func runBuild(cmd *Command, args []string) {
genNewGoPath() //genNewGoPath()
com.ColorLog("[INFO] building ...\n") com.ColorLog("[INFO] building ...\n")

2
cmd/gen.go

@ -73,5 +73,5 @@ func runGen(ctx *cli.Context) {
log.Fatal("", err.Error()) log.Fatal("", err.Error())
} }
log.Success("SUCC", "Gen", "Generate gopmfile successful!") log.Success("SUCC", "Gen", "Generate gopmfile successfully!")
} }

12
cmd/get.go

@ -67,7 +67,7 @@ func runGet(ctx *cli.Context) {
log.Fatal("", err.Error()) 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") doc.LoadPkgNameList(doc.HomeDir + "/data/pkgname.list")
installRepoPath = doc.HomeDir + "/repos" installRepoPath = doc.HomeDir + "/repos"
@ -84,14 +84,14 @@ func runGet(ctx *cli.Context) {
func getByGopmfile(ctx *cli.Context) { func getByGopmfile(ctx *cli.Context) {
if !com.IsFile(".gopmfile") { 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(".") gf := doc.NewGopmfile(".")
absPath, err := filepath.Abs(".") absPath, err := filepath.Abs(".")
if err != nil { 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()) log.Fatal("", err.Error())
} }
@ -254,8 +254,8 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
continue continue
} else { } else {
// Invalid import path. // Invalid import path.
com.ColorLog("[WARN] Skipped invalid package path( %s => %s:%s )\n", log.Error("", "Skipped invalid package: "+fmt.Sprintf("%s@%s:%s",
n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)))
failConut++ failConut++
} }
} }
@ -272,7 +272,7 @@ func downloadPackage(nod *doc.Node) (*doc.Node, []string) {
if err != nil { if err != nil {
log.Error("Get", "Fail to download pakage: "+nod.ImportPath) log.Error("Get", "Fail to download pakage: "+nod.ImportPath)
log.Fatal("", err.Error()) log.Error("", err.Error())
failConut++ failConut++
os.RemoveAll(installRepoPath + "/" + doc.GetProjectPath(nod.ImportPath) + "/") os.RemoveAll(installRepoPath + "/" + doc.GetProjectPath(nod.ImportPath) + "/")
return nil, nil return nil, nil

325
cmd/gopath.go

@ -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
// }
} }

2
cmd/install.go

@ -35,7 +35,7 @@ func printInstallPrompt(flag string) {
} }
func runInstall(cmd *Command, args []string) { func runInstall(cmd *Command, args []string) {
genNewGoPath() //genNewGoPath()
com.ColorLog("[INFO] installing ...\n") com.ColorLog("[INFO] installing ...\n")

52
cmd/run.go

@ -15,52 +15,54 @@
package cmd package cmd
import ( import (
"github.com/Unknwon/com" "fmt"
"go/build" "go/build"
"os" "os"
"os/exec" "os/exec"
)
var CmdRun = &Command{ "github.com/codegangsta/cli"
UsageLine: "run",
Short: "run according a gopmfile",
Long: `
run just like go run
`,
}
func init() { "github.com/gpmgo/gopm/log"
CmdRun.Run = runRun )
CmdRun.Flags = map[string]bool{}
} 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 <file names>`,
Action: runRun,
} }
func runRun(cmd *Command, args []string) { func runRun(ctx *cli.Context) {
gopath := build.Default.GOPATH gopath := build.Default.GOPATH
genNewGoPath() genNewGoPath(ctx)
com.ColorLog("[INFO] running ...\n")
cmdArgs := []string{"go", "run"} cmdArgs := []string{"go", "run"}
cmdArgs = append(cmdArgs, args...) cmdArgs = append(cmdArgs, ctx.Args()...)
bCmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) bCmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
bCmd.Stdout = os.Stdout bCmd.Stdout = os.Stdout
bCmd.Stderr = os.Stderr bCmd.Stderr = os.Stderr
log.Log("===== application outputs start =====\n")
err := bCmd.Run() err := bCmd.Run()
fmt.Println()
log.Log("====== application outputs end ======")
if err != nil { if err != nil {
com.ColorLog("[ERRO] run failed: %v\n", err) log.Error("Run", "Fail to execute")
return log.Fatal("", err.Error())
} }
com.ColorLog("[TRAC] set GOPATH=%v\n", gopath) log.Trace("Set back GOPATH=%s", gopath)
err = os.Setenv("GOPATH", gopath) err = os.Setenv("GOPATH", gopath)
if err != nil { if err != nil {
com.ColorLog("[ERRO] %v\n", err) log.Error("Run", "Fail to set back GOPATH")
return log.Fatal("", err.Error())
} }
com.ColorLog("[SUCC] run successfully!\n") log.Success("SUCC", "Run", "Command execute successfully!")
} }

1
doc/conf.go

@ -26,6 +26,7 @@ import (
const ( const (
GopmFileName = ".gopmfile" GopmFileName = ".gopmfile"
RawHomeDir = "~/.gopm"
) )
var ( var (

4
gopm.go

@ -29,10 +29,9 @@ import (
// Test that go1.1 tag above is included in builds. main.go refers to this definition. // Test that go1.1 tag above is included in builds. main.go refers to this definition.
const go11tag = true const go11tag = true
const APP_VER = "0.5.3.1109" const APP_VER = "0.5.4.1110"
// //cmd.CmdSearch, // //cmd.CmdSearch,
// cmd.CmdRun,
// cmd.CmdBuild, // cmd.CmdBuild,
// cmd.CmdInstall, // cmd.CmdInstall,
@ -58,6 +57,7 @@ func main() {
app.Commands = []cli.Command{ app.Commands = []cli.Command{
cmd.CmdGet, cmd.CmdGet,
cmd.CmdGen, cmd.CmdGen,
cmd.CmdRun,
} }
app.Run(os.Args) app.Run(os.Args)
} }

Loading…
Cancel
Save