forked from gobuffalo/buffalo
/
build.go
82 lines (67 loc) · 2.6 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cmd
import (
"context"
"os"
"path/filepath"
"github.com/gobuffalo/buffalo/buffalo/cmd/build"
"github.com/gobuffalo/buffalo/meta"
"github.com/markbates/sigtx"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var buildOptions = struct {
build.Options
SkipAssets bool
Tags string
}{
Options: build.Options{},
}
var xbuildCmd = &cobra.Command{
Use: "build",
Aliases: []string{"b", "bill"},
Short: "Builds a Buffalo binary, including bundling of assets (packr & webpack)",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := sigtx.WithCancel(context.Background(), os.Interrupt)
defer cancel()
buildOptions.Options.WithAssets = !buildOptions.SkipAssets
if buildOptions.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
b := build.New(ctx, buildOptions.Options)
if buildOptions.Tags != "" {
b.Tags = append(b.Tags, buildOptions.Tags)
}
go func() {
<-ctx.Done()
if ctx.Err() == context.Canceled {
logrus.Info("~~~ BUILD CANCELLED ~~~")
err := b.Cleanup()
if err != nil {
logrus.Fatal(err)
}
}
}()
err := b.Run()
if err != nil {
return errors.WithStack(err)
}
logrus.Infof("\nYou application was successfully built at %s\n", filepath.Join(b.Root, b.Bin))
return nil
},
}
func init() {
RootCmd.AddCommand(xbuildCmd)
pwd, _ := os.Getwd()
buildOptions.App = meta.New(pwd)
xbuildCmd.Flags().StringVarP(&buildOptions.Bin, "output", "o", buildOptions.Bin, "set the name of the binary")
xbuildCmd.Flags().StringVarP(&buildOptions.Tags, "tags", "t", "", "compile with specific build tags")
xbuildCmd.Flags().BoolVarP(&buildOptions.ExtractAssets, "extract-assets", "e", false, "extract the assets and put them in a distinct archive")
xbuildCmd.Flags().BoolVarP(&buildOptions.SkipAssets, "skip-assets", "k", false, "skip running webpack and building assets")
xbuildCmd.Flags().BoolVarP(&buildOptions.Static, "static", "s", false, "build a static binary using --ldflags '-linkmode external -extldflags \"-static\"'")
xbuildCmd.Flags().StringVar(&buildOptions.LDFlags, "ldflags", "", "set any ldflags to be passed to the go build")
xbuildCmd.Flags().BoolVarP(&buildOptions.Debug, "debug", "d", false, "print debugging information")
xbuildCmd.Flags().BoolVarP(&buildOptions.Compress, "compress", "c", true, "compress static files in the binary")
xbuildCmd.Flags().BoolVar(&buildOptions.SkipTemplateValidation, "skip-template-validation", false, "skip validating plush templates")
xbuildCmd.Flags().StringVarP(&buildOptions.Environment, "environment", "", "development", "set the environment for the binary")
}