Browse Source

clean code: github.go

pull/103/head
Unknown 12 years ago
parent
commit
864b1d79b1
  1. 209
      doc/github.go

209
doc/github.go

@ -5,17 +5,16 @@
package doc package doc
import ( import (
/*"archive/zip" "archive/zip"
"bytes" "bytes"
"fmt" "io"
"io"*/
"net/http" "net/http"
/*"os" "os"
"path"*/ "path"
"regexp" "regexp"
//"strings" "strings"
//"github.com/GPMGo/gpm/utils" "github.com/GPMGo/gpm/utils"
) )
var ( var (
@ -33,113 +32,127 @@ func GetGithubDoc(client *http.Client, match map[string]string, commit string) (
SetGithubCredentials("1862bcb265171f37f36c", "308d71ab53ccd858416cfceaed52d5d5b7d53c5f") SetGithubCredentials("1862bcb265171f37f36c", "308d71ab53ccd858416cfceaed52d5d5b7d53c5f")
match["cred"] = githubCred match["cred"] = githubCred
/* // JSON struct for github.com.
var refs []*struct { var refs []*struct {
Object struct { Ref string
Type string Url string
Sha string Object struct {
Url string Sha string
} Type string
Ref string Url string
Url string
} }
}
// Check if has specific commit. // bundle and snapshot will have commit 'B' and 'S',
if len(commit) == 0 { // but does not need to download dependencies.
// Get up-to-date version. isCheckImport := len(commit) == 0
err := httpGetJSON(client, expand("https://api.github.com/repos/{owner}/{repo}/git/refs?{cred}", match), &refs)
if err != nil {
return nil, err
}
tags := make(map[string]string) // Check if download with specific revision.
for _, ref := range refs { if isCheckImport || len(commit) == 1 {
switch { // Get up-to-date version.
case strings.HasPrefix(ref.Ref, "refs/heads/"): err := httpGetJSON(client, expand("https://api.github.com/repos/{owner}/{repo}/git/refs?{cred}", match), &refs)
tags[ref.Ref[len("refs/heads/"):]] = ref.Object.Sha if err != nil {
case strings.HasPrefix(ref.Ref, "refs/tags/"): return nil, nil, err
tags[ref.Ref[len("refs/tags/"):]] = ref.Object.Sha }
}
}
// Check revision tag. tags := make(map[string]string)
match["tag"], commit, err = bestTag(tags, "master") for _, ref := range refs {
if err != nil { switch {
return nil, err case strings.HasPrefix(ref.Ref, "refs/heads/"):
tags[ref.Ref[len("refs/heads/"):]] = ref.Object.Sha
case strings.HasPrefix(ref.Ref, "refs/tags/"):
tags[ref.Ref[len("refs/tags/"):]] = ref.Object.Sha
} }
} }
match["sha"] = commit // Check revision tag.
// Download zip. match["tag"], commit, err = bestTag(tags, "master")
p, err := httpGetBytes(client, expand("https://github.com/{owner}/{repo}/archive/{sha}.zip", match), nil)
if err != nil { if err != nil {
return nil, err return nil, nil, err
}
}
// We use .zip here.
// zip : https://github.com/{owner}/{repo}/archive/{sha}.zip
// tarball : https://github.com/{owner}/{repo}/tarball/{sha}
match["sha"] = commit
// Downlaod archive.
p, err := httpGetBytes(client, expand("https://github.com/{owner}/{repo}/archive/{sha}.zip", match), nil)
if err != nil {
return nil, nil, err
}
r, err := zip.NewReader(bytes.NewReader(p), int64(len(p)))
if err != nil {
return nil, nil, err
}
shaName := expand("{repo}-{sha}", match)
paths := utils.GetGOPATH()
importPath := "github.com/" + expand("{owner}/{repo}", match)
installPath := paths[0] + "/src/" + importPath
// Remove old files.
os.RemoveAll(installPath)
// Create destination directory.
os.Mkdir(installPath, os.ModePerm)
dirs := make([]string, 0, 5)
for _, f := range r.File {
absPath := strings.Replace(f.FileInfo().Name(), shaName, installPath, 1)
// Check if it is directory or not.
if strings.HasSuffix(absPath, "/") {
// Directory.
dirs = append(dirs, absPath)
continue
} }
r, err := zip.NewReader(bytes.NewReader(p), int64(len(p))) // Get files from archive.
rc, err := f.Open()
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
//defer r.Close()
shaName := expand("{repo}-{sha}", match)
paths := utils.GetGOPATH()
importPath := "github.com/" + expand("{owner}/{repo}", match)
installPath := paths[0] + "/src/" + importPath
// Create destination directory
os.Mkdir(installPath, os.ModePerm)
dirs := make([]string, 0, 5)
for _, f := range r.File {
absPath := strings.Replace(f.FileInfo().Name(), shaName, installPath, 1)
fmt.Printf("Unzipping %s...", absPath)
// Check if it is directory or not.
if strings.HasSuffix(absPath, "/") {
// Directory.
dirs = append(dirs, absPath)
continue
}
// Get files from archive // Create diretory before create file
rc, err := f.Open() os.MkdirAll(path.Dir(absPath), os.ModePerm)
if err != nil { // Write data to file
return nil, err fw, _ := os.Create(absPath)
} if err != nil {
return nil, nil, err
}
// Create diretory before create file _, err = io.Copy(fw, rc)
os.MkdirAll(path.Dir(absPath), os.ModePerm) if err != nil {
// Write data to file return nil, nil, err
fw, _ := os.Create(absPath) }
if err != nil {
return nil, err
}
n, err := io.Copy(fw, rc) /*localF, _ := os.Open(absPath)
if err != nil { fbytes := make([]byte, f.FileInfo().Size())
return nil, err n, _ := localF.Read(fbytes)
}
fmt.Println(n) // Check if Go source file.
if n > 0 && strings.HasSuffix(absPath, ".go") {
/*localF, _ := os.Open(absPath) files = append(files, &source{
fbytes := make([]byte, f.FileInfo().Size()) name: srcName,
n, _ := localF.Read(fbytes) data: fbytes,
})
// Check if Go source file.
if n > 0 && strings.HasSuffix(absPath, ".go") {
files = append(files, &source{
name: srcName,
data: fbytes,
})
}*/
/* }
pkg := &Package{
ImportPath: importPath,
AbsPath: installPath,
Commit: commit,
Dirs: dirs,
}*/ }*/
}
// Check if need to check imports.
if isCheckImport {
} else {
}
/*pkg := &Package{
ImportPath: importPath,
AbsPath: installPath,
Commit: commit,
Dirs: dirs,
}*/
return nil, nil, nil return nil, nil, nil
} }

Loading…
Cancel
Save