-
Notifications
You must be signed in to change notification settings - Fork 21
/
templates_gradle.go
120 lines (103 loc) · 3.15 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
// Copyright 2022, Pulumi Corporation. All rights reserved.
package java
import (
"bytes"
_ "embed"
"fmt"
"strings"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
)
func genGradleProject(
pkg *schema.Package,
packageInfo *PackageInfo,
files fs,
) error {
ctx := newGradleTemplateContext(pkg, packageInfo)
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
}
//go:embed settings.gradle.template
var settingsGradleTemplate string
//go:embed build.gradle.template
var buildGradleTemplate string
type gradleTemplateContext struct {
Version string
GroupID string
ArtifactID string
ProjectName string
RootProjectName string
ProjectURL string
ProjectGitURL string
ProjectDescription string
ProjectInceptionYear string
Dependencies map[string]string
DeveloperID string
DeveloperName string
DeveloperEmail string
LicenceName string
LicenceURL string
GradleNexusPublishPluginEnabled bool
GradleNexusPublishPluginVersion string
GradleTestJUnitPlatformEnabled bool
}
func newGradleTemplateContext(
pkg *schema.Package,
packageInfo *PackageInfo,
) gradleTemplateContext {
ctx := gradleTemplateContext{
ProjectURL: pkg.Repository,
ProjectGitURL: formatGitURL(pkg.Repository),
GradleTestJUnitPlatformEnabled: packageInfo.GradleTest == "JUnitPlatform",
}
if packageInfo.GradleNexusPublishPluginVersion != "" {
ctx.GradleNexusPublishPluginEnabled = true
ctx.GradleNexusPublishPluginVersion = packageInfo.GradleNexusPublishPluginVersion
}
if pkg.Version != nil {
ctx.Version = pkg.Version.String()
} else {
ctx.Version = "0.0.1"
}
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"
}
if pkg.Publisher == "Pulumi" ||
pkg.Homepage == "https://pulumi.com" ||
pkg.Homepage == "https://pulumi.io" {
ctx.ArtifactID = pkg.Name
ctx.GroupID = "com.pulumi"
ctx.DeveloperID = "pulumi"
ctx.DeveloperName = "Pulumi"
ctx.DeveloperEmail = "support@pulumi.com"
ctx.ProjectDescription = pkg.Description
ctx.ProjectInceptionYear = "2022"
ctx.ProjectName = fmt.Sprintf("pulumi-%s", ctx.ArtifactID)
}
if ctx.RootProjectName == "" {
ctx.RootProjectName = packageInfo.BasePackageOrDefault() + pkg.Name
}
return ctx
}
func formatGitURL(url string) string {
if strings.HasPrefix(url, "https://github.com/") {
return fmt.Sprintf("git@github.com/%s.git",
strings.TrimPrefix(url, "https://github.com/"))
}
return url
}