forked from SteamWar/SteamWar
Improve Server starter 'steamwar.devserver.gradle'
See build.gradle.kts of BauSystem
This commit is contained in:
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user