github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/base/build/command.go (about)

     1  package build
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/wawandco/ox/plugins/core"
     9  )
    10  
    11  var _ core.Command = (*Command)(nil)
    12  
    13  type Command struct {
    14  	buildPlugins []core.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  func (c Command) Alias() string {
    30  	return "b"
    31  }
    32  
    33  //HelpText returns the help Text of build function
    34  func (b Command) HelpText() string {
    35  	return "builds a buffalo app from within the root folder of the project"
    36  }
    37  
    38  // Run builds a buffalo app from within the root folder of the project
    39  // To do so, It:x
    40  // - Sets GO_ENV to be production
    41  // - Runs NPM or YARN depending on what if finds
    42  // - Runs Packr, Pkger or Other Packing tool
    43  // - Injects database.yml and inflections.
    44  // - Overrides main.go to add migrate
    45  // - Runs go build
    46  func (b *Command) Run(ctx context.Context, root string, args []string) error {
    47  	err := b.setenv()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	for _, builder := range b.beforeBuilders {
    53  		fmt.Printf(">>> %v BeforeBuilder Running \n", builder.Name())
    54  
    55  		err = builder.RunBeforeBuild(ctx, root, args)
    56  		if err != nil {
    57  			return err
    58  		}
    59  	}
    60  
    61  	for _, builder := range b.builders {
    62  		fmt.Printf(">>> %v Builder Running \n", builder.Name())
    63  
    64  		err = builder.Build(ctx, root, args)
    65  		if err != nil {
    66  			return err
    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 []core.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  }