Browse Source

Fix code confilt

pull/103/head
Unknown 12 years ago
parent
commit
43cd3bf870
  1. 2
      README.md
  2. 48
      cmd/get.go
  3. 14
      cmd/search.go
  4. 12
      cmd/serve.go
  5. 2
      doc/bitbucket.go
  6. 2
      doc/github.go
  7. 2
      doc/google.go
  8. 2
      doc/launchpad.go
  9. 2
      doc/oschina.go
  10. 1
      doc/struct.go
  11. 171
      doc/utils.go
  12. 15
      doc/utils_windows.go
  13. 10
      doc/vcs.go
  14. 92
      doc/walker.go
  15. 19
      gopm.go

2
README.md

@ -9,7 +9,7 @@ gopm(Go Package Manager) is a Go package manage tool for search, install, update
## Credits
- [garyburd/gddo](https://github.com/garyburd/gddo)
- [Go Walker](https://github.com/Unknwon/gowalker)
## License

48
cmd/get.go

@ -17,9 +17,9 @@ package cmd
import (
"errors"
"fmt"
"os/user"
"strings"
"github.com/Unknwon/com"
"github.com/gpmgo/gopm/doc"
)
@ -70,11 +70,11 @@ func init() {
func printGetPrompt(flag string) {
switch flag {
case "-d":
doc.ColorLog("[INFO] You enabled download without installing.\n")
com.ColorLog("[INFO] You enabled download without installing.\n")
case "-u":
doc.ColorLog("[INFO] You enabled force update.\n")
com.ColorLog("[INFO] You enabled force update.\n")
case "-e":
doc.ColorLog("[INFO] You enabled download dependencies of example(s).\n")
com.ColorLog("[INFO] You enabled download dependencies of example(s).\n")
}
}
@ -97,7 +97,7 @@ func checkFlags(flags map[string]bool, args []string, print func(string)) int {
fmt.Println("DISABLE: " + f)
}
} else {
doc.ColorLog("[ERRO] Unknown flag: %s.\n", f)
com.ColorLog("[ERRO] Unknown flag: %s.\n", f)
return -1
}
num = i + 1
@ -116,18 +116,18 @@ func runGet(cmd *Command, args []string) {
// Check length of arguments.
if len(args) < 1 {
doc.ColorLog("[ERROR] Please list the package that you want to install.\n")
com.ColorLog("[ERROR] Please list the package that you want to install.\n")
return
}
curUser, err := user.Current()
hd, err := doc.GetHomeDir()
if err != nil {
doc.ColorLog("[ERROR] Fail to get current user[ %s ]\n", err)
com.ColorLog("[ERROR] Fail to get current user[ %s ]\n", err)
return
}
installRepoPath = strings.Replace(reposDir, "~", curUser.HomeDir, -1)
doc.ColorLog("[INFO] Packages will be installed into( %s )\n", installRepoPath)
installRepoPath = strings.Replace(reposDir, "~", hd, -1)
com.ColorLog("[INFO] Packages will be installed into( %s )\n", installRepoPath)
nodes := []*doc.Node{}
// ver describles branch, tag or commit.
@ -136,7 +136,7 @@ func runGet(cmd *Command, args []string) {
if len(args) >= 2 {
t, ver, err = validPath(args[1])
if err != nil {
doc.ColorLog("[ERROR] Fail to parse 'args'[ %s ]\n", err)
com.ColorLog("[ERROR] Fail to parse 'args'[ %s ]\n", err)
return
}
}
@ -152,7 +152,7 @@ func runGet(cmd *Command, args []string) {
// Download package(s).
downloadPackages(nodes)
doc.ColorLog("[INFO] %d package(s) downloaded, %d failed.\n",
com.ColorLog("[INFO] %d package(s) downloaded, %d failed.\n",
downloadCount, failConut)
}
@ -170,9 +170,9 @@ func downloadPackages(nodes []*doc.Node) {
if len(n.Value) > 0 {
installPath += "." + n.Value
}
if doc.IsExist(installPath) {
doc.ColorLog("[WARN] Skipped installed package( %s => %s:%s )\n",
n.ImportPath, n.Type, n.Value)
if com.IsExist(installPath) {
com.ColorLog("[WARN] Skipped installed package( %s => %s:%s )\n",
n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))
continue
}
}
@ -198,19 +198,19 @@ func downloadPackages(nodes []*doc.Node) {
// Only save package information with specific commit.
if nod != nil {
// Save record in local nodes.
doc.ColorLog("[SUCC] Downloaded package( %s => %s:%s )\n",
n.ImportPath, n.Type, n.Value)
com.ColorLog("[SUCC] Downloaded package( %s => %s:%s )\n",
n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))
downloadCount++
saveNode(nod)
}
} else {
doc.ColorLog("[WARN] Skipped downloaded package( %s => %s:%s )\n",
n.ImportPath, n.Type, n.Value)
com.ColorLog("[WARN] Skipped downloaded package( %s => %s:%s )\n",
n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))
}
} else {
// Invalid import path.
doc.ColorLog("[WARN] Skipped invalid package path( %s => %s:%s )\n",
n.ImportPath, n.Type, n.Value)
com.ColorLog("[WARN] Skipped invalid package path( %s => %s:%s )\n",
n.ImportPath, n.Type, doc.CheckNodeValue(n.Value))
failConut++
}
}
@ -218,15 +218,15 @@ func downloadPackages(nodes []*doc.Node) {
// downloadPackage downloads package either use version control tools or not.
func downloadPackage(nod *doc.Node) (*doc.Node, []string) {
doc.ColorLog("[TRAC] Downloading package( %s => %s:%s )\n",
nod.ImportPath, nod.Type, nod.Value)
com.ColorLog("[TRAC] Downloading package( %s => %s:%s )\n",
nod.ImportPath, nod.Type, doc.CheckNodeValue(nod.Value))
// Mark as donwloaded.
downloadCache[nod.ImportPath] = true
imports, err := doc.PureDownload(nod, installRepoPath, CmdGet.Flags)
if err != nil {
doc.ColorLog("[ERRO] Download falied[ %s ]\n", err)
com.ColorLog("[ERRO] Download falied[ %s ]\n", err)
failConut++
return nil, nil
}

14
cmd/search.go

@ -20,7 +20,7 @@ import (
"io/ioutil"
"net/http"
"github.com/gpmgo/gopm/doc"
"github.com/Unknwon/com"
)
var CmdSearch = &Command{
@ -46,7 +46,7 @@ func init() {
func printSearchPrompt(flag string) {
switch flag {
case "-e":
doc.ColorLog("[INFO] You enabled exactly search.\n")
com.ColorLog("[INFO] You enabled exactly search.\n")
}
}
@ -61,7 +61,7 @@ func runSearch(cmd *Command, args []string) {
// Check length of arguments.
if len(args) < 1 {
doc.ColorLog("[ERROR] Please input package's keyword.\n")
com.ColorLog("[ERROR] Please input package's keyword.\n")
return
}
@ -88,7 +88,7 @@ func search(host, port, keyword string, isExactly bool) {
}
resp, err := http.Get(url)
if err != nil {
doc.ColorLog(err.Error())
com.ColorLog(err.Error())
return
}
defer resp.Body.Close()
@ -96,20 +96,20 @@ func search(host, port, keyword string, isExactly bool) {
if resp.StatusCode == 200 {
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
doc.ColorLog(err.Error())
com.ColorLog(err.Error())
return
}
pkgs := make([]string, 0)
err = json.Unmarshal(contents, &pkgs)
if err != nil {
doc.ColorLog(err.Error())
com.ColorLog(err.Error())
return
}
for i, pkg := range pkgs {
fmt.Println(i+1, pkg)
}
} else {
doc.ColorLog(resp.Status)
com.ColorLog(resp.Status)
}
}

12
cmd/serve.go

@ -26,6 +26,8 @@ import (
"os"
"strconv"
"strings"
"github.com/Unknwon/com"
)
var (
@ -61,7 +63,7 @@ func init() {
func printServePrompt(flag string) {
switch flag {
case "-l":
doc.ColorLog("[INFO] You enabled start a service only localhost.\n")
com.ColorLog("[INFO] You enabled start a service only localhost.\n")
}
}
@ -147,7 +149,7 @@ func saveNode(nod *doc.Node) error {
url := fmt.Sprintf("http://%v:%v/add?%v", "localhost", "8991", nod.ImportPath)
resp, err := http.Get(url)
if err != nil {
doc.ColorLog(err.Error())
com.ColorLog("%v\n", err.Error())
return err
}
defer resp.Body.Close()
@ -330,7 +332,7 @@ func runningStatus() (int, int, int) {
}
func startService(listen string) {
homeDir, err := doc.HomeDir()
homeDir, err := doc.GetHomeDir()
if err != nil {
fmt.Println(err)
return
@ -386,7 +388,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(rId)
pkg, err := dbGet(fmt.Sprintf("pkg:%v", rId))
if err != nil {
doc.ColorLog(err.Error())
com.ColorLog(err.Error())
continue
}
pkgs = append(pkgs, pkg)
@ -403,7 +405,7 @@ func searcheHandler(w http.ResponseWriter, r *http.Request) {
_, err := dbGet("index:" + key)
if err != nil {
doc.ColorLog(err.Error())
com.ColorLog(err.Error())
continue
}

2
doc/bitbucket.go

@ -188,7 +188,7 @@ func getBitbucketDoc(client *http.Client, match map[string]string, installRepoPa
// Check if need to check imports.
if nod.IsGetDeps {
for _, d := range dirs {
importPkgs, err := CheckImports(d+"/", match["importPath"])
importPkgs, err := CheckImports(d+"/", match["importPath"], nod)
if err != nil {
return nil, err
}

2
doc/github.go

@ -150,7 +150,7 @@ func getGithubDoc(client *http.Client, match map[string]string, installRepoPath
// Check if need to check imports.
if nod.IsGetDeps {
for _, d := range dirs {
importPkgs, err := CheckImports(d, match["importPath"])
importPkgs, err := CheckImports(d, match["importPath"], nod)
if err != nil {
return nil, err
}

2
doc/google.go

@ -78,7 +78,7 @@ func getGoogleDoc(client *http.Client, match map[string]string, installRepoPath
// Check if need to check imports.
if nod.IsGetDeps {
imports := getImports(installPath+"/", match, cmdFlags)
imports := getImports(installPath+"/", match, cmdFlags, nod)
return imports, err
}

2
doc/launchpad.go

@ -128,7 +128,7 @@ func getLaunchpadDoc(client *http.Client, match map[string]string, installRepoPa
// Check if need to check imports.
if nod.IsGetDeps {
for _, d := range dirs {
importPkgs, err := CheckImports(d+"/", match["importPath"])
importPkgs, err := CheckImports(d+"/", match["importPath"], nod)
if err != nil {
return nil, err
}

2
doc/oschina.go

@ -123,7 +123,7 @@ func getOSCDoc(client *http.Client, match map[string]string, installRepoPath str
// Check if need to check imports.
if nod.IsGetDeps {
for _, d := range dirs {
importPkgs, err := CheckImports(d, match["importPath"])
importPkgs, err := CheckImports(d, match["importPath"], nod)
if err != nil {
return nil, err
}

1
doc/struct.go

@ -37,6 +37,7 @@ type Node struct {
DownloadURL string
Type string
Value string // Branch, tag or commit.
Synopsis string
IsGetDeps bool
}

171
doc/utils.go

@ -15,162 +15,19 @@
package doc
import (
"fmt"
"os"
"os/user"
"path"
"regexp"
"runtime"
"strings"
//"syscall"
)
// IsExist returns if a file or directory exists
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// Non-Windows.
const (
Gray = uint8(iota + 90)
Red
Green
Yellow
Blue
Magenta
//NRed = uint8(31) // Normal
EndColor = "\033[0m"
)
// Windows.
const (
WDefault = uintptr(iota)
WBlue
WGreen
WCyan
WRed
WPurple
WYellow
WGray
WSilver
WLightBlue
WLime
WLightCyan
WLightRed
WLightPurple
WLightYellow
WWhite
"github.com/Unknwon/com"
)
// ColorLog colors log and print to stdout.
// Log format: <level> <content [highlight][path]> [ error ].
// Level: TRAC -> blue; ERRO -> red; WARN -> Magenta; SUCC -> green; others -> default.
// Content: default; path: yellow; error -> red.
// Level has to be surrounded by "[" and "]".
// Highlights have to be surrounded by "# " and " #"(space).
// Paths have to be surrounded by "( " and " )"(sapce).
// Errors have to be surrounded by "[ " and " ]"(space).
func ColorLog(format string, a ...interface{}) {
log := fmt.Sprintf(format, a...)
if runtime.GOOS != "windows" {
var clog string
// Level.
i := strings.Index(log, "]")
if log[0] == '[' && i > -1 {
clog += "[" + getColorLevel(log[1:i]) + "]"
}
log = log[i+1:]
// Error.
log = strings.Replace(log, "[ ", fmt.Sprintf("[\033[%dm", Red), -1)
log = strings.Replace(log, " ]", EndColor+"]", -1)
// Path.
log = strings.Replace(log, "( ", fmt.Sprintf("(\033[%dm", Yellow), -1)
log = strings.Replace(log, " )", EndColor+")", -1)
// Highlights.
log = strings.Replace(log, "# ", fmt.Sprintf("\033[%dm", Gray), -1)
log = strings.Replace(log, " #", EndColor, -1)
log = clog + log
} else {
// Level.
i := strings.Index(log, "]")
if log[0] == '[' && i > -1 {
fmt.Print("[")
printColorLevel(log[1:i])
fmt.Print("]")
}
log = log[i+1:]
}
fmt.Print(log)
}
// getColorLevel returns colored level string by given level.
func getColorLevel(level string) string {
level = strings.ToUpper(level)
switch level {
case "TRAC":
return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level)
case "ERRO":
return fmt.Sprintf("\033[%dm%s\033[0m", Red, level)
case "WARN":
return fmt.Sprintf("\033[%dm%s\033[0m", Magenta, level)
case "SUCC":
return fmt.Sprintf("\033[%dm%s\033[0m", Green, level)
default:
return level
}
}
// printColorLevel prints color level prompt, this is only for Windows.
func printColorLevel(level string) {
cc := WDefault
level = strings.ToUpper(level)
switch level {
case "TRAC":
cc = WBlue
case "ERRO":
cc = WRed
case "WARN":
cc = WPurple
case "SUCC":
cc = WGreen
default:
cc = WWhite
}
fmt.Println(cc)
/*kernel32 := syscall.NewLazyDLL("kernel32.dll")
proc := kernel32.NewProc("SetConsoleTextAttribute")
handle, _, _ := proc.Call(uintptr(syscall.Stdout), uintptr(cc))
fmt.Print(level)
handle, _, _ = proc.Call(uintptr(syscall.Stdout), uintptr(WSilver))
CloseHandle := kernel32.NewProc("CloseHandle")
CloseHandle.Call(handle)*/
}
// GetGOPATH returns all paths in GOPATH variable.
func GetGOPATH() []string {
gopath := os.Getenv("GOPATH")
var paths []string
if runtime.GOOS == "windows" {
gopath = strings.Replace(gopath, "\\", "/", -1)
paths = strings.Split(gopath, ";")
} else {
paths = strings.Split(gopath, ":")
}
return paths
}
// GetGOPATH returns best matched GOPATH.
func GetBestMatchGOPATH(appPath string) string {
paths := GetGOPATH()
paths := com.GetGOPATHs()
for _, p := range paths {
if strings.HasPrefix(p, appPath) {
return strings.Replace(p, "\\", "/", -1)
@ -199,14 +56,14 @@ func GetDirsInfo(rootPath string) ([]os.FileInfo, error) {
// or doesn't exist.
func CheckIsExistWithVCS(path string) bool {
// Check if directory exist.
if !IsExist(path) {
if !com.IsExist(path) {
return false
}
// Check if only has VCS folder.
dirs, err := GetDirsInfo(path)
if err != nil {
ColorLog("[ERRO] CheckIsExistWithVCS -> [ %s ]\n", err)
com.ColorLog("[ERRO] CheckIsExistWithVCS -> [ %s ]\n", err)
return false
}
@ -227,7 +84,7 @@ func CheckIsExistWithVCS(path string) bool {
// CheckIsExistInGOPATH checks if given package import path exists in any path in GOPATH/src,
// and returns corresponding GOPATH.
func CheckIsExistInGOPATH(importPath string) (string, bool) {
paths := GetGOPATH()
paths := com.GetGOPATHs()
for _, p := range paths {
if CheckIsExistWithVCS(p + "/src/" + importPath + "/") {
return p, true
@ -763,11 +620,25 @@ func IsGoRepoPath(importPath string) bool {
return standardPath[importPath]
}
func HomeDir() (string, error) {
func GetHomeDir() (string, error) {
if runtime.GOOS != "windows" {
curUser, err := user.Current()
if err != nil {
return "", err
}
return curUser.HomeDir, nil
} else {
hd, err := com.HomeDir()
if err != nil {
return "", err
}
return hd, nil
}
}
func CheckNodeValue(v string) string {
if len(v) == 0 {
return "<UTD>"
}
return v
}

15
doc/utils_windows.go

@ -1,15 +0,0 @@
package doc
import (
"errors"
"os"
)
func HomeDir() (string, error) {
dir := os.Getenv("userprofile")
if dir == "" {
return "", errors.New()
}
return dir, nil
}

10
doc/vcs.go

@ -223,7 +223,7 @@ func PureDownload(nod *Node, installRepoPath string, flags map[string]bool) ([]s
return s.get(HttpClient, match, installRepoPath, nod, flags)
}
ColorLog("[TRAC] Cannot match any service, getting dynamic...\n")
com.ColorLog("[TRAC] Cannot match any service, getting dynamic...\n")
return getDynamic(HttpClient, nod, installRepoPath, flags)
}
@ -347,7 +347,7 @@ metaScan:
return match, nil
}
func getImports(rootPath string, match map[string]string, cmdFlags map[string]bool) (imports []string) {
func getImports(rootPath string, match map[string]string, cmdFlags map[string]bool, nod *Node) (imports []string) {
dirs, err := GetDirsInfo(rootPath)
if err != nil {
return nil
@ -356,7 +356,7 @@ func getImports(rootPath string, match map[string]string, cmdFlags map[string]bo
for _, d := range dirs {
if d.IsDir() && !(!cmdFlags["-e"] && strings.Contains(d.Name(), "example")) {
absPath := rootPath + d.Name() + "/"
importPkgs, err := CheckImports(absPath, match["importPath"])
importPkgs, err := CheckImports(absPath, match["importPath"], nod)
if err != nil {
return nil
}
@ -367,7 +367,7 @@ func getImports(rootPath string, match map[string]string, cmdFlags map[string]bo
}
// checkImports checks package denpendencies.
func CheckImports(absPath, importPath string) (importPkgs []string, err error) {
func CheckImports(absPath, importPath string, nod *Node) (importPkgs []string, err error) {
dir, err := os.Open(absPath)
if err != nil {
return nil, err
@ -407,7 +407,7 @@ func CheckImports(absPath, importPath string) (importPkgs []string, err error) {
// Check if has Go source files.
if len(files) > 0 {
w := &walker{ImportPath: importPath}
importPkgs, err = w.build(files)
importPkgs, err = w.build(files, nod)
if err != nil {
return nil, err
}

92
doc/walker.go

@ -18,6 +18,7 @@ import (
"bytes"
"go/ast"
"go/build"
"go/doc"
"go/parser"
"go/token"
"io"
@ -26,6 +27,10 @@ import (
"path"
"runtime"
"strings"
"unicode"
"unicode/utf8"
"github.com/Unknwon/com"
)
type sliceWriter struct{ p *[]byte }
@ -82,7 +87,7 @@ func simpleImporter(imports map[string]*ast.Object, path string) (*ast.Object, e
}
// build gets imports from source files.
func (w *walker) build(srcs []*source) ([]string, error) {
func (w *walker) build(srcs []*source, nod *Node) ([]string, error) {
// Add source files to walker, I skipped references here.
w.srcs = make(map[string]*source)
for _, src := range srcs {
@ -113,7 +118,7 @@ func (w *walker) build(srcs []*source) ([]string, error) {
if nogo {
err = nil
} else {
ColorLog("[WARN] Error occurs when check imports[ %s ]\n", err)
com.ColorLog("[WARN] Error occurs when check imports[ %s ]\n", err)
return nil, nil
}
}
@ -140,5 +145,88 @@ func (w *walker) build(srcs []*source) ([]string, error) {
}
}
apkg, _ := ast.NewPackage(w.fset, files, simpleImporter, nil)
mode := doc.Mode(0)
if w.ImportPath == "builtin" {
mode |= doc.AllDecls
}
pdoc := doc.New(apkg, w.ImportPath, mode)
nod.Synopsis = Synopsis(pdoc.Doc)
return imports, err
}
var badSynopsisPrefixes = []string{
"Autogenerated by Thrift Compiler",
"Automatically generated ",
"Auto-generated by ",
"Copyright ",
"COPYRIGHT ",
`THE SOFTWARE IS PROVIDED "AS IS"`,
"TODO: ",
"vim:",
}
// Synopsis extracts the first sentence from s. All runs of whitespace are
// replaced by a single space.
func Synopsis(s string) string {
parts := strings.SplitN(s, "\n\n", 2)
s = parts[0]
var buf []byte
const (
other = iota
period
space
)
last := space
Loop:
for i := 0; i < len(s); i++ {
b := s[i]
switch b {
case ' ', '\t', '\r', '\n':
switch last {
case period:
break Loop
case other:
buf = append(buf, ' ')
last = space
}
case '.':
last = period
buf = append(buf, b)
default:
last = other
buf = append(buf, b)
}
}
// Ensure that synopsis fits an App Engine datastore text property.
const m = 400
if len(buf) > m {
buf = buf[:m]
if i := bytes.LastIndex(buf, []byte{' '}); i >= 0 {
buf = buf[:i]
}
buf = append(buf, " ..."...)
}
s = string(buf)
r, n := utf8.DecodeRuneInString(s)
if n < 0 || unicode.IsPunct(r) || unicode.IsSymbol(r) {
// ignore Markdown headings, editor settings, Go build constraints, and * in poorly formatted block comments.
s = ""
} else {
for _, prefix := range badSynopsisPrefixes {
if strings.HasPrefix(s, prefix) {
s = ""
break
}
}
}
return s
}

19
gopm.go

@ -26,7 +26,6 @@ import (
"unicode"
"unicode/utf8"
"github.com/Unknwon/com"
"github.com/gpmgo/gopm/cmd"
)
@ -68,27 +67,11 @@ var commands = []*cmd.Command{
helpTestfunc,*/
}
// We don't use init() to initialize
// bacause we need to get execute path in runtime.
func initialize() bool {
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
// Get application execute path.
var err error
cmd.AppPath, err = com.GetSrcPath("github.com/gpmgoo/gopm")
if err != nil {
return false
}
return true
}
func main() {
// Initialization.
/*if !initialize() {
return
}*/
// Check length of arguments.
args := os.Args[1:]
if len(args) < 1 {

Loading…
Cancel
Save