Skip to content

Commit

Permalink
Added --install-zip to install plugin from zipfile, Fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvam committed Mar 4, 2015
1 parent d70bf1e commit ad3c6c6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
11 changes: 11 additions & 0 deletions gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func main() {
initializeProject(*initialize)
} else if *install != "" {
downloadAndInstallPlugin(*install, *installVersion)
} else if *installZip != "" {
installPluginZip(*installZip)
} else if *update != "" {
updatePlugin(*update)
} else if *addPlugin != "" {
Expand Down Expand Up @@ -144,6 +146,7 @@ var initialize = flag.String([]string{"-init"}, "", "Initializes project structu
var install = flag.String([]string{"-install"}, "", "Downloads and installs a plugin. Eg: gauge --install java")
var update = flag.String([]string{"-update"}, "", "Updates a plugin. Eg: gauge --update java")
var installVersion = flag.String([]string{"-plugin-version"}, "", "Version of plugin to be installed. This is used with --install")
var installZip = flag.String([]string{"-install-zip"}, "", "Installs the plugin from zip file. Eg: gauge --install-zip zipfile")
var currentEnv = flag.String([]string{"-env"}, "default", "Specifies the environment. If not specified, default will be used")
var addPlugin = flag.String([]string{"-add-plugin"}, "", "Adds the specified plugin to the current project")
var pluginArgs = flag.String([]string{"-plugin-args"}, "", "Specified additional arguments to the plugin. This is used together with --add-plugin")
Expand Down Expand Up @@ -183,6 +186,14 @@ func downloadAndInstallPlugin(plugin, version string) {
}
}

func installPluginZip(zipFile string) {
if err := installPluginFromZip(zipFile); err != nil {
log.Warning("Failed to install plugin from zip file : %s\n", err)
} else {
log.Info("Successfully installed plugin from zipFile")
}
}

func runInBackground() {
var port int
var err error
Expand Down
61 changes: 60 additions & 1 deletion install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/getgauge/gauge/config"
"os"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
Expand Down Expand Up @@ -195,12 +196,17 @@ func getInstallDescription(plugin string) (*installDescription, error) {
if err != nil {
return nil, err
}

return getInstallDescriptionFromJson(installJson)
}

func getInstallDescriptionFromJson(installJson string) (*installDescription, error) {
InstallJsonContents, readErr := common.ReadFileContents(installJson)
if readErr != nil {
return nil, readErr
}
installDescription := &installDescription{}
if err = json.Unmarshal([]byte(InstallJsonContents), installDescription); err != nil {
if err := json.Unmarshal([]byte(InstallJsonContents), installDescription); err != nil {
return nil, err
}
return installDescription, nil
Expand Down Expand Up @@ -284,3 +290,56 @@ func (a ByDecreasingVersion) Less(i, j int) bool {
version2, _ := parseVersion(a[j].Version)
return version1.isGreaterThan(version2)
}

func installPluginFromZip(zipFile string) error {
unzippedPluginDir, err := common.UnzipArchive(zipFile)
if err != nil {
return errors.New(fmt.Sprintf("Failed to Unzip plugin-zip file %s.", err))
}
log.Info("Plugin unzipped to => %s\n", unzippedPluginDir)
hasPluginJson := common.FileExists(unzippedPluginDir + fmt.Sprintf("%c", filepath.Separator) + "plugin.json")
if hasPluginJson {
return installPluginFromDir(unzippedPluginDir)
} else {
return installRunnerFromDir(unzippedPluginDir)
}
}

func installRunnerFromDir(unzippedPluginDir string) error {
jsonFile, err := common.GetFileWithJsonExtensionInDir(unzippedPluginDir)
if err != nil {
return err
}
var r runner
contents, err := common.ReadFileContents(unzippedPluginDir + fmt.Sprintf("%c", filepath.Separator) + jsonFile)
if err != nil {
return err
}
err = json.Unmarshal([]byte(contents), &r)
if err != nil {
return err
}
return copyPluginFilesToGaugeInstallDir(unzippedPluginDir, r.Id, r.Version)
}

func copyPluginFilesToGaugeInstallDir(unzippedPluginDir string, pluginId string, version string) error {
log.Info("Installing Plugin => %s %s\n", pluginId, version)

pluginsDir, err := common.GetPrimaryPluginsInstallDir()
if err != nil {
return err
}
versionedPluginDir := path.Join(pluginsDir, pluginId, version)
if common.DirExists(versionedPluginDir) {
return errors.New(fmt.Sprintf("Plugin %s %s already installed at %s", pluginId, version, versionedPluginDir))
}
return common.MirrorDir(unzippedPluginDir, versionedPluginDir)
}

func installPluginFromDir(unzippedPluginDir string) error {
pd, err := getPluginDescriptorFromJson(unzippedPluginDir + fmt.Sprintf("%c", filepath.Separator) + "plugin.json")
if err != nil {
return err
}
return copyPluginFilesToGaugeInstallDir(unzippedPluginDir, pd.Id, pd.Version)
}
3 changes: 3 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ func getPluginDescriptor(pluginId, pluginVersion string) (*pluginDescriptor, err
if err != nil {
return nil, err
}
return getPluginDescriptorFromJson(pluginJson)
}

func getPluginDescriptorFromJson(pluginJson string) (*pluginDescriptor, error) {
pluginJsonContents, err := common.ReadFileContents(pluginJson)
if err != nil {
return nil, err
Expand Down
7 changes: 5 additions & 2 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ type testRunner struct {
}

type runner struct {
Name string
Run struct {
Id string
Name string
Version string
Description string
Run struct {
Windows []string
Linux []string
Darwin []string
Expand Down

0 comments on commit ad3c6c6

Please sign in to comment.