github.com/wawandco/oxplugins@v0.7.11/lifecycle/build/command.go (about)

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