-
Notifications
You must be signed in to change notification settings - Fork 582
/
versioning.go
243 lines (226 loc) · 6.67 KB
/
versioning.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package versioning
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/SAP/jenkins-library/pkg/maven"
"github.com/SAP/jenkins-library/pkg/piperutils"
)
// Coordinates to address the artifact coordinates like groupId, artifactId, version and packaging
type Coordinates struct {
GroupID string
ArtifactID string
Version string
Packaging string
}
// Artifact defines the versioning operations for various build tools
type Artifact interface {
VersioningScheme() string
GetVersion() (string, error)
SetVersion(string) error
GetCoordinates() (Coordinates, error)
}
// Options define build tool specific settings in order to properly retrieve e.g. the version / coordinates of an artifact
type Options struct {
ProjectSettingsFile string
DockerImage string
GlobalSettingsFile string
M2Path string
Defines []string
VersionSource string
VersionSection string
VersionField string
VersioningScheme string
HelmUpdateAppVersion bool
CAPVersioningPreference string
}
// Utils defines the versioning operations for various build tools
type Utils interface {
Stdout(out io.Writer)
Stderr(err io.Writer)
RunExecutable(e string, p ...string) error
DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error
Glob(pattern string) (matches []string, err error)
FileExists(filename string) (bool, error)
Copy(src, dest string) (int64, error)
MkdirAll(path string, perm os.FileMode) error
FileWrite(path string, content []byte, perm os.FileMode) error
FileRead(path string) ([]byte, error)
FileRemove(path string) error
}
type mvnRunner struct{}
func (m *mvnRunner) Execute(options *maven.ExecuteOptions, utils maven.Utils) (string, error) {
return maven.Execute(options, utils)
}
func (m *mvnRunner) Evaluate(options *maven.EvaluateOptions, expression string, utils maven.Utils) (string, error) {
return maven.Evaluate(options, expression, utils)
}
var fileExists func(string) (bool, error)
// GetArtifact returns the build tool specific implementation for retrieving version, etc. of an artifact
func GetArtifact(buildTool, buildDescriptorFilePath string, opts *Options, utils Utils) (Artifact, error) {
var artifact Artifact
if fileExists == nil {
fileExists = piperutils.FileExists
}
// CAPVersioningPreference can only be 'maven' or 'npm'. Verification done on artifactPrepareVersion.yaml level
if buildTool == "CAP" {
buildTool = opts.CAPVersioningPreference
}
switch buildTool {
case "custom":
var err error
artifact, err = customArtifact(buildDescriptorFilePath, opts.VersionField, opts.VersionSection, opts.VersioningScheme)
if err != nil {
return artifact, err
}
case "docker":
artifact = &Docker{
utils: utils,
options: opts,
path: buildDescriptorFilePath,
versionSource: opts.VersionSource,
versioningScheme: opts.VersioningScheme,
}
case "dub":
if len(buildDescriptorFilePath) == 0 {
buildDescriptorFilePath = "dub.json"
}
artifact = &JSONfile{
path: buildDescriptorFilePath,
versionField: "version",
}
case "gradle":
if len(buildDescriptorFilePath) == 0 {
buildDescriptorFilePath = "gradle.properties"
}
artifact = &Gradle{
path: buildDescriptorFilePath,
versionField: opts.VersionField,
utils: utils,
}
case "golang":
if len(buildDescriptorFilePath) == 0 {
var err error
buildDescriptorFilePath, err = searchDescriptor([]string{"go.mod", "VERSION", "version.txt"}, fileExists)
if err != nil {
return artifact, err
}
}
switch buildDescriptorFilePath {
case "go.mod":
artifact = &GoMod{path: buildDescriptorFilePath, fileExists: fileExists}
break
default:
artifact = &Versionfile{path: buildDescriptorFilePath}
}
case "helm":
artifact = &HelmChart{
path: buildDescriptorFilePath,
utils: utils,
updateAppVersion: opts.HelmUpdateAppVersion,
}
case "maven":
if len(buildDescriptorFilePath) == 0 {
buildDescriptorFilePath = "pom.xml"
}
artifact = &Maven{
runner: &mvnRunner{},
utils: utils,
options: maven.EvaluateOptions{
PomPath: buildDescriptorFilePath,
ProjectSettingsFile: opts.ProjectSettingsFile,
GlobalSettingsFile: opts.GlobalSettingsFile,
M2Path: opts.M2Path,
Defines: opts.Defines,
},
}
case "mta":
if len(buildDescriptorFilePath) == 0 {
buildDescriptorFilePath = "mta.yaml"
}
artifact = &YAMLfile{
path: buildDescriptorFilePath,
versionField: "version",
artifactIDField: "ID",
}
case "npm", "yarn":
if len(buildDescriptorFilePath) == 0 {
buildDescriptorFilePath = "package.json"
}
artifact = &JSONfile{
path: buildDescriptorFilePath,
versionField: "version",
}
case "pip":
if len(buildDescriptorFilePath) == 0 {
var err error
buildDescriptorFilePath, err = searchDescriptor([]string{"setup.py", "version.txt", "VERSION"}, fileExists)
if err != nil {
return artifact, err
}
}
artifact = &Pip{
path: buildDescriptorFilePath,
fileExists: fileExists,
}
case "sbt":
if len(buildDescriptorFilePath) == 0 {
var err error
buildDescriptorFilePath, err = searchDescriptor([]string{"sbtDescriptor.json", "build.sbt"}, fileExists)
if err != nil {
return artifact, err
}
}
artifact = &JSONfile{
path: buildDescriptorFilePath,
versionField: "version",
}
default:
return artifact, fmt.Errorf("build tool '%v' not supported", buildTool)
}
return artifact, nil
}
func searchDescriptor(supported []string, existsFunc func(string) (bool, error)) (string, error) {
var descriptor string
for _, f := range supported {
exists, _ := existsFunc(f)
if exists {
descriptor = f
break
}
}
if len(descriptor) == 0 {
return "", fmt.Errorf("no build descriptor available, supported: %v", supported)
}
return descriptor, nil
}
func customArtifact(buildDescriptorFilePath, field, section, scheme string) (Artifact, error) {
switch filepath.Ext(buildDescriptorFilePath) {
case ".cfg", ".ini":
return &INIfile{
path: buildDescriptorFilePath,
versionField: field,
versionSection: section,
versioningScheme: scheme,
}, nil
case ".json":
return &JSONfile{
path: buildDescriptorFilePath,
versionField: field,
}, nil
case ".yaml", ".yml":
return &YAMLfile{
path: buildDescriptorFilePath,
versionField: field,
}, nil
case ".txt", "":
return &Versionfile{
path: buildDescriptorFilePath,
versioningScheme: scheme,
}, nil
default:
return nil, fmt.Errorf("file type not supported: '%v'", buildDescriptorFilePath)
}
}