-
Notifications
You must be signed in to change notification settings - Fork 55
/
gradle.go
216 lines (182 loc) · 6.04 KB
/
gradle.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package gradle
import (
"fmt"
gofrogcmd "github.com/jfrog/gofrog/io"
"github.com/jfrog/jfrog-cli-core/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/utils/config"
"github.com/jfrog/jfrog-cli-core/utils/coreutils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
const gradleExtractorDependencyVersion = "4.15.2"
const gradleInitScriptTemplate = "gradle.init"
const usePlugin = "useplugin"
const useWrapper = "usewrapper"
const gradleBuildInfoProperties = "BUILDINFO_PROPFILE"
type GradleCommand struct {
tasks string
configPath string
configuration *utils.BuildConfiguration
rtDetails *config.ArtifactoryDetails
threads int
}
func NewGradleCommand() *GradleCommand {
return &GradleCommand{}
}
// Returns the ArtfiactoryDetails. The information returns from the config file provided.
func (gc *GradleCommand) RtDetails() (*config.ArtifactoryDetails, error) {
// Get the rtDetails from the config file.
var err error
if gc.rtDetails == nil {
vConfig, err := utils.ReadConfigFile(gc.configPath, utils.YAML)
if err != nil {
return nil, err
}
gc.rtDetails, err = utils.GetRtDetails(vConfig)
}
return gc.rtDetails, err
}
func (gc *GradleCommand) SetRtDetails(rtDetails *config.ArtifactoryDetails) *GradleCommand {
gc.rtDetails = rtDetails
return gc
}
func (gc *GradleCommand) Run() error {
gradleDependenciesDir, gradlePluginFilename, err := downloadGradleDependencies()
if err != nil {
return err
}
gradleRunConfig, err := createGradleRunConfig(gc.tasks, gc.configPath, gc.configuration, gc.threads, gradleDependenciesDir, gradlePluginFilename)
if err != nil {
return err
}
defer os.Remove(gradleRunConfig.env[gradleBuildInfoProperties])
if err := gofrogcmd.RunCmd(gradleRunConfig); err != nil {
return err
}
return nil
}
func (gc *GradleCommand) CommandName() string {
return "rt_gradle"
}
func (gc *GradleCommand) SetConfiguration(configuration *utils.BuildConfiguration) *GradleCommand {
gc.configuration = configuration
return gc
}
func (gc *GradleCommand) SetConfigPath(configPath string) *GradleCommand {
gc.configPath = configPath
return gc
}
func (gc *GradleCommand) SetTasks(tasks string) *GradleCommand {
gc.tasks = tasks
return gc
}
func (gc *GradleCommand) SetThreads(threads int) *GradleCommand {
gc.threads = threads
return gc
}
func downloadGradleDependencies() (gradleDependenciesDir, gradlePluginFilename string, err error) {
dependenciesPath, err := config.GetJfrogDependenciesPath()
if err != nil {
return
}
gradleDependenciesDir = filepath.Join(dependenciesPath, "gradle", gradleExtractorDependencyVersion)
gradlePluginFilename = fmt.Sprintf("build-info-extractor-gradle-%s-uber.jar", gradleExtractorDependencyVersion)
filePath := fmt.Sprintf("org/jfrog/buildinfo/build-info-extractor-gradle/%s", gradleExtractorDependencyVersion)
downloadPath := path.Join(filePath, gradlePluginFilename)
filepath.Join(gradleDependenciesDir, gradlePluginFilename)
err = utils.DownloadExtractorIfNeeded(downloadPath, filepath.Join(gradleDependenciesDir, gradlePluginFilename))
return
}
func createGradleRunConfig(tasks, configPath string, configuration *utils.BuildConfiguration, threads int, gradleDependenciesDir, gradlePluginFilename string) (*gradleRunConfig, error) {
runConfig := &gradleRunConfig{env: map[string]string{}}
runConfig.tasks = tasks
vConfig, err := utils.ReadConfigFile(configPath, utils.YAML)
if err != nil {
return nil, err
}
runConfig.gradle, err = getGradleExecPath(vConfig.GetBool(useWrapper))
if err != nil {
return nil, err
}
if threads > 0 {
vConfig.Set(utils.FORK_COUNT, threads)
}
runConfig.env[gradleBuildInfoProperties], err = utils.CreateBuildInfoPropertiesFile(configuration.BuildName, configuration.BuildNumber, vConfig, utils.Gradle)
if err != nil {
return nil, err
}
if !vConfig.GetBool(usePlugin) {
runConfig.initScript, err = getInitScript(gradleDependenciesDir, gradlePluginFilename)
if err != nil {
return nil, err
}
}
return runConfig, nil
}
func getInitScript(gradleDependenciesDir, gradlePluginFilename string) (string, error) {
gradleDependenciesDir, err := filepath.Abs(gradleDependenciesDir)
if err != nil {
return "", errorutils.CheckError(err)
}
initScriptPath := filepath.Join(gradleDependenciesDir, gradleInitScriptTemplate)
exists, err := fileutils.IsFileExists(initScriptPath, false)
if exists || err != nil {
return initScriptPath, err
}
gradlePluginPath := filepath.Join(gradleDependenciesDir, gradlePluginFilename)
gradlePluginPath = strings.Replace(gradlePluginPath, "\\", "\\\\", -1)
initScriptContent := strings.Replace(utils.GradleInitScript, "${pluginLibDir}", gradlePluginPath, -1)
if !fileutils.IsPathExists(gradleDependenciesDir, false) {
err = os.MkdirAll(gradleDependenciesDir, 0777)
if errorutils.CheckError(err) != nil {
return "", err
}
}
return initScriptPath, errorutils.CheckError(ioutil.WriteFile(initScriptPath, []byte(initScriptContent), 0644))
}
type gradleRunConfig struct {
gradle string
tasks string
initScript string
env map[string]string
}
func (config *gradleRunConfig) GetCmd() *exec.Cmd {
var cmd []string
cmd = append(cmd, config.gradle)
if config.initScript != "" {
cmd = append(cmd, "--init-script", config.initScript)
}
cmd = append(cmd, strings.Split(config.tasks, " ")...)
log.Info("Running gradle command:", strings.Join(cmd, " "))
return exec.Command(cmd[0], cmd[1:]...)
}
func (config *gradleRunConfig) GetEnv() map[string]string {
return config.env
}
func (config *gradleRunConfig) GetStdWriter() io.WriteCloser {
return nil
}
func (config *gradleRunConfig) GetErrWriter() io.WriteCloser {
return nil
}
func getGradleExecPath(useWrapper bool) (string, error) {
if useWrapper {
if coreutils.IsWindows() {
return "gradlew.bat", nil
}
return "./gradlew", nil
}
gradleExec, err := exec.LookPath("gradle")
if err != nil {
return "", errorutils.CheckError(err)
}
return gradleExec, nil
}