Browse Source

add support for child package build

pull/103/head
Lunny Xiao 11 years ago
parent
commit
58d75a92d7
  1. 2
      .gopmfile
  2. 78
      cmd/build.go
  3. 60
      doc/gopmfile.go

2
.gopmfile

@ -0,0 +1,2 @@
[target]
path = github.com/gpmgo/gopm

78
cmd/build.go

@ -15,7 +15,7 @@
package cmd package cmd
import ( import (
"errors" //"errors"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/gpmgo/gopm/doc" "github.com/gpmgo/gopm/doc"
"go/build" "go/build"
@ -43,28 +43,23 @@ func printBuildPrompt(flag string) {
} }
func getGopmPkgs(path string, inludeSys bool) (map[string]*doc.Pkg, error) { func getGopmPkgs(path string, inludeSys bool) (map[string]*doc.Pkg, error) {
abs, err := filepath.Abs(doc.GopmFileName) abs, err := filepath.Abs(filepath.Join(path, doc.GopmFileName))
if err != nil { if err != nil {
return nil, err return nil, err
} }
// load import path // load import path
gf := doc.NewGopmfile() gf := doc.NewGopmfile()
var builds *doc.Section
if com.IsExist(abs) { if com.IsExist(abs) {
err := gf.Load(abs) err := gf.Load(abs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else { var ok bool
sec := doc.NewSection() if builds, ok = gf.Sections["build"]; !ok {
sec.Name = "build" builds = nil
gf.Sections[sec.Name] = sec }
}
var builds *doc.Section
var ok bool
if builds, ok = gf.Sections["build"]; !ok {
return nil, errors.New("no found build section\n")
} }
pkg, err := build.ImportDir(path, build.AllowBinary) pkg, err := build.ImportDir(path, build.AllowBinary)
@ -75,11 +70,13 @@ func getGopmPkgs(path string, inludeSys bool) (map[string]*doc.Pkg, error) {
pkgs := make(map[string]*doc.Pkg) pkgs := make(map[string]*doc.Pkg)
for _, name := range pkg.Imports { for _, name := range pkg.Imports {
if inludeSys || !isStdPkg(name) { if inludeSys || !isStdPkg(name) {
if dep, ok := builds.Deps[name]; ok { if builds != nil {
pkgs[name] = dep.Pkg if dep, ok := builds.Deps[name]; ok {
} else { pkgs[name] = dep.Pkg
pkgs[name] = doc.NewDefaultPkg(name) continue
}
} }
pkgs[name] = doc.NewDefaultPkg(name)
} }
} }
return pkgs, nil return pkgs, nil
@ -91,6 +88,12 @@ func pkgInCache(name string, cachePkgs map[string]*doc.Pkg) bool {
return ok return ok
} }
func autoLink(oldPath, newPath string) error {
newPPath, _ := filepath.Split(newPath)
os.MkdirAll(newPPath, os.ModePerm)
return makeLink(oldPath, newPath)
}
func getChildPkgs(cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg) error { func getChildPkgs(cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg) error {
pkgs, err := getGopmPkgs(cpath, false) pkgs, err := getGopmPkgs(cpath, false)
if err != nil { if err != nil {
@ -139,6 +142,23 @@ func runBuild(cmd *Command, args []string) {
return return
} }
gf := doc.NewGopmfile()
var pkgName string
gpmPath := filepath.Join(curPath, doc.GopmFileName)
if com.IsExist(gpmPath) {
com.ColorLog("[INFO] loading .gopmfile ...\n")
err := gf.Load(gpmPath)
if err != nil {
com.ColorLog("[ERRO] load .gopmfile failed: %v\n", err)
return
}
}
if target, ok := gf.Sections["target"]; ok {
pkgName = target.Props["path"]
com.ColorLog("[INFO] target name is %v\n", pkgName)
}
installRepoPath = strings.Replace(reposDir, "~", hd, -1) installRepoPath = strings.Replace(reposDir, "~", hd, -1)
cachePkgs := make(map[string]*doc.Pkg) cachePkgs := make(map[string]*doc.Pkg)
@ -158,23 +178,25 @@ func runBuild(cmd *Command, args []string) {
newPath := filepath.Join(newGoPathSrc, name) newPath := filepath.Join(newGoPathSrc, name)
paths := strings.Split(name, "/") paths := strings.Split(name, "/")
var isExistP bool var isExistP bool
var isCurChild bool
for i := 0; i < len(paths)-1; i++ { for i := 0; i < len(paths)-1; i++ {
pName := strings.Join(paths[:len(paths)-1-i], "/") pName := strings.Join(paths[:len(paths)-1-i], "/")
if _, ok := cachePkgs[pName]; ok { if _, ok := cachePkgs[pName]; ok {
isExistP = true isExistP = true
break break
} }
if pkgName == pName {
isCurChild = true
break
}
}
if isCurChild {
continue
} }
if !isExistP { if !isExistP {
pName := filepath.Join(paths[:len(paths)-1]...)
newPPath := filepath.Join(newGoPathSrc, pName)
//com.ColorLog("[TRAC] create dirs %v\n", newPPath)
os.MkdirAll(newPPath, os.ModePerm)
com.ColorLog("[INFO] linked %v\n", name) com.ColorLog("[INFO] linked %v\n", name)
err = autoLink(oldPath, newPath)
err = makeLink(oldPath, newPath)
if err != nil { if err != nil {
com.ColorLog("[ERRO] make link error %v\n", err) com.ColorLog("[ERRO] make link error %v\n", err)
return return
@ -182,6 +204,16 @@ func runBuild(cmd *Command, args []string) {
} }
} }
if pkgName != "" {
newPath := filepath.Join(newGoPathSrc, pkgName)
com.ColorLog("[INFO] linked %v\n", pkgName)
err = autoLink(curPath, newPath)
if err != nil {
com.ColorLog("[ERRO] make link error %v\n", err)
return
}
}
gopath := build.Default.GOPATH gopath := build.Default.GOPATH
com.ColorLog("[TRAC] set GOPATH=%v\n", newGoPath) com.ColorLog("[TRAC] set GOPATH=%v\n", newGoPath)
err = os.Setenv("GOPATH", newGoPath) err = os.Setenv("GOPATH", newGoPath)

60
doc/gopmfile.go

@ -2,7 +2,6 @@ package doc
import ( import (
"bufio" "bufio"
"errors"
"os" "os"
"strings" "strings"
) )
@ -30,12 +29,15 @@ type Depend struct {
} }
type Section struct { type Section struct {
Name string Name string
Deps map[string]*Depend Deps map[string]*Depend
Props map[string]string
} }
func NewSection() *Section { func NewSection() *Section {
return &Section{Deps: make(map[string]*Depend)} return &Section{Deps: make(map[string]*Depend),
Props: make(map[string]string),
}
} }
type Gopmfile struct { type Gopmfile struct {
@ -53,41 +55,45 @@ func (this *Gopmfile) Load(path string) error {
} }
scanner := bufio.NewScanner(f) scanner := bufio.NewScanner(f)
var sec *Section
for scanner.Scan() { for scanner.Scan() {
var sec *Section
text := strings.TrimSpace(scanner.Text()) text := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(text, "[") { if strings.HasPrefix(text, "[") && strings.HasSuffix(text, "]") {
sec = NewSection() sec = NewSection()
if strings.HasSuffix(text, "]") { sec.Name = text[1 : len(text)-1]
sec.Name = text[1 : len(text)-1]
} else {
return errors.New("need section")
}
this.Sections[sec.Name] = sec this.Sections[sec.Name] = sec
} else { } else {
if sec == nil { if sec == nil {
continue continue
} }
if sec.Name == "target" {
var dep *Depend ss := strings.Split(text, "=")
for _, op := range Ops { if len(ss) == 1 {
if strings.Contains(text, op) { sec.Props[strings.TrimSpace(ss[0])] = strings.TrimSpace(ss[0])
ss := strings.Split(text, op) } else if len(ss) == 2 {
pkver := strings.Split(ss[1], ":") sec.Props[strings.TrimSpace(ss[0])] = strings.TrimSpace(ss[1])
var tp, value string }
tp = pkver[0] } else {
if len(pkver) == 2 { var dep *Depend
value = pkver[1] for _, op := range Ops {
if strings.Contains(text, op) {
ss := strings.Split(text, op)
pkver := strings.Split(ss[1], ":")
var tp, value string
tp = pkver[0]
if len(pkver) == 2 {
value = pkver[1]
}
dep = &Depend{NewPkg(ss[0], tp, value), ss[1], value}
break
} }
dep = &Depend{NewPkg(ss[0], tp, value), ss[1], value}
break
} }
}
if dep == nil { if dep == nil {
dep = &Depend{NewDefaultPkg(text), Equeal, ""} dep = &Depend{NewDefaultPkg(text), Equeal, ""}
}
sec.Deps[dep.Pkg.ImportPath] = dep
} }
sec.Deps[dep.Pkg.ImportPath] = dep
} }
} }

Loading…
Cancel
Save