add build command

This commit is contained in:
c9s 2020-10-26 13:27:07 +08:00
parent 9017d2a9a7
commit ac0a26b005
2 changed files with 115 additions and 27 deletions

73
pkg/cmd/build.go Normal file
View File

@ -0,0 +1,73 @@
package cmd
import (
"context"
"path/filepath"
"runtime"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/c9s/bbgo/pkg/config"
)
func init() {
BuildCmd.Flags().StringP("output", "o", "", "binary output")
BuildCmd.Flags().String("os", runtime.GOOS, "GOOS")
BuildCmd.Flags().String("arch", runtime.GOARCH, "GOARCH")
BuildCmd.Flags().String("config", "config/bbgo.yaml", "strategy config file")
RootCmd.AddCommand(BuildCmd)
}
var BuildCmd = &cobra.Command{
Use: "build",
Short: "build cross-platform binary",
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
if len(configFile) == 0 {
return errors.New("--config option is required")
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return err
}
userConfig, err := config.Load(configFile)
if err != nil {
return err
}
goOS, err := cmd.Flags().GetString("os")
if err != nil {
return err
}
goArch, err := cmd.Flags().GetString("arch")
if err != nil {
return err
}
buildDir := filepath.Join("build", "bbgow")
binary, err := build(ctx, buildDir, userConfig, goOS, goArch, &output)
if err != nil {
return err
}
log.Infof("build succeeded: %s", binary)
return nil
},
}

View File

@ -41,7 +41,7 @@ func init() {
RootCmd.AddCommand(RunCmd) RootCmd.AddCommand(RunCmd)
} }
var runTemplate = template.Must(template.New("main").Parse(`package main var wrapperTemplate = template.Must(template.New("main").Parse(`package main
// DO NOT MODIFY THIS FILE. THIS FILE IS GENERATED FOR IMPORTING STRATEGIES // DO NOT MODIFY THIS FILE. THIS FILE IS GENERATED FOR IMPORTING STRATEGIES
import ( import (
"github.com/c9s/bbgo/pkg/cmd" "github.com/c9s/bbgo/pkg/cmd"
@ -59,7 +59,7 @@ func main() {
func compileRunFile(filepath string, config *config.Config) error { func compileRunFile(filepath string, config *config.Config) error {
var buf = bytes.NewBuffer(nil) var buf = bytes.NewBuffer(nil)
if err := runTemplate.Execute(buf, config); err != nil { if err := wrapperTemplate.Execute(buf, config); err != nil {
return err return err
} }
@ -151,11 +151,11 @@ var RunCmd = &cobra.Command{
return nil return nil
} }
var flagsArgs = []string{"run", "--no-compile"} var runArgs = []string{"run", "--no-compile"}
cmd.Flags().Visit(func(flag *flag.Flag) { cmd.Flags().Visit(func(flag *flag.Flag) {
flagsArgs = append(flagsArgs, flag.Name, flag.Value.String()) runArgs = append(runArgs, flag.Name, flag.Value.String())
}) })
flagsArgs = append(flagsArgs, args...) runArgs = append(runArgs, args...)
goOS, err := cmd.Flags().GetString("os") goOS, err := cmd.Flags().GetString("os")
if err != nil { if err != nil {
@ -167,8 +167,7 @@ var RunCmd = &cobra.Command{
return err return err
} }
var buildEnvs = []string{"GOOS=" + goOS, "GOARCH=" + goArch} return buildAndRun(ctx, userConfig, goOS, goArch, runArgs...)
return compileAndRun(ctx, userConfig, goOS, goArch, buildEnvs, flagsArgs...)
}, },
} }
@ -187,10 +186,42 @@ func compile(buildDir string, userConfig *config.Config) error {
return nil return nil
} }
func compileAndRun(ctx context.Context, userConfig *config.Config, goOS, goArch string, buildEnvs []string, args ...string) error { func build(ctx context.Context, buildDir string, userConfig *config.Config, goOS, goArch string, output *string) (string, error) {
if err := compile(buildDir, userConfig); err != nil {
return "", err
}
cwd, err := os.Getwd()
if err != nil {
return "", err
}
buildEnvs := []string{"GOOS=" + goOS, "GOARCH=" + goArch}
buildTarget := filepath.Join(cwd, buildDir)
log.Infof("building binary from %s...", buildTarget)
binary := fmt.Sprintf("bbgow-%s-%s", goOS, goArch)
if output != nil {
binary = *output
}
buildCmd := exec.CommandContext(ctx, "go", "build", "-tags", "wrapper", "-o", binary, buildTarget)
buildCmd.Env = append(os.Environ(), buildEnvs...)
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
return "", err
}
return binary, nil
}
func buildAndRun(ctx context.Context, userConfig *config.Config, goOS, goArch string, args ...string) error {
buildDir := filepath.Join("build", "bbgow") buildDir := filepath.Join("build", "bbgow")
if err := compile(buildDir, userConfig); err != nil { binary, err := build(ctx, buildDir, userConfig, goOS, goArch, nil)
if err != nil {
return err return err
} }
@ -199,28 +230,12 @@ func compileAndRun(ctx context.Context, userConfig *config.Config, goOS, goArch
return err return err
} }
buildTarget := filepath.Join(cwd, buildDir)
log.Infof("building binary from %s...", buildTarget)
binary := fmt.Sprintf("bbgow-%s-%s", goOS, goArch)
buildCmd := exec.CommandContext(ctx, "go", "build", "-tags", "wrapper", "-o", binary, buildTarget)
buildCmd.Env = append(os.Environ(), buildEnvs...)
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
return err
}
executePath := filepath.Join(cwd, binary) executePath := filepath.Join(cwd, binary)
log.Infof("running wrapper binary, args: %v", args) log.Infof("running wrapper binary, args: %v", args)
runCmd := exec.CommandContext(ctx, executePath, args...) runCmd := exec.CommandContext(ctx, executePath, args...)
runCmd.Stdout = os.Stdout runCmd.Stdout = os.Stdout
runCmd.Stderr = os.Stderr runCmd.Stderr = os.Stderr
if err := runCmd.Run(); err != nil { return runCmd.Run()
return err
}
return nil
} }