// downloadPackage downloads package either use version control tools or not. func downloadPackage(ctx *cli.Context, nod *doc.Node) (*doc.Node, []string) { log.Message("Downloading", fmt.Sprintf("package: %s@%s:%s", nod.ImportPath, nod.Type, doc.CheckNodeValue(nod.Value))) // Mark as donwloaded. downloadCache[nod.RootPath] = true // Check if only need to use VCS tools. var imports []string var err error gopathDir := path.Join(installGopath, nod.RootPath) vcs := getVcsName(gopathDir) if ctx.Bool("update") && ctx.Bool("gopath") && len(vcs) > 0 { err = updateByVcs(vcs, gopathDir) imports = doc.GetAllImports([]string{gopathDir}, nod.RootPath, false) } else { // If package has revision and exist, then just check dependencies. if nod.IsGetDepsOnly { return nod, doc.GetAllImports([]string{path.Join(installRepoPath, nod.RootPath) + versionSuffix(nod.Value)}, nod.RootPath, ctx.Bool("example")) } nod.Revision = doc.LocalNodes.MustValue(nod.RootPath, "value") imports, err = doc.PureDownload(nod, installRepoPath, ctx) //CmdGet.Flags) } if err != nil { log.Error("get", "Fail to download pakage: "+nod.ImportPath) log.Error("", "\t"+err.Error()) failConut++ os.RemoveAll(installRepoPath + "/" + nod.RootPath) return nil, nil } return nod, imports }
// scan a directory and gen a gopm file func runGen(ctx *cli.Context) { setup(ctx) if !com.IsExist(".gopmfile") { os.Create(".gopmfile") } gf, err := goconfig.LoadConfigFile(".gopmfile") if err != nil { log.Error("gen", "Cannot load gopmfile:") log.Fatal("", "\t"+err.Error()) } // Get dependencies. imports := doc.GetAllImports([]string{workDir}, parseTarget(gf.MustValue("target", "path")), ctx.Bool("example")) for _, p := range imports { p = doc.GetProjectPath(p) if strings.HasSuffix(workDir, p) { continue } if value := gf.MustValue("deps", p); len(value) == 0 { gf.SetValue("deps", p, "") } } err = goconfig.SaveConfigFile(gf, ".gopmfile") if err != nil { log.Error("gen", "Fail to save gopmfile:") log.Fatal("", "\t"+err.Error()) } log.Success("SUCC", "gen", "Generate gopmfile successfully!") }
func getByGopmfile(ctx *cli.Context) { // Check if gopmfile exists and generate one if not. if !com.IsFile(".gopmfile") { runGen(ctx) } gf := doc.NewGopmfile(".") targetPath := parseTarget(gf.MustValue("target", "path")) // Get dependencies. imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example")) nodes := make([]*doc.Node, 0, len(imports)) for _, p := range imports { p = doc.GetProjectPath(p) // Skip subpackage(s) of current project. if isSubpackage(p, targetPath) { continue } node := doc.NewNode(p, p, doc.BRANCH, "", true) // Check if user specified the version. if v, err := gf.GetValue("deps", p); err == nil && len(v) > 0 { node.Type, node.Value = validPath(v) } nodes = append(nodes, node) } downloadPackages(ctx, nodes) doc.SaveLocalNodes() log.Log("%d package(s) downloaded, %d failed", downloadCount, failConut) }
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("", "\t"+err.Error()) } var builds map[string]string if com.IsFile(absPath + "/" + doc.GOPM_FILE_NAME) { gf := doc.NewGopmfile(absPath) if builds, err = gf.GetSection("deps"); err != nil { builds = nil } } imports := doc.GetAllImports([]string{dirPath}, ".", false, false) pkgs = make(map[string]*doc.Pkg) for _, name := range imports { if name == "C" { continue } if !doc.IsGoRepoPath(name) { if builds != nil { if info, ok := builds[name]; ok { // Check version. there should chek // local first because d:\ contains : if com.IsDir(info) { pkgs[name] = &doc.Pkg{ ImportPath: name, Type: doc.LOCAL, Value: info, } continue } else if i := strings.Index(info, ":"); i > -1 { pkgs[name] = &doc.Pkg{ ImportPath: name, Type: info[:i], Value: info[i+1:], } continue } } } pkgs[name] = doc.NewDefaultPkg(name) } } return pkgs, nil }
func runGen(ctx *cli.Context) { setup(ctx) if !com.IsExist(".gopmfile") { os.Create(".gopmfile") } gf, err := goconfig.LoadConfigFile(".gopmfile") if err != nil { log.Error("gen", "Cannot load gopmfile:") log.Fatal("", "\t"+err.Error()) } targetPath := parseTarget(gf.MustValue("target", "path")) // Get and set dependencies. imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example"), false) for _, p := range imports { p = doc.GetProjectPath(p) // Skip subpackage(s) of current project. if isSubpackage(p, targetPath) { continue } // Check if user specified the version. if value := gf.MustValue("deps", p); len(value) == 0 { gf.SetValue("deps", p, "") } } // Get and set resources. res := make([]string, 0, len(commonRes)) for _, cr := range commonRes { if com.IsExist(cr) { res = append(res, cr) } } gf.SetValue("res", "include", strings.Join(res, "|")) err = goconfig.SaveConfigFile(gf, ".gopmfile") if err != nil { log.Error("gen", "Fail to save gopmfile:") log.Fatal("", "\t"+err.Error()) } log.Success("SUCC", "gen", "Generate gopmfile successfully!") }
func runInstall(ctx *cli.Context) { setup(ctx) var target string switch len(ctx.Args()) { case 0: if !com.IsFile(".gopmfile") { break } gf := doc.NewGopmfile(".") target = gf.MustValue("target", "path") case 1: target = ctx.Args()[0] default: log.Fatal("install", "Too many arguments") } // Get GOPATH. installGopath = com.GetGOPATHs()[0] if com.IsDir(installGopath) { isHasGopath = true log.Log("Indicated GOPATH: %s", installGopath) installGopath += "/src" } else { if ctx.Bool("gopath") { 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") } else { // It's OK that no GOPATH setting // when user does not specify to use. log.Warn("No GOPATH setting available") } } genNewGoPath(ctx, false) var installRepos []string if ctx.Bool("pkg") { curPath, _ := filepath.Abs(".") installRepos = doc.GetAllImports([]string{curPath}, ".", ctx.Bool("example")) } else { if len(target) == 0 { target = pkgName } installRepos = []string{target} } log.Trace("Installing...") for _, repo := range installRepos { cmdArgs := []string{"go", "install"} if ctx.Bool("verbose") { cmdArgs = append(cmdArgs, "-v") } cmdArgs = append(cmdArgs, repo) err := execCmd(newGoPath, newCurPath, cmdArgs...) if err != nil { log.Error("install", "Fail to install program:") log.Fatal("", "\t"+err.Error()) } } log.Success("SUCC", "install", "Command executed successfully!") }