Skip to content

Commit

Permalink
Add Gradle task to launch pre-compiled build
Browse files Browse the repository at this point in the history
  • Loading branch information
scroix committed Apr 7, 2024
1 parent 29ae098 commit 86d7043
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions nodel-jyhost/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,65 @@ tasks.register('startApplication') {
}
}

def cleanupTask = tasks.register('cleanupApplication') {
doLast {
if (process != null && process.isAlive()) {
logger.lifecycle("Terminating the spawned process...")
process.destroy()
logger.lifecycle("Spawned process terminated.")
}
}
}

tasks.register('startApplicationInteractive') {
dependsOn 'cleanNodelhostTemp'

// Declare a variable to store the spawned process
process = null

doLast {
def nodelhostTempDir = new File("$projectDir/nodelhost-temp")
if (!nodelhostTempDir.exists()) {
nodelhostTempDir.mkdirs()
}

def processBuilder = new ProcessBuilder(
'java',
'-cp', sourceSets.main.runtimeClasspath.asPath,
'-ea',
'org.nodel.jyhost.Launch'
)
processBuilder.directory(nodelhostTempDir)

// Redirect the process output to the Gradle logger
processBuilder.redirectErrorStream(true)
process = processBuilder.start()

// Create a separate thread to consume the process output
Thread outputThread = new Thread({
process.inputStream.eachLine { line ->
logger.lifecycle(line)
}
})
outputThread.start()

// Wait for the process to terminate
process.waitFor()

// Wait for the output thread to finish
outputThread.join()
}

// Configure the task to run only if the process is not already running
onlyIf {
process == null || !process.isAlive()
}

// Specify the cleanup task to run after this task, even if it fails or is stopped
finalizedBy cleanupTask
}


tasks.register('stopApplication') {
doLast {
if (process != null) {
Expand Down

0 comments on commit 86d7043

Please sign in to comment.