Browse Source

Improved get, bin, gen

pull/103/head
Unknown 11 years ago
parent
commit
31245fdd40
  1. 65
      cmd/bin.go
  2. 14
      cmd/gen.go
  3. 139
      cmd/get.go
  4. 4
      cmd/gopath.go
  5. 1
      cmd/run.go
  6. 1
      cmd/test.go
  7. 2
      cmd/update.go
  8. 24
      doc/conf.go
  9. 10
      doc/struct.go
  10. 7
      log/log.go
  11. 1
      log/log_windows.go

65
cmd/bin.go

@ -49,12 +49,10 @@ contains main package`,
func runBin(ctx *cli.Context) { func runBin(ctx *cli.Context) {
if len(ctx.Args()) == 0 { if len(ctx.Args()) == 0 {
log.Error("Bin", "Fail to start command") log.Error("bin", "Cannot start command:")
log.Fatal("", "No package specified") log.Fatal("", "\tNo package specified")
} }
doc.LoadPkgNameList(doc.HomeDir + "/data/pkgname.list")
installRepoPath = doc.HomeDir + "/repos" installRepoPath = doc.HomeDir + "/repos"
// Check arguments. // Check arguments.
@ -63,14 +61,14 @@ func runBin(ctx *cli.Context) {
num = 2 num = 2
} }
if len(ctx.Args()) != num { if len(ctx.Args()) != num {
log.Error("Bin", "Fail to start command") log.Error("bin", "Cannot start command:")
log.Fatal("", "Invalid argument number") log.Fatal("", "\tMissing indicated path to build binary")
} }
// Check if given directory exists. // Check if given directory exists.
if ctx.Bool("dir") && !com.IsDir(ctx.Args()[1]) { if ctx.Bool("dir") && !com.IsDir(ctx.Args()[1]) {
log.Error("Bin", "Fail to start command") log.Error("bin", "Cannot start command:")
log.Fatal("", "Given directory does not exist") log.Fatal("", "\tIndicated path does not exist or is not a directory")
} }
// Parse package version. // Parse package version.
@ -82,49 +80,43 @@ func runBin(ctx *cli.Context) {
pkgPath = info[:i] pkgPath = info[:i]
_, ver, err = validPath(info[i+1:]) _, ver, err = validPath(info[i+1:])
if err != nil { if err != nil {
log.Error("Bin", "Fail to parse version") log.Error("bin", "Cannot parse package version")
log.Fatal("", err.Error()) log.Error("", err.Error()+":")
log.Error("", "\t"+info[i+1:])
log.Help("Try 'gopm help get' to get more information")
} }
} }
// Check package name. // Check package name.
if !strings.Contains(pkgPath, "/") { if !strings.Contains(pkgPath, "/") {
name, ok := doc.PackageNameList[pkgPath] pkgPath = doc.GetPkgFullPath(pkgPath)
if !ok {
log.Error("Bin", "Invalid package name: "+pkgPath)
log.Fatal("", "No match in the package name list")
}
pkgPath = name
} }
// Get code. // Get code.
stdout, _, _ := com.ExecCmd("gopm", "get", ctx.Args()[0]) stdout, _, _ := com.ExecCmd("gopm", "get", "-r", ctx.Args()[0])
if len(stdout) > 0 { if len(stdout) > 0 {
fmt.Print(stdout) fmt.Print(stdout)
} }
// Check if previous steps were successful. // Check if previous steps were successful.
repoPath := installRepoPath + "/" + pkgPath repoPath := installRepoPath + "/" + pkgPath + versionSuffix(ver)
if len(ver) > 0 {
repoPath += "." + ver
}
if !com.IsDir(repoPath) { if !com.IsDir(repoPath) {
log.Error("Bin", "Fail to continue command") log.Error("bin", "Cannot continue command:")
log.Fatal("", "Previous steps weren't successful") log.Fatal("", "\tPrevious steps weren't successful")
} }
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
log.Error("Bin", "Fail to get work directory") log.Error("bin", "Cannot get work directory:")
log.Fatal("", err.Error()) log.Fatal("", "\t"+err.Error())
} }
// Change to repository path. // Change to repository path.
log.Log("Changing work directory to %s", repoPath) log.Log("Changing work directory to %s", repoPath)
err = os.Chdir(repoPath) err = os.Chdir(repoPath)
if err != nil { if err != nil {
log.Error("Bin", "Fail to change work directory") log.Error("bin", "Fail to change work directory:")
log.Fatal("", err.Error()) log.Fatal("", "\t"+err.Error())
} }
// Build application. // Build application.
@ -134,11 +126,11 @@ func runBin(ctx *cli.Context) {
} }
defer func() { defer func() {
// Clean files. // Clean files.
os.RemoveAll(path.Join(repoPath, doc.VENDOR)) //os.RemoveAll(path.Join(repoPath, doc.VENDOR))
}() }()
// Check if previous steps were successful. // Check if previous steps were successful.
if com.IsFile(doc.GopmFileName) { if com.IsFile(doc.GOPM_FILE_NAME) {
log.Trace("Loading gopmfile...") log.Trace("Loading gopmfile...")
gf := doc.NewGopmfile(".") gf := doc.NewGopmfile(".")
@ -157,10 +149,9 @@ func runBin(ctx *cli.Context) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
binName += ".exe" binName += ".exe"
} }
binPath := path.Join(doc.VENDOR, "src", pkgPath, binName) if !com.IsFile(binName) {
if !com.IsFile(binPath) { log.Error("bin", "Cannot continue command:")
log.Error("Bin", "Fail to continue command") log.Fatal("", "\tPrevious steps weren't successful or the project does not contain main package")
log.Fatal("", "Previous steps weren't successful or the project does not contain main package")
} }
// Move binary to given directory. // Move binary to given directory.
@ -168,15 +159,15 @@ func runBin(ctx *cli.Context) {
if ctx.Bool("dir") { if ctx.Bool("dir") {
movePath = ctx.Args()[1] movePath = ctx.Args()[1]
} }
err = os.Rename(binPath, movePath+"/"+binName) err = os.Rename(binName, movePath+"/"+binName)
if err != nil { if err != nil {
log.Error("Bin", "Fail to move binary") log.Error("bin", "Fail to move binary:")
log.Fatal("", err.Error()) log.Fatal("", "\t"+err.Error())
} }
os.Chmod(movePath+"/"+binName, os.ModePerm) os.Chmod(movePath+"/"+binName, os.ModePerm)
log.Log("Changing work directory back to %s", wd) log.Log("Changing work directory back to %s", wd)
os.Chdir(wd) os.Chdir(wd)
log.Success("SUCC", "Bin", "Command execute successfully!") log.Success("SUCC", "bin", "Command execute successfully!")
} }

14
cmd/gen.go

@ -47,14 +47,14 @@ func runGen(ctx *cli.Context) {
gf, err := goconfig.LoadConfigFile(".gopmfile") gf, err := goconfig.LoadConfigFile(".gopmfile")
if err != nil { if err != nil {
log.Error("Gen", "Fail to load gopmfile") log.Error("gen", "Cannot load gopmfile:")
log.Fatal("", err.Error()) log.Fatal("", "\t"+err.Error())
} }
curPath, err := os.Getwd() curPath, err := os.Getwd()
if err != nil { if err != nil {
log.Error("Gen", "Fail to get work directory") log.Error("gen", "Cannot get work directory:")
log.Fatal("", err.Error()) log.Fatal("", "\t"+err.Error())
} }
// Get dependencies. // Get dependencies.
@ -73,9 +73,9 @@ func runGen(ctx *cli.Context) {
err = goconfig.SaveConfigFile(gf, ".gopmfile") err = goconfig.SaveConfigFile(gf, ".gopmfile")
if err != nil { if err != nil {
log.Error("Gen", "Fail to save gopmfile") log.Error("gen", "Fail to save gopmfile:")
log.Fatal("", err.Error()) log.Fatal("", "\t"+err.Error())
} }
log.Success("SUCC", "Gen", "Generate gopmfile successfully!") log.Success("SUCC", "gen", "Generate gopmfile successfully!")
} }

139
cmd/get.go

@ -31,8 +31,8 @@ import (
) )
var ( var (
installRepoPath string installRepoPath string // The path of gopm local repository.
installGopath string installGopath string // The first path in the GOPATH.
downloadCache map[string]bool // Saves packages that have been downloaded. downloadCache map[string]bool // Saves packages that have been downloaded.
downloadCount int downloadCount int
failConut int failConut int
@ -41,7 +41,7 @@ var (
var CmdGet = cli.Command{ var CmdGet = cli.Command{
Name: "get", Name: "get",
Usage: "fetch remote package(s) and dependencies to local repository", Usage: "fetch remote package(s) and dependencies to local repository",
Description: `Command get fetches a package, and any pakcages that it depents on. Description: `Command get fetches a package, and any pakcage that it depents on.
If the package has a gopmfile, the fetch process will be driven by that. If the package has a gopmfile, the fetch process will be driven by that.
gopm get gopm get
@ -50,12 +50,16 @@ gopm get <package name>@[<tag|commit|branch>:<value>]
Can specify one or more: gopm get beego@tag:v0.9.0 github.com/beego/bee Can specify one or more: gopm get beego@tag:v0.9.0 github.com/beego/bee
If no argument is supplied, then gopmfile must be present`, If no argument is supplied, then gopmfile must be present.
If no version specified and package exists in GOPATH,
it will be skipped unless user enabled '--remote, -r' option
then all the packages go into gopm local repository.`,
Action: runGet, Action: runGet,
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{"gopath, g", "download package(s) to GOPATH"}, cli.BoolFlag{"gopath, g", "download all pakcages to GOPATH"},
cli.BoolFlag{"force, f", "force to update pakcage(s) and dependencies"}, cli.BoolFlag{"force, f", "force to update pakcage(s) and dependencies"},
cli.BoolFlag{"example, e", "download dependencies for example(s)"}, cli.BoolFlag{"example, e", "download dependencies for example folder"},
cli.BoolFlag{"remote, r", "download all pakcages to gopm local repository"},
}, },
} }
@ -64,19 +68,26 @@ func init() {
} }
func runGet(ctx *cli.Context) { func runGet(ctx *cli.Context) {
doc.LoadPkgNameList(doc.HomeDir + "/data/pkgname.list") // Check conflicts.
if ctx.Bool("gopath") && ctx.Bool("remote") {
if ctx.Bool("gopath") { log.Error("get", "Command options have conflicts")
installGopath = com.GetGOPATHs()[0] log.Error("", "Following options are not supposed to use at same time:")
if !com.IsDir(installGopath) { log.Error("", "\t'--gopath, -g' '--remote, -r'")
log.Error("Get", "Fail to start command") log.Help("Try 'gopm help get' to get more information")
log.Fatal("", "GOPATH does not exist: "+installGopath) }
}
log.Log("Indicate GOPATH: %s", installGopath)
installGopath += "/src" // Get GOPATH.
installGopath = com.GetGOPATHs()[0]
if !com.IsDir(installGopath) {
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")
} }
log.Log("Indicated GOPATH: %s", installGopath)
installGopath += "/src"
// The gopm local repository.
installRepoPath = doc.HomeDir + "/repos" installRepoPath = doc.HomeDir + "/repos"
log.Log("Local repository path: %s", installRepoPath) log.Log("Local repository path: %s", installRepoPath)
@ -92,15 +103,20 @@ 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.Error("get", "Gopmfile not found")
log.Error("", "No argument is supplied and no gopmfile exists")
log.Help("\n%s\n%s\n%s",
"Work directory is supposed to have gopmfile when there is no argument supplied",
"Try 'gopm gen' to auto-generate gopmfile",
"Try 'gopm help gen' to get more information")
} }
gf := doc.NewGopmfile(".") gf := doc.NewGopmfile(".")
absPath, err := filepath.Abs(".") absPath, err := filepath.Abs(".")
if err != nil { if err != nil {
log.Error("Get", "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("", "\t"+err.Error())
} }
log.Log("Work directory: %s", absPath) log.Log("Work directory: %s", absPath)
@ -117,8 +133,10 @@ func getByGopmfile(ctx *cli.Context) {
if v, err := gf.GetValue("deps", p); err == nil && len(v) > 0 { if v, err := gf.GetValue("deps", p); err == nil && len(v) > 0 {
tp, ver, err := validPath(v) tp, ver, err := validPath(v)
if err != nil { if err != nil {
log.Error("", "Fail to parse version") log.Error("get", "Cannot parse dependency version")
log.Fatal("", err.Error()) log.Error("", err.Error()+":")
log.Error("", "\t"+v)
log.Help("Try 'gopm help get' to get more information")
} }
node.Type = tp node.Type = tp
node.Value = ver node.Value = ver
@ -131,7 +149,8 @@ func getByGopmfile(ctx *cli.Context) {
if doc.LocalNodes != nil { if doc.LocalNodes != nil {
if err := goconfig.SaveConfigFile(doc.LocalNodes, if err := goconfig.SaveConfigFile(doc.LocalNodes,
doc.HomeDir+doc.LocalNodesFile); err != nil { doc.HomeDir+doc.LocalNodesFile); err != nil {
log.Error("Get", "Fail to save localnodes.list") log.Error("get", "Fail to save localnodes.list:")
log.Error("", "\t"+err.Error())
} }
} }
@ -142,27 +161,24 @@ func getByGopmfile(ctx *cli.Context) {
func getByPath(ctx *cli.Context) { func getByPath(ctx *cli.Context) {
nodes := make([]*doc.Node, 0, len(ctx.Args())) nodes := make([]*doc.Node, 0, len(ctx.Args()))
for _, info := range ctx.Args() { for _, info := range ctx.Args() {
pkgName := info pkgPath := info
node := doc.NewNode(pkgName, pkgName, doc.BRANCH, "", true) node := doc.NewNode(pkgPath, pkgPath, doc.BRANCH, "", true)
if i := strings.Index(info, "@"); i > -1 { if i := strings.Index(info, "@"); i > -1 {
pkgName = info[:i] pkgPath = info[:i]
tp, ver, err := validPath(info[i+1:]) tp, ver, err := validPath(info[i+1:])
if err != nil { if err != nil {
log.Error("Get", "Fail to parse version") log.Error("get", "Cannot parse dependency version")
log.Fatal("", err.Error()) log.Error("", err.Error()+":")
log.Error("", "\t"+info[i+1:])
log.Help("Try 'gopm help get' to get more information")
} }
node = doc.NewNode(pkgName, pkgName, tp, ver, true) node = doc.NewNode(pkgPath, pkgPath, tp, ver, true)
} }
// Check package name. // Check package name.
if !strings.Contains(pkgName, "/") { if !strings.Contains(pkgPath, "/") {
name, ok := doc.PackageNameList[pkgName] pkgPath = doc.GetPkgFullPath(pkgPath)
if !ok {
log.Error("Get", "Invalid package name: "+pkgName)
log.Fatal("", "No match in the package name list")
}
pkgName = name
} }
nodes = append(nodes, node) nodes = append(nodes, node)
@ -173,7 +189,8 @@ func getByPath(ctx *cli.Context) {
if doc.LocalNodes != nil { if doc.LocalNodes != nil {
if err := goconfig.SaveConfigFile(doc.LocalNodes, if err := goconfig.SaveConfigFile(doc.LocalNodes,
doc.HomeDir+doc.LocalNodesFile); err != nil { doc.HomeDir+doc.LocalNodesFile); err != nil {
log.Error("Get", "Fail to save localnodes.list") log.Error("get", "Fail to save localnodes.list:")
log.Error("", "\t"+err.Error())
} }
} }
@ -182,12 +199,11 @@ func getByPath(ctx *cli.Context) {
} }
func copyToGopath(srcPath, destPath string) { func copyToGopath(srcPath, destPath string) {
fmt.Println(destPath)
os.RemoveAll(destPath) os.RemoveAll(destPath)
err := com.CopyDir(srcPath, destPath) err := com.CopyDir(srcPath, destPath)
if err != nil { if err != nil {
log.Error("Download", "Fail to copy to GOPATH") log.Error("download", "Fail to copy to GOPATH:")
log.Fatal("", err.Error()) log.Fatal("", "\t"+err.Error())
} }
} }
@ -200,14 +216,14 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
// Check if it is a valid remote path. // Check if it is a valid remote path.
if doc.IsValidRemotePath(n.ImportPath) { if doc.IsValidRemotePath(n.ImportPath) {
gopathDir := path.Join(installGopath, n.ImportPath) gopathDir := path.Join(installGopath, n.ImportPath)
installPath := path.Join(installRepoPath, doc.GetProjectPath(n.ImportPath)) n.RootPath = doc.GetProjectPath(n.ImportPath)
if len(n.Value) > 0 { installPath := path.Join(installRepoPath, n.RootPath) +
installPath += "." + n.Value versionSuffix(n.Value)
}
if !ctx.Bool("force") { if !ctx.Bool("force") {
// Check if package has been downloaded. // Check if package has been downloaded.
if com.IsExist(installPath) { if (len(n.Value) == 0 && !ctx.Bool("remote") && com.IsExist(gopathDir)) ||
com.IsExist(installPath) {
log.Trace("Skipped installed package: %s@%s:%s", log.Trace("Skipped installed package: %s@%s:%s",
n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))
@ -216,22 +232,22 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
} }
continue continue
} else { } else {
doc.LocalNodes.SetValue(doc.GetProjectPath(n.ImportPath), "value", "") doc.LocalNodes.SetValue(n.RootPath, "value", "")
} }
} }
if !downloadCache[n.ImportPath] { if !downloadCache[n.RootPath] {
// Download package. // Download package.
nod, imports := downloadPackage(ctx, n) nod, imports := downloadPackage(ctx, n)
if len(imports) > 0 { if len(imports) > 0 {
var gf *goconfig.ConfigFile var gf *goconfig.ConfigFile
// Check if has gopmfile // Check if has gopmfile
if com.IsFile(installPath + "/" + doc.GopmFileName) { if com.IsFile(installPath + "/" + doc.GOPM_FILE_NAME) {
log.Log("Found gopmgile: %s@%s:%s", log.Log("Found gopmgile: %s@%s:%s",
n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))
gf = doc.NewGopmfile(installPath /* + "/.gopmfile"*/) gf = doc.NewGopmfile(installPath)
} }
// Need to download dependencies. // Need to download dependencies.
@ -249,8 +265,10 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
len(v) > 0 { len(v) > 0 {
tp, ver, err := validPath(v) tp, ver, err := validPath(v)
if err != nil { if err != nil {
log.Error("Download", "Fail to parse version") log.Error("download", "Cannot parse dependency version")
log.Fatal("", err.Error()) log.Error("", err.Error()+":")
log.Error("", "\t"+v)
log.Help("Try 'gopm help get' to get more information")
} }
nodes[i].Type = tp nodes[i].Type = tp
nodes[i].Value = ver nodes[i].Value = ver
@ -268,7 +286,7 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
// Only save non-commit node. // Only save non-commit node.
if len(nod.Value) == 0 && len(nod.Revision) > 0 { if len(nod.Value) == 0 && len(nod.Revision) > 0 {
doc.LocalNodes.SetValue(doc.GetProjectPath(nod.ImportPath), "value", nod.Revision) doc.LocalNodes.SetValue(nod.RootPath, "value", nod.Revision)
} }
if ctx.Bool("gopath") { if ctx.Bool("gopath") {
@ -283,7 +301,7 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
continue continue
} else { } else {
// Invalid import path. // Invalid import path.
log.Error("", "Skipped invalid package: "+fmt.Sprintf("%s@%s:%s", log.Error("download", "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++
} }
@ -297,14 +315,14 @@ func downloadPackage(ctx *cli.Context, nod *doc.Node) (*doc.Node, []string) {
// Mark as donwloaded. // Mark as donwloaded.
downloadCache[nod.ImportPath] = true downloadCache[nod.ImportPath] = true
nod.Revision = doc.LocalNodes.MustValue(doc.GetProjectPath(nod.ImportPath), "value") nod.Revision = doc.LocalNodes.MustValue(nod.RootPath, "value")
imports, err := doc.PureDownload(nod, installRepoPath, ctx) //CmdGet.Flags) imports, err := doc.PureDownload(nod, installRepoPath, ctx) //CmdGet.Flags)
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.Error("", err.Error()) log.Error("", "\t"+err.Error())
failConut++ failConut++
os.RemoveAll(installRepoPath + "/" + doc.GetProjectPath(nod.ImportPath) + "/") os.RemoveAll(installRepoPath + "/" + nod.RootPath)
return nil, nil return nil, nil
} }
return nod, imports return nod, imports
@ -312,7 +330,7 @@ func downloadPackage(ctx *cli.Context, nod *doc.Node) (*doc.Node, []string) {
// validPath checks if the information of the package is valid. // validPath checks if the information of the package is valid.
func validPath(info string) (string, string, error) { func validPath(info string) (string, string, error) {
infos := strings.SplitN(info, ":", 2) infos := strings.Split(info, ":")
l := len(infos) l := len(infos)
switch { switch {
@ -328,3 +346,10 @@ func validPath(info string) (string, string, error) {
return "", "", errors.New("Invalid version information") return "", "", errors.New("Invalid version information")
} }
} }
func versionSuffix(value string) string {
if len(value) > 0 {
return "." + value
}
return ""
}

4
cmd/gopath.go

@ -26,7 +26,7 @@ func getGopmPkgs(dirPath string, isTest bool) (pkgs map[string]*doc.Pkg, err err
var builds map[string]string var builds map[string]string
if com.IsFile(absPath + "/" + doc.GopmFileName) { if com.IsFile(absPath + "/" + doc.GOPM_FILE_NAME) {
gf := doc.NewGopmfile(absPath) gf := doc.NewGopmfile(absPath)
if builds, err = gf.GetSection("deps"); err != nil { if builds, err = gf.GetSection("deps"); err != nil {
@ -190,7 +190,7 @@ func genNewGoPath(ctx *cli.Context, isTest bool) {
installRepoPath = doc.HomeDir + "/repos" installRepoPath = doc.HomeDir + "/repos"
if com.IsFile(curPath + "/" + doc.GopmFileName) { if com.IsFile(curPath + "/" + doc.GOPM_FILE_NAME) {
log.Trace("Loading gopmfile...") log.Trace("Loading gopmfile...")
gf := doc.NewGopmfile(curPath) gf := doc.NewGopmfile(curPath)

1
cmd/run.go

@ -16,6 +16,7 @@ package cmd
import ( import (
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/gpmgo/gopm/log" "github.com/gpmgo/gopm/log"
) )

1
cmd/test.go

@ -16,6 +16,7 @@ package cmd
import ( import (
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/gpmgo/gopm/log" "github.com/gpmgo/gopm/log"
) )

2
cmd/update.go

@ -127,7 +127,7 @@ func runUpdate(ctx *cli.Context) {
}() }()
// Check if previous steps were successful. // Check if previous steps were successful.
if com.IsFile(doc.GopmFileName) { if com.IsFile(doc.GOPM_FILE_NAME) {
log.Trace("Loading gopmfile...") log.Trace("Loading gopmfile...")
gf := doc.NewGopmfile(".") gf := doc.NewGopmfile(".")

24
doc/conf.go

@ -17,6 +17,7 @@ package doc
import ( import (
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
"github.com/Unknwon/com" "github.com/Unknwon/com"
@ -26,8 +27,8 @@ import (
) )
const ( const (
GopmFileName = ".gopmfile" GOPM_FILE_NAME = ".gopmfile"
RawHomeDir = "~/.gopm" RawHomeDir = "~/.gopm"
) )
var ( var (
@ -46,13 +47,16 @@ func init() {
HomeDir = strings.Replace(RawHomeDir, "~", hd, -1) HomeDir = strings.Replace(RawHomeDir, "~", hd, -1)
LoadLocalNodes() LoadLocalNodes()
LoadPkgNameList(HomeDir + "/data/pkgname.list")
} }
// NewGopmfile loads gopmgile in given directory.
func NewGopmfile(dirPath string) *goconfig.ConfigFile { func NewGopmfile(dirPath string) *goconfig.ConfigFile {
gf, err := goconfig.LoadConfigFile(path.Join(dirPath, GopmFileName)) dirPath, _ = filepath.Abs(dirPath)
gf, err := goconfig.LoadConfigFile(path.Join(dirPath, GOPM_FILE_NAME))
if err != nil { if err != nil {
log.Error("", "Fail to load gopmfile") log.Error("", "Fail to load gopmfile:")
log.Fatal("", err.Error()) log.Fatal("", "\t"+err.Error())
} }
return gf return gf
} }
@ -85,6 +89,16 @@ func LoadPkgNameList(filePath string) {
} }
} }
func GetPkgFullPath(short string) string {
name, ok := PackageNameList[short]
if !ok {
log.Error("", "Invalid package name")
log.Error("", "No match in the package name list:")
log.Fatal("", "\t"+short)
}
return name
}
func LoadLocalNodes() { func LoadLocalNodes() {
if !com.IsDir(HomeDir + "/data") { if !com.IsDir(HomeDir + "/data") {
os.MkdirAll(HomeDir+"/data", os.ModePerm) os.MkdirAll(HomeDir+"/data", os.ModePerm)

10
doc/struct.go

@ -36,6 +36,7 @@ const (
type Pkg struct { type Pkg struct {
ImportPath string ImportPath string
RootPath string
Type string Type string
Value string // Branch, tag or commit. Value string // Branch, tag or commit.
} }
@ -48,7 +49,7 @@ func (pkg *Pkg) VerString() string {
} }
func NewPkg(importPath, tp, value string) *Pkg { func NewPkg(importPath, tp, value string) *Pkg {
return &Pkg{importPath, tp, value} return &Pkg{importPath, "", tp, value}
} }
func NewDefaultPkg(importPath string) *Pkg { func NewDefaultPkg(importPath string) *Pkg {
@ -65,9 +66,10 @@ type Node struct {
func NewNode(importPath, downloadUrl, tp, value string, isGetDeps bool) *Node { func NewNode(importPath, downloadUrl, tp, value string, isGetDeps bool) *Node {
return &Node{ return &Node{
Pkg: Pkg{ImportPath: importPath, Pkg: Pkg{
Type: tp, ImportPath: importPath,
Value: value, Type: tp,
Value: value,
}, },
DownloadURL: downloadUrl, DownloadURL: downloadUrl,
IsGetDeps: isGetDeps, IsGetDeps: isGetDeps,

7
log/log.go

@ -14,6 +14,7 @@
// +build !windows // +build !windows
// Package log provides npm-like style log output.
package log package log
import ( import (
@ -58,3 +59,9 @@ func Message(hl, msg string) {
} }
fmt.Printf("gopm %s%s %s\n", brush.Yellow("MSG!"), hl, msg) fmt.Printf("gopm %s%s %s\n", brush.Yellow("MSG!"), hl, msg)
} }
func Help(format string, args ...interface{}) {
fmt.Printf("gopm %s %s\n", brush.Cyan("HELP"),
fmt.Sprintf(format, args...))
os.Exit(2)
}

1
log/log_windows.go

@ -12,6 +12,7 @@
// License for the specific language governing permissions and limitations // License for the specific language governing permissions and limitations
// under the License. // under the License.
// Package log provides npm-like style log output.
package log package log
import ( import (

Loading…
Cancel
Save