spigot-plugin-template/scripts/gen.py
2021-04-01 16:26:55 -05:00

179 lines
6.9 KiB
Python

import sys
import os
import xml.etree.ElementTree as ET
import requests
import shutil
DEFAULT_BUKKIT_VER = "LATEST"
POM_NAMESPACE = "http://maven.apache.org/POM/4.0.0"
DEV_SERVER_PATH="development_server/"
MVN_CMD = "mvn -B archetype:generate -DgroupId={groupId} -DartifactId={artifactId} -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4";
BUILDTOOLS_URL = "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar"
BUILDTOOLS_CMD = "java -jar BuildTools.jar --rev {ver}"
BUILDTOOLS_DIR = "buildtools/"
DEFAULT_SERVER_VER = "latest"
DEFAULT_JVM_DEBUG_PORT = 25577
if (os.getcwd().endswith("scripts/")):
os.chdir("../")
if sys.version_info.major != 3:
print("Script requires Python3 to run. You are currently running {0}.".format(sys.version))
exit(1)
def project_gen() -> int:
print("Generate Project:")
print("Step 1) Generate Maven project.")
groupId = input("groupId (package): xyz.reslate.")
while (len(groupId) == 0 or (not groupId.islower() and not groupId.isupper()) or (groupId.isupper())):
print("GroupId cannot be empty and must be all lowercase.")
groupId = input("groupId (package): xyz.reslate.")
artifactId = input("artifactId (name of plugin): ")
while (len(artifactId) == 0 or (not artifactId.islower() and not artifactId.isupper()) or (artifactId.isupper())):
print("artifactId cannot be empty and must be all lowercase.")
artifactId = input("artifactId (name of plugin):")
print("Executing Maven command: \"{0}\"".format(MVN_CMD.format(groupId = groupId, artifactId = artifactId)))
if (os.system(MVN_CMD.format(groupId = groupId, artifactId = artifactId)) != 0):
print("An error occurred while executing Maven step. Stopping.")
return 1
print("Step 2) Modify pom.xml")
bukkitver = input("bukkit dependency version (default: \"{0}\"): ".format(DEFAULT_BUKKIT_VER))
if (len(bukkitver) == 0): bukkitver = DEFAULT_BUKKIT_VER
bukkitvercheck = input("Entered \"{0}\". Is this fine? (Y/n)".format(bukkitver))
if (len(bukkitvercheck) == 0): bukkitvercheck = "y"
while (bukkitvercheck.lower() != "y"):
if bukkitvercheck.lower() == "n":
bukkitver = input("bukkit dependency version (Maven Dep. Ver.): ")
bukkitvercheck = input("Entered \"{0}\". Is this fine? (Y/n)".format(bukkitver))
else:
bukkitvercheck = input("Please enter \"y\" to continue, or \"n\" to enter version again: ")
print("Using version: \"{0}\"".format(bukkitver))
pompath = "{0}/pom.xml".format(artifactId);
ET.register_namespace("", POM_NAMESPACE)
pomxml = ET.parse(pompath)
pomroot = pomxml.getroot()
repositorieselem = ET.Element("repositories")
repositoryelem = ET.SubElement(repositorieselem, "repository")
ET.SubElement(repositoryelem, "id").text = "spigot-repo"
ET.SubElement(repositoryelem, "url").text = "https://hub.spigotmc.org/nexus/content/repositories/public/"
pomroot.append(repositorieselem)
bukkitdep = ET.Element("dependency")
ET.SubElement(bukkitdep, "groupId").text = "org.bukkit"
ET.SubElement(bukkitdep, "artifactId").text = "bukkit"
ET.SubElement(bukkitdep, "version").text = bukkitver
ET.SubElement(bukkitdep, "type").text = "jar"
ET.SubElement(bukkitdep, "scope").text = "provided"
dependencies = pomroot.find("{" + POM_NAMESPACE + "}" + "dependencies")
dependencies.append(bukkitdep)
pomxml.write(pompath)
print("Step 3) Validate pom.xml")
if os.system("mvn -B validate -f {0}".format(pompath)) != 0:
print("An error has occurred. Please check output and fix issue before running again.")
return 1
print("pom.xml validated.")
print("Step 4) Resolve dependencies")
if os.system("mvn -B dependency:resolve -f {0}".format(pompath)) != 0:
print("An error has occurred. Please check output and fix issue before running again.")
return 1
print("Project generation complete.")
return 0
def gen_dev_server() -> int:
print("Generate Development Server:")
if not os.path.exists(DEV_SERVER_PATH):
os.mkdir(DEV_SERVER_PATH)
os.chdir(DEV_SERVER_PATH)
print("Step 1) Download BuildTools")
os.mkdir(BUILDTOOLS_DIR)
os.chdir(BUILDTOOLS_DIR)
buildtoolrequest = requests.get(BUILDTOOLS_URL, allow_redirects=True)
buildtoolsfile = open("BuildTools.jar", "wb")
buildtoolsfile.write(buildtoolrequest.content)
buildtoolsfile.close()
print("Step 2) Generate server JAR with BuildTools.jar")
bukkitver = input("Bukkit version to use (default: \"{0}\"): ".format(DEFAULT_SERVER_VER))
if (len(bukkitver) == 0): bukkitver = DEFAULT_SERVER_VER
bukkitververify = input("Attempt to use \"{0}\"? (Y/n)".format(bukkitver))
if (len(bukkitververify) == 0): bukkitververify = "y"
while bukkitververify.lower() != "y":
if (bukkitververify.lower() == "n"):
bukkitver = input("Bukkit version to use: ")
bukkitververify = input("Attempt to use \"{0}\"? (Y/n)".format(bukkitver))
else:
bukkitververify = input("Attempt to use \"{0}\"? Please enter \"y\" or \"n\": ".format(bukkitver))
if os.system(BUILDTOOLS_CMD.format(ver = bukkitver)) != 0:
print("Error while running build tools.")
return 1
print("Step 3) Moving JARs and cleaning up.")
print("Moving generated JAR file.")
if bukkitver == DEFAULT_SERVER_VER:
direntries = os.listdir()
for entry in direntries:
if (entry.startswith("spigot") and entry.endswith(".jar")):
shutil.move(entry, "../spigot.jar")
else:
shutil.move("spigot-{1}.jar".format(bukkitver), "../spigot.jar")
os.chdir("../")
print("Deleting \"{0}\".".format(BUILDTOOLS_DIR))
shutil.rmtree(BUILDTOOLS_DIR)
os.chdir("../")
print("Done generating files for development server.")
return 0
def reset() -> None:
pomxmldir = find_pomxmldir()
if os.path.exists(pomxmldir):
shutil.rmtree(pomxmldir)
print("Deleted Maven project directory.")
if os.path.exists(DEV_SERVER_PATH):
shutil.rmtree(DEV_SERVER_PATH)
print("Deleted development server directory.")
def find_pomxml() -> str:
return find_pomxmldir() + "/pom.xml"
def find_pomxmldir() -> str:
directories = os.listdir()
for directory in directories:
if os.path.isdir(directory) and "pom.xml" in os.listdir(directory):
return directory
if __name__ == "__main__":
if len(sys.argv) != 2:
print("usage: {0} <setup | project | server | reset>".format(sys.argv[0]))
exit(1)
else:
if sys.argv[1] == "setup":
project_gen()
gen_dev_server()
elif sys.argv[1] == "project":
project_gen()
elif sys.argv[1] == "server":
gen_dev_server()
elif sys.argv[1] == "reset":
reset()