From 50543ddd4e6ef50de6d5b906df4e8c5d8a530fff Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Thu, 17 Apr 2025 09:56:41 +0200 Subject: [PATCH] Improve Server starter 'steamwar.devserver.gradle' See build.gradle.kts of BauSystem --- BauSystem/build.gradle.kts | 21 +++++ buildSrc/src/steamwar.devserver.gradle | 111 +++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 buildSrc/src/steamwar.devserver.gradle diff --git a/BauSystem/build.gradle.kts b/BauSystem/build.gradle.kts index 712238c8..423a8e98 100644 --- a/BauSystem/build.gradle.kts +++ b/BauSystem/build.gradle.kts @@ -1,3 +1,5 @@ +import DevServer + /* * This file is a part of the SteamWar software. * @@ -20,6 +22,7 @@ plugins { `java-library` alias(libs.plugins.shadow) + steamwar.devserver } tasks.build { @@ -34,3 +37,21 @@ dependencies { implementation(project(":BauSystem:BauSystem_20")) implementation(project(":BauSystem:BauSystem_21")) } + +tasks.register("DevBau20") { + group = "run" + description = "Run a 1.20 Dev Bau" + dependsOn(":SpigotCore:shadowJar") + dependsOn(":BauSystem:shadowJar") + worldName = "245" + template = "Bau20" +} + +tasks.register("DevBau21") { + group = "run" + description = "Run a 1.21 Dev Bau" + dependsOn(":SpigotCore:shadowJar") + dependsOn(":BauSystem:shadowJar") + worldName = "245" + template = "Bau21" +} diff --git a/buildSrc/src/steamwar.devserver.gradle b/buildSrc/src/steamwar.devserver.gradle new file mode 100644 index 00000000..22993d27 --- /dev/null +++ b/buildSrc/src/steamwar.devserver.gradle @@ -0,0 +1,111 @@ +/* + * 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 . + */ + +import org.gradle.api.DefaultTask +import org.gradle.api.GradleException +import org.gradle.api.Project +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.bundling.AbstractArchiveTask + +plugins { +} + +class DevServer extends DefaultTask { + + @Input + worldName = "" + + @Input + template = "" + + DevServer() { + super() + doLast { + uploadDependencies() + startDevServer() + } + finalizedBy(new Finalizer()) + } + + @Internal + def BufferedWriter processInput; + + class Finalizer extends DefaultTask { + + Finalizer() { + super() + doLast { + if (processInput != null) { + processInput.write("stop\n") + processInput.flush() + } + } + } + } + + void uploadDependencies() { + this.dependsOn.forEach { + Project resolved; + if (it instanceof Project) { + resolved = (Project) it; + } else if (it instanceof String) { + resolved = project.findProject(it.substring(0, it.lastIndexOf(':'))) + } else { + throw new GradleException("Illegal argument for uploading dependencies") + } + + def archive = ((AbstractArchiveTask) resolved.tasks.findByName("shadowJar")).archiveFile.get().asFile + println("Uploading $archive to ~/plugins/${template}") + new ProcessBuilder("scp", archive.absolutePath, "sw:~/plugins/$template/${archive.name.replace("-all", "")}").start().waitFor() + println("Uploaded $archive to ~/plugins/${template}") + } + } + + void startDevServer() { + println("Starting $template with world $worldName and plugins in ~/plugins/${template}") + + def process = new ProcessBuilder("ssh", "sw", "-T", "dev.py -w $worldName -p plugins/$template $template").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 + } +} \ No newline at end of file