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
import (
"errors"
//"errors"
"github.com/Unknwon/com"
"github.com/gpmgo/gopm/doc"
"go/build"
@ -43,28 +43,23 @@ func printBuildPrompt(flag string) {
}
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 {
return nil, err
}
// load import path
gf := doc.NewGopmfile()
var builds *doc.Section
if com.IsExist(abs) {
err := gf.Load(abs)
if err != nil {
return nil, err
}
} else {
sec := doc.NewSection()
sec.Name = "build"
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")
var ok bool
if builds, ok = gf.Sections["build"]; !ok {
builds = nil
}
}
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)
for _, name := range pkg.Imports {
if inludeSys || !isStdPkg(name) {
if dep, ok := builds.Deps[name]; ok {
pkgs[name] = dep.Pkg
} else {
pkgs[name] = doc.NewDefaultPkg(name)
if builds != nil {
if dep, ok := builds.Deps[name]; ok {
pkgs[name] = dep.Pkg
continue
}
}
pkgs[name] = doc.NewDefaultPkg(name)
}
}
return pkgs, nil
@ -91,6 +88,12 @@ func pkgInCache(name string, cachePkgs map[string]*doc.Pkg) bool {
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 {
pkgs, err := getGopmPkgs(cpath, false)
if err != nil {
@ -139,6 +142,23 @@ func runBuild(cmd *Command, args []string) {
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)
cachePkgs := make(map[string]*doc.Pkg)
@ -158,23 +178,25 @@ func runBuild(cmd *Command, args []string) {
newPath := filepath.Join(newGoPathSrc, name)
paths := strings.Split(name, "/")
var isExistP bool
var isCurChild bool
for i := 0; i < len(paths)-1; i++ {
pName := strings.Join(paths[:len(paths)-1-i], "/")
if _, ok := cachePkgs[pName]; ok {
isExistP = true
break
}
if pkgName == pName {
isCurChild = true
break
}
}
if isCurChild {
continue
}
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)
err = makeLink(oldPath, newPath)
err = autoLink(oldPath, newPath)
if err != nil {
com.ColorLog("[ERRO] make link error %v\n", err)
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
com.ColorLog("[TRAC] set GOPATH=%v\n", newGoPath)
err = os.Setenv("GOPATH", newGoPath)

60
doc/gopmfile.go

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

Loading…
Cancel
Save