github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/cmds/build/main.go (about)

     1  package build
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/gobuffalo/here"
     7  	"github.com/gobuffalo/plugins"
     8  	"github.com/gobuffalo/plugins/plugio"
     9  	"github.com/gobuffalo/plugins/plugprint"
    10  )
    11  
    12  func (cmd *Cmd) Main(ctx context.Context, root string, args []string) error {
    13  	plugs := cmd.ScopedPlugins()
    14  	if sub := FindBuilderFromArgs(args, plugs); sub != nil {
    15  		return sub.Build(ctx, root, args[1:])
    16  	}
    17  
    18  	flags := cmd.Flags()
    19  	if err := flags.Parse(args); err != nil {
    20  		return plugins.Wrap(cmd, err)
    21  	}
    22  
    23  	if cmd.help {
    24  		return plugprint.Print(plugio.Stdout(cmd.ScopedPlugins()...), cmd)
    25  	}
    26  
    27  	err := cmd.run(ctx, root, args)
    28  	return cmd.afterBuild(ctx, root, args, err)
    29  }
    30  
    31  func (cmd *Cmd) run(ctx context.Context, root string, args []string) error {
    32  	info, err := here.Dir(root)
    33  	if err != nil {
    34  		return plugins.Wrap(cmd, err)
    35  	}
    36  
    37  	if err = cmd.beforeBuild(ctx, root, args); err != nil {
    38  		return plugins.Wrap(cmd, err)
    39  	}
    40  
    41  	plugs := cmd.ScopedPlugins()
    42  
    43  	if err := cmd.pack(ctx, info, plugs); err != nil {
    44  		return plugins.Wrap(cmd, err)
    45  	}
    46  
    47  	return cmd.build(ctx, root)
    48  }
    49  
    50  func (cmd *Cmd) beforeBuild(ctx context.Context, root string, args []string) error {
    51  	plugs := cmd.ScopedPlugins()
    52  	for _, p := range plugs {
    53  		if bb, ok := p.(BeforeBuilder); ok {
    54  			if err := bb.BeforeBuild(ctx, root, args); err != nil {
    55  				return plugins.Wrap(p, err)
    56  			}
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  func (cmd *Cmd) afterBuild(ctx context.Context, root string, args []string, err error) error {
    63  	plugs := cmd.ScopedPlugins()
    64  	for _, p := range plugs {
    65  		if bb, ok := p.(AfterBuilder); ok {
    66  			if err := bb.AfterBuild(ctx, root, args, err); err != nil {
    67  				return plugins.Wrap(p, err)
    68  			}
    69  		}
    70  	}
    71  	return err
    72  }