-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgauge-ruby.go
188 lines (167 loc) · 5.18 KB
/
gauge-ruby.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*----------------------------------------------------------------
* Copyright (c) ThoughtWorks, Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE.txt in the project root for license information.
*----------------------------------------------------------------*/
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"path"
"github.com/getgauge/common"
)
const (
step_implementation_file = "step_implementation.rb"
step_implementations_dir = "step_implementations"
gem_file = "Gemfile"
ruby_properties_file = "ruby.properties"
skelDir = "skel"
envDir = "env"
)
var start = flag.Bool("start", false, "Start the gauge ruby runner")
var initialize = flag.Bool("init", false, "Initialize the gauge ruby runner")
var pluginDir = ""
var projectRoot = ""
type logger struct {
LogLevel string `json:"logLevel"`
Message string `json:"message"`
}
type initializerFunc func()
func logMessage(level, text string) {
c := &logger{LogLevel: level, Message: text}
if b, err := json.Marshal(c); err != nil {
fmt.Println(text)
} else {
fmt.Println(string(b))
}
}
func showMessage(action, filename string) string {
return fmt.Sprintf(" %s %s\n", action, filename)
}
func createStepImplementationsDirectory() {
createDirectory(path.Join(projectRoot, "step_implementations"))
}
func createEnvDir() {
createDirectory(path.Join(projectRoot, "env", "default"))
}
func createDirectory(filePath string) {
logMessage("info", showMessage("create", filePath))
if !common.DirExists(filePath) {
err := os.MkdirAll(filePath, 0755)
if err != nil {
logMessage("error", fmt.Sprintf("Failed to make directory. %s\n", err.Error()))
}
} else {
logMessage("info", showMessage("skip", filePath))
}
}
func createStepImplementationFile() {
destFile := path.Join(projectRoot, step_implementations_dir, step_implementation_file)
logMessage("info", showMessage("create", destFile))
if common.FileExists(destFile) {
logMessage("info", showMessage("skip", destFile))
} else {
srcFile := path.Join(skelDir, step_implementation_file)
err := common.CopyFile(srcFile, destFile)
if err != nil {
logMessage("error", fmt.Sprintf("Failed to copy %s. %s", srcFile, err.Error()))
}
}
}
func createOrAppendToGemFile() {
destFile := path.Join(projectRoot, gem_file)
srcFile := path.Join(skelDir, gem_file)
logMessage("info", showMessage("create", destFile))
if !common.FileExists(destFile) {
err := common.CopyFile(srcFile, destFile)
if err != nil {
logMessage("error", fmt.Sprintf("Failed to copy %s. %s", srcFile, err.Error()))
}
}
logMessage("info", showMessage("append", destFile))
f, err := os.OpenFile(destFile, os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
defer f.Close()
version, err := common.GetGaugePluginVersion("ruby")
if err != nil {
panic(err)
}
if _, err = f.WriteString(fmt.Sprintf("gem 'gauge-ruby', '~>%s', :group => [:development, :test]\n", version)); err != nil {
panic(err)
}
}
func createRubyPropertiesFile() {
destFile := path.Join(projectRoot, envDir, "default", ruby_properties_file)
logMessage("info", showMessage("create", destFile))
if common.FileExists(destFile) {
logMessage("info", showMessage("skip", destFile))
} else {
srcFile := path.Join(skelDir, envDir, ruby_properties_file)
err := common.CopyFile(srcFile, destFile)
if err != nil {
logMessage("error", fmt.Sprintf("Failed to copy %s. %s", srcFile, err.Error()))
}
}
}
func createOrAppendGitignore() {
destFile := path.Join(projectRoot, ".gitignore")
srcFile := path.Join(pluginDir, skelDir, ".gitignore")
logMessage("info", showMessage("create", destFile))
if err := common.AppendToFile(srcFile, destFile); err != nil {
logMessage("error", err.Error())
}
}
func printUsage() {
flag.PrintDefaults()
os.Exit(2)
}
func runCommand(cmdName string, arg ...string) error {
cmd := exec.Command(cmdName, arg...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
//TODO: move to logs
//fmt.Println(cmd.Args)
err := cmd.Run()
return err
}
func main() {
flag.Parse()
var err error
pluginDir, err = os.Getwd()
if err != nil {
logMessage("fatal", fmt.Sprintf("Failed to find current working directory: %s \n", err))
os.Exit(1)
}
projectRoot = os.Getenv(common.GaugeProjectRootEnv)
if projectRoot == "" {
fmt.Printf("Could not find %s env. Ruby Runner exiting...", common.GaugeProjectRootEnv)
os.Exit(1)
}
if *start {
os.Chdir(projectRoot)
err = runCommand("bundle", "exec", "ruby", "-e", "require 'gauge_runtime'")
if err != nil {
logMessage("fatal", fmt.Sprintf("Ruby runner Failed. Reason: %s\n", err.Error()))
os.Exit(1)
}
} else if *initialize {
funcs := []initializerFunc{createStepImplementationsDirectory, createStepImplementationFile, createEnvDir, createRubyPropertiesFile, createOrAppendToGemFile, createOrAppendGitignore}
for _, f := range funcs {
f()
}
os.Chdir(projectRoot)
fmt.Printf("Running bundle install.. in %s\n", projectRoot)
err = runCommand("bundle", "install")
if err != nil {
logMessage("error", fmt.Sprintf("bundle install Failed. Reason: %s\nConsider running `bundle install` again.\n", err.Error()))
}
} else {
printUsage()
}
}