Skip to content

Commit fcc6010

Browse files
committed
feat: add pom metadata toggle, fix plugin id in subprojects
1 parent ec54774 commit fcc6010

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

pkg/openapi/openapigenerator/template_properties.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"sort"
7+
"strconv"
78
"strings"
89
)
910

@@ -12,6 +13,7 @@ type TemplatePropertyDef struct {
1213
Description string
1314
DefaultValue string
1415
EnvVar string
16+
Boolean bool
1517
}
1618

1719
var templatePropertyRegistry = map[string][]TemplatePropertyDef{
@@ -40,6 +42,13 @@ var templatePropertyRegistry = map[string][]TemplatePropertyDef{
4042
DefaultValue: "",
4143
EnvVar: "PRIMECODEGEN_TPL_OPENAPI_JAVA_HTTPCLIENT_GRADLE_PLUGINMANAGEMENT_REPOSITORYURL",
4244
},
45+
{
46+
Key: "gradle.pomMetadata.enabled",
47+
Description: "Enable writing custom pom metadata block in root build.gradle.kts",
48+
DefaultValue: "true",
49+
EnvVar: "PRIMECODEGEN_TPL_OPENAPI_JAVA_HTTPCLIENT_GRADLE_POMMETADATA_ENABLED",
50+
Boolean: true,
51+
},
4352
},
4453
"openapi-kotlin-httpclient": {
4554
{
@@ -66,6 +75,13 @@ var templatePropertyRegistry = map[string][]TemplatePropertyDef{
6675
DefaultValue: "",
6776
EnvVar: "PRIMECODEGEN_TPL_OPENAPI_KOTLIN_HTTPCLIENT_GRADLE_PLUGINMANAGEMENT_REPOSITORYURL",
6877
},
78+
{
79+
Key: "gradle.pomMetadata.enabled",
80+
Description: "Enable writing custom pom metadata block in root build.gradle.kts",
81+
DefaultValue: "true",
82+
EnvVar: "PRIMECODEGEN_TPL_OPENAPI_KOTLIN_HTTPCLIENT_GRADLE_POMMETADATA_ENABLED",
83+
Boolean: true,
84+
},
6985
},
7086
}
7187

@@ -110,12 +126,32 @@ func ResolveTemplateProperties(templateId string, provided map[string]string) (m
110126
}
111127

112128
for key, value := range provided {
113-
if _, ok := byKey[key]; !ok {
129+
def, ok := byKey[key]
130+
if !ok {
114131
return nil, fmt.Errorf("unknown --tpl-prop key %q for template %s (allowed: %s)", key, templateId, strings.Join(AllowedTemplatePropertyKeys(templateId), ", "))
115132
}
133+
if def.Boolean {
134+
parsedValue, parseErr := strconv.ParseBool(value)
135+
if parseErr != nil {
136+
return nil, fmt.Errorf("invalid boolean value %q for --tpl-prop key %q", value, key)
137+
}
138+
value = strconv.FormatBool(parsedValue)
139+
}
116140
out[key] = value
117141
}
118142

143+
for _, def := range defs {
144+
if !def.Boolean {
145+
continue
146+
}
147+
value := out[def.Key]
148+
parsedValue, err := strconv.ParseBool(value)
149+
if err != nil {
150+
return nil, fmt.Errorf("invalid boolean value %q for key %q from default/env/cli", value, def.Key)
151+
}
152+
out[def.Key] = strconv.FormatBool(parsedValue)
153+
}
154+
119155
return out, nil
120156
}
121157

pkg/openapi/openapigenerator/template_properties_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,35 @@ func TestResolveTemplatePropertiesDefaults(t *testing.T) {
3333
assert.Equal(t, "0.20.1", resolved["gradle.configurationPlugin.version"])
3434
assert.Equal(t, "projectConfiguration", resolved["gradle.projectConfiguration.blockName"])
3535
assert.Equal(t, "", resolved["gradle.pluginManagement.repositoryUrl"])
36+
assert.Equal(t, "true", resolved["gradle.pomMetadata.enabled"])
3637
}
3738

3839
func TestResolveTemplatePropertiesEnvAndCliPrecedence(t *testing.T) {
3940
t.Setenv("PRIMECODEGEN_TPL_OPENAPI_JAVA_HTTPCLIENT_GRADLE_CONFIGURATIONPLUGIN_ID", "com.env.configuration")
4041
t.Setenv("PRIMECODEGEN_TPL_OPENAPI_JAVA_HTTPCLIENT_GRADLE_PLUGINMANAGEMENT_REPOSITORYURL", "https://env.example/maven")
42+
t.Setenv("PRIMECODEGEN_TPL_OPENAPI_JAVA_HTTPCLIENT_GRADLE_POMMETADATA_ENABLED", "false")
4143

4244
resolved, err := ResolveTemplateProperties("openapi-java-httpclient", map[string]string{
4345
"gradle.configurationPlugin.id": "com.cli.configuration",
4446
"gradle.pluginManagement.repositoryUrl": "https://cli.example/maven",
47+
"gradle.pomMetadata.enabled": "TRUE",
4548
})
4649
require.NoError(t, err)
4750

4851
assert.Equal(t, "com.cli.configuration", resolved["gradle.configurationPlugin.id"])
4952
assert.Equal(t, "https://cli.example/maven", resolved["gradle.pluginManagement.repositoryUrl"])
53+
assert.Equal(t, "true", resolved["gradle.pomMetadata.enabled"])
54+
}
55+
56+
func TestResolveTemplatePropertiesBooleanValidation(t *testing.T) {
57+
t.Setenv("PRIMECODEGEN_TPL_OPENAPI_JAVA_HTTPCLIENT_GRADLE_POMMETADATA_ENABLED", "not-bool")
58+
_, err := ResolveTemplateProperties("openapi-java-httpclient", map[string]string{})
59+
assert.ErrorContains(t, err, "invalid boolean value")
60+
61+
_, err = ResolveTemplateProperties("openapi-java-httpclient", map[string]string{
62+
"gradle.pomMetadata.enabled": "invalid",
63+
})
64+
assert.ErrorContains(t, err, "invalid boolean value")
5065
}
5166

5267
func TestResolveTemplatePropertiesUnknownKey(t *testing.T) {

pkg/template/templates/openapi-java-httpclient/build.gradle.kts.root.gohtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ plugins {
55
alias(libs.plugins.configuration)
66
}
77

8+
val configurationPluginId = libs.plugins.configuration.get().pluginId
9+
810
subprojects {
9-
apply(plugin = libs.plugins.configuration.get().pluginId)
11+
apply(plugin = configurationPluginId)
1012

1113
{{ index .Common.GeneratorProperties "gradle.projectConfiguration.blockName" }} {
1214
type.set(me.philippheuer.projectcfg.domain.ProjectType.LIBRARY)
@@ -22,6 +24,7 @@ subprojects {
2224
{{- end }}
2325
javadocLint.set(listOf("none"))
2426

27+
{{- if eq (index .Common.GeneratorProperties "gradle.pomMetadata.enabled") "true" }}
2528
pom = { pom ->
2629
{{- if .Metadata.RepositoryUrl }}
2730
pom.url.set("https://{{ .Metadata.RepositoryUrl }}")
@@ -56,5 +59,6 @@ subprojects {
5659
}
5760
{{- end }}
5861
}
62+
{{- end }}
5963
}
6064
}

pkg/template/templates/openapi-kotlin-httpclient/build.gradle.kts.root.gohtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ plugins {
55
alias(libs.plugins.configuration)
66
}
77

8+
val configurationPluginId = libs.plugins.configuration.get().pluginId
9+
810
subprojects {
9-
apply(plugin = libs.plugins.configuration.get().pluginId)
11+
apply(plugin = configurationPluginId)
1012

1113
{{ index .Common.GeneratorProperties "gradle.projectConfiguration.blockName" }} {
1214
type.set(me.philippheuer.projectcfg.domain.ProjectType.LIBRARY)
@@ -22,6 +24,7 @@ subprojects {
2224
{{- end }}
2325
javadocLint.set(listOf("none"))
2426

27+
{{- if eq (index .Common.GeneratorProperties "gradle.pomMetadata.enabled") "true" }}
2528
pom = { pom ->
2629
{{- if .Metadata.RepositoryUrl }}
2730
pom.url.set("https://{{ .Metadata.RepositoryUrl }}")
@@ -56,5 +59,6 @@ subprojects {
5659
}
5760
{{- end }}
5861
}
62+
{{- end }}
5963
}
6064
}

0 commit comments

Comments
 (0)