Files
ForkWar/buildSrc/src/steamwar.devserver.gradle
T
2025-04-20 08:27:56 +02:00

148 lines
4.8 KiB
Groovy

/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
plugins {
}
class DevServer extends DefaultTask {
@Input
@Optional
String worldName = null
@Input
String template = null
@Input
@Optional
Integer port = null
@Input
@Optional
String jar = null
// Add Configs for FightServer!
DevServer() {
super()
doFirst {
List<Project> projects = []
projects.add(project)
while (projects.first.parent != null) {
projects.add(0, projects.first.parent)
}
def properties = new Properties()
projects.forEach {
def file = new File(it.projectDir, "steamwar.properties")
if (file.exists()) {
properties.load(new FileInputStream(file))
}
}
if (worldName == null) worldName = properties.get("worldName")
host = properties.get("host")
if (worldName == null) {
throw new GradleException("Please supply the 'worldName' in a 'steamwar.properties' files either in this project dir or any parent project!")
}
if (host == null) {
throw new GradleException("Please supply the 'host' in a 'steamwar.properties' files either in this project dir or any parent project!")
}
}
doLast {
uploadDependencies()
startDevServer()
}
finalizedBy(new Finalizer())
}
@Internal
BufferedWriter processInput
@Internal
String host
class Finalizer extends DefaultTask {
Finalizer() {
super()
doLast {
if (processInput != null) {
processInput.write(template.endsWith("Velocity") ? "end\n" : "stop\n")
processInput.flush()
}
}
}
}
void uploadDependencies() {
this.dependsOn.forEach {
Project resolved
AbstractArchiveTask archiveTask
if (it instanceof String) {
resolved = project.findProject(it.substring(0, it.lastIndexOf(':')))
archiveTask = (AbstractArchiveTask) resolved.tasks.findByName(it.substring(it.lastIndexOf(':') + 1))
} else {
throw new GradleException("Illegal argument for uploading dependencies")
}
def archive = archiveTask.archiveFile.get().asFile
println("Uploading $archive to ~/$template/plugins")
new ProcessBuilder("scp", archive.absolutePath, "$host:~/$template/plugins/${archive.name.replace("-all", "")}").start().waitFor()
println("Uploaded $archive to ~/$template/plugins")
}
}
void startDevServer() {
def devPy = new StringBuilder().append("dev.py")
if (port != null) devPy.append(" --port $port")
if (worldName != null) devPy.append(" -w $template/$worldName")
devPy.append(" -p $template/plugins")
if (jar != null) devPy.append(" --jar $jar")
println("Starting $template with command ${devPy.toString()}")
devPy.append(" $template")
def process = new ProcessBuilder("ssh", host, "-T", devPy.toString()).start()
def running = true;
def processOutput = new BufferedReader(new InputStreamReader(process.inputStream))
new Thread({
while (running) {
if (processOutput.ready()) {
println(processOutput.readLine())
}
}
}).start()
processInput = new BufferedWriter(new OutputStreamWriter(process.outputStream))
def input = new BufferedReader(new InputStreamReader(System.in))
new Thread({
while (running) {
def text = input.readLine()
if (text == null) break
processInput.write(text)
processInput.newLine()
processInput.flush()
}
}).start()
process.waitFor()
processInput = null
running = false
}
}