github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/lifecycle/build/command.go (about)

     1  package build
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/wawandco/oxpecker/internal/log"
     9  	"github.com/wawandco/oxpecker/plugins"
    10  )
    11  
    12  var _ plugins.Command = (*Command)(nil)
    13  
    14  type Command struct {
    15  	buildPlugins []plugins.Plugin
    16  
    17  	builders       []Builder
    18  	afterBuilders  []AfterBuilder
    19  	beforeBuilders []BeforeBuilder
    20  }
    21  
    22  func (b Command) Name() string {
    23  	return "build"
    24  }
    25  
    26  func (b Command) ParentName() string {
    27  	return ""
    28  }
    29  
    30  func (c Command) Alias() string {
    31  	return "b"
    32  }
    33  
    34  //HelpText returns the help Text of build function
    35  func (b Command) HelpText() string {
    36  	return "builds a buffalo app from within the root folder of the project"
    37  }
    38  
    39  // Run builds a buffalo app from within the root folder of the project
    40  // To do so, It:x
    41  // - Sets GO_ENV to be production
    42  // - Runs NPM or YARN depending on what if finds
    43  // - Runs Packr, Pkger or Other Packing tool
    44  // - Injects database.yml and inflections.
    45  // - Overrides main.go to add migrate
    46  // - Runs go build
    47  func (b *Command) Run(ctx context.Context, root string, args []string) error {
    48  	err := b.setenv()
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	for _, builder := range b.beforeBuilders {
    54  		fmt.Printf(">>> %v BeforeBuilder Running \n", builder.Name())
    55  
    56  		err = builder.RunBeforeBuild(ctx, root, args)
    57  		if err != nil {
    58  			log.Error(err.Error())
    59  			break
    60  		}
    61  	}
    62  
    63  	if err == nil {
    64  		for _, builder := range b.builders {
    65  			fmt.Printf(">>> %v Builder Running \n", builder.Name())
    66  
    67  			err = builder.Build(ctx, root, args)
    68  			if err != nil {
    69  				log.Errorf("%v\n", err.Error())
    70  				break
    71  			}
    72  		}
    73  	}
    74  
    75  	for _, afterBuilder := range b.afterBuilders {
    76  		fmt.Printf(">>> %v AfterBuilder Running \n", afterBuilder.Name())
    77  
    78  		err = afterBuilder.RunAfterBuild(root, args)
    79  		if err != nil {
    80  			return err
    81  		}
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func (b *Command) Receive(plugins []plugins.Plugin) {
    88  	for _, plugin := range plugins {
    89  		isBuildPlugin := false
    90  		if ptool, ok := plugin.(BeforeBuilder); ok {
    91  			isBuildPlugin = true
    92  			b.beforeBuilders = append(b.beforeBuilders, ptool)
    93  		}
    94  
    95  		if ptool, ok := plugin.(Builder); ok {
    96  			isBuildPlugin = true
    97  			b.builders = append(b.builders, ptool)
    98  		}
    99  
   100  		if ptool, ok := plugin.(AfterBuilder); ok {
   101  			isBuildPlugin = true
   102  			b.afterBuilders = append(b.afterBuilders, ptool)
   103  		}
   104  
   105  		if isBuildPlugin {
   106  			b.buildPlugins = append(b.buildPlugins, plugin)
   107  		}
   108  	}
   109  }
   110  
   111  func (b *Command) setenv() error {
   112  	env := os.Getenv("GO_ENV")
   113  	if env != "" {
   114  		return nil
   115  	}
   116  
   117  	return os.Setenv("GO_ENV", "production")
   118  }