Browse Source

v0.1.0 build 0521

pull/103/head
Unknown 12 years ago
parent
commit
c3093cb6a8
  1. 2
      .gitignore
  2. 1
      README.md
  3. 2
      conf/gpm.toml
  4. 9
      doc/bitbucket.go
  5. 9
      doc/github.go
  6. 9
      doc/google.go
  7. 9
      doc/launchpad.go
  8. 16
      doc/struct.go
  9. 38
      gpm.go
  10. 44
      install.go

2
.gitignore vendored

@ -20,5 +20,7 @@ _cgo_export.*
_testmain.go _testmain.go
*.exe *.exe
gpm gpm
data/
repo/ repo/

1
README.md

@ -12,7 +12,6 @@ gpm(Go Package Manager) is a Go package manage tool for search, install, update
## Todo ## 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. - All errors should have specific title for exactly where were created.
- Add i18n support for all strings. - Add i18n support for all strings.
- Add gpm working principle design. - Add gpm working principle design.

2
conf/gpm.toml

@ -1,7 +1,7 @@
# This is a configuration file for gpm with toml format. # This is a configuration file for gpm with toml format.
title = "gpm(Go Package Manager)" title = "gpm(Go Package Manager)"
version = "v0.0.6 Build 0521" version = "v0.1.0 Build 0521"
username = "" username = ""
password = "" password = ""
user_language = "en-US" user_language = "en-US"

9
doc/bitbucket.go

@ -22,7 +22,7 @@ var (
) )
// GetBitbucketDoc downloads tarball from bitbucket.org. // 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. // Check version control.
if m := bitbucketEtagRe.FindStringSubmatch(commit); m != nil { if m := bitbucketEtagRe.FindStringSubmatch(commit); m != nil {
match["vcs"] = m[1] match["vcs"] = m[1]
@ -141,9 +141,8 @@ func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH
} }
} }
pkg := &Package{ node := &Node{
ImportPath: match["importPath"], ImportPath: projectPath,
AbsPath: installPath,
Commit: commit, 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. // checkDir checks if current directory has been saved.

9
doc/github.go

@ -26,7 +26,7 @@ func SetGithubCredentials(id, secret string) {
} }
// GetGithubDoc downloads tarball from github.com. // 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") SetGithubCredentials("1862bcb265171f37f36c", "308d71ab53ccd858416cfceaed52d5d5b7d53c5f")
match["cred"] = githubCred match["cred"] = githubCred
@ -134,9 +134,8 @@ func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH, c
} }
} }
pkg := &Package{ node := &Node{
ImportPath: match["importPath"], ImportPath: projectPath,
AbsPath: installPath,
Commit: commit, 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
} }

9
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. // 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) setupGoogleMatch(match)
// Check version control. // Check version control.
if m := googleEtagRe.FindStringSubmatch(commit); m != nil { 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 return nil, nil, err
} }
pkg := &Package{ node := &Node{
ImportPath: match["importPath"], ImportPath: projectPath,
AbsPath: installPath,
Commit: commit, Commit: commit,
} }
var imports []string 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 { func downloadFiles(client *http.Client, match map[string]string, rootPath, installPath, commit string, dirs []string) error {

9
doc/launchpad.go

@ -19,7 +19,7 @@ import (
var LaunchpadPattern = regexp.MustCompile(`^launchpad\.net/(?P<repo>(?P<project>[a-z0-9A-Z_.\-]+)(?P<series>/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(?P<dir>/[a-z0-9A-Z_.\-/]+)*$`) var LaunchpadPattern = regexp.MustCompile(`^launchpad\.net/(?P<repo>(?P<project>[a-z0-9A-Z_.\-]+)(?P<series>/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(?P<dir>/[a-z0-9A-Z_.\-/]+)*$`)
// GetLaunchpadDoc downloads tarball from launchpad.net. // 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"] != "" { if match["project"] != "" && match["series"] != "" {
rc, err := httpGet(client, expand("https://code.launchpad.net/{project}{series}/.bzr/branch-format", match), nil) 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{ node := &Node{
ImportPath: match["importPath"], ImportPath: projectPath,
AbsPath: installPath,
Commit: commit, Commit: commit,
} }
@ -139,5 +138,5 @@ func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH
} }
} }
return pkg, imports, err return node, imports, err
} }

16
doc/struct.go

@ -10,17 +10,11 @@ import (
"time" "time"
) )
// Package represents a package. // Node represents a node structure.
type Package struct { type Node struct {
// Package import path. ImportPath string `json:"import_path"`
ImportPath string Commit string
AbsPath string Date string
// Revision tag and project tags.
Commit string
// Imports.
Imports []string
} }
// source is source code file. // source is source code file.

38
gpm.go

@ -7,6 +7,7 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -20,11 +21,14 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/GPMGo/gpm/doc"
"github.com/GPMGo/gpm/utils"
) )
var ( var (
config tomlConfig config tomlConfig
appPath string // Application path. appPath string // Application path.
localNodes []*doc.Node
) )
type tomlConfig struct { type tomlConfig struct {
@ -100,7 +104,7 @@ func getAppPath() bool {
} }
// loadUsage loads usage according to user language. // loadUsage loads usage according to user language.
func loadUsage(lang, appPath string) bool { func loadUsage(lang string) bool {
// Load main usage. // Load main usage.
f, err := os.Open(appPath + "i18n/" + lang + "/usage.tpl") f, err := os.Open(appPath + "i18n/" + lang + "/usage.tpl")
if err != nil { if err != nil {
@ -139,6 +143,27 @@ func loadUsage(lang, appPath string) bool {
return true 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 // We don't use init() to initialize
// bacause we need to get execute path in runtime. // bacause we need to get execute path in runtime.
func initialize() bool { func initialize() bool {
@ -154,7 +179,7 @@ func initialize() bool {
} }
// Load usages by language. // Load usages by language.
if !loadUsage(config.Lang, appPath) { if !loadUsage(config.Lang) {
return false return false
} }
@ -162,6 +187,11 @@ func initialize() bool {
os.MkdirAll(appPath+"repo/bundles/", os.ModePerm) os.MkdirAll(appPath+"repo/bundles/", os.ModePerm)
os.MkdirAll(appPath+"repo/snapshots/", os.ModePerm) os.MkdirAll(appPath+"repo/snapshots/", os.ModePerm)
// Initialize local nodes.
if !loadLocalNodes() {
return false
}
return true return true
} }

44
install.go

@ -5,8 +5,10 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"os"
"os/exec" "os/exec"
"regexp" "regexp"
"strings" "strings"
@ -126,6 +128,16 @@ func runInstall(cmd *Command, args []string) {
cmdArgs[1] = k cmdArgs[1] = k
executeGoCommand(cmdArgs) 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.") fmt.Println("Well done.")
@ -146,17 +158,17 @@ func downloadPackages(pkgs, commits []string) {
case utils.IsValidRemotePath(p): case utils.IsValidRemotePath(p):
if !downloadCache[p] { if !downloadCache[p] {
// Download package. // Download package.
pkg, imports := downloadPackage(p, commits[i]) node, imports := downloadPackage(p, commits[i])
if len(imports) > 0 { if len(imports) > 0 {
// Need to download dependencies. // Need to download dependencies.
tags := make([]string, len(imports)) tags := make([]string, len(imports))
downloadPackages(imports, tags) downloadPackages(imports, tags)
continue
} }
// Only save package information with specific commit. // Only save package information with specific commit.
if pkg != nil { if node != nil {
// Save record in local database. // Save record in local nodes.
saveNode(node)
//fmt.Printf("Saved information: %s:%s.\n", pkg.ImportPath, pkg.Commit) //fmt.Printf("Saved information: %s:%s.\n", pkg.ImportPath, pkg.Commit)
} }
} else { } 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. // 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. // Check if use version control tools.
switch { switch {
case !cmdInstall.Flags["-p"] && case !cmdInstall.Flags["-p"] &&
@ -191,13 +217,13 @@ func downloadPackage(path, commit string) (pkg *doc.Package, imports []string) {
downloadCache[path] = true downloadCache[path] = true
var err error var err error
pkg, imports, err = pureDownload(path, commit) node, imports, err = pureDownload(path, commit)
if err != nil { if err != nil {
fmt.Printf("Fail to download package(%s) with error: %s.\n", path, err) fmt.Printf("Fail to download package(%s) with error: %s.\n", path, err)
return nil, nil return nil, nil
} }
return pkg, imports return node, imports
} }
} }
@ -218,7 +244,7 @@ func checkGoGetFlags() (args []string) {
type service struct { type service struct {
pattern *regexp.Regexp pattern *regexp.Regexp
prefix string 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. // 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. // 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 { for _, s := range services {
if s.get == nil || !strings.HasPrefix(path, s.prefix) { if s.get == nil || !strings.HasPrefix(path, s.prefix) {
continue continue

Loading…
Cancel
Save