-
Notifications
You must be signed in to change notification settings - Fork 34
/
build.gradle.kts
242 lines (220 loc) · 8.28 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
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
import groovy.json.JsonSlurper
import org.jetbrains.kotlin.utils.sure
import org.pkl.core.Version
import org.pkl.gradle.task.ProjectPackageTask
import java.io.OutputStream
import java.net.URI
import java.nio.file.Files
import java.nio.file.Path
import javax.net.ssl.HttpsURLConnection
import kotlin.io.path.absolutePathString
import kotlin.io.path.isDirectory
import kotlin.io.path.name
import kotlin.io.path.pathString
import kotlin.math.ceil
import kotlin.math.log10
plugins {
kotlin("jvm").version(libs.versions.kotlin)
alias(libs.plugins.pkl)
alias(libs.plugins.spotless)
}
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") {
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(), "(/// |/\\*\\*|module |import |amends |(\\w+))")
target("**/*.pkl", "**/PklProject")
}
}
kotlin {
jvmToolchain(17)
}
repositories {
mavenCentral()
}
dependencies {
testImplementation(libs.pklCore)
testImplementation(libs.junitEngine)
testImplementation(libs.junitParams)
}
val repositoryUrl = "https://github.com/apple/pkl-pantry"
val repositoryApiUrl = repositoryUrl.replace(Regex("github.com/"), "api.github.com/repos/")
val projectDirs: List<File> =
Files.list(Path.of("packages"))
.filter { it.isDirectory() }
.map { it.toFile() }
.toList()
val outputDir = layout.buildDirectory
pkl {
project {
resolvers {
register("resolveProjects") {
projectDirectories.from(projectDirs)
}
}
packagers {
register("createPackages") {
projectDirectories.from(projectDirs)
outputPath.set(outputDir.dir("generated/packages/%{name}/%{version}"))
junitReportsDir.set(outputDir.dir("test-results"))
}
}
}
}
val resolveProjects = tasks.named("resolveProjects") {
group = "build"
inputs.files(fileTree("packages/") { include("PklProject") })
outputs.files(fileTree("packages/") { include("PklProject.deps.json") })
}
val createPackages = tasks.named<ProjectPackageTask>("createPackages") {
group = "build"
}
/**
* Return the set of packages that are dependents of the input package.
*/
@Suppress("UNCHECKED_CAST")
fun gatherDependentPackages(packageDirName: String): List<String> {
return buildList {
for (pkg in Files.list(file("packages/").toPath())) {
if (pkg.name == packageDirName || !pkg.isDirectory()) {
continue
}
val jsonFile = pkg.resolve("PklProject.deps.json")
val json = JsonSlurper().parse(jsonFile) as Map<String, Any>
val dependencies = json["resolvedDependencies"] as Map<String, Map<String, String>>
for ((_, value) in dependencies) {
if (value["path"] == "../$packageDirName") {
add(pkg.name)
}
}
}
}
}
val decorateFailureWithDependentPackageInformation by tasks.registering {
onlyIf {
@Suppress("SENSELESS_COMPARISON")
createPackages.get().state.failure != null
}
doLast {
val createPackagesTask = createPackages.get()
@Suppress("UNNECESSARY_NOT_NULL_ASSERTION")
val failureMessage = createPackagesTask.state.failure!!.cause?.message ?: return@doLast
if (!failureMessage.contains("was already published with different contents")) {
return@doLast
}
val packageName = Regex("`(.+)`").find(failureMessage)?.groups?.get(1)?.value ?: return@doLast
val dirName = packageName.drop("package://pkg.pkl-lang.org/pkl-pantry/".length)
.dropLastWhile { it != '@' }
.dropLast(1)
val dependentPackages = gatherDependentPackages(dirName)
if (dependentPackages.isEmpty()) {
return@doLast
}
val msg = buildString {
appendLine("The following packages depend on this package, and also need to have their version updated:")
for (pkgName in dependentPackages) {
appendLine("* $pkgName")
}
}
createPackagesTask.state.addFailure(TaskExecutionException(createPackagesTask, RuntimeException(msg)))
}
}
createPackages.get().finalizedBy(decorateFailureWithDependentPackageInformation)
val isInCircleCi = System.getenv("CIRCLE_PROJECT_REPONAME") != null
val prepareCiGit by tasks.registering {
if (isInCircleCi) {
exec {
commandLine("git", "config", "user.email", "[email protected]")
}
exec {
commandLine("git", "config", "user.name", "The Pkl Team (automation)")
}
}
}
repositories {
mavenCentral()
}
val prepareReleases by tasks.registering {
group = "build"
dependsOn(createPackages, prepareCiGit)
inputs.files(projectDirs)
doLast {
val releaseDir = file(outputDir.dir("releases"))
releaseDir.deleteRecursively()
val count = projectDirs.count()
val fmt = "%${ceil(log10(count.toDouble())).toInt()}d"
for (i in projectDirs.indices) {
val dir = projectDirs[i]
print(" [${fmt.format(i + 1)}/$count] $dir: ")
val allVersions = file(outputDir.dir("generated/packages/${dir.name}")).list()
if (allVersions == null) {
println("∅")
continue
}
val latestVersion = allVersions.map(Version::parse).sortedWith(Version.comparator()).last()
val pkg = "${dir.name}@$latestVersion"
print("$pkg: ")
val conn = URI("${repositoryUrl}/releases/tag/${dir.name}@$latestVersion")
.toURL()
.openConnection() as HttpsURLConnection
if (conn.responseCode == 200) {
println("⏩")
continue
}
val taskOutput = StringBuilder()
exec {
commandLine("git", "tag", "-l", pkg)
logging.addStandardOutputListener { taskOutput.append(it) }
standardOutput = OutputStream.nullOutputStream()
}
if (taskOutput.contains(pkg)) {
println("☑️")
continue
}
for (artifact in file(outputDir.dir("generated/packages/${dir.name}/$latestVersion")).listFiles()!!) {
artifact.copyTo(releaseDir.resolve("$pkg/${artifact.name}"), true)
}
println("✅")
}
}
}
tasks.test {
useJUnitPlatform()
dependsOn(createPackages)
}
tasks.build {
dependsOn(prepareReleases)
}