Skip to content

Commit

Permalink
Merge branch 'feature/OIDF-64' of github.com:Sphereon-Opensource/Open…
Browse files Browse the repository at this point in the history
…ID-Federation into feature/OIDF-63
  • Loading branch information
jcmelati committed Jan 15, 2025
2 parents 7ebfb87 + 4d6f83f commit 8e81a6d
Showing 1 changed file with 6 additions and 115 deletions.
121 changes: 6 additions & 115 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import java.io.ByteArrayOutputStream

plugins {
// this is necessary to avoid the plugins to be loaded multiple times
// in each subproject's classloader
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.jetbrainsCompose) apply false
Expand All @@ -20,8 +20,10 @@ fun getNpmVersion(): String {
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)
Expand All @@ -37,9 +39,10 @@ fun getNpmVersion(): String {

allprojects {
group = "com.sphereon.oid.fed"
version = "0.1.2-SNAPSHOT"
version = "0.2.0-SNAPSHOT"
val npmVersion by extra { getNpmVersion() }

// Common repository configuration for all projects
repositories {
mavenCentral()
mavenLocal()
Expand All @@ -65,115 +68,3 @@ subprojects {
}
}
}

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.")
}
}
}
}

0 comments on commit 8e81a6d

Please sign in to comment.