From 7326b09b3d91ba8cf398cc5467d0493118c255e2 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 11 Nov 2013 01:03:11 -0500 Subject: [PATCH] Done build and install --- README.md | 17 ++++++++++------- cmd/build.go | 42 +++++++++++++++++++---------------------- cmd/gopath.go | 45 +++++++++++++++++++++++++++----------------- cmd/install.go | 51 +++++++++++++++++++++++++++++--------------------- cmd/run.go | 5 +++-- gopm.go | 7 +++---- 6 files changed, 93 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 9abcafd48..1fbf8532f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ gopm - Go Package Manager Gopm(Go Package Manager) is a Go package manage tool for search, install, update and share packages in Go. -Current Version: **v0.5.1** +Current Version: **v0.5.5** # Requirement @@ -40,16 +40,19 @@ USAGE: gopm [global options] command [command options] [arguments...] VERSION: - 0.5.2.1109 + 0.5.5.1111 COMMANDS: - get fetch remote package(s) and dependencies to local repository - gen generate a gopmfile according current go project - help, h Shows a list of commands or help for one command + get fetch remote package(s) and dependencies to local repository + gen generate a gopmfile according current go project + run link dependencies and go run + build link dependencies and go build + install link dependencies and go install + help, h Shows a list of commands or help for one command GLOBAL OPTIONS: - --version print the version - --help, -h show help + --version print the version + --help, -h show help ``` diff --git a/cmd/build.go b/cmd/build.go index 8069f558a..e94e3b2c4 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -15,37 +15,33 @@ package cmd import ( - "github.com/Unknwon/com" -) + "github.com/codegangsta/cli" -var CmdBuild = &Command{ - UsageLine: "build", - Short: "build according a gopmfile", - Long: ` -build just like go build -`, -} + "github.com/gpmgo/gopm/log" +) -func init() { - CmdBuild.Run = runBuild - CmdBuild.Flags = map[string]bool{} -} +var CmdBuild = cli.Command{ + Name: "build", + Usage: "link dependencies and go build", + Description: `Command build links dependencies according to gopmfile, +and execute 'go build' -func printBuildPrompt(flag string) { +gopm build `, + Action: runBuild, } -func runBuild(cmd *Command, args []string) { - //genNewGoPath() +func runBuild(ctx *cli.Context) { + genNewGoPath(ctx) - com.ColorLog("[INFO] building ...\n") + log.Trace("Building...") - cmds := []string{"go", "build"} - cmds = append(cmds, args...) - err := execCmd(newGoPath, newCurPath, cmds...) + cmdArgs := []string{"go", "build"} + cmdArgs = append(cmdArgs, ctx.Args()...) + err := execCmd(newGoPath, newCurPath, cmdArgs...) if err != nil { - com.ColorLog("[ERRO] build failed: %v\n", err) - return + log.Error("Build", "Fail to build program") + log.Fatal("", err.Error()) } - com.ColorLog("[SUCC] build successfully!\n") + log.Success("SUCC", "Build", "Command execute successfully!") } diff --git a/cmd/gopath.go b/cmd/gopath.go index ed76c69e2..960df2adb 100644 --- a/cmd/gopath.go +++ b/cmd/gopath.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "go/build" "os" "os/exec" @@ -110,44 +111,54 @@ var newGoPath string func execCmd(gopath, curPath string, args ...string) error { cwd, err := os.Getwd() if err != nil { - return err + log.Error("", "Fail to get work directory") + log.Fatal("", err.Error()) } - com.ColorLog("[INFO] change current dir from %v to %v\n", cwd, curPath) - err = os.Chdir(filepath.Join(cwd, "vendor")) - if err != nil { - com.ColorLog("[ERRO] change current directory error %v\n", err) - return err - } + log.Log("Changing work directory to %s", curPath) err = os.Chdir(curPath) if err != nil { - com.ColorLog("[ERRO] change current directory error %v\n", err) - return err + log.Error("", "Fail to change work directory") + log.Fatal("", err.Error()) } - defer os.Chdir(cwd) + defer func() { + log.Log("Changing work directory back to %s", cwd) + os.Chdir(cwd) + }() + ccmd := exec.Command("cd", curPath) ccmd.Stdout = os.Stdout ccmd.Stderr = os.Stderr err = ccmd.Run() if err != nil { - com.ColorLog("[ERRO] change current directory error %v\n", err) - return err + log.Error("", "Fail to change work directory") + log.Fatal("", err.Error()) } oldGoPath := os.Getenv("GOPATH") - com.ColorLog("[TRAC] set GOPATH from %v to %v\n", oldGoPath, gopath) + log.Log("Setting GOPATH to %s", gopath) err = os.Setenv("GOPATH", gopath) if err != nil { - com.ColorLog("[ERRO] %v\n", err) - return err + log.Error("", "Fail to setting GOPATH") + log.Fatal("", err.Error()) } - defer os.Setenv("GOPATH", oldGoPath) + defer func() { + log.Log("Setting GOPATH back to %s", oldGoPath) + os.Setenv("GOPATH", oldGoPath) + }() cmd := exec.Command(args[0], args[1:]...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - return cmd.Run() + + log.Log("===== application outputs start =====\n") + + err = cmd.Run() + + fmt.Println() + log.Log("====== application outputs end ======") + return err } func genNewGoPath(ctx *cli.Context) { diff --git a/cmd/install.go b/cmd/install.go index 58ef75a27..8080d1b4c 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -16,36 +16,45 @@ package cmd import ( "github.com/Unknwon/com" + "github.com/codegangsta/cli" + + "github.com/gpmgo/gopm/doc" + "github.com/gpmgo/gopm/log" ) -var CmdInstall = &Command{ - UsageLine: "install", - Short: "install according a gopmfile", - Long: ` -install just like go install -`, -} +var CmdInstall = cli.Command{ + Name: "install", + Usage: "link dependencies and go install", + Description: `Command install links dependencies according to gopmfile, +and execute 'go install' -func init() { - CmdInstall.Run = runInstall - CmdInstall.Flags = map[string]bool{} +gopm install`, + Action: runInstall, } -func printInstallPrompt(flag string) { -} +func runInstall(ctx *cli.Context) { + if !com.IsFile(".gopmfile") { + log.Fatal("Install", "No gopmfile exist in work directory") + } + + gf := doc.NewGopmfile(".") + target := gf.MustValue("target", "path") + if len(target) == 0 { + log.Fatal("Install", "Cannot find target in gopmfile") + } -func runInstall(cmd *Command, args []string) { - //genNewGoPath() + genNewGoPath(ctx) - com.ColorLog("[INFO] installing ...\n") + log.Trace("Installing...") - cmds := []string{"go", "install"} - cmds = append(cmds, args...) - err := execCmd(newGoPath, newCurPath, cmds...) + cmdArgs := []string{"go", "install"} + cmdArgs = append(cmdArgs, ctx.Args()...) + cmdArgs = append(cmdArgs, target) + err := execCmd(newGoPath, newCurPath, cmdArgs...) if err != nil { - com.ColorLog("[ERRO] install failed: %v\n", err) - return + log.Error("Install", "Fail to install program") + log.Fatal("", err.Error()) } - com.ColorLog("[SUCC] install successfully!\n") + log.Success("SUCC", "Install", "Command execute successfully!") } diff --git a/cmd/run.go b/cmd/run.go index 65a04eb47..88764eea0 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -28,9 +28,10 @@ import ( var CmdRun = cli.Command{ Name: "run", Usage: "link dependencies and go run", - Description: `Command run links dependencies according to gopmfile + Description: `Command run links dependencies according to gopmfile, +and execute 'go run' -gopm run `, +gopm run `, Action: runRun, } diff --git a/gopm.go b/gopm.go index dc1d0ff3e..6b2ab0f62 100644 --- a/gopm.go +++ b/gopm.go @@ -29,12 +29,9 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.5.4.1110" +const APP_VER = "0.5.5.1111" // //cmd.CmdSearch, -// cmd.CmdBuild, -// cmd.CmdInstall, - // cmdClean, // cmdDoc, // cmdEnv, @@ -58,6 +55,8 @@ func main() { cmd.CmdGet, cmd.CmdGen, cmd.CmdRun, + cmd.CmdBuild, + cmd.CmdInstall, } app.Run(os.Args) }