Browse Source

Use common node package.

pull/103/head
Ewan Chou 12 years ago
parent
commit
fdc115eeae
  1. 5
      cmd/check.go
  2. 53
      cmd/install.go
  3. 30
      cmd/remove.go
  4. 3
      cmd/struct.go
  5. 21
      doc/bitbucket.go
  6. 21
      doc/github.go
  7. 23
      doc/google.go
  8. 13
      doc/launchpad.go
  9. 10
      doc/struct.go
  10. 12
      gopm.go

5
cmd/check.go

@ -11,6 +11,7 @@ import (
"github.com/GPMGo/gopm/doc"
"github.com/GPMGo/gopm/utils"
"github.com/GPMGo/node"
)
var CmdCheck = &Command{
@ -100,9 +101,9 @@ func runCheck(cmd *Command, args []string) {
installGOPATH = utils.GetBestMatchGOPATH(AppPath)
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("%s\n", PromptMsg["DownloadPath"]), installGOPATH))
// Generate temporary nodes.
nodes := make([]*doc.Node, len(uninstallList))
nodes := make([]*node.Node, len(uninstallList))
for i := range nodes {
nodes[i] = new(doc.Node)
nodes[i] = new(node.Node)
nodes[i].ImportPath = uninstallList[i]
}
// Download packages.

53
cmd/install.go

@ -16,6 +16,7 @@ import (
"github.com/GPMGo/gopm/doc"
"github.com/GPMGo/gopm/utils"
"github.com/GPMGo/node"
)
var (
@ -123,9 +124,9 @@ func runInstall(cmd *Command, args []string) {
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("%s\n", PromptMsg["DownloadPath"]), installGOPATH))
// Generate temporary nodes.
nodes := make([]*doc.Node, len(args))
nodes := make([]*node.Node, len(args))
for i := range nodes {
nodes[i] = new(doc.Node)
nodes[i] = new(node.Node)
nodes[i].ImportPath = args[i]
}
// Download packages.
@ -167,7 +168,7 @@ func runInstall(cmd *Command, args []string) {
}
// chekcDeps checks dependencies of nodes.
func chekcDeps(nodes []*doc.Node) (depnodes []*doc.Node) {
func chekcDeps(nodes []*node.Node) (depnodes []*node.Node) {
for _, n := range nodes {
// Make sure it will not download all dependencies automatically.
if len(n.Value) == 0 {
@ -180,7 +181,7 @@ func chekcDeps(nodes []*doc.Node) (depnodes []*doc.Node) {
}
// checkLocalBundles checks if the bundle is in local file system.
func checkLocalBundles(bundle string) (nodes []*doc.Node) {
func checkLocalBundles(bundle string) (nodes []*node.Node) {
for _, b := range LocalBundles {
if bundle == b.Name {
nodes = append(nodes, chekcDeps(b.Nodes)...)
@ -193,7 +194,7 @@ func checkLocalBundles(bundle string) (nodes []*doc.Node) {
// 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(nodes []*node.Node) {
// Check all packages, they may be bundles, snapshots or raw packages path.
for _, n := range nodes {
// Check if it is a bundle or snapshot.
@ -225,22 +226,22 @@ func downloadPackages(nodes []*doc.Node) {
case utils.IsValidRemotePath(n.ImportPath):
if !downloadCache[n.ImportPath] {
// Download package.
node, imports := downloadPackage(n)
nod, imports := downloadPackage(n)
if len(imports) > 0 {
// Need to download dependencies.
// Generate temporary nodes.
nodes := make([]*doc.Node, len(imports))
nodes := make([]*node.Node, len(imports))
for i := range nodes {
nodes[i] = new(doc.Node)
nodes[i] = new(node.Node)
nodes[i].ImportPath = imports[i]
}
downloadPackages(nodes)
}
// Only save package information with specific commit.
if node != nil {
if nod != nil {
// Save record in local nodes.
saveNode(node)
saveNode(nod)
}
} else {
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["SkipDownloaded"]), n.ImportPath)
@ -253,7 +254,7 @@ func downloadPackages(nodes []*doc.Node) {
}
// saveNode saves node into local nodes.
func saveNode(n *doc.Node) {
func saveNode(n *node.Node) {
// Node dependencies list.
n.Deps = nil
@ -270,14 +271,14 @@ func saveNode(n *doc.Node) {
}
// downloadPackage downloads package either use version control tools or not.
func downloadPackage(node *doc.Node) (*doc.Node, []string) {
func downloadPackage(nod *node.Node) (*node.Node, []string) {
// Check if use version control tools.
switch {
case CmdInstall.Flags["-v"] &&
((node.ImportPath[0] == 'g' && isHasGit) || (node.ImportPath[0] == 'c' && isHasHg)): // github.com, code.google.com
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["InstallByGoGet"]), node.ImportPath)
((nod.ImportPath[0] == 'g' && isHasGit) || (nod.ImportPath[0] == 'c' && isHasHg)): // github.com, code.google.com
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["InstallByGoGet"]), nod.ImportPath)
args := checkGoGetFlags()
args = append(args, node.ImportPath)
args = append(args, nod.ImportPath)
executeCommand("go", args)
return nil, nil
default: // Pure download.
@ -286,18 +287,18 @@ func downloadPackage(node *doc.Node) (*doc.Node, []string) {
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["NoVCSTool"]))
}
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["DownloadStatus"]), node.ImportPath)
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["DownloadStatus"]), nod.ImportPath)
// Mark as donwloaded.
downloadCache[node.ImportPath] = true
downloadCache[nod.ImportPath] = true
imports, err := pureDownload(node)
imports, err := pureDownload(nod)
if err != nil {
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] %s\n", PromptMsg["DownloadError"]), node.ImportPath, err))
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] %s\n", PromptMsg["DownloadError"]), nod.ImportPath, err))
return nil, nil
}
return node, imports
return nod, imports
}
}
@ -318,7 +319,7 @@ func checkGoGetFlags() (args []string) {
type service struct {
pattern *regexp.Regexp
prefix string
get func(*http.Client, map[string]string, string, *doc.Node, map[string]bool) ([]string, error)
get func(*http.Client, map[string]string, string, *node.Node, map[string]bool) ([]string, error)
}
// services is the list of source code control services handled by gopkgdoc.
@ -330,12 +331,12 @@ var services = []*service{
}
// pureDownload downloads package without version control.
func pureDownload(node *doc.Node) ([]string, error) {
func pureDownload(nod *node.Node) ([]string, error) {
for _, s := range services {
if s.get == nil || !strings.HasPrefix(node.ImportPath, s.prefix) {
if s.get == nil || !strings.HasPrefix(nod.ImportPath, s.prefix) {
continue
}
m := s.pattern.FindStringSubmatch(node.ImportPath)
m := s.pattern.FindStringSubmatch(nod.ImportPath)
if m == nil {
if s.prefix != "" {
return nil,
@ -343,13 +344,13 @@ func pureDownload(node *doc.Node) ([]string, error) {
}
continue
}
match := map[string]string{"importPath": node.ImportPath}
match := map[string]string{"importPath": nod.ImportPath}
for i, n := range s.pattern.SubexpNames() {
if n != "" {
match[n] = m[i]
}
}
return s.get(doc.HttpClient, match, installGOPATH, node, CmdInstall.Flags)
return s.get(doc.HttpClient, match, installGOPATH, nod, CmdInstall.Flags)
}
return nil, errors.New(fmt.Sprintf("%s", PromptMsg["NotFoundError"]))
}

30
cmd/remove.go

@ -11,8 +11,8 @@ import (
"runtime"
"strings"
"github.com/GPMGo/gopm/doc"
"github.com/GPMGo/gopm/utils"
"github.com/GPMGo/node"
)
var (
@ -36,9 +36,9 @@ func runRemove(cmd *Command, args []string) {
}
// Generate temporary nodes.
nodes := make([]*doc.Node, len(args))
nodes := make([]*node.Node, len(args))
for i := range nodes {
nodes[i] = new(doc.Node)
nodes[i] = new(node.Node)
nodes[i].ImportPath = args[i]
}
@ -61,7 +61,7 @@ func runRemove(cmd *Command, args []string) {
}
// removePackages removes packages from local file system.
func removePackages(nodes []*doc.Node) {
func removePackages(nodes []*node.Node) {
// Check all packages, they may be bundles, snapshots or raw packages path.
for _, n := range nodes {
// Check if it is a bundle or snapshot.
@ -92,14 +92,14 @@ func removePackages(nodes []*doc.Node) {
case utils.IsValidRemotePath(n.ImportPath):
if !removeCache[n.ImportPath] {
// Remove package.
node, imports := removePackage(n)
nod, imports := removePackage(n)
if len(imports) > 0 {
fmt.Println("Check denpendencies for removing package has not been supported.")
}
// Remove record in local nodes.
if node != nil {
removeNode(node)
if nod != nil {
removeNode(nod)
}
}
default:
@ -110,7 +110,7 @@ func removePackages(nodes []*doc.Node) {
}
// removeNode removes node from local nodes.
func removeNode(n *doc.Node) {
func removeNode(n *node.Node) {
// Check if this node exists.
for i, v := range LocalNodes {
if n.ImportPath == v.ImportPath {
@ -121,17 +121,17 @@ func removeNode(n *doc.Node) {
}
// removePackage removes package from local file system.
func removePackage(node *doc.Node) (*doc.Node, []string) {
func removePackage(nod *node.Node) (*node.Node, []string) {
// Find package in GOPATH.
paths := utils.GetGOPATH()
for _, p := range paths {
absPath := p + "/src/" + utils.GetProjectPath(node.ImportPath) + "/"
absPath := p + "/src/" + utils.GetProjectPath(nod.ImportPath) + "/"
if utils.IsExist(absPath) {
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["RemovePackage"]), node.ImportPath)
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["RemovePackage"]), nod.ImportPath)
// Remove files.
os.RemoveAll(absPath)
// Remove file in GOPATH/bin
proName := utils.GetExecuteName(node.ImportPath)
proName := utils.GetExecuteName(nod.ImportPath)
paths := utils.GetGOPATH()
var gopath string
@ -142,15 +142,15 @@ func removePackage(node *doc.Node) (*doc.Node, []string) {
}
}
pkgList := []string{node.ImportPath}
pkgList := []string{nod.ImportPath}
removePackageFiles(gopath, pkgList)
return node, nil
return nod, nil
}
}
// Cannot find package.
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["PackageNotFound"]), node.ImportPath)
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["PackageNotFound"]), nod.ImportPath)
return nil, nil
}

3
cmd/struct.go

@ -12,6 +12,7 @@ import (
"strings"
"github.com/GPMGo/gopm/doc"
"github.com/GPMGo/node"
)
var (
@ -20,7 +21,7 @@ var (
)
var (
LocalNodes []*doc.Node
LocalNodes []*node.Node
LocalBundles []*doc.Bundle
)

21
doc/bitbucket.go

@ -17,6 +17,7 @@ import (
"strings"
"github.com/GPMGo/gopm/utils"
"github.com/GPMGo/node"
)
var (
@ -25,9 +26,9 @@ var (
)
// GetBitbucketDoc downloads tarball from bitbucket.org.
func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH string, node *Node, cmdFlags map[string]bool) ([]string, error) {
func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH string, nod *node.Node, cmdFlags map[string]bool) ([]string, error) {
// Check version control.
if m := bitbucketEtagRe.FindStringSubmatch(node.Value); m != nil {
if m := bitbucketEtagRe.FindStringSubmatch(nod.Value); m != nil {
match["vcs"] = m[1]
} else {
var repo struct {
@ -41,10 +42,10 @@ func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH
// bundle and snapshot will have commit 'B' and 'S',
// but does not need to download dependencies.
isCheckImport := len(node.Value) == 0
isCheckImport := len(nod.Value) == 0
switch {
case isCheckImport || len(node.Value) == 1:
case isCheckImport || len(nod.Value) == 1:
// Get up-to-date version.
tags := make(map[string]string)
for _, nodeType := range []string{"branches", "tags"} {
@ -66,15 +67,15 @@ func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH
return nil, err
}
node.Type = "commit"
node.Value = match["commit"]
nod.Type = "commit"
nod.Value = match["commit"]
case !isCheckImport: // Bundle or snapshot.
// Check downlaod type.
switch node.Type {
switch nod.Type {
case "tag", "commit", "branch":
match["commit"] = node.Value
match["commit"] = nod.Value
default:
return nil, errors.New("Unknown node type: " + node.Type)
return nil, errors.New("Unknown node type: " + nod.Type)
}
}
@ -90,7 +91,7 @@ func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH
projectPath := expand("bitbucket.org/{owner}/{repo}", match)
installPath := installGOPATH + "/src/" + projectPath
node.ImportPath = projectPath
nod.ImportPath = projectPath
// Remove old files.
os.RemoveAll(installPath + "/")

21
doc/github.go

@ -16,6 +16,7 @@ import (
"strings"
"github.com/GPMGo/gopm/utils"
"github.com/GPMGo/node"
)
var (
@ -35,7 +36,7 @@ func SetGithubCredentials(token string) {
}
// GetGithubDoc downloads tarball from github.com.
func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH string, node *Node, cmdFlags map[string]bool) ([]string, error) {
func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH string, nod *node.Node, cmdFlags map[string]bool) ([]string, error) {
match["cred"] = githubCred
// JSON struct for github.com.
@ -51,10 +52,10 @@ func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH st
// bundle and snapshot will have commit 'B' and 'S',
// but does not need to download dependencies.
isCheckImport := len(node.Value) == 0
isCheckImport := len(nod.Value) == 0
switch {
case isCheckImport || len(node.Value) == 1:
case isCheckImport || len(nod.Value) == 1:
// Get up-to-date version.
err := httpGetJSON(client, expand("https://api.github.com/repos/{owner}/{repo}/git/refs?{cred}", match), &refs)
if err != nil {
@ -77,15 +78,15 @@ func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH st
return nil, err
}
node.Type = "commit"
node.Value = match["sha"]
nod.Type = "commit"
nod.Value = match["sha"]
case !isCheckImport: // Bundle or snapshot.
// Check downlaod type.
switch node.Type {
switch nod.Type {
case "tag", "commit", "branch":
match["sha"] = node.Value
match["sha"] = nod.Value
default:
return nil, errors.New("Unknown node type: " + node.Type)
return nil, errors.New("Unknown node type: " + nod.Type)
}
}
@ -100,13 +101,13 @@ func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH st
}
shaName := expand("{repo}-{sha}", match)
if node.Type == "tag" {
if nod.Type == "tag" {
shaName = strings.Replace(shaName, "-v", "-", 1)
}
projectPath := expand("github.com/{owner}/{repo}", match)
installPath := installGOPATH + "/src/" + projectPath
node.ImportPath = projectPath
nod.ImportPath = projectPath
// Remove old files.
os.RemoveAll(installPath + "/")

23
doc/google.go

@ -13,6 +13,7 @@ import (
"strings"
"github.com/GPMGo/gopm/utils"
"github.com/GPMGo/node"
)
var (
@ -50,10 +51,10 @@ 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 string, node *Node, cmdFlags map[string]bool) ([]string, error) {
func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH string, nod *node.Node, cmdFlags map[string]bool) ([]string, error) {
setupGoogleMatch(match)
// Check version control.
if m := googleEtagRe.FindStringSubmatch(node.Value); m != nil {
if m := googleEtagRe.FindStringSubmatch(nod.Value); m != nil {
match["vcs"] = m[1]
} else if err := getGoogleVCS(client, match); err != nil {
return nil, err
@ -61,15 +62,15 @@ func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH st
// bundle and snapshot will have commit 'B' and 'S',
// but does not need to download dependencies.
isCheckImport := len(node.Value) == 0
if len(node.Value) == 1 {
node.Value = ""
isCheckImport := len(nod.Value) == 0
if len(nod.Value) == 1 {
nod.Value = ""
}
rootPath := expand("http://{subrepo}{dot}{repo}.googlecode.com/{vcs}{dir}/", match)
// Scrape the repo browser to find the project revision and individual Go files.
p, err := HttpGetBytes(client, rootPath+"?r="+node.Value, nil)
p, err := HttpGetBytes(client, rootPath+"?r="+nod.Value, nil)
if err != nil {
return nil, err
}
@ -79,13 +80,13 @@ func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH st
return nil,
errors.New("doc.GetGoogleDoc(): Could not find revision for " + match["importPath"])
} else {
node.Type = "commit"
node.Value = string(m[1])
nod.Type = "commit"
nod.Value = string(m[1])
}
projectPath := expand("code.google.com/p/{repo}{dot}{subrepo}{dir}", match)
installPath := installGOPATH + "/src/" + projectPath
node.ImportPath = projectPath
nod.ImportPath = projectPath
// Remove old files.
os.RemoveAll(installPath + "/")
@ -105,7 +106,7 @@ func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH st
files = append(files, &source{
name: fname,
rawURL: expand("http://{subrepo}{dot}{repo}.googlecode.com/{vcs}{dir}/{0}", match, fname) + "?r=" + node.Value,
rawURL: expand("http://{subrepo}{dot}{repo}.googlecode.com/{vcs}{dir}/{0}", match, fname) + "?r=" + nod.Value,
})
}
@ -143,7 +144,7 @@ func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH st
}
}
err = downloadFiles(client, match, rootPath, installPath+"/", node.Value, dirs)
err = downloadFiles(client, match, rootPath, installPath+"/", nod.Value, dirs)
if err != nil {
return nil, err
}

13
doc/launchpad.go

@ -16,12 +16,13 @@ import (
"strings"
"github.com/GPMGo/gopm/utils"
"github.com/GPMGo/node"
)
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.
func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH string, node *Node, cmdFlags map[string]bool) ([]string, error) {
func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH string, nod *node.Node, cmdFlags map[string]bool) ([]string, error) {
if match["project"] != "" && match["series"] != "" {
rc, err := httpGet(client, expand("https://code.launchpad.net/{project}{series}/.bzr/branch-format", match), nil)
@ -40,15 +41,15 @@ func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH
// bundle and snapshot will have commit 'B' and 'S',
// but does not need to download dependencies.
isCheckImport := len(node.Value) == 0
isCheckImport := len(nod.Value) == 0
var downloadPath string
// Check if download with specific revision.
if isCheckImport || len(node.Value) == 1 {
if isCheckImport || len(nod.Value) == 1 {
downloadPath = expand("https://bazaar.launchpad.net/+branch/{repo}/tarball", match)
node.Type = "commit"
nod.Type = "commit"
} else {
downloadPath = expand("https://bazaar.launchpad.net/+branch/{repo}/tarball/"+node.Value, match)
downloadPath = expand("https://bazaar.launchpad.net/+branch/{repo}/tarball/"+nod.Value, match)
}
// Scrape the repo browser to find the project revision and individual Go files.
@ -59,7 +60,7 @@ func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH
projectPath := expand("launchpad.net/{repo}", match)
installPath := installGOPATH + "/src/" + projectPath
node.ImportPath = projectPath
nod.ImportPath = projectPath
// Remove old files.
os.RemoveAll(installPath + "/")

10
doc/struct.go

@ -8,15 +8,9 @@ import (
"go/token"
"os"
"time"
"github.com/GPMGo/node"
)
// Node represents a node.
type Node struct {
ImportPath string `json:"import_path"`
Type, Value string
Deps []*Node // Dependencies.
}
// Bundle represents a bundle.
type Bundle struct {
Id int64
@ -24,7 +18,7 @@ type Bundle struct {
Name string `json:"bundle_name"`
Timestamp int64
Comment string
Nodes []*Node
Nodes []*node.Node
}
// source is source code file.

12
gopm.go

@ -263,9 +263,9 @@ func main() {
}
// Check commands and run.
for _, cmd := range commands {
if cmd.Name() == args[0] && cmd.Run != nil {
cmd.Run(cmd, args[1:])
for _, comm := range commands {
if comm.Name() == args[0] && comm.Run != nil {
comm.Run(comm, args[1:])
exit()
return
}
@ -335,9 +335,9 @@ func help(args []string) {
arg := args[0]
for _, cmd := range commands {
if cmd.Name() == arg {
tmpl(os.Stdout, helpTemplate, cmd)
for _, comm := range commands {
if comm.Name() == arg {
tmpl(os.Stdout, helpTemplate, comm)
// not exit 2: succeeded at 'go help cmd'.
return
}

Loading…
Cancel
Save