2021-04-01 21:10:11 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2022-01-20 10:24:03 +00:00
|
|
|
from tokenize import group
|
2021-04-01 21:10:11 +00:00
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import requests
|
|
|
|
import shutil
|
|
|
|
|
2021-04-02 01:43:29 +00:00
|
|
|
DEFAULT_SPIGOT_VER = "LATEST"
|
2021-04-01 21:10:11 +00:00
|
|
|
POM_NAMESPACE = "http://maven.apache.org/POM/4.0.0"
|
|
|
|
DEV_SERVER_PATH="development_server/"
|
2022-01-20 10:11:05 +00:00
|
|
|
|
|
|
|
# Command from: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
|
|
|
|
MVN_CMD = "mvn -B archetype:generate -DgroupId={groupId} -DartifactId={artifactId} -DarchetypeArtifactId=maven-archetype-quickstart"
|
2021-04-01 21:10:11 +00:00
|
|
|
|
|
|
|
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 sys.version_info.major != 3:
|
|
|
|
print("Script requires Python3 to run. You are currently running {0}.".format(sys.version))
|
|
|
|
exit(1)
|
|
|
|
|
2022-01-20 10:16:56 +00:00
|
|
|
if (os.getcwd().endswith("SpigotPluginBaseTools/")):
|
2021-04-02 01:20:33 +00:00
|
|
|
os.chdir("../")
|
|
|
|
|
2021-04-01 21:10:11 +00:00
|
|
|
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.")
|
2022-01-20 10:24:03 +00:00
|
|
|
groupId = input("groupId (package): xyz.reslate.")
|
|
|
|
groupId = "xyz.reslate." + groupId
|
|
|
|
|
2021-04-01 21:10:11 +00:00
|
|
|
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")
|
2021-04-02 01:43:29 +00:00
|
|
|
spigotver = input("Spigot dependency version (default: \"{0}\"): ".format(DEFAULT_SPIGOT_VER))
|
|
|
|
if (len(spigotver) == 0): spigotver = DEFAULT_SPIGOT_VER
|
|
|
|
|
|
|
|
spigotvercheck = input("Using \"{0}\". Is this fine? (Y/n)".format(spigotver))
|
|
|
|
if (len(spigotvercheck) == 0): spigotvercheck = "y"
|
|
|
|
while (spigotvercheck.lower() != "y"):
|
|
|
|
if spigotvercheck.lower() == "n":
|
|
|
|
spigotver = input("Spigot dependency version (Maven Dep. Ver.): ")
|
|
|
|
spigotvercheck = input("Entered \"{0}\". Is this fine? (Y/n)".format(spigotver))
|
2021-04-01 21:10:11 +00:00
|
|
|
else:
|
2021-04-02 01:43:29 +00:00
|
|
|
spigotvercheck = input("Please enter \"y\" to continue, or \"n\" to enter version again: ")
|
|
|
|
print("Using version: \"{0}\"".format(spigotver))
|
2021-04-01 21:10:11 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-04-02 01:43:29 +00:00
|
|
|
spigotdep = ET.Element("dependency")
|
|
|
|
ET.SubElement(spigotdep, "groupId").text = "org.spigotmc"
|
|
|
|
ET.SubElement(spigotdep, "artifactId").text = "spigot-api"
|
|
|
|
ET.SubElement(spigotdep, "version").text = spigotver
|
|
|
|
ET.SubElement(spigotdep, "type").text = "jar"
|
|
|
|
ET.SubElement(spigotdep, "scope").text = "provided"
|
2021-04-01 21:10:11 +00:00
|
|
|
|
|
|
|
dependencies = pomroot.find("{" + POM_NAMESPACE + "}" + "dependencies")
|
2021-04-02 01:43:29 +00:00
|
|
|
dependencies.append(spigotdep)
|
2021-04-01 21:10:11 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2021-04-02 01:43:29 +00:00
|
|
|
spigotver = input("Spigot version to use (buildtools rev.) (default: \"{0}\"): ".format(DEFAULT_SERVER_VER))
|
|
|
|
if (len(spigotver) == 0): spigotver = DEFAULT_SERVER_VER
|
|
|
|
spigotververify = input("Attempt to use \"{0}\"? (Y/n)".format(spigotver))
|
|
|
|
if (len(spigotververify) == 0): spigotververify = "y"
|
|
|
|
while spigotververify.lower() != "y":
|
|
|
|
if (spigotververify.lower() == "n"):
|
|
|
|
spigotver = input("Spigot version to use: ")
|
|
|
|
spigotververify = input("Attempt to use \"{0}\"? (Y/n)".format(spigotver))
|
2021-04-01 21:10:11 +00:00
|
|
|
else:
|
2021-04-02 01:43:29 +00:00
|
|
|
spigotververify = input("Attempt to use \"{0}\"? Please enter \"y\" or \"n\": ".format(spigotver))
|
2021-04-01 21:10:11 +00:00
|
|
|
|
2021-04-02 01:43:29 +00:00
|
|
|
if os.system(BUILDTOOLS_CMD.format(ver = spigotver)) != 0:
|
2021-04-01 21:10:11 +00:00
|
|
|
print("Error while running build tools.")
|
|
|
|
return 1
|
|
|
|
|
|
|
|
print("Step 3) Moving JARs and cleaning up.")
|
|
|
|
|
|
|
|
print("Moving generated JAR file.")
|
2021-04-02 01:43:29 +00:00
|
|
|
if spigotver == DEFAULT_SERVER_VER:
|
2021-04-01 21:10:11 +00:00
|
|
|
direntries = os.listdir()
|
|
|
|
for entry in direntries:
|
|
|
|
if (entry.startswith("spigot") and entry.endswith(".jar")):
|
|
|
|
shutil.move(entry, "../spigot.jar")
|
|
|
|
else:
|
2021-04-02 01:43:29 +00:00
|
|
|
shutil.move("spigot-{0}.jar".format(spigotver), "../spigot.jar")
|
2021-04-01 21:10:11 +00:00
|
|
|
|
|
|
|
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()
|
2021-04-02 01:43:29 +00:00
|
|
|
if len(pomxmldir) != 0:
|
2021-04-01 21:10:11 +00:00
|
|
|
shutil.rmtree(pomxmldir)
|
2021-04-01 21:26:55 +00:00
|
|
|
print("Deleted Maven project directory.")
|
2021-04-01 21:10:11 +00:00
|
|
|
|
|
|
|
if os.path.exists(DEV_SERVER_PATH):
|
|
|
|
shutil.rmtree(DEV_SERVER_PATH)
|
2021-04-01 21:26:55 +00:00
|
|
|
print("Deleted development server directory.")
|
2021-04-01 21:10:11 +00:00
|
|
|
|
|
|
|
def find_pomxml() -> str:
|
|
|
|
return find_pomxmldir() + "/pom.xml"
|
|
|
|
|
|
|
|
def find_pomxmldir() -> str:
|
|
|
|
directories = os.listdir()
|
2022-01-20 23:11:44 +00:00
|
|
|
if os.path.exists("./pom.xml"):
|
|
|
|
return "./"
|
|
|
|
|
2021-04-01 21:10:11 +00:00
|
|
|
for directory in directories:
|
|
|
|
if os.path.isdir(directory) and "pom.xml" in os.listdir(directory):
|
|
|
|
return directory
|
2021-04-02 01:43:29 +00:00
|
|
|
|
|
|
|
def __printhelp():
|
|
|
|
print("usage: {0} <setup | project | server | reset>".format(sys.argv[0]))
|
2021-04-01 21:10:11 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) != 2:
|
2021-04-02 01:43:29 +00:00
|
|
|
__printhelp()
|
2021-04-01 21:10:11 +00:00
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
if sys.argv[1] == "setup":
|
2021-04-02 01:15:24 +00:00
|
|
|
if project_gen() != 0: exit(1)
|
|
|
|
if gen_dev_server() != 0: exit(1)
|
2021-04-01 21:10:11 +00:00
|
|
|
elif sys.argv[1] == "project":
|
2021-04-02 01:15:24 +00:00
|
|
|
if project_gen() != 0: exit(1)
|
2021-04-01 21:10:11 +00:00
|
|
|
elif sys.argv[1] == "server":
|
2021-04-02 01:15:24 +00:00
|
|
|
if gen_dev_server() != 0: exit(1)
|
2021-04-01 21:10:11 +00:00
|
|
|
elif sys.argv[1] == "reset":
|
2021-04-02 01:43:29 +00:00
|
|
|
reset()
|
|
|
|
else:
|
|
|
|
__printhelp()
|
|
|
|
exit(1)
|