import sys import os from tokenize import group import xml.etree.ElementTree as ET import requests import shutil DEFAULT_SPIGOT_VER = "LATEST" POM_NAMESPACE = "http://maven.apache.org/POM/4.0.0" DEV_SERVER_PATH="development_server/" # 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" 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) if (os.getcwd().endswith("SpigotPluginBaseTools/")): os.chdir("../") def project_gen() -> int: print("Generate Project:") print("Step 1) Generate Maven project.") groupId = input("groupId (package): net.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): net.reslate.") groupId = "net.reslate." + groupId 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") spigot_ver = input("Spigot dependency version (default: \"{0}\"): ".format(DEFAULT_SPIGOT_VER)) if (len(spigot_ver) == 0): spigot_ver = DEFAULT_SPIGOT_VER spigot_ver_check = input("Using \"{0}\". Is this fine? (Y/n)".format(spigot_ver)) if (len(spigot_ver_check) == 0): spigot_ver_check = "y" while (spigot_ver_check.lower() != "y"): if spigot_ver_check.lower() == "n": spigot_ver = input("Spigot dependency version (Maven Dep. Ver.): ") spigot_ver_check = input("Entered \"{0}\". Is this fine? (Y/n)".format(spigot_ver)) else: spigot_ver_check = input("Please enter \"y\" to continue, or \"n\" to enter version again: ") print("Using version: \"{0}\"".format(spigot_ver)) pom_path = "{0}/pom.xml".format(artifactId) ET.register_namespace("", POM_NAMESPACE) pom_xml = ET.parse(pom_path) pom_root = pom_xml.getroot() repositories_elem = ET.Element("repositories") repository_elem = ET.SubElement(repositories_elem, "repository") ET.SubElement(repository_elem, "id").text = "spigot-repo" ET.SubElement(repository_elem, "url").text = "https://hub.spigotmc.org/nexus/content/repositories/public/" pom_root.append(repositories_elem) spigot_dep = ET.Element("dependency") ET.SubElement(spigot_dep, "groupId").text = "org.spigotmc" ET.SubElement(spigot_dep, "artifactId").text = "spigot-api" ET.SubElement(spigot_dep, "version").text = spigot_ver ET.SubElement(spigot_dep, "type").text = "jar" ET.SubElement(spigot_dep, "scope").text = "provided" dependencies = pom_root.find("{" + POM_NAMESPACE + "}" + "dependencies") dependencies.append(spigot_dep) pom_xml.write(pom_path) print("Step 3) Validate pom.xml") if os.system("mvn -B validate -f {0}".format(pom_path)) != 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(pom_path)) != 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) build_tool_request = requests.get(BUILDTOOLS_URL, allow_redirects=True) build_tools_file = open("BuildTools.jar", "wb") build_tools_file.write(build_tool_request.content) build_tools_file.close() print("Step 2) Generate server JAR with BuildTools.jar") spigot_ver = input("Spigot version to use (buildtools rev.) (default: \"{0}\"): ".format(DEFAULT_SERVER_VER)) if (len(spigot_ver) == 0): spigot_ver = DEFAULT_SERVER_VER spigot_ver_verify = input("Attempt to use \"{0}\"? (Y/n)".format(spigot_ver)) if (len(spigot_ver_verify) == 0): spigot_ver_verify = "y" while spigot_ver_verify.lower() != "y": if (spigot_ver_verify.lower() == "n"): spigot_ver = input("Spigot version to use: ") spigot_ver_verify = input("Attempt to use \"{0}\"? (Y/n)".format(spigot_ver)) else: spigot_ver_verify = input("Attempt to use \"{0}\"? Please enter \"y\" or \"n\": ".format(spigot_ver)) if os.system(BUILDTOOLS_CMD.format(ver = spigot_ver)) != 0: print("Error while running build tools.") return 1 print("Step 3) Moving JARs and cleaning up.") print("Moving generated JAR file.") if spigot_ver == DEFAULT_SERVER_VER: dir_entries = os.listdir() for entry in dir_entries: if (entry.startswith("spigot") and entry.endswith(".jar")): shutil.move(entry, "../spigot.jar") else: shutil.move("spigot-{0}.jar".format(spigot_ver), "../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: pom_xml_dir = find_pom_xml_dir() if len(pom_xml_dir) != 0: shutil.rmtree(pom_xml_dir) print("Deleted Maven project directory.") if os.path.exists(DEV_SERVER_PATH): shutil.rmtree(DEV_SERVER_PATH) print("Deleted development server directory.") def find_pom_xml() -> str: return find_pom_xml_dir() + "/pom.xml" def find_pom_xml_dir() -> str: directories = os.listdir() if os.path.exists("./pom.xml"): return "./" for directory in directories: if os.path.isdir(directory) and "pom.xml" in os.listdir(directory): return directory def __print_help(): print("usage: {0} ".format(sys.argv[0])) if __name__ == "__main__": if len(sys.argv) != 2: __print_help() exit(1) else: if sys.argv[1] == "setup": if project_gen() != 0: exit(1) if gen_dev_server() != 0: exit(1) elif sys.argv[1] == "project": if project_gen() != 0: exit(1) elif sys.argv[1] == "server": if gen_dev_server() != 0: exit(1) elif sys.argv[1] == "reset": reset() else: __print_help() exit(1)