mirror of
https://github.com/Dark98/SliceBeam.git
synced 2026-07-03 00:38:53 +00:00
Gradle startBackend Fixes
This commit is contained in:
+131
-39
@@ -91,49 +91,141 @@ def findWindowsNpm() {
|
|||||||
return candidates.isEmpty() ? null : candidates.first().absolutePath
|
return candidates.isEmpty() ? null : candidates.first().absolutePath
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.register("startBackend", Exec) {
|
def findWindowsNode() {
|
||||||
workingDir "${rootDir}/backend"
|
def candidates = []
|
||||||
if (isWindows) {
|
def pathEnv = System.getenv("PATH") ?: ""
|
||||||
doFirst {
|
pathEnv.split(";").each { p ->
|
||||||
def npmPath = (project.findProperty("npmPath") ?: System.getenv("NPM_PATH"))?.toString()
|
if (!p) return
|
||||||
if (!npmPath) {
|
def exe = new File(p, "node.exe")
|
||||||
npmPath = findWindowsNpm()
|
if (exe.exists()) candidates << exe
|
||||||
}
|
}
|
||||||
if (!npmPath) {
|
def nvmHome = System.getenv("NVM_HOME")
|
||||||
def out = new ByteArrayOutputStream()
|
def nvmSymlink = System.getenv("NVM_SYMLINK")
|
||||||
exec {
|
if (nvmSymlink) {
|
||||||
commandLine "cmd", "/c", "where.exe", "npm"
|
def exe = new File(nvmSymlink, "node.exe")
|
||||||
standardOutput = out
|
if (exe.exists()) candidates << exe
|
||||||
errorOutput = out
|
}
|
||||||
ignoreExitValue = true
|
if (nvmHome) {
|
||||||
}
|
def current = new File(nvmHome, "current")
|
||||||
def line = out.toString().readLines().find { it.toLowerCase().endsWith("npm.cmd") || it.toLowerCase().endsWith("npm.exe") }
|
if (current.exists()) {
|
||||||
if (line) {
|
def exe = new File(current, "node.exe")
|
||||||
npmPath = line.trim()
|
if (exe.exists()) candidates << exe
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!npmPath) {
|
|
||||||
throw new GradleException("npm not found. Set -PnpmPath=... or ensure npm is visible to cmd.exe. If using nvm, try your NVM_HOME version's npm.cmd.")
|
|
||||||
}
|
|
||||||
def nodeModules = new File(workingDir, "node_modules")
|
|
||||||
if (!nodeModules.exists()) {
|
|
||||||
exec {
|
|
||||||
workingDir "${rootDir}/backend"
|
|
||||||
commandLine "cmd", "/c", npmPath, "install"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
commandLine "cmd", "/c", "start", "\"\"", npmPath, "start"
|
|
||||||
}
|
}
|
||||||
|
def versions = new File(nvmHome)
|
||||||
|
if (versions.exists()) {
|
||||||
|
versions.listFiles()?.findAll { it.isDirectory() }?.each { v ->
|
||||||
|
def exe = new File(v, "node.exe")
|
||||||
|
if (exe.exists()) candidates << exe
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
def programFiles = System.getenv("ProgramFiles")
|
||||||
|
if (programFiles) {
|
||||||
|
def exe = new File(programFiles, "nodejs\\node.exe")
|
||||||
|
if (exe.exists()) candidates << exe
|
||||||
|
}
|
||||||
|
def localAppData = System.getenv("LOCALAPPDATA")
|
||||||
|
if (localAppData) {
|
||||||
|
def nvmLocal = new File(localAppData, "nvm")
|
||||||
|
if (nvmLocal.exists()) {
|
||||||
|
nvmLocal.listFiles()?.findAll { it.isDirectory() }?.each { v ->
|
||||||
|
def exe = new File(v, "node.exe")
|
||||||
|
if (exe.exists()) candidates << exe
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return candidates.isEmpty() ? null : candidates.first().absolutePath
|
||||||
|
}
|
||||||
|
|
||||||
|
def backendDir = file("${rootDir}/backend")
|
||||||
|
def backendNodeModulesDir = new File(backendDir, "node_modules")
|
||||||
|
def npmPathProp = providers.gradleProperty("npmPath")
|
||||||
|
.orElse(providers.environmentVariable("NPM_PATH"))
|
||||||
|
|
||||||
|
def resolvedWindowsNpm = null
|
||||||
|
def resolvedWindowsNode = null
|
||||||
|
if (isWindows) {
|
||||||
|
resolvedWindowsNpm = npmPathProp.orNull?.toString() ?: findWindowsNpm()
|
||||||
|
if (!resolvedWindowsNpm) {
|
||||||
|
throw new GradleException("npm not found. Set -PnpmPath=... or ensure npm is visible to cmd.exe. If using nvm, try your NVM_HOME version's npm.cmd.")
|
||||||
|
}
|
||||||
|
resolvedWindowsNode = findWindowsNode()
|
||||||
|
if (!resolvedWindowsNode) {
|
||||||
|
throw new GradleException("node.exe not found. Ensure Node.js is installed and visible to cmd.exe.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.register("installBackendDeps", Exec) {
|
||||||
|
workingDir backendDir
|
||||||
|
onlyIf { !backendNodeModulesDir.exists() }
|
||||||
|
if (isWindows) {
|
||||||
|
commandLine "cmd", "/c", resolvedWindowsNpm, "install"
|
||||||
} else {
|
} else {
|
||||||
doFirst {
|
commandLine "sh", "-c", "${npmCmd} install"
|
||||||
def nodeModules = new File(workingDir, "node_modules")
|
}
|
||||||
if (!nodeModules.exists()) {
|
}
|
||||||
exec {
|
|
||||||
workingDir "${rootDir}/backend"
|
tasks.register("startBackend") {
|
||||||
commandLine "sh", "-c", "${npmCmd} install"
|
dependsOn("installBackendDeps")
|
||||||
|
doLast {
|
||||||
|
def command = isWindows
|
||||||
|
? [resolvedWindowsNode, "--no-deprecation", "server.js"]
|
||||||
|
: ["sh", "-c", "${npmCmd} start"]
|
||||||
|
|
||||||
|
def process = new ProcessBuilder(command)
|
||||||
|
.directory(backendDir)
|
||||||
|
.start()
|
||||||
|
|
||||||
|
def stopProcess = {
|
||||||
|
if (!process.isAlive()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (isWindows) {
|
||||||
|
new ProcessBuilder("cmd", "/c", "taskkill", "/PID", process.pid().toString(), "/T", "/F")
|
||||||
|
.start()
|
||||||
|
.waitFor()
|
||||||
|
} else {
|
||||||
|
process.destroy()
|
||||||
|
process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS)
|
||||||
|
if (process.isAlive()) {
|
||||||
|
process.destroyForcibly()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
commandLine "sh", "-c", "${npmCmd} start &"
|
}
|
||||||
|
|
||||||
|
def pumpStream = { input, output ->
|
||||||
|
Thread.start {
|
||||||
|
input.withReader("UTF-8") { reader ->
|
||||||
|
String line
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
output.println(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def outThread = pumpStream(process.inputStream, System.out)
|
||||||
|
def errThread = pumpStream(process.errorStream, System.err)
|
||||||
|
|
||||||
|
def shutdownHook = new Thread({
|
||||||
|
stopProcess()
|
||||||
|
}, "stop-backend-on-gradle-exit")
|
||||||
|
|
||||||
|
Runtime.getRuntime().addShutdownHook(shutdownHook)
|
||||||
|
try {
|
||||||
|
def exitCode = process.waitFor()
|
||||||
|
outThread.join(1000)
|
||||||
|
errThread.join(1000)
|
||||||
|
if (exitCode != 0) {
|
||||||
|
throw new GradleException("Backend process exited with code ${exitCode}")
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
stopProcess()
|
||||||
|
try {
|
||||||
|
Runtime.getRuntime().removeShutdownHook(shutdownHook)
|
||||||
|
} catch (IllegalStateException ignored) {
|
||||||
|
// Ignore; JVM is already shutting down and hook execution has begun.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -7,6 +7,7 @@
|
|||||||
# Specifies the JVM arguments used for the daemon process.
|
# Specifies the JVM arguments used for the daemon process.
|
||||||
# The setting is particularly useful for tweaking memory settings.
|
# The setting is particularly useful for tweaking memory settings.
|
||||||
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
|
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
|
||||||
|
org.gradle.console=plain
|
||||||
# When configured, Gradle will run in incubating parallel mode.
|
# When configured, Gradle will run in incubating parallel mode.
|
||||||
# This option should only be used with decoupled projects. For more details, visit
|
# This option should only be used with decoupled projects. For more details, visit
|
||||||
# https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
|
# https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
|
||||||
@@ -19,4 +20,4 @@ android.useAndroidX=true
|
|||||||
# resources declared in the library itself and none from the library's dependencies,
|
# resources declared in the library itself and none from the library's dependencies,
|
||||||
# thereby reducing the size of the R class for that library
|
# thereby reducing the size of the R class for that library
|
||||||
android.nonTransitiveRClass=true
|
android.nonTransitiveRClass=true
|
||||||
org.gradle.configuration-cache=true
|
org.gradle.configuration-cache=true
|
||||||
|
|||||||
Reference in New Issue
Block a user