github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/plugins/install/install.go (about)

     1  package install
     2  
     3  import (
     4  	"fmt"
     5  	"go/build"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  
    10  	"github.com/gobuffalo/buffalo/genny/add"
    11  	"github.com/gobuffalo/buffalo/plugins/plugdeps"
    12  	"github.com/gobuffalo/genny/v2"
    13  )
    14  
    15  // New installs plugins and then added them to the config file
    16  func New(opts *Options) (*genny.Group, error) {
    17  	gg := &genny.Group{}
    18  
    19  	if err := opts.Validate(); err != nil {
    20  		return gg, err
    21  	}
    22  
    23  	aopts := &add.Options{
    24  		App:     opts.App,
    25  		Plugins: opts.Plugins,
    26  	}
    27  
    28  	if err := aopts.Validate(); err != nil {
    29  		return gg, err
    30  	}
    31  
    32  	g := genny.New()
    33  	proot := filepath.Join(opts.App.Root, "plugins")
    34  	for _, p := range opts.Plugins {
    35  		if len(p.GoGet) == 0 {
    36  			continue
    37  		}
    38  
    39  		var args []string
    40  		if len(p.Tags) > 0 {
    41  			args = append(args, "-tags", p.Tags.String())
    42  		}
    43  		bargs := []string{"get"}
    44  		bargs = append(bargs, args...)
    45  		bargs = append(bargs, p.GoGet)
    46  		g.Command(exec.Command("go", bargs...))
    47  		if opts.Vendor {
    48  			g.RunFn(pRun(proot, p))
    49  		}
    50  	}
    51  	gg.Add(g)
    52  
    53  	g, err := add.New(aopts)
    54  	if err != nil {
    55  		return gg, err
    56  	}
    57  
    58  	gg.Add(g)
    59  
    60  	return gg, nil
    61  }
    62  
    63  func pRun(proot string, p plugdeps.Plugin) genny.RunFn {
    64  	return func(r *genny.Runner) error {
    65  		c := build.Default
    66  		if c.GOOS == "windows" {
    67  			return fmt.Errorf("vendoring of plugins is currently not supported on windows. PRs are VERY welcome! :)")
    68  		}
    69  
    70  		bp := filepath.Join(c.GOPATH, "bin", p.Binary)
    71  		sf, err := r.FindFile(bp)
    72  		if err != nil {
    73  			return err
    74  		}
    75  
    76  		pbp := filepath.Join(proot, p.Binary)
    77  		r.Disk.Delete(pbp)
    78  
    79  		df := genny.NewFile(pbp, sf)
    80  		if err := r.File(df); err != nil {
    81  			return err
    82  		}
    83  
    84  		os.Chmod(pbp, 0555)
    85  
    86  		return nil
    87  	}
    88  }