Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Commit

Permalink
Finish command gen
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Aug 14, 2014
1 parent 6d13295 commit 213394d
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .gopmfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
path = github.com/gpmgo/gopm

[deps]
github.com/codegangsta/cli =
github.com/Unknwon/com =
github.com/Unknwon/goconfig =
github.com/aybabtme/color =
github.com/codegangsta/cli =
github.com/Unknwon/cae =

[res]
Expand Down
2 changes: 1 addition & 1 deletion cmd/bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func runBin(ctx *cli.Context) {
return
}

gf, target, _, err := genGopmfile()
gf, target, _, err := genGopmfile(ctx)
if err != nil {
errors.SetError(err)
return
Expand Down
95 changes: 33 additions & 62 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func setup(ctx *cli.Context) (err error) {

if !ctx.Bool("remote") {
if ctx.Bool("local") {
gf, _, _, err := genGopmfile()
gf, _, _, err := genGopmfile(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -275,8 +275,7 @@ func getDepPkgs(
if _, ok := depPkgs[pkg.RootPath]; !ok {
var newPath string
if !build.IsLocalImport(name) && pkg.Type != doc.LOCAL {
pkgPath := strings.Replace(
pkg.ImportPath, pkg.RootPath, pkg.RootPath+pkg.ValSuffix(), 1)
pkgPath := pkg.RootPath + pkg.ValSuffix()
newPath = path.Join(setting.InstallRepoPath, pkgPath)
if len(pkg.ValSuffix()) == 0 && !ctx.Bool("remote") &&
com.IsDir(path.Join(setting.InstallGopath, pkgPath)) {
Expand All @@ -287,6 +286,7 @@ func getDepPkgs(
} else {
if !com.IsExist(newPath) || ctx.Bool("update") {
node := doc.NewNode(pkg.ImportPath, pkg.Type, pkg.Value, true)
node.IsGetDeps = false
if err = downloadPackages(target, ctx, []*doc.Node{node}); err != nil {
return err
}
Expand All @@ -303,7 +303,18 @@ func getDepPkgs(
}
}
depPkgs[pkg.RootPath] = pkg
if err = getDepPkgs(gf, ctx, pkg.ImportPath, newPath, depPkgs, false); err != nil {

curPath = path.Join(doc.VENDOR, "src", pkg.RootPath)
log.Log("Linking %s", pkg.RootPath+pkg.ValSuffix())
if err := autoLink(newPath, curPath); err != nil {
if setting.LibraryMode {
return fmt.Errorf("Fail to make link dependency: %v", err)
}
log.Error("", "Fail to make link dependency:")
log.Fatal("", "\t"+err.Error())
}

if err = getDepPkgs(gf, ctx, pkg.ImportPath, curPath, depPkgs, false); err != nil {
return err
}
}
Expand All @@ -319,84 +330,32 @@ func autoLink(oldPath, newPath string) error {
func genNewGopath(ctx *cli.Context, isTest bool) (string, string, string, error) {
log.Trace("Work directory: %s", setting.WorkDir)

gf, target, _, err := genGopmfile()
gf, err := loadGopmfile(setting.GOPMFILE)
if err != nil {
return "", "", "", err
}

// Check dependencies.
target := doc.ParseTarget(gf.MustValue("target", "path"))
if target == "." {
_, target = filepath.Split(setting.WorkDir)
}

// Check and loads dependency pakcages.
depPkgs := make(map[string]*doc.Pkg)
if err := getDepPkgs(gf, ctx, target, setting.WorkDir, depPkgs, isTest); err != nil {
if setting.LibraryMode {
return "", "", "", fmt.Errorf("Fail to get dependency pakcages: %v", err)
}
log.Error("", "Fail to get dependency pakcages:")
log.Fatal("", "\t"+err.Error())
}

// Clean old files.
newGopath := path.Join(setting.WorkDir, doc.VENDOR)
newGopathSrc := path.Join(newGopath, "src")
os.RemoveAll(newGopathSrc)
os.MkdirAll(newGopathSrc, os.ModePerm)

for name, pkg := range depPkgs {
var oldPath string
if pkg.Type == doc.LOCAL {
oldPath, _ = filepath.Abs(pkg.Value)
} else {
oldPath = path.Join(setting.InstallRepoPath, name) + pkg.ValSuffix()
}

newPath := path.Join(newGopathSrc, name)
paths := strings.Split(name, "/")
var isExist, isCurChild bool
if name == target {
continue
}

for i := 0; i < len(paths)-1; i++ {
pName := strings.Join(paths[:len(paths)-1-i], "/")
if _, ok := depPkgs[pName]; ok {
isExist = true
break
}
if target == pName {
isCurChild = true
break
}
}
if isCurChild {
continue
}

if !isExist && (!pkg.IsEmptyVal() || ctx.Bool("remote") ||
!com.IsDir(path.Join(setting.InstallGopath, pkg.ImportPath))) {

log.Log("Linking %s", name+pkg.ValSuffix())
if err := autoLink(oldPath, newPath); err != nil {
if setting.LibraryMode {
return "", "", "", fmt.Errorf("Fail to make link dependency: %v", err)
}
log.Error("", "Fail to make link dependency:")
log.Fatal("", "\t"+err.Error())
}
}
}

// Link self.
targetRoot := doc.GetRootPath(target)
newCurPath := path.Join(newGopathSrc, target)
log.Log("Linking %s", targetRoot)
if setting.Debug {
fmt.Println(target)
fmt.Println(path.Join(strings.TrimSuffix(setting.WorkDir, target), targetRoot))
fmt.Println(path.Join(newGopathSrc, targetRoot))
}
if err := autoLink(path.Join(
strings.TrimSuffix(setting.WorkDir, target), targetRoot),
if err := autoLink(path.Join(strings.TrimSuffix(setting.WorkDir, target), targetRoot),
path.Join(newGopathSrc, targetRoot)); err != nil &&
!strings.Contains(err.Error(), "file exists") {
if setting.LibraryMode {
Expand All @@ -405,6 +364,18 @@ func genNewGopath(ctx *cli.Context, isTest bool) (string, string, string, error)
log.Error("", "Fail to make link self:")
log.Fatal("", "\t"+err.Error())
}

// Check and loads dependency pakcages.
depPkgs := make(map[string]*doc.Pkg)
if err := getDepPkgs(gf, ctx, target, setting.WorkDir, depPkgs, isTest); err != nil {
if setting.LibraryMode {
return "", "", "", fmt.Errorf("Fail to get dependency pakcages: %v", err)
}
log.Error("", "Fail to get dependency pakcages:")
log.Fatal("", "\t"+err.Error())
}

newCurPath := path.Join(newGopathSrc, target)
return target, newGopath, newCurPath, nil
}

Expand Down
40 changes: 13 additions & 27 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package cmd
import (
"os"
"path"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -52,7 +51,7 @@ func runGen(ctx *cli.Context) {
return
}

gf, _, _, err := genGopmfile()
gf, _, _, err := genGopmfile(ctx)
if err != nil {
errors.SetError(err)
return
Expand All @@ -78,39 +77,26 @@ func runGen(ctx *cli.Context) {

// genGopmfile generates gopmfile and returns it,
// along with target and dependencies.
func genGopmfile() (*goconfig.ConfigFile, string, []string, error) {
func genGopmfile(ctx *cli.Context) (*goconfig.ConfigFile, string, []string, error) {
if !com.IsExist(setting.GOPMFILE) {
os.Create(setting.GOPMFILE)
}

if com.IsSliceContainsStr([]string{"gen", "list"}, ctx.Command.Name) {
os.RemoveAll(doc.VENDOR)
if !setting.Debug {
defer os.RemoveAll(doc.VENDOR)
}
}
gf, err := loadGopmfile(setting.GOPMFILE)
if err != nil {
return nil, "", nil, err
}

// Check dependencies.
target := doc.ParseTarget(gf.MustValue("target", "path"))
rootPath := doc.GetRootPath(target)

oldGopath := os.Getenv("GOPATH")
if len(oldGopath) == 0 || !com.IsExist(path.Join(oldGopath, "src", rootPath)) {
tmpPath := path.Join(setting.InstallRepoPath, rootPath)
os.RemoveAll(tmpPath)
os.MkdirAll(path.Dir(tmpPath), os.ModePerm)

relPath, err := filepath.Rel(target, rootPath)
if err != nil {
log.Error("", "Fail to get relative path of target")
log.Fatal("", "\t"+err.Error())
}
absPath, _ := filepath.Abs(relPath)
if setting.IsWindows {
com.CopyDir(absPath, tmpPath, func(filePath string) bool {
return !strings.Contains(filePath, ".git")
})
} else {
os.Symlink(absPath, tmpPath)
}
target, _, _, err := genNewGopath(ctx, false)
if err != nil {
return nil, "", nil, err
}
rootPath := doc.GetRootPath(target)

imports, err := doc.GetImports(target, rootPath, setting.WorkDir, false)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Unknown
// Copyright 2014 Unknwon
//
// 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
Expand Down Expand Up @@ -139,8 +139,11 @@ func downloadPackage(ctx *cli.Context, n *doc.Node) (*doc.Node, []string, error)

if !n.IsGetDeps {
imports = nil
} else {
fmt.Println(n.InstallPath, n.ValSuffix())
// err=autoLink(n.InstallPath, newPath)
}
return n, imports, nil
return n, imports, err
}

// downloadPackages downloads packages with certain commit,
Expand Down Expand Up @@ -285,7 +288,7 @@ func getPackages(target string, ctx *cli.Context, nodes []*doc.Node) (err error)

func getByGopmfile(ctx *cli.Context) error {
// Make sure gopmfile exists and up-to-date.
gf, target, imports, err := genGopmfile()
gf, target, imports, err := genGopmfile(ctx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func runInstall(ctx *cli.Context) {
var target, srcPath string
switch len(ctx.Args()) {
case 0:
_, target, _, err = genGopmfile()
_, target, _, err = genGopmfile(ctx)
if err != nil {
errors.SetError(err)
return
Expand Down
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func runList(ctx *cli.Context) {
return
}

gf, _, imports, err := genGopmfile()
gf, _, imports, err := genGopmfile(ctx)
if err != nil {
errors.SetError(err)
return
Expand Down
4 changes: 1 addition & 3 deletions gopm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// +build go1.2

// Copyright 2014 Unknown
// Copyright 2014 Unknwon
//
// 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
Expand Down
24 changes: 12 additions & 12 deletions gopm/gopm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build go1.2

// Copyright 2014 Unknown
// Copyright 2014 Unknwon
//
// 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
Expand Down Expand Up @@ -28,7 +28,7 @@ import (
"github.com/gpmgo/gopm/modules/setting"
)

const APP_VER = "0.7.1.0613 Beta"
const APP_VER = "0.7.2.0727 Beta"

func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
Expand All @@ -41,17 +41,17 @@ func Run(args []string) *setting.Error {
app.Usage = "Go Package Manager"
app.Version = APP_VER
app.Commands = []cli.Command{
cmd.CmdBin,
cmd.CmdGen,
// cmd.CmdBin,
// cmd.CmdGen,
cmd.CmdGet,
cmd.CmdList,
cmd.CmdConfig,
cmd.CmdRun,
cmd.CmdTest,
cmd.CmdBuild,
cmd.CmdInstall,
cmd.CmdClean,
cmd.CmdUpdate,
// cmd.CmdList,
// cmd.CmdConfig,
// cmd.CmdRun,
// cmd.CmdTest,
// cmd.CmdBuild,
// cmd.CmdInstall,
// cmd.CmdClean,
// cmd.CmdUpdate,
// CmdSearch,
}
app.Flags = append(app.Flags, []cli.Flag{
Expand Down
22 changes: 2 additions & 20 deletions modules/doc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"go/build"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
Expand Down Expand Up @@ -91,18 +90,6 @@ func IsGoRepoPath(name string) bool {

// GetImports returns package denpendencies.
func GetImports(importPath, rootPath, srcPath string, isTest bool) ([]string, error) {
tmpGopath := setting.InstallRepoPath
if setting.IsWindows {
// Windows's local repository path has "/src" suffix.
tmpGopath = path.Dir(tmpGopath)
} else {
if !com.IsExist(path.Join(setting.InstallRepoPath, "src")) {
if err := os.Symlink(setting.InstallRepoPath, path.Join(setting.InstallRepoPath, "src")); err != nil {
log.Error("", "Fail to setting symlink:")
log.Fatal("", "\t"+err.Error())
}
}
}
oldGopath := os.Getenv("GOPATH")

sep := ":"
Expand All @@ -111,7 +98,7 @@ func GetImports(importPath, rootPath, srcPath string, isTest bool) ([]string, er
}

ctxt := build.Default
ctxt.GOPATH = tmpGopath + sep + oldGopath
ctxt.GOPATH = VENDOR + sep + oldGopath
pkg, err := ctxt.Import(importPath, srcPath, build.AllowBinary)
if err != nil {
if _, ok := err.(*build.NoGoError); !ok {
Expand All @@ -134,12 +121,7 @@ func GetImports(importPath, rootPath, srcPath string, isTest bool) ([]string, er
if IsGoRepoPath(name) {
continue
} else if strings.HasPrefix(name, rootPath) {
relPath, err := filepath.Rel(importPath, name)
if err != nil {
log.Error("", "Fail to get relative path of import")
log.Fatal("", "\t"+err.Error())
}
moreImports, err := GetImports(name, rootPath, "./"+relPath, isTest)
moreImports, err := GetImports(name, rootPath, name, isTest)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 213394d

Please sign in to comment.