Browse Source

Work on #13

pull/103/head
Unknown 11 years ago
parent
commit
cbfb81009d
  1. 2
      README.md
  2. 7
      cmd/gen.go
  3. 6
      cmd/get.go
  4. 16
      cmd/gopath.go
  5. 5
      cmd/install.go
  6. 106
      doc/utils.go
  7. 7
      gopm.go

2
README.md

@ -21,7 +21,7 @@ USAGE:
gopm [global options] command [command options] [arguments...] gopm [global options] command [command options] [arguments...]
VERSION: VERSION:
0.6.4.0318 0.6.5.0320
COMMANDS: COMMANDS:
get fetch remote package(s) and dependencies to local repository get fetch remote package(s) and dependencies to local repository

7
cmd/gen.go

@ -1,4 +1,4 @@
// Copyright 2013 gopm authors. // Copyright 2013-2014 gopm authors.
// //
// Licensed under the Apache License, Version 2.0 (the "License"): you may // Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain // not use this file except in compliance with the License. You may obtain
@ -58,11 +58,12 @@ func runGen(ctx *cli.Context) {
targetPath := parseTarget(gf.MustValue("target", "path")) targetPath := parseTarget(gf.MustValue("target", "path"))
// Get and set dependencies. // Get and set dependencies.
imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example")) imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example"), false)
for _, p := range imports { for _, p := range imports {
p = doc.GetProjectPath(p) p = doc.GetProjectPath(p)
// Skip subpackage(s) of current project. // Skip subpackage(s) of current project.
if strings.HasSuffix(workDir, p) || strings.HasPrefix(p, targetPath) { if strings.HasSuffix(strings.Replace(workDir, "\\", "/", -1), p) ||
strings.HasPrefix(p, targetPath) {
continue continue
} }

6
cmd/get.go

@ -123,7 +123,7 @@ func getByGopmfile(ctx *cli.Context) {
targetPath := parseTarget(gf.MustValue("target", "path")) targetPath := parseTarget(gf.MustValue("target", "path"))
// Get dependencies. // Get dependencies.
imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example")) imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example"), false)
nodes := make([]*doc.Node, 0, len(imports)) nodes := make([]*doc.Node, 0, len(imports))
for _, p := range imports { for _, p := range imports {
@ -316,12 +316,12 @@ func downloadPackage(ctx *cli.Context, nod *doc.Node) (*doc.Node, []string) {
vcs := getVcsName(gopathDir) vcs := getVcsName(gopathDir)
if ctx.Bool("update") && ctx.Bool("gopath") && len(vcs) > 0 { if ctx.Bool("update") && ctx.Bool("gopath") && len(vcs) > 0 {
err = updateByVcs(vcs, gopathDir) err = updateByVcs(vcs, gopathDir)
imports = doc.GetAllImports([]string{gopathDir}, nod.RootPath, false) imports = doc.GetAllImports([]string{gopathDir}, nod.RootPath, false, false)
} else { } else {
// If package has revision and exist, then just check dependencies. // If package has revision and exist, then just check dependencies.
if nod.IsGetDepsOnly { if nod.IsGetDepsOnly {
return nod, doc.GetAllImports([]string{path.Join(installRepoPath, nod.RootPath) + versionSuffix(nod.Value)}, return nod, doc.GetAllImports([]string{path.Join(installRepoPath, nod.RootPath) + versionSuffix(nod.Value)},
nod.RootPath, ctx.Bool("example")) nod.RootPath, ctx.Bool("example"), false)
} }
nod.Revision = doc.LocalNodes.MustValue(nod.RootPath, "value") nod.Revision = doc.LocalNodes.MustValue(nod.RootPath, "value")
imports, err = doc.PureDownload(nod, installRepoPath, ctx) //CmdGet.Flags) imports, err = doc.PureDownload(nod, installRepoPath, ctx) //CmdGet.Flags)

16
cmd/gopath.go

@ -1,3 +1,17 @@
// Copyright 2013-2014 gopm authors.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package cmd package cmd
import ( import (
@ -35,7 +49,7 @@ func getGopmPkgs(dirPath string, isTest bool) (pkgs map[string]*doc.Pkg, err err
} }
} }
imports := doc.GetAllImports([]string{dirPath}, ".", false) imports := doc.GetAllImports([]string{dirPath}, ".", false, false)
pkgs = make(map[string]*doc.Pkg) pkgs = make(map[string]*doc.Pkg)
for _, name := range imports { for _, name := range imports {
if name == "C" { if name == "C" {

5
cmd/install.go

@ -1,4 +1,4 @@
// Copyright 2013 gopm authors. // Copyright 2013-2014 gopm authors.
// //
// Licensed under the Apache License, Version 2.0 (the "License"): you may // Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain // not use this file except in compliance with the License. You may obtain
@ -83,8 +83,7 @@ func runInstall(ctx *cli.Context) {
var installRepos []string var installRepos []string
if ctx.Bool("pkg") { if ctx.Bool("pkg") {
curPath, _ := filepath.Abs(".") curPath, _ := filepath.Abs(".")
installRepos = doc.GetAllImports([]string{curPath}, installRepos = doc.GetAllImports([]string{curPath}, ".", ctx.Bool("example"), false)
".", ctx.Bool("example"))
} else { } else {
if len(target) == 0 { if len(target) == 0 {
target = pkgName target = pkgName

106
doc/utils.go

@ -1,4 +1,4 @@
// Copyright 2013 gopm authors. // Copyright 2013-2014 gopm authors.
// //
// Licensed under the Apache License, Version 2.0 (the "License"): you may // Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain // not use this file except in compliance with the License. You may obtain
@ -15,11 +15,16 @@
package doc package doc
import ( import (
"bytes"
"go/build" "go/build"
"io"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/Unknwon/com" "github.com/Unknwon/com"
@ -49,16 +54,43 @@ func GetDirsInfo(rootPath string) ([]os.FileInfo, error) {
return dirs, nil return dirs, nil
} }
// GetImports returns package denpendencies. // A Source describles a Source code file.
func GetImports(absPath, importPath string, example bool) []string { type Source struct {
pkg, err := build.ImportDir(absPath, build.AllowBinary) SrcName string
if err != nil { SrcData []byte
if _, ok := err.(*build.NoGoError); !ok { }
log.Error("", "Fail to get imports")
log.Fatal("", err.Error()) func (s *Source) Name() string { return s.SrcName }
} func (s *Source) Size() int64 { return int64(len(s.SrcData)) }
func (s *Source) Mode() os.FileMode { return 0 }
func (s *Source) ModTime() time.Time { return time.Time{} }
func (s *Source) IsDir() bool { return false }
func (s *Source) Sys() interface{} { return nil }
func (s *Source) Data() []byte { return s.SrcData }
type Context struct {
build.Context
importPath string
srcFiles map[string]*Source
}
func (ctx *Context) readDir(dir string) ([]os.FileInfo, error) {
fis := make([]os.FileInfo, 0, len(ctx.srcFiles))
for _, src := range ctx.srcFiles {
fis = append(fis, src)
} }
return fis, nil
}
func (ctx *Context) openFile(path string) (r io.ReadCloser, err error) {
if src, ok := ctx.srcFiles[filepath.Base(path)]; ok {
return ioutil.NopCloser(bytes.NewReader(src.Data())), nil
}
return nil, os.ErrNotExist
}
// GetImports returns package denpendencies.
func GetImports(absPath, importPath string, example, test bool) []string {
fis, err := GetDirsInfo(absPath) fis, err := GetDirsInfo(absPath)
if err != nil { if err != nil {
log.Error("", "Fail to get directory's information") log.Error("", "Fail to get directory's information")
@ -66,23 +98,57 @@ func GetImports(absPath, importPath string, example bool) []string {
} }
absPath += "/" absPath += "/"
imports := make([]string, 0, len(pkg.Imports)) ctx := new(Context)
for _, p := range pkg.Imports { ctx.importPath = importPath
if !IsGoRepoPath(p) && !strings.HasPrefix(p, importPath) { ctx.srcFiles = make(map[string]*Source)
imports = append(imports, p) ctx.Context = build.Default
} ctx.JoinPath = path.Join
} ctx.IsAbsPath = path.IsAbs
ctx.ReadDir = ctx.readDir
ctx.OpenFile = ctx.openFile
// TODO: Load too much, need to make sure which is imported which are not. // TODO: Load too much, need to make sure which is imported which are not.
dirs := make([]string, 0, len(imports)) dirs := make([]string, 0, 10)
for _, fi := range fis { for _, fi := range fis {
if fi.IsDir() && !strings.Contains(fi.Name(), VENDOR) { if strings.Contains(fi.Name(), VENDOR) {
continue
}
if fi.IsDir() {
dirs = append(dirs, absPath+fi.Name()) dirs = append(dirs, absPath+fi.Name())
continue
} else if !test && strings.HasSuffix(fi.Name(), "_test.go") {
continue
} else if !strings.HasSuffix(fi.Name(), ".go") || strings.HasPrefix(fi.Name(), ".") ||
strings.HasPrefix(fi.Name(), "_") {
continue
}
src := &Source{SrcName: fi.Name()}
src.SrcData, err = ioutil.ReadFile(absPath + fi.Name())
if err != nil {
log.Error("", "Fail to read file")
log.Fatal("", err.Error())
}
ctx.srcFiles[fi.Name()] = src
}
pkg, err := ctx.ImportDir(absPath, build.AllowBinary)
if err != nil {
if _, ok := err.(*build.NoGoError); !ok {
log.Error("", "Fail to get imports")
log.Fatal("", err.Error())
}
}
imports := make([]string, 0, len(pkg.Imports))
for _, p := range pkg.Imports {
if !IsGoRepoPath(p) && !strings.HasPrefix(p, importPath) {
imports = append(imports, p)
} }
} }
if len(dirs) > 0 { if len(dirs) > 0 {
imports = append(imports, GetAllImports(dirs, importPath, example)...) imports = append(imports, GetAllImports(dirs, importPath, example, test)...)
} }
return imports return imports
} }
@ -95,11 +161,11 @@ func isVcsPath(dirPath string) bool {
} }
// GetAllImports returns all imports in given directory and all sub-directories. // GetAllImports returns all imports in given directory and all sub-directories.
func GetAllImports(dirs []string, importPath string, example bool) (imports []string) { func GetAllImports(dirs []string, importPath string, example, test bool) (imports []string) {
for _, d := range dirs { for _, d := range dirs {
if !isVcsPath(d) && if !isVcsPath(d) &&
!(!example && strings.Contains(d, "example")) { !(!example && strings.Contains(d, "example")) {
imports = append(imports, GetImports(d, importPath, example)...) imports = append(imports, GetImports(d, importPath, example, test)...)
} }
} }
return imports return imports

7
gopm.go

@ -29,9 +29,10 @@ import (
// Test that go1.1 tag above is included in builds. main.go refers to this definition. // Test that go1.1 tag above is included in builds. main.go refers to this definition.
const go11tag = true const go11tag = true
const APP_VER = "0.6.4.0318" const APP_VER = "0.6.5.0320"
// //cmd.CmdSearch, // cmd.CmdTest,
// cmd.CmdSearch,
// cmdClean, // cmdClean,
// cmdDoc, // cmdDoc,
// cmdEnv, // cmdEnv,
@ -39,7 +40,6 @@ const APP_VER = "0.6.4.0318"
// cmdList, // cmdList,
// cmdTool, // cmdTool,
// cmdVet, // cmdVet,
// }
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
@ -59,7 +59,6 @@ func main() {
cmd.CmdInstall, cmd.CmdInstall,
cmd.CmdUpdate, cmd.CmdUpdate,
cmd.CmdConfig, cmd.CmdConfig,
//cmd.CmdTest,
} }
app.Flags = append(app.Flags, []cli.Flag{ app.Flags = append(app.Flags, []cli.Flag{
cli.BoolFlag{"noterm", "disable color output"}, cli.BoolFlag{"noterm", "disable color output"},

Loading…
Cancel
Save