github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/fix/plugins.go (about)

     1  package fix
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path"
     8  	"strings"
     9  
    10  	"github.com/JarrahG/buffalocli/internal/genny/plugins/install"
    11  
    12  	cmdPlugins "github.com/JarrahG/buffalocli/internal/cmd/plugins"
    13  	"github.com/JarrahG/buffalocli/internal/plugins"
    14  	"github.com/JarrahG/buffalocli/internal/plugins/plugdeps"
    15  	"github.com/gobuffalo/genny/v2"
    16  )
    17  
    18  var oldPlugins = []string{
    19  	"github.com/gobuffalo/buffalo-pop",
    20  	"github.com/gobuffalo/buffalo-pop/v2",
    21  }
    22  
    23  // CleanPluginCache cleans the plugins cache folder by removing it
    24  func CleanPluginCache(r *genny.Runner) error {
    25  	fmt.Println("~~~ Cleaning plugins cache ~~~")
    26  	os.RemoveAll(plugins.CachePath)
    27  	return nil
    28  }
    29  
    30  // ReinstallPlugins installs latest versions of the plugins
    31  func ReinstallPlugins(opts *Options) genny.RunFn {
    32  	return func(r *genny.Runner) error {
    33  		plugs, err := plugdeps.List(opts.App)
    34  		if err != nil && !errors.Is(err, plugdeps.ErrMissingConfig) {
    35  			return err
    36  		}
    37  
    38  		fmt.Println("~~~ Reinstalling plugins ~~~")
    39  
    40  		gg, err := install.New(&install.Options{
    41  			App:     opts.App,
    42  			Plugins: plugs.List(),
    43  		})
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		r.WithGroup(gg)
    49  		return nil
    50  	}
    51  }
    52  
    53  // RemoveOldPlugins removes old and deprecated plugins
    54  func RemoveOldPlugins(opts *Options) genny.RunFn {
    55  	return func(r *genny.Runner) error {
    56  		fmt.Println("~~~ Removing old plugins ~~~")
    57  
    58  		plugs, err := plugdeps.List(opts.App)
    59  		if err != nil && !errors.Is(err, plugdeps.ErrMissingConfig) {
    60  			return err
    61  		}
    62  
    63  		for _, p := range oldPlugins {
    64  			a := strings.TrimSpace(p)
    65  			bin := path.Base(a)
    66  			plugs.Remove(plugdeps.Plugin{
    67  				Binary: bin,
    68  			})
    69  
    70  			fmt.Println("~~~ Removing", p, "plugin ~~~")
    71  			r.WithRun(cmdPlugins.NewEncodePluginsRunner(opts.App, plugs))
    72  		}
    73  		return nil
    74  	}
    75  }