// 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 getChildPkgs(ctx *cli.Context, cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg, isTest bool) error { log.Trace("Current Path: 1 %s", cpath) pkgs, err := getGopmPkgs(cpath, isTest) if err != nil { return errors.New("Fail to get gopmfile deps: " + err.Error()) } for name, pkg := range pkgs { pkg.RootPath = doc.GetProjectPath(pkg.ImportPath) if !pkgInCache(pkg.RootPath, cachePkgs) { var newPath string if !build.IsLocalImport(name) && pkg.Type != doc.LOCAL { suf := versionSuffix(pkg.Value) pkgPath := strings.Replace( pkg.ImportPath, pkg.RootPath, pkg.RootPath+suf, 1) newPath = filepath.Join(installRepoPath, pkgPath) if len(suf) == 0 && !ctx.Bool("remote") && com.IsDir(filepath.Join(installGopath, pkgPath)) { newPath = filepath.Join(installGopath, pkgPath) } if pkgName != "" && strings.HasPrefix(pkg.ImportPath, pkgName) { newPath = filepath.Join(curPath, strings.TrimPrefix(pkg.ImportPath, pkgName)) } else { if !com.IsExist(newPath) || ctx.Bool("update") { node := doc.NewNode(pkg.ImportPath, pkg.ImportPath, pkg.Type, pkg.Value, true) nodes := []*doc.Node{node} downloadPackages(ctx, nodes) // TODO: Should handler download failed } } } else { if pkg.Type == doc.LOCAL { newPath, err = filepath.Abs(pkg.Value) } else { newPath, err = filepath.Abs(name) } if err != nil { return err } } cachePkgs[pkg.RootPath] = pkg err = getChildPkgs(ctx, newPath, pkg, cachePkgs, false) if err != nil { return err } } } return 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!") }
// downloadPackages downloads packages with certain commit, // if the commit is empty string, then it downloads all dependencies, // otherwise, it only downloada package with specific commit only. func downloadPackages(ctx *cli.Context, nodes []*doc.Node) { // Check all packages, they may be raw packages path. for _, n := range nodes { // Check if local reference if n.Type == doc.LOCAL { continue } // Check if it is a valid remote path or C. if n.ImportPath == "C" { continue } else if !doc.IsValidRemotePath(n.ImportPath) { // Invalid import path. log.Error("download", "Skipped invalid package: "+fmt.Sprintf("%s@%s:%s", n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))) failConut++ continue } // Valid import path. gopathDir := path.Join(installGopath, n.ImportPath) n.RootPath = doc.GetProjectPath(n.ImportPath) installPath := path.Join(installRepoPath, n.RootPath) + versionSuffix(n.Value) if isSubpackage(n.RootPath, ".") { continue } // Indicates whether need to download package again. if n.IsFixed() && com.IsExist(installPath) { n.IsGetDepsOnly = true } if !ctx.Bool("update") { // Check if package has been downloaded. if (len(n.Value) == 0 && !ctx.Bool("remote") && com.IsExist(gopathDir)) || com.IsExist(installPath) { log.Trace("Skipped installed package: %s@%s:%s", n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) // Only copy when no version control. if ctx.Bool("gopath") && com.IsExist(installPath) || len(getVcsName(gopathDir)) == 0 { copyToGopath(installPath, gopathDir) } continue } else { doc.LocalNodes.SetValue(n.RootPath, "value", "") } } if downloadCache[n.RootPath] { log.Trace("Skipped downloaded package: %s@%s:%s", n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) continue } // Download package. nod, imports := downloadPackage(ctx, n) if len(imports) > 0 { var gf *goconfig.ConfigFile // Check if has gopmfile. if com.IsFile(installPath + "/" + doc.GOPM_FILE_NAME) { log.Log("Found gopmfile: %s@%s:%s", n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) gf = doc.NewGopmfile(installPath) } // Need to download dependencies. // Generate temporary nodes. nodes := make([]*doc.Node, len(imports)) for i := range nodes { nodes[i] = doc.NewNode(imports[i], imports[i], doc.BRANCH, "", true) if gf == nil { continue } // Check if user specified the version. if v, err := gf.GetValue("deps", imports[i]); err == nil && len(v) > 0 { nodes[i].Type, nodes[i].Value = validPath(v) } } downloadPackages(ctx, nodes) } // Only save package information with specific commit. if nod == nil { continue } // Save record in local nodes. log.Success("SUCC", "GET", fmt.Sprintf("%s@%s:%s", n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))) downloadCount++ // Only save non-commit node. if len(nod.Value) == 0 && len(nod.Revision) > 0 { doc.LocalNodes.SetValue(nod.RootPath, "value", nod.Revision) } if ctx.Bool("gopath") && com.IsExist(installPath) && !ctx.Bool("update") && len(getVcsName(path.Join(installGopath, nod.RootPath))) == 0 { copyToGopath(installPath, gopathDir) } } }