-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtemplates_gradle.go
249 lines (214 loc) · 8.44 KB
/
templates_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
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
244
245
246
247
248
249
// Copyright 2022, Pulumi Corporation. All rights reserved.
package java
import (
"bytes"
_ "embed"
"encoding/base64"
"fmt"
"net/url"
"slices"
"strings"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
)
const DefaultGradleNexusPublishPluginVersion = "2.0.0"
func genGradleProject(
pkg *schema.Package,
packageInfo *PackageInfo,
files fs,
legacyBuildFiles bool,
) error {
if err := gradleValidatePackage(pkg); err != nil {
return err
}
ctx := newGradleTemplateContext(pkg, packageInfo, legacyBuildFiles)
templates := map[string]string{
"build.gradle": buildGradleTemplate,
"settings.gradle": settingsGradleTemplate,
}
for fileName, template := range templates {
var buf bytes.Buffer
if err := Template(fileName, template).Execute(&buf, ctx); err != nil {
return err
}
files.add(fileName, buf.Bytes())
}
return nil
}
func gradleValidatePackage(pkg *schema.Package) error {
validationErrors := []string{}
if pkg.Description == "" {
v := `"description" needs to be non-empty to satisfy POM validation rules`
validationErrors = append(validationErrors, v)
}
if pkg.Repository == "" {
v := `"repository" needs to be non-empty to satisfy POM validation rules; ` +
`a valid example is "https://github.com/myorg/mypkg"`
validationErrors = append(validationErrors, v)
}
if len(validationErrors) != 0 {
msg := "Pulumi Package Schema has %d issues that obstruct generating a valid Gradle project:\n- %s"
return fmt.Errorf(msg, len(validationErrors), strings.Join(validationErrors, "\n- "))
}
return nil
}
//go:embed settings.gradle.template
var settingsGradleTemplate string
//go:embed build.gradle.template
var buildGradleTemplate string
type gradleTemplateContext struct {
Name string
Version string
GroupID string
ProjectName string
RootProjectName string
ProjectURL string
ProjectGitURL string
ProjectDescription string
ProjectInceptionYear string
Repositories []string
Dependencies map[string]string
DeveloperID string
DeveloperName string
DeveloperEmail string
LicenceName string
LicenceURL string
GradleNexusPublishPluginEnabled bool
GradleNexusPublishPluginVersion string
GradleTestJUnitPlatformEnabled bool
PluginDownloadURL string
Parameterization *gradleTemplateParameterization
}
type gradleTemplateParameterization struct {
Name string
Version string
Value string
}
func newGradleTemplateContext(
pkg *schema.Package,
packageInfo *PackageInfo,
legacyBuildFiles bool,
) gradleTemplateContext {
ctx := gradleTemplateContext{
Name: pkg.Name,
ProjectDescription: pkg.Description,
ProjectURL: pkg.Repository,
ProjectGitURL: formatGitURL(pkg.Repository),
GradleTestJUnitPlatformEnabled: packageInfo.GradleTest == "JUnitPlatform",
PluginDownloadURL: pkg.PluginDownloadURL,
}
if pkg.Version != nil {
ctx.Version = pkg.Version.String()
} else {
ctx.Version = "0.0.1"
}
if pkg.Parameterization != nil {
ctx.Parameterization = &gradleTemplateParameterization{
Name: ctx.Name,
Version: ctx.Version,
Value: base64.StdEncoding.EncodeToString(pkg.Parameterization.Parameter),
}
ctx.Name = pkg.Parameterization.BaseProvider.Name
ctx.Version = pkg.Parameterization.BaseProvider.Version.String()
}
/*
For `legacyBuildFiles == true` we have the following behavior
|-----------------------------------------|--------------------------------------|
| PackageInfo | Behaviour |
|-----------------------------------------|--------------------------------------|
| | no gradle file, default |
|-----------------------------------------|--------------------------------------|
| buildFiles: gradle | gradle file without nexus plugin |
|-----------------------------------------|--------------------------------------|
| buildFiles: gradle, | gradle file with nexus plugin, using |
| gradleNexusPublishPluginVersion: 2.0.0 | default version of the plugin |
|-----------------------------------------|--------------------------------------|
| buildFiles: gradle, | gradle file with nexus plugin, using |
| gradleNexusPublishPluginVersion: $VER | specified version of the plugin |
|-----------------------------------------|--------------------------------------|
For `legacyBuildFiles == false` we have the following behavior, with the default
being `buildFiles: gradle-nexus`:
|-----------------------------------------|--------------------------------------|
| PackageInfo | Behaviour |
|-----------------------------------------|--------------------------------------|
| buildFiles: none | no gradle file |
|-----------------------------------------|--------------------------------------|
| buildFiles: gradle-nexus | gradle file with nexus plugin, using |
| | the default version 2.0.0 |
|-----------------------------------------|--------------------------------------|
| buildFiles: gradle-nexus, | gradle file with nexus plugin, using |
| gradleNexusPublishPluginVersion: $VER | version $VER |
|-----------------------------------------|--------------------------------------|
| buildFiles: gradle | gradle file without nexus plugin |
|-----------------------------------------|--------------------------------------|
| buildFiles: gradle | gradle file with nexus plugin, using |
| gradleNexusPublishPluginVersion: $VER | specified version of the plugin |
|-----------------------------------------|--------------------------------------|
*/
if legacyBuildFiles {
// In legacy mode, we require the user to provide the Gradle Nexus Publish Plugin version.
if packageInfo.GradleNexusPublishPluginVersion != "" {
ctx.GradleNexusPublishPluginEnabled = true
ctx.GradleNexusPublishPluginVersion = packageInfo.GradleNexusPublishPluginVersion
}
} else if packageInfo.BuildFiles == "" || packageInfo.BuildFiles == "gradle-nexus" {
version := DefaultGradleNexusPublishPluginVersion
if packageInfo.GradleNexusPublishPluginVersion != "" {
version = packageInfo.GradleNexusPublishPluginVersion
}
ctx.GradleNexusPublishPluginEnabled = true
ctx.GradleNexusPublishPluginVersion = version
} else if packageInfo.BuildFiles == "gradle" && packageInfo.GradleNexusPublishPluginVersion != "" {
ctx.GradleNexusPublishPluginEnabled = true
ctx.GradleNexusPublishPluginVersion = packageInfo.GradleNexusPublishPluginVersion
}
if packageInfo.Repositories != nil {
ctx.Repositories = packageInfo.Repositories
slices.Sort(ctx.Repositories)
} else {
ctx.Repositories = []string{}
}
if packageInfo.Dependencies != nil {
ctx.Dependencies = packageInfo.Dependencies
} else {
ctx.Dependencies = map[string]string{}
}
if pkg.License == "Apache-2.0" {
ctx.LicenceName = "The Apache License, Version 2.0"
ctx.LicenceURL = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
ctx.GroupID = packageInfo.BasePackage
if ctx.GroupID == "" {
ctx.GroupID = "com.pulumi"
}
if isPublishedByPulumi(pkg) {
ctx.GroupID = "com.pulumi"
ctx.DeveloperID = "pulumi"
ctx.DeveloperName = "Pulumi"
ctx.DeveloperEmail = "[email protected]"
ctx.ProjectInceptionYear = "2022"
ctx.ProjectName = fmt.Sprintf("pulumi-%s", ctx.Name)
}
if ctx.RootProjectName == "" {
ctx.RootProjectName = packageInfo.BasePackageOrDefault() + pkg.Name
}
return ctx
}
func isPublishedByPulumi(pkg *schema.Package) bool {
if strings.EqualFold(pkg.Publisher, "pulumi") {
return true
}
u, err := url.Parse(pkg.Homepage)
if err == nil {
if strings.HasSuffix(u.Host, "pulumi.com") || u.Host == "pulumi.io" {
return true
}
}
return false
}
func formatGitURL(url string) string {
if strings.HasPrefix(url, "https://github.com/") {
return fmt.Sprintf("[email protected]/%s.git",
strings.TrimPrefix(url, "https://github.com/"))
}
return url
}