-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.gradle.kts
204 lines (177 loc) · 5.4 KB
/
build.gradle.kts
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
import java.net.URI
import java.net.URL
import org.gradle.plugins.ide.idea.model.IdeaLanguageLevel
plugins {
kotlin("jvm").version(libs.versions.kotlin)
alias(libs.plugins.pkl)
alias(libs.plugins.spotless)
idea
}
repositories {
mavenCentral()
}
kotlin {
jvmToolchain(17)
}
val pklPackageVersion = "1.1.1"
val k8sVersions = listOf(
"v1.19.6",
"v1.20.15",
"v1.21.5",
"v1.22.2",
"v1.23.4",
"v1.24.17",
"v1.25.16",
"v1.26.12",
"v1.27.9",
"v1.28.5",
"v1.29.0",
"v1.30.0"
)
val isCiBuild = System.getenv("CI") != null
configurations {
all {
resolutionStrategy {
// make sure Kotlin reflect is the same version as core Kotlin (moshi-kotlin might bring in a different version)
force("org.jetbrains.kotlin:kotlin-reflect:${libs.versions.kotlin.get()}")
}
}
}
dependencies {
implementation(libs.moshiKotlin)
// used to quote identifiers
implementation(libs.pklCore)
// used for lexing
implementation(libs.antlr)
}
idea {
project {
languageLevel = IdeaLanguageLevel("17")
jdkName = "17"
}
}
tasks.idea {
doFirst {
throw GradleException(
"To open this project in IntelliJ, go to File->Open and select the project's root directory. Do *not* run `./gradlew idea`."
)
}
}
tasks.compileKotlin {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs +
listOf("-Xjsr305=strict", "-Xjvm-default=all", "-opt-in=kotlin.RequiresOptIn")
}
}
tasks.processResources {
eachFile {
if (name == "PklProject.template") {
expand("pklPackageVersion" to pklPackageVersion)
}
}
}
private data class K8sVersion(
val inputUrl: URL,
val outputFile: File
)
val downloadsDir = "${layout.buildDirectory.get()}/downloads"
val downloadOpenApiSpec by tasks.registering {
val versions = k8sVersions.map { version ->
K8sVersion(
inputUrl = URI("https://raw.githubusercontent.com/kubernetes/kubernetes/$version/api/openapi-spec/swagger.json").toURL(),
outputFile = file("$downloadsDir/swagger-$version.json")
)
}
inputs.property("k8sInputUrls", versions.map { it.inputUrl.toString() })
outputs.files(versions.map { it.outputFile })
onlyIf { versions.any { !it.outputFile.exists() } }
doLast {
for (version in versions) {
val text = version.inputUrl.readText()
// retain top-level properties `definitions` and `info`, remove the rest
val index = text.indexOf("\"title\": \"Kubernetes\"")
if (index == -1) throw GradleException("Failed to truncate Open API Spec.")
val index2 = text.indexOf('}', index)
if (index2 == -1) throw GradleException("Failed to truncate Open API Spec.")
val truncated = text.substring(0, index2 + 1) + "\n}"
version.outputFile.writeText(truncated)
}
}
}
fun cyan(text: String) = "\u001B[36m$text\u001B[0m"
val generateTemplates by tasks.registering(JavaExec::class) {
dependsOn(downloadOpenApiSpec)
outputs.upToDateWhen { false }
val outputDir = file("generated-package")
outputs.dir(outputDir)
doFirst {
outputDir.deleteRecursively()
}
classpath = sourceSets.main.get().runtimeClasspath
mainClass.set("org.pkl.k8s.templates.MainKt")
argumentProviders.add(CommandLineArgumentProvider {
listOf(outputDir.path, downloadsDir) + k8sVersions
})
doLast {
println("Wrote packages to ${cyan(outputDir.path)}")
}
}
pkl {
project {
packagers {
register("packageTemplates") {
projectDirectories.from("generated-package")
outputPath.set(file("${layout.buildDirectory.get()}/distributions/k8s"))
}
}
}
}
tasks.named("packageTemplates") {
dependsOn(generateTemplates)
group = "build"
}
spotless {
kotlin {
licenseHeader(
"""
/**
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
""".trimIndent()
)
}
format("pkl") {
target("*.pkl", "PklProject.template")
licenseHeader(
"""
//===----------------------------------------------------------------------===//
// Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
""".trimIndent(), "///"
)
}
}