github.com/jacobsoderblom/buffalo@v0.11.0/buffalo/cmd/build.go (about)

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