-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
246 lines (208 loc) · 7.26 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
243
244
245
246
import java.io.ByteArrayOutputStream
tasks.register("installGitHooks", Copy::class) {
group = "git hooks"
description = "Installs git hooks from .githooks directory"
val sourceDir = file(".githooks")
val targetDir = file(".git/hooks")
from(sourceDir) {
include("**/*")
}
into(targetDir)
fileMode = 0b111101101 // 755 in octal: rwxr-xr-x
inputs.dir(sourceDir)
outputs.dir(targetDir)
doFirst {
sourceDir.mkdirs()
targetDir.mkdirs()
val preCommitFile = sourceDir.resolve("pre-commit")
if (!preCommitFile.exists()) {
throw GradleException("pre-commit hook file not found in .githooks directory")
}
println("Installing Git hooks...")
}
outputs.upToDateWhen {
val preCommitSource = sourceDir.resolve("pre-commit")
val preCommitTarget = targetDir.resolve("pre-commit")
if (!preCommitTarget.exists()) {
return@upToDateWhen false
}
val isUpToDate = preCommitSource.lastModified() <= preCommitTarget.lastModified() &&
preCommitSource.length() == preCommitTarget.length()
isUpToDate
}
}
tasks.matching { it.name == "build" }.configureEach {
dependsOn("installGitHooks")
}
gradle.projectsEvaluated {
tasks.named("prepareKotlinBuildScriptModel").configure {
dependsOn("installGitHooks")
}
}
plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.jetbrainsCompose) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.kotlinJvm) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
alias(libs.plugins.springboot) apply false
alias(libs.plugins.springDependencyManagement) apply false
alias(libs.plugins.kotlinPluginSpring) apply false
id("maven-publish")
id("com.github.node-gradle.node") version "7.0.1"
}
fun getNpmVersion(): String {
val baseVersion = project.version.toString()
if (!baseVersion.endsWith("-SNAPSHOT")) {
return baseVersion
}
// For SNAPSHOT versions, create an unstable.<commit-hash> version
val versionBase = baseVersion.removeSuffix("-SNAPSHOT")
// Get git commit hash
val gitCommitHash = try {
val process = ProcessBuilder("git", "rev-parse", "--short=7", "HEAD")
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start()
process.inputStream.bufferedReader().use { it.readLine() }
} catch (e: Exception) {
"unknown"
}
return "$versionBase-unstable.$gitCommitHash"
}
allprojects {
group = "com.sphereon.oid.fed"
version = "0.4.4-SNAPSHOT"
val npmVersion by extra { getNpmVersion() }
// Common repository configuration for all projects
repositories {
mavenCentral()
mavenLocal()
google()
}
}
subprojects {
plugins.withType<MavenPublishPlugin> {
configure<PublishingExtension> {
repositories {
maven {
name = "sphereon-opensource"
val snapshotsUrl = "https://nexus.sphereon.com/repository/sphereon-opensource-snapshots/"
val releasesUrl = "https://nexus.sphereon.com/repository/sphereon-opensource-releases/"
url = uri(if (version.toString().endsWith("SNAPSHOT")) snapshotsUrl else releasesUrl)
credentials {
username = System.getenv("NEXUS_USERNAME")
password = System.getenv("NEXUS_PASSWORD")
}
}
}
// Ensure unique coordinates for different publication types
publications.withType<MavenPublication> {
val publicationName = name
if (publicationName == "kotlinMultiplatform") {
artifactId = "${project.name}-multiplatform"
} else if (publicationName == "mavenKotlin") {
artifactId = "${project.name}-jvm"
}
}
}
}
}
tasks.register("checkDockerStatus") {
group = "docker"
description = "Checks if Docker containers are running"
doLast {
val output = ByteArrayOutputStream()
val process = exec {
commandLine("docker", "compose", "ps", "-q", "db", "local-kms-db")
isIgnoreExitValue = true
standardOutput = output
}
val containersRunning = process.exitValue == 0 && output.toString().trim().isNotEmpty()
project.ext.set("containersRunning", containersRunning)
if (containersRunning) {
println("Required Docker containers are already running")
} else {
println("Required Docker containers are not running")
}
}
}
tasks.register("dockerCleanup") {
group = "docker"
description = "Stops and removes specific Docker containers"
doLast {
exec {
commandLine("docker", "compose", "rm", "-fsv", "db", "local-kms-db")
}
}
}
fun waitForDatabase() {
var ready = false
var attempts = 0
val maxAttempts = 30
while (!ready && attempts < maxAttempts) {
try {
val process = exec {
commandLine("docker", "compose", "exec", "-T", "db", "pg_isready", "-U", "postgres")
isIgnoreExitValue = true
}
ready = process.exitValue == 0
} catch (e: Exception) {
}
if (!ready) {
attempts++
Thread.sleep(2000)
println("Waiting for database to be ready... (Attempt $attempts/$maxAttempts)")
}
}
if (!ready) {
throw GradleException("Database failed to become ready within the timeout period")
}
println("Database is ready!")
}
tasks.register("waitForDatabase") {
group = "docker"
description = "Waits for the database to be ready"
doLast {
waitForDatabase()
}
}
tasks.register("dockerStart") {
group = "docker"
description = "Starts specific Docker containers"
doLast {
exec {
commandLine("docker", "compose", "up", "-d", "db", "local-kms-db")
}
waitForDatabase()
}
}
tasks.register("dockerEnsureRunning") {
group = "docker"
description = "Ensures Docker containers are running, starting them if needed"
dependsOn("checkDockerStatus")
doLast {
if (!project.ext.has("containersRunning") || !project.ext.get("containersRunning").toString().toBoolean()) {
exec {
commandLine("docker", "compose", "rm", "-fsv", "db", "local-kms-db")
}
exec {
commandLine("docker", "compose", "up", "-d", "db", "local-kms-db")
}
}
waitForDatabase()
project.ext.set("containersRunning", true)
}
}
subprojects {
tasks.matching { it.name == "build" }.configureEach {
dependsOn(rootProject.tasks.named("dockerEnsureRunning"))
doFirst {
if (!rootProject.ext.has("containersRunning") || !rootProject.ext.get("containersRunning").toString()
.toBoolean()
) {
throw GradleException("Docker containers are not running. Please run './gradlew dockerEnsureRunning' first.")
}
}
}
}