From c3093cb6a89f5ba34d979c028eede3e986d73c93 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 21 May 2013 19:07:25 -0400 Subject: [PATCH] v0.1.0 build 0521 --- .gitignore | 2 ++ README.md | 1 - conf/gpm.toml | 2 +- doc/bitbucket.go | 9 ++++----- doc/github.go | 9 ++++----- doc/google.go | 9 ++++----- doc/launchpad.go | 9 ++++----- doc/struct.go | 16 +++++----------- gpm.go | 38 ++++++++++++++++++++++++++++++++++---- install.go | 44 +++++++++++++++++++++++++++++++++++--------- 10 files changed, 93 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index d184396f0..9997532ad 100644 --- a/.gitignore +++ b/.gitignore @@ -20,5 +20,7 @@ _cgo_export.* _testmain.go *.exe + gpm +data/ repo/ \ No newline at end of file diff --git a/README.md b/README.md index 9ae9bc79a..42aa62c83 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ gpm(Go Package Manager) is a Go package manage tool for search, install, update ## Todo -- Save node information after downloaded, and check for next time, reduce download times. - All errors should have specific title for exactly where were created. - Add i18n support for all strings. - Add gpm working principle design. diff --git a/conf/gpm.toml b/conf/gpm.toml index 8a3f7687f..6cf9be447 100644 --- a/conf/gpm.toml +++ b/conf/gpm.toml @@ -1,7 +1,7 @@ # This is a configuration file for gpm with toml format. title = "gpm(Go Package Manager)" -version = "v0.0.6 Build 0521" +version = "v0.1.0 Build 0521" username = "" password = "" user_language = "en-US" \ No newline at end of file diff --git a/doc/bitbucket.go b/doc/bitbucket.go index ed26adcf2..a206844a0 100644 --- a/doc/bitbucket.go +++ b/doc/bitbucket.go @@ -22,7 +22,7 @@ var ( ) // GetBitbucketDoc downloads tarball from bitbucket.org. -func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH, commit string, cmdFlags map[string]bool) (*Package, []string, error) { +func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH, commit string, cmdFlags map[string]bool) (*Node, []string, error) { // Check version control. if m := bitbucketEtagRe.FindStringSubmatch(commit); m != nil { match["vcs"] = m[1] @@ -141,9 +141,8 @@ func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH } } - pkg := &Package{ - ImportPath: match["importPath"], - AbsPath: installPath, + node := &Node{ + ImportPath: projectPath, Commit: commit, } @@ -160,7 +159,7 @@ func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH } } - return pkg, imports, err + return node, imports, err } // checkDir checks if current directory has been saved. diff --git a/doc/github.go b/doc/github.go index a163222b0..04ba6e489 100644 --- a/doc/github.go +++ b/doc/github.go @@ -26,7 +26,7 @@ func SetGithubCredentials(id, secret string) { } // GetGithubDoc downloads tarball from github.com. -func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH, commit string, cmdFlags map[string]bool) (*Package, []string, error) { +func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH, commit string, cmdFlags map[string]bool) (*Node, []string, error) { SetGithubCredentials("1862bcb265171f37f36c", "308d71ab53ccd858416cfceaed52d5d5b7d53c5f") match["cred"] = githubCred @@ -134,9 +134,8 @@ func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH, c } } - pkg := &Package{ - ImportPath: match["importPath"], - AbsPath: installPath, + node := &Node{ + ImportPath: projectPath, Commit: commit, } @@ -153,5 +152,5 @@ func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH, c } } - return pkg, imports, err + return node, imports, err } diff --git a/doc/google.go b/doc/google.go index 303192d08..3cc0c1b3b 100644 --- a/doc/google.go +++ b/doc/google.go @@ -48,7 +48,7 @@ func getGoogleVCS(client *http.Client, match map[string]string) error { } // GetGoogleDoc downloads raw files from code.google.com. -func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH, commit string, cmdFlags map[string]bool) (*Package, []string, error) { +func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH, commit string, cmdFlags map[string]bool) (*Node, []string, error) { setupGoogleMatch(match) // Check version control. if m := googleEtagRe.FindStringSubmatch(commit); m != nil { @@ -132,9 +132,8 @@ func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH, c return nil, nil, err } - pkg := &Package{ - ImportPath: match["importPath"], - AbsPath: installPath, + node := &Node{ + ImportPath: projectPath, Commit: commit, } var imports []string @@ -164,7 +163,7 @@ func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH, c } } - return pkg, imports, err + return node, imports, err } func downloadFiles(client *http.Client, match map[string]string, rootPath, installPath, commit string, dirs []string) error { diff --git a/doc/launchpad.go b/doc/launchpad.go index e49fa728f..207d36c88 100644 --- a/doc/launchpad.go +++ b/doc/launchpad.go @@ -19,7 +19,7 @@ import ( var LaunchpadPattern = regexp.MustCompile(`^launchpad\.net/(?P(?P[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-/]+)*$`) // GetLaunchpadDoc downloads tarball from launchpad.net. -func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH, commit string, cmdFlags map[string]bool) (*Package, []string, error) { +func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH, commit string, cmdFlags map[string]bool) (*Node, []string, error) { if match["project"] != "" && match["series"] != "" { rc, err := httpGet(client, expand("https://code.launchpad.net/{project}{series}/.bzr/branch-format", match), nil) @@ -120,9 +120,8 @@ func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH } } - pkg := &Package{ - ImportPath: match["importPath"], - AbsPath: installPath, + node := &Node{ + ImportPath: projectPath, Commit: commit, } @@ -139,5 +138,5 @@ func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH } } - return pkg, imports, err + return node, imports, err } diff --git a/doc/struct.go b/doc/struct.go index d92bf03a6..5df542dbf 100644 --- a/doc/struct.go +++ b/doc/struct.go @@ -10,17 +10,11 @@ import ( "time" ) -// Package represents a package. -type Package struct { - // Package import path. - ImportPath string - AbsPath string - - // Revision tag and project tags. - Commit string - - // Imports. - Imports []string +// Node represents a node structure. +type Node struct { + ImportPath string `json:"import_path"` + Commit string + Date string } // source is source code file. diff --git a/gpm.go b/gpm.go index 1d21a97d1..d96bf368e 100644 --- a/gpm.go +++ b/gpm.go @@ -7,6 +7,7 @@ package main import ( + "encoding/json" "fmt" "io" "os" @@ -20,11 +21,14 @@ import ( "unicode/utf8" "github.com/BurntSushi/toml" + "github.com/GPMGo/gpm/doc" + "github.com/GPMGo/gpm/utils" ) var ( - config tomlConfig - appPath string // Application path. + config tomlConfig + appPath string // Application path. + localNodes []*doc.Node ) type tomlConfig struct { @@ -100,7 +104,7 @@ func getAppPath() bool { } // loadUsage loads usage according to user language. -func loadUsage(lang, appPath string) bool { +func loadUsage(lang string) bool { // Load main usage. f, err := os.Open(appPath + "i18n/" + lang + "/usage.tpl") if err != nil { @@ -139,6 +143,27 @@ func loadUsage(lang, appPath string) bool { return true } +// loadLocalNodes loads nodes information from local file system. +func loadLocalNodes() bool { + if !utils.IsExist(appPath + "data/nodes.json") { + os.MkdirAll(appPath+"data/", os.ModePerm) + } else { + fr, err := os.Open(appPath + "data/nodes.json") + if err != nil { + fmt.Println(err) + return false + } + defer fr.Close() + + err = json.NewDecoder(fr).Decode(&localNodes) + if err != nil && err != io.EOF { + fmt.Println(err) + return false + } + } + return true +} + // We don't use init() to initialize // bacause we need to get execute path in runtime. func initialize() bool { @@ -154,7 +179,7 @@ func initialize() bool { } // Load usages by language. - if !loadUsage(config.Lang, appPath) { + if !loadUsage(config.Lang) { return false } @@ -162,6 +187,11 @@ func initialize() bool { os.MkdirAll(appPath+"repo/bundles/", os.ModePerm) os.MkdirAll(appPath+"repo/snapshots/", os.ModePerm) + // Initialize local nodes. + if !loadLocalNodes() { + return false + } + return true } diff --git a/install.go b/install.go index 565608ccc..59540efd7 100644 --- a/install.go +++ b/install.go @@ -5,8 +5,10 @@ package main import ( + "encoding/json" "fmt" "net/http" + "os" "os/exec" "regexp" "strings" @@ -126,6 +128,16 @@ func runInstall(cmd *Command, args []string) { cmdArgs[1] = k executeGoCommand(cmdArgs) } + + // Save local nodes to file. + fw, err := os.Create(appPath + "data/nodes.json") + if err != nil { + fmt.Println(err) + return + } + defer fw.Close() + fbytes, _ := json.MarshalIndent(&localNodes, "", "\t") + fw.Write(fbytes) } fmt.Println("Well done.") @@ -146,17 +158,17 @@ func downloadPackages(pkgs, commits []string) { case utils.IsValidRemotePath(p): if !downloadCache[p] { // Download package. - pkg, imports := downloadPackage(p, commits[i]) + node, imports := downloadPackage(p, commits[i]) if len(imports) > 0 { // Need to download dependencies. tags := make([]string, len(imports)) downloadPackages(imports, tags) - continue } // Only save package information with specific commit. - if pkg != nil { - // Save record in local database. + if node != nil { + // Save record in local nodes. + saveNode(node) //fmt.Printf("Saved information: %s:%s.\n", pkg.ImportPath, pkg.Commit) } } else { @@ -169,8 +181,22 @@ func downloadPackages(pkgs, commits []string) { } } +// saveNode saves node into local nodes. +func saveNode(n *doc.Node) { + // Check if this node exists. + for _, v := range localNodes { + if n.ImportPath == v.ImportPath { + v = n + return + } + } + + // Add new node. + localNodes = append(localNodes, n) +} + // downloadPackage download package either use version control tools or not. -func downloadPackage(path, commit string) (pkg *doc.Package, imports []string) { +func downloadPackage(path, commit string) (node *doc.Node, imports []string) { // Check if use version control tools. switch { case !cmdInstall.Flags["-p"] && @@ -191,13 +217,13 @@ func downloadPackage(path, commit string) (pkg *doc.Package, imports []string) { downloadCache[path] = true var err error - pkg, imports, err = pureDownload(path, commit) + node, imports, err = pureDownload(path, commit) if err != nil { fmt.Printf("Fail to download package(%s) with error: %s.\n", path, err) return nil, nil } - return pkg, imports + return node, imports } } @@ -218,7 +244,7 @@ func checkGoGetFlags() (args []string) { type service struct { pattern *regexp.Regexp prefix string - get func(*http.Client, map[string]string, string, string, map[string]bool) (*doc.Package, []string, error) + get func(*http.Client, map[string]string, string, string, map[string]bool) (*doc.Node, []string, error) } // services is the list of source code control services handled by gopkgdoc. @@ -230,7 +256,7 @@ var services = []*service{ } // pureDownload downloads package without version control. -func pureDownload(path, commit string) (pinfo *doc.Package, imports []string, err error) { +func pureDownload(path, commit string) (*doc.Node, []string, error) { for _, s := range services { if s.get == nil || !strings.HasPrefix(path, s.prefix) { continue