diff --git a/cmd/gen.go b/cmd/gen.go index 499c80921..c7673a1c0 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -64,12 +64,6 @@ func getPkgs(path string, inludeSys bool) ([]string, error) { // scan a directory and gen a gopm file func runGen(cmd *Command, args []string) { - // Check flags. - num := checkFlags(cmd.Flags, args, printGenPrompt) - if num == -1 { - return - } - args = args[num:] var gopmFile string = ".gopmfile" if len(args) > 0 { diff --git a/cmd/get.go b/cmd/get.go index fde907389..4912b3594 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -18,6 +18,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "strings" "github.com/Unknwon/com" @@ -59,145 +60,149 @@ func init() { } func runGet(ctx *cli.Context) { + hd, err := com.HomeDir() + if err != nil { + log.Error("get", "Fail to get current user") + log.Fatal("", err.Error()) + } + + installRepoPath = strings.Replace(reposDir, "~", hd, -1) + log.Log("Local repository path: %s", installRepoPath) + // Check number of arguments. switch len(ctx.Args()) { case 0: getByGopmfile(ctx) + default: + getByPath(ctx) } } func getByGopmfile(ctx *cli.Context) { if !com.IsFile(".gopmfile") { - log.Fatal("install", "No argument is supplied and no gopmfile exist") + log.Fatal("get", "No argument is supplied and no gopmfile exist") } - hd, err := com.HomeDir() + gf := doc.NewGopmfile(".") + + absPath, err := filepath.Abs(".") if err != nil { - log.Error("install", "Fail to get current user") + log.Error("", "Fail to get absolute path of work directory") log.Fatal("", err.Error()) } - installRepoPath = strings.Replace(reposDir, "~", hd, -1) - log.Log("Local repository path: %s", installRepoPath) + log.Log("Work directory: %s", absPath) - // TODO: 获取依赖包 + // Get dependencies. + imports := doc.GetAllImports([]string{absPath}, + gf.MustValue("target", "path"), ctx.Bool("example")) - log.Error("install", "command haven't done yet!") -} + nodes := make([]*doc.Node, 0, len(imports)) + for _, p := range imports { + node := doc.NewNode(p, p, doc.BRANCH, "", true) -func processGet() { - -} - -func runGet1(cmd *Command, args []string) { - nodes := []*doc.Node{} - // ver describles branch, tag or commit. - var t, ver string = doc.BRANCH, "" - - var err error - if len(args) >= 2 { - t, ver, err = validPath(args[1]) - if err != nil { - com.ColorLog("[ERROR] Fail to parse 'args'[ %s ]\n", err) - return + // Check if user specified the version. + if v, err := gf.GetValue("deps", p); err == nil { + tp, ver, err := validPath(v) + if err != nil { + log.Error("", "Fail to parse version") + log.Fatal("", err.Error()) + } + node.Type = tp + node.Value = ver } + nodes = append(nodes, node) } - node := doc.NewNode(args[0], args[0], t, ver, true) - nodes = append(nodes, node) + downloadPackages(ctx, nodes) - // Download package(s). - downloadPackages(nodes) - - com.ColorLog("[INFO] %d package(s) downloaded, %d failed.\n", + log.Log("%d package(s) downloaded, %d failed", downloadCount, failConut) } -// printGetPrompt prints prompt information to users to -// let them know what's going on. -func printGetPrompt(flag string) { - switch flag { - case "-d": - com.ColorLog("[INFO] You enabled download without installing.\n") - case "-u": - com.ColorLog("[INFO] You enabled force update.\n") - case "-e": - com.ColorLog("[INFO] You enabled download dependencies of example(s).\n") - } -} - -// checkFlags checks if the flag exists with correct format. -func checkFlags(flags map[string]bool, args []string, print func(string)) int { - num := 0 // Number of valid flags, use to cut out. - for i, f := range args { - // Check flag prefix '-'. - if !strings.HasPrefix(f, "-") { - // Not a flag, finish check process. - break +func getByPath(ctx *cli.Context) { + nodes := make([]*doc.Node, 0, len(ctx.Args())) + for _, info := range ctx.Args() { + pkg := info + node := doc.NewNode(pkg, pkg, doc.BRANCH, "", true) + + if i := strings.Index(info, "@"); i > -1 { + pkg = info[:i] + tp, ver, err := validPath(info[i+1:]) + if err != nil { + log.Error("", "Fail to parse version") + log.Fatal("", err.Error()) + } + node = doc.NewNode(pkg, pkg, tp, ver, true) } - // Check if it a valid flag. - if v, ok := flags[f]; ok { - flags[f] = !v - if !v { - print(f) - } else { - fmt.Println("DISABLE: " + f) + // Cheeck package name. + if !strings.Contains(pkg, "/") { + name, ok := doc.PackageNameList[pkg] + if !ok { + log.Error("", "Invalid package name: "+pkg) + log.Fatal("", "No match in the package name list") } - } else { - com.ColorLog("[ERRO] Unknown flag: %s.\n", f) - return -1 + pkg = name } - num = i + 1 + + nodes = append(nodes, node) } - return num + downloadPackages(ctx, nodes) + + log.Log("%d package(s) downloaded, %d failed", + downloadCount, failConut) } // 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(nodes []*doc.Node) { +func downloadPackages(ctx *cli.Context, nodes []*doc.Node) { // Check all packages, they may be raw packages path. for _, n := range nodes { // Check if it is a valid remote path. if doc.IsValidRemotePath(n.ImportPath) { - // if !CmdGet.Flags["-u"] { - // // Check if package has been downloaded. - // installPath := installRepoPath + "/" + doc.GetProjectPath(n.ImportPath) - // if len(n.Value) > 0 { - // installPath += "." + n.Value - // } - // if com.IsExist(installPath) { - // com.ColorLog("[WARN] Skipped installed package( %s => %s:%s )\n", - // n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) - // continue - // } - // } + if !ctx.Bool("force") { + // Check if package has been downloaded. + installPath := installRepoPath + "/" + doc.GetProjectPath(n.ImportPath) + if len(n.Value) > 0 { + installPath += "." + n.Value + } + if com.IsExist(installPath) { + log.Trace("Skipped installed package: %s@%s:%s", + n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) + continue + } + } if !downloadCache[n.ImportPath] { // Download package. nod, imports := downloadPackage(n) if len(imports) > 0 { + // TODO: 检查是否有 gopmfile + // 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) } - downloadPackages(nodes) + downloadPackages(ctx, nodes) } // Only save package information with specific commit. if nod != nil { // Save record in local nodes. - com.ColorLog("[SUCC] Downloaded package( %s => %s:%s )\n", - n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) + log.Success("SUCC", "GET", fmt.Sprintf("%s@%s:%s", + n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))) downloadCount++ + + // TODO: 保存包信息 //saveNode(nod) } } else { - com.ColorLog("[WARN] Skipped downloaded package( %s => %s:%s )\n", + log.Trace("Skipped downloaded package: %s@%s:%s", n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) } } else if n.ImportPath == "C" { @@ -213,15 +218,16 @@ func downloadPackages(nodes []*doc.Node) { // downloadPackage downloads package either use version control tools or not. func downloadPackage(nod *doc.Node) (*doc.Node, []string) { - com.ColorLog("[TRAC] Downloading package( %s => %s:%s )\n", - nod.ImportPath, nod.Type, doc.CheckNodeValue(nod.Value)) + log.Message("Downloading", fmt.Sprintf("package: %s@%s:%s", + nod.ImportPath, nod.Type, doc.CheckNodeValue(nod.Value))) // Mark as donwloaded. downloadCache[nod.ImportPath] = true imports, err := doc.PureDownload(nod, installRepoPath, nil) //CmdGet.Flags) if err != nil { - com.ColorLog("[ERRO] Download falied( %s )[ %s ]\n", nod.ImportPath, err) + log.Error("Get", "Fail to download pakage: "+nod.ImportPath) + log.Fatal("", err.Error()) failConut++ os.RemoveAll(installRepoPath + "/" + doc.GetProjectPath(nod.ImportPath) + "/") return nil, nil @@ -231,12 +237,10 @@ func downloadPackage(nod *doc.Node) (*doc.Node, []string) { // validPath checks if the information of the package is valid. func validPath(info string) (string, string, error) { - infos := strings.Split(info, ":") + infos := strings.SplitN(info, ":", 2) l := len(infos) switch { - case l > 2: - return "", "", errors.New("Invalid information of package") case l == 1: return doc.BRANCH, "", nil case l == 2: @@ -246,6 +250,6 @@ func validPath(info string) (string, string, error) { } return infos[0], infos[1], nil default: - return "", "", errors.New("Cannot match any case") + return "", "", errors.New("Invalid version information") } } diff --git a/cmd/gopath.go b/cmd/gopath.go index ff93bb1b3..b85a640f5 100644 --- a/cmd/gopath.go +++ b/cmd/gopath.go @@ -3,51 +3,51 @@ package cmd import ( "github.com/Unknwon/com" "github.com/gpmgo/gopm/doc" - "go/build" + //"go/build" "os" "os/exec" "path/filepath" - "strings" + //"strings" ) 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 pkgs, nil + // 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 pkgInCache(name string, cachePkgs map[string]*doc.Pkg) bool { @@ -63,41 +63,41 @@ func autoLink(oldPath, newPath string) error { } 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 - } + // 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 + // } return nil } @@ -150,89 +150,89 @@ func execCmd(gopath, curPath string, args ...string) error { } 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 - } + // 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 + // } } diff --git a/cmd/search.go b/cmd/search.go index c24ba43ae..17f8ad405 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -52,12 +52,6 @@ func printSearchPrompt(flag string) { // search packages func runSearch(cmd *Command, args []string) { - // Check flags. - num := checkFlags(cmd.Flags, args, printSearchPrompt) - if num == -1 { - return - } - args = args[num:] // Check length of arguments. if len(args) < 1 { diff --git a/doc/doc b/doc/doc deleted file mode 120000 index 9be75f6a1..000000000 --- a/doc/doc +++ /dev/null @@ -1 +0,0 @@ -/Users/lunny/go/src/github.com/gpmgo/gopm/doc \ No newline at end of file diff --git a/doc/gopmfile.go b/doc/gopmfile.go index 781149fef..d947796c0 100644 --- a/doc/gopmfile.go +++ b/doc/gopmfile.go @@ -1,105 +1,40 @@ +// Copyright 2013 gopm authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + package doc import ( - "bufio" - "os" - "strings" -) - -const ( - Greater = ">" - GreaterOrEq = ">=" - Equeal = "=" - Lesser = "<" - LesserOrEq = "<=" -) + "github.com/Unknwon/goconfig" -var ( - Ops = []string{GreaterOrEq, LesserOrEq, Greater, Equeal, Lesser} + "github.com/gpmgo/gopm/log" ) const ( GopmFileName = ".gopmfile" ) -type Depend struct { - Pkg *Pkg - Op string - Ver string -} - -type Section struct { - Name string - Deps map[string]*Depend - Props map[string]string -} - -func NewSection() *Section { - return &Section{Deps: make(map[string]*Depend), - Props: make(map[string]string), - } -} - -type Gopmfile struct { - Sections map[string]*Section -} - -func NewGopmfile() *Gopmfile { - return &Gopmfile{Sections: make(map[string]*Section)} -} - -func (this *Gopmfile) Load(path string) error { - f, err := os.Open(path) +func NewGopmfile(dirPath string) *goconfig.ConfigFile { + gf, err := goconfig.LoadConfigFile(dirPath + "/" + GopmFileName) if err != nil { - return err + log.Error("", "Fail to load gopmfile") + log.Fatal("", err.Error()) } - - scanner := bufio.NewScanner(f) - var sec *Section - for scanner.Scan() { - text := strings.TrimSpace(scanner.Text()) - if strings.HasPrefix(text, "[") && strings.HasSuffix(text, "]") { - sec = NewSection() - sec.Name = text[1 : len(text)-1] - this.Sections[sec.Name] = sec - } else { - if sec == nil { - continue - } - if sec.Name == "target" { - ss := strings.Split(text, "=") - if len(ss) == 1 { - sec.Props[strings.TrimSpace(ss[0])] = strings.TrimSpace(ss[0]) - } else if len(ss) == 2 { - sec.Props[strings.TrimSpace(ss[0])] = strings.TrimSpace(ss[1]) - } - } else { - var dep *Depend - for _, op := range Ops { - if strings.Contains(text, op) { - ss := strings.Split(text, op) - pkver := strings.Split(ss[1], ":") - var tp, value string - tp = pkver[0] - if len(pkver) == 2 { - value = pkver[1] - } - dep = &Depend{NewPkg(ss[0], tp, value), ss[1], value} - break - } - } - - if dep == nil { - dep = &Depend{NewDefaultPkg(text), Equeal, ""} - } - sec.Deps[dep.Pkg.ImportPath] = dep - } - } - } - - return nil + return gf } -func (this *Gopmfile) Save(path string) error { - return nil +var PackageNameList map[string]string + +func init() { + PackageNameList = make(map[string]string) } diff --git a/doc/utils.go b/doc/utils.go index b21cd54a9..9af8cfcbb 100644 --- a/doc/utils.go +++ b/doc/utils.go @@ -21,33 +21,96 @@ import ( "strings" "github.com/Unknwon/com" -) -// GetGOPATH returns best matched GOPATH. -func GetBestMatchGOPATH(appPath string) string { - paths := com.GetGOPATHs() - for _, p := range paths { - if strings.HasPrefix(p, appPath) { - return strings.Replace(p, "\\", "/", -1) - } - } - return paths[0] -} + "github.com/gpmgo/gopm/log" +) // GetDirsInfo returns os.FileInfo of all sub-directories in root path. -func GetDirsInfo(rootPath string) ([]os.FileInfo, error) { +func GetDirsInfo(rootPath string) []os.FileInfo { rootDir, err := os.Open(rootPath) if err != nil { - return nil, err + log.Error("", "Fail to open directory") + log.Fatal("", err.Error()) } defer rootDir.Close() dirs, err := rootDir.Readdir(0) if err != nil { - return nil, err + log.Error("", "Fail to read directory") + log.Fatal("", err.Error()) } - return dirs, err + return dirs +} + +// GetImports returns package denpendencies. +func GetImports(absPath, importPath string, example bool) (imports []string) { + fis := GetDirsInfo(absPath) + absPath += "/" + + dirs := make([]string, 0) + files := make([]*source, 0, 10) + for _, fi := range fis { + if fi.IsDir() { + dirs = append(dirs, absPath+fi.Name()) + continue + } + + if strings.HasSuffix(fi.Name(), ".go") { + data, err := com.ReadFile(absPath + fi.Name()) + if err != nil { + log.Error("", "Fail to read file") + log.Fatal("", err.Error()) + } + + files = append(files, &source{ + name: fi.Name(), + data: data, + }) + } + } + + var err error + if len(files) > 0 { + w := &walker{ImportPath: importPath} + imports, err = w.build(files, nil) + if err != nil { + log.Error("", "Fail to get imports") + log.Fatal("", err.Error()) + } + } + + if len(dirs) > 0 { + imports = append(imports, GetAllImports(dirs, importPath, example)...) + } + return imports +} + +func isVcsPath(dirPath string) bool { + return strings.Contains(dirPath, "/.git") || + strings.Contains(dirPath, "/.hg") || + strings.Contains(dirPath, "/.svn") +} + +func GetAllImports(dirs []string, importPath string, example bool) (imports []string) { + for _, d := range dirs { + if !isVcsPath(d) && + !(!example && strings.Contains(d, "example")) { + imports = append(imports, GetImports(d, importPath, example)...) + } + } + return imports +} + +// GetGOPATH returns best matched GOPATH. +func GetBestMatchGOPATH(appPath string) string { + paths := com.GetGOPATHs() + for _, p := range paths { + if strings.HasPrefix(p, appPath) { + return strings.Replace(p, "\\", "/", -1) + } + } + return paths[0] } // CheckIsExistWithVCS returns false if directory only has VCS folder, @@ -59,11 +122,7 @@ func CheckIsExistWithVCS(path string) bool { } // Check if only has VCS folder. - dirs, err := GetDirsInfo(path) - if err != nil { - com.ColorLog("[ERRO] CheckIsExistWithVCS -> [ %s ]\n", err) - return false - } + dirs := GetDirsInfo(path) if len(dirs) > 1 { return true @@ -478,7 +537,6 @@ var standardPath = map[string]bool{ "cmd/cgo": true, "cmd/fix": true, "cmd/go": true, - "cmd/godoc": true, "cmd/gofmt": true, "cmd/vet": true, "cmd/yacc": true, @@ -594,6 +652,7 @@ var standardPath = map[string]bool{ "runtime/cgo": true, "runtime/debug": true, "runtime/pprof": true, + "runtime/race": true, "sort": true, "strconv": true, "strings": true, diff --git a/doc/vcs.go b/doc/vcs.go index 51885bb52..a7ab3288a 100644 --- a/doc/vcs.go +++ b/doc/vcs.go @@ -243,10 +243,7 @@ metaScan: } func getImports(rootPath string, match map[string]string, cmdFlags map[string]bool, nod *Node) (imports []string) { - dirs, err := GetDirsInfo(rootPath) - if err != nil { - return nil - } + dirs := GetDirsInfo(rootPath) for _, d := range dirs { if d.IsDir() && !(!cmdFlags["-e"] && strings.Contains(d.Name(), "example")) { diff --git a/doc/walker.go b/doc/walker.go index 95ecb5aa9..11e740b4a 100644 --- a/doc/walker.go +++ b/doc/walker.go @@ -153,10 +153,14 @@ func (w *walker) build(srcs []*source, nod *Node) ([]string, error) { } pdoc := doc.New(apkg, w.ImportPath, mode) - nod.Synopsis = Synopsis(pdoc.Doc) - if i := strings.Index(nod.Synopsis, "\n"); i > -1 { - nod.Synopsis = nod.Synopsis[:i] + + if nod != nil { + nod.Synopsis = Synopsis(pdoc.Doc) + if i := strings.Index(nod.Synopsis, "\n"); i > -1 { + nod.Synopsis = nod.Synopsis[:i] + } } + return imports, err } diff --git a/log/log_nonwindows.go b/log/log_nonwindows.go index 1c913a218..f32490655 100644 --- a/log/log_nonwindows.go +++ b/log/log_nonwindows.go @@ -23,9 +23,9 @@ import ( func Error(hl, msg string) { if len(hl) > 0 { - hl = brush.Red(hl).String() + hl = " " + brush.Red(hl).String() } - fmt.Printf("gopm %s %s %s\n", brush.Red("ERR!"), hl, msg) + fmt.Printf("gopm %s%s %s\n", brush.Red("ERR!"), hl, msg) } func Fatal(hl, msg string) { @@ -37,3 +37,22 @@ func Log(format string, args ...interface{}) { fmt.Printf("gopm %s %s\n", brush.White("INFO"), fmt.Sprintf(format, args...)) } + +func Trace(format string, args ...interface{}) { + fmt.Printf("gopm %s %s\n", brush.Blue("TRAC"), + fmt.Sprintf(format, args...)) +} + +func Success(title, hl, msg string) { + if len(hl) > 0 { + hl = " " + brush.Green(hl).String() + } + fmt.Printf("gopm %s%s %s\n", brush.Green(title), hl, msg) +} + +func Message(hl, msg string) { + if len(hl) > 0 { + hl = " " + brush.Yellow(hl).String() + } + fmt.Printf("gopm %s%s %s\n", brush.Yellow("MSG!"), hl, msg) +} diff --git a/log/log_windows.go b/log/log_windows.go new file mode 100644 index 000000000..0c180263b --- /dev/null +++ b/log/log_windows.go @@ -0,0 +1,56 @@ +// Copyright 2013 gopm authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +package log + +import ( + "fmt" + "os" +) + +func Error(hl, msg string) { + if len(hl) > 0 { + hl = " " + hl + } + fmt.Printf("gopm ERR!%s %s\n", hl, msg) +} + +func Fatal(hl, msg string) { + Error(hl, msg) + os.Exit(2) +} + +func Log(format string, args ...interface{}) { + fmt.Printf("gopm INFO %s\n", + fmt.Sprintf(format, args...)) +} + +func Trace(format string, args ...interface{}) { + fmt.Printf("gopm TRAC %s\n", + fmt.Sprintf(format, args...)) +} + +func Success(title, hl, msg string) { + if len(hl) > 0 { + hl = " " + hl + } + fmt.Printf("gopm %s%s %s\n", title, hl, msg) +} + +func Message(hl, msg string) { + if len(hl) > 0 { + hl = " " + hl + } + fmt.Printf("gopm MSG!%s %s\n", hl, msg) +}