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

Commit

Permalink
Completely new version of Gopm
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Sep 12, 2014
1 parent 213394d commit b9f79e3
Show file tree
Hide file tree
Showing 47 changed files with 4,821 additions and 2,556 deletions.
13 changes: 13 additions & 0 deletions .bra.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[run]
init_cmds = [["go", "install"]]
watch_all = true
watch_dirs = [
"$WORKDIR/lib",
"$WORKDIR/cmd",
"$WORKDIR/modules",
]
watch_exts = [".go"]
build_delay = 1500
cmds = [
["go", "install"]
]
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ USAGE:
Gopm [global options] command [command options] [arguments...]
VERSION:
0.7.1.0613 Beta
0.8.0.0912 Beta
COMMANDS:
bin download and link dependencies and build binary
list list all dependencies of current project
gen generate a gopmfile for current Go project
get fetch remote package(s) and dependencies
list list all dependencies of current project
bin download and link dependencies and build binary
config configurate gopm settings
run link dependencies and go run
test link dependencies and go test
Expand All @@ -43,8 +43,8 @@ GLOBAL OPTIONS:
--noterm, -n disable color output
--strict, -s strict mode
--debug, -d debug mode
--version, -v print the version
--help, -h show help
--version, -v print the version
```

## License
Expand Down
129 changes: 49 additions & 80 deletions cmd/bin.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 All @@ -22,9 +22,8 @@ import (
"runtime"
"strings"

"github.com/Unknwon/com"
"github.com/codegangsta/cli"

"github.com/gpmgo/gopm/modules/base"
"github.com/gpmgo/gopm/modules/cli"
"github.com/gpmgo/gopm/modules/doc"
"github.com/gpmgo/gopm/modules/errors"
"github.com/gpmgo/gopm/modules/log"
Expand All @@ -44,10 +43,10 @@ Can only specify one each time, and only works for projects that
contain main package`,
Action: runBin,
Flags: []cli.Flag{
cli.StringFlag{"dir, d", "./", "build binary to given directory"},
cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"},
cli.BoolFlag{"remote, r", "build with pakcages in gopm local repository only"},
cli.BoolFlag{"verbose, v", "show process details"},
cli.StringFlag{"dir, d", "./", "build binary to given directory", ""},
cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any", ""},
cli.BoolFlag{"remote, r", "build with pakcages in gopm local repository only", ""},
cli.BoolFlag{"verbose, v", "show process details", ""},
},
}

Expand All @@ -58,23 +57,14 @@ func runBin(ctx *cli.Context) {
}

if len(ctx.Args()) != 1 {
if setting.LibraryMode {
errors.SetError(fmt.Errorf("Incorrect number of arguments for command: should have 1"))
return
}
log.Error("bin", "Incorrect number of arguments for command")
log.Error("", "\tshould have 1")
log.Help("Try 'gopm help bin' to get more information")
errors.SetError(fmt.Errorf("Incorrect number of arguments for command: should have 1"))
return
}

// Check if given directory exists if specified.
if ctx.IsSet("dir") && !com.IsDir(ctx.String("dir")) {
if setting.LibraryMode {
errors.SetError(fmt.Errorf("Indicated path does not exist or not a directory"))
return
}
log.Error("bin", "Cannot start command:")
log.Fatal("", "\tIndicated path does not exist or not a directory")
if ctx.IsSet("dir") && !base.IsDir(ctx.String("dir")) {
errors.SetError(fmt.Errorf("Indicated path does not exist or not a directory"))
return
}

// Parse package version.
Expand All @@ -93,7 +83,11 @@ func runBin(ctx *cli.Context) {

// Check package name.
if !strings.Contains(pkgPath, "/") {
tmpPath := setting.GetPkgFullPath(pkgPath)
tmpPath, err := setting.GetPkgFullPath(pkgPath)
if err != nil {
errors.SetError(err)
return
}
if tmpPath != pkgPath {
n = doc.NewNode(tmpPath, n.Type, n.Value, n.IsGetDeps)
}
Expand All @@ -106,61 +100,48 @@ func runBin(ctx *cli.Context) {

// Check if previous steps were successful.
if !n.IsExist() {
if setting.LibraryMode {
errors.SetError(fmt.Errorf("Download steps weren't successful"))
return
}
log.Error("bin", "Cannot continue command:")
log.Fatal("", "\tDownload steps weren't successful")
}

buildPath := path.Join(setting.InstallRepoPath, n.ImportPath)
oldWorkDir := setting.WorkDir
// Change to repository path.
log.Log("Changing work directory to %s", buildPath)
if err := os.Chdir(buildPath); err != nil {
if setting.LibraryMode {
errors.SetError(fmt.Errorf("Fail to change work directory: %v", err))
return
}
log.Error("bin", "Fail to change work directory:")
log.Fatal("", "\t"+err.Error())
errors.SetError(fmt.Errorf("Download steps weren't successful"))
return
}
setting.WorkDir = buildPath

tmpVendor := path.Join("vendor", path.Base(n.RootPath))
os.RemoveAll(tmpVendor)
os.RemoveAll(setting.VENDOR)
// TODO: should use .gopm/temp path.
os.RemoveAll(path.Join(buildPath, doc.VENDOR))
if err := autoLink(n.InstallPath, tmpVendor); err != nil {
errors.SetError(fmt.Errorf("Fail to link slef: %v", err))
return
}
os.Chdir(tmpVendor)
oldWorkDir := setting.WorkDir
setting.WorkDir = path.Join(setting.WorkDir, tmpVendor)
if !setting.Debug {
defer os.RemoveAll(path.Join(buildPath, doc.VENDOR))
defer func() {
os.Chdir(oldWorkDir)
os.RemoveAll("vendor")
os.RemoveAll(setting.VENDOR)
}()
}

if err := buildBinary(ctx); err != nil {
errors.SetError(err)
return
}

gf, target, _, err := genGopmfile(ctx)
gf, target, err := parseGopmfile(setting.GOPMFILE)
if err != nil {
errors.SetError(err)
return
}
if target == "." {
_, target = filepath.Split(setting.WorkDir)
}

// Because build command moved binary to root path.
binName := path.Base(target)
if runtime.GOOS == "windows" {
binName += ".exe"
}
if !com.IsFile(binName) {
if setting.LibraryMode {
errors.SetError(fmt.Errorf("Previous steps weren't successful or the project does not contain main package"))
return
}
log.Error("bin", "Binary does not exist:")
log.Error("", "\t"+binName)
log.Fatal("", "\tPrevious steps weren't successful or the project does not contain main package")
if !base.IsFile(binName) {
errors.SetError(fmt.Errorf("Previous steps weren't successful or the project does not contain main package"))
return
}

// Move binary to given directory.
Expand All @@ -171,44 +152,32 @@ func runBin(ctx *cli.Context) {
movePath = path.Join(runtime.GOROOT(), "pkg/tool", runtime.GOOS+"_"+runtime.GOARCH)
}

if com.IsExist(movePath + "/" + binName) {
if err := os.Remove(movePath + "/" + binName); err != nil {
log.Warn("Cannot remove binary in work directory:")
log.Warn("\t %s", err)
if base.IsExist(path.Join(movePath, binName)) {
if err := os.Remove(path.Join(movePath, binName)); err != nil {
log.Warn("Cannot remove binary in work directory: %v", err)
}
}

if err := os.Rename(binName, movePath+"/"+binName); err != nil {
if setting.LibraryMode {
errors.SetError(fmt.Errorf("Fail to move binary: %v", err))
return
}
log.Error("bin", "Fail to move binary:")
log.Fatal("", "\t"+err.Error())
errors.SetError(fmt.Errorf("Fail to move binary: %v", err))
return
}
os.Chmod(movePath+"/"+binName, os.ModePerm)

includes := strings.Split(gf.MustValue("res", "include"), "|")
if len(includes) > 0 {
log.Log("Copying resources to %s", movePath)
log.Info("Copying resources to %s", movePath)
for _, include := range includes {
if com.IsDir(include) {
if base.IsDir(include) {
os.RemoveAll(path.Join(movePath, include))
if err := com.CopyDir(include, filepath.Join(movePath, include)); err != nil {
if setting.LibraryMode {
errors.AppendError(errors.NewErrCopyResource(include))
} else {
log.Error("bin", "Fail to copy following resource:")
log.Error("", "\t"+include)
}
if err := base.CopyDir(include, filepath.Join(movePath, include)); err != nil {
errors.AppendError(errors.NewErrCopyResource(include))
}
}
}
}

log.Log("Changing work directory back to %s", oldWorkDir)
os.Chdir(oldWorkDir)

log.Success("SUCC", "bin", "Command executed successfully!")
log.Info("Command executed successfully!")
fmt.Println("Binary has been built into: " + movePath)
os.Exit(0) // FIXME: I don't what the hack is going on when delete this line.
}
50 changes: 22 additions & 28 deletions cmd/build.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 All @@ -19,10 +19,8 @@ import (
"os"
"path"

"github.com/Unknwon/com"
"github.com/codegangsta/cli"

"github.com/gpmgo/gopm/modules/doc"
"github.com/gpmgo/gopm/modules/base"
"github.com/gpmgo/gopm/modules/cli"
"github.com/gpmgo/gopm/modules/errors"
"github.com/gpmgo/gopm/modules/log"
"github.com/gpmgo/gopm/modules/setting"
Expand All @@ -37,42 +35,38 @@ and execute 'go build'
gopm build <go build commands>`,
Action: runBuild,
Flags: []cli.Flag{
cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"},
cli.BoolFlag{"remote, r", "build with pakcages in gopm local repository only"},
cli.BoolFlag{"verbose, v", "show process details"},
cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any", ""},
cli.BoolFlag{"remote, r", "build with pakcages in gopm local repository only", ""},
cli.BoolFlag{"verbose, v", "show process details", ""},
},
}

func buildBinary(ctx *cli.Context, args ...string) error {
target, newGopath, newCurPath, err := genNewGopath(ctx, false)
_, target, err := parseGopmfile(setting.GOPMFILE)
if err != nil {
return err
}

log.Trace("Building...")
if err := linkVendors(ctx); err != nil {
return err
}

log.Info("Building...")

cmdArgs := []string{"go", "build"}
cmdArgs = append(cmdArgs, args...)
if err := execCmd(newGopath, newCurPath, cmdArgs...); err != nil {
if setting.LibraryMode {
return fmt.Errorf("Fail to build program: %v", err)
}
log.Error("build", "Fail to build program:")
log.Fatal("", "\t"+err.Error())
cmdArgs = append(cmdArgs, ctx.Args()...)
if err := execCmd(setting.DefaultVendor, setting.WorkDir, cmdArgs...); err != nil {
return fmt.Errorf("fail to build program: %v", err)
}

if setting.IsWindowsXP {
fName := path.Base(target)
binName := fName + ".exe"
os.Remove(binName)
exePath := path.Join(newCurPath, doc.VENDOR, "src", target, binName)
if com.IsFile(exePath) {
if err := os.Rename(exePath, path.Join(newCurPath, binName)); err != nil {
if setting.LibraryMode {
return fmt.Errorf("Fail to move binary: %v", err)
}
log.Error("build", "Fail to move binary:")
log.Fatal("", "\t"+err.Error())
exePath := path.Join(setting.DefaultVendorSrc, target, binName)
if base.IsFile(exePath) {
if err := os.Rename(exePath, path.Join(setting.WorkDir, binName)); err != nil {
return fmt.Errorf("fail to move binary: %v", err)
}
} else {
log.Warn("No binary generated")
Expand All @@ -87,14 +81,14 @@ func runBuild(ctx *cli.Context) {
return
}

os.RemoveAll(doc.VENDOR)
os.RemoveAll(setting.DefaultVendor)
if !setting.Debug {
defer os.RemoveAll(doc.VENDOR)
defer os.RemoveAll(setting.DefaultVendor)
}
if err := buildBinary(ctx, ctx.Args()...); err != nil {
errors.SetError(err)
return
}

log.Success("SUCC", "build", "Command executed successfully!")
log.Info("Command executed successfully!")
}
12 changes: 4 additions & 8 deletions cmd/clean.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 All @@ -16,11 +16,8 @@ package cmd

import (
"os"
"path"

"github.com/codegangsta/cli"

"github.com/gpmgo/gopm/modules/doc"
"github.com/gpmgo/gopm/modules/cli"
"github.com/gpmgo/gopm/modules/errors"
"github.com/gpmgo/gopm/modules/setting"
)
Expand All @@ -33,7 +30,7 @@ var CmdClean = cli.Command{
gopm clean`,
Action: runClean,
Flags: []cli.Flag{
cli.BoolFlag{"verbose, v", "show process details"},
cli.BoolFlag{"verbose, v", "show process details", ""},
},
}

Expand All @@ -43,6 +40,5 @@ func runClean(ctx *cli.Context) {
return
}

os.RemoveAll(doc.VENDOR)
os.RemoveAll(path.Join(setting.HomeDir, ".gopm/temp"))
os.RemoveAll(setting.DefaultVendor)
}
Loading

0 comments on commit b9f79e3

Please sign in to comment.