github.com/getgauge/gauge@v1.6.9/skel/plugin.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package skel
     8  
     9  import (
    10  	"github.com/getgauge/gauge/env"
    11  	"github.com/getgauge/gauge/logger"
    12  	"github.com/getgauge/gauge/plugin"
    13  	"github.com/getgauge/gauge/plugin/install"
    14  	"os"
    15  	"strconv"
    16  	"strings"
    17  )
    18  
    19  const (
    20  	screenshot = "screenshot"
    21  	html       = "html-report"
    22  )
    23  
    24  var SetupPlugins = func(silent bool) {
    25  	installPlugins(getPluginsToInstall(), silent)
    26  }
    27  
    28  func getPluginsToInstall() (plugins []string) {
    29  	requiredPlugins := []string{html}
    30  	if screenshotEnabled, err := strconv.ParseBool(strings.TrimSpace(os.Getenv(env.ScreenshotOnFailure))); err == nil && screenshotEnabled {
    31  		requiredPlugins = append(requiredPlugins, screenshot)
    32  	}
    33  	for _, p := range requiredPlugins {
    34  		if !plugin.IsPluginInstalled(p, "") {
    35  			plugins = append(plugins, p)
    36  		}
    37  	}
    38  	return
    39  }
    40  
    41  func installPlugins(plugins []string, silent bool) {
    42  	if len(plugins) > 0 {
    43  		logger.Info(true, "Installing required plugins.")
    44  	}
    45  	for _, p := range plugins {
    46  		installPlugin(p, silent)
    47  	}
    48  }
    49  
    50  func installPlugin(name string, silent bool) {
    51  	logger.Debugf(true, "Installing plugin '%s'", name)
    52  	res := install.Plugin(name, "", silent)
    53  	if res.Error != nil {
    54  		logger.Debug(true, res.Error.Error())
    55  	} else if res.Version != "" {
    56  		logger.Infof(true, "Successfully installed plugin '%s' version %s", name, res.Version)
    57  	} else {
    58  		logger.Infof(true, "Successfully installed plugin '%s'", name)
    59  	}
    60  }