diff --git a/test/auto_apply_plugin_metadata.py b/test/auto_apply_plugin_metadata.py index 95efb91..d3bf1a7 100644 --- a/test/auto_apply_plugin_metadata.py +++ b/test/auto_apply_plugin_metadata.py @@ -1,56 +1,84 @@ import ast import json import sys -from ast import Call, Dict, Name, Constant, keyword +from urllib.request import urlopen -DEBUG = True +def get_latest_version(plugin_name, category): + base_url = "https://github.com/bombsquad-community/plugin-manager/raw/main/" + endpoints = { + "minigames": "plugins/minigames.json", + "utilities": "plugins/utilities.json", + "maps": "plugins/maps.json", + "plugman": "index.json" + } + + try: + with urlopen(f"{base_url}{endpoints[category]}") as response: + data = json.loads(response.read().decode('utf-8')) + + # Handle plugman separately + if category == "plugman": + version = next(iter(data.get("versions"))) + return version + + # For plugins + plugin = data.get("plugins", {}).get(plugin_name) + if not plugin: + return None + + # Get latest version from versions dict + if "versions" in plugin and isinstance(plugin["versions"], dict): + latest_version = next(iter(plugin["versions"])) # Gets first key + return latest_version + + + except Exception as e: + raise e + - -def debug_print(*args, **kwargs): - if DEBUG: - print(*args, **kwargs) +def update_plugman_json(version): + with open("index.json", "r+") as file: + data = json.load(file) + plugman_version = int(get_latest_version("plugin_manager", "plugman").replace(".", "")) + current_version = int(version["version"].replace(".", "")) + + if current_version > plugman_version: + with open("index.json", "r+") as file: + data = json.load(file) + data[current_version] = None + data["versions"] = dict( + sorted(data["versions"].items(), reverse=True) + ) + def update_plugin_json(plugin_info, category): name = plugin_info["plugin_name"] - import os - - print(os.getcwd()) with open(f"plugins/{category}/{category}.json", "r+") as file: data = json.load(file) try: # Check if plugin is already in the json plugin = data["plugins"][name] - plugman_version = int(plugin["version"].replace(".", "")) + plugman_version = int(get_latest_version(name, category).replace(".", "")) current_version = int(plugin_info["version"].replace(".", "")) - # `or` In case another change was made on the plugin while still on pr - if current_version > plugman_version or current_version == plugman_version: + # Ensure the version is always greater from the already released version + if current_version > plugman_version: plugin["versions"][plugin_info["version"]] = None # Ensure latest version appears first - plugin["versions"] = dict(sorted(plugin["versions"].items(), reverse=True)) + plugin["versions"] = dict( + sorted(plugin["versions"].items(), reverse=True) + ) plugin["description"] = plugin_info["description"] plugin["external_url"] = plugin_info["external_url"] - plugin["authors"] = [ - { - "name": ", ".join(plugin_info["author"]), - "discord": plugin_info["discord"], - "email": plugin_info["email"], - } - ] + plugin["authors"] = plugin_info["authors"] elif current_version < plugman_version: raise Exception("Version cant be lower than the previous") except KeyError: data["plugins"][name] = { "description": plugin_info["description"], "external_url": plugin_info["external_url"], - "authors": [ - { - "name": ", ".join(plugin_info["author"]), - "discord": plugin_info["discord"], - "email": plugin_info["email"], - } - ], + "authors": plugin_info["authors"], "versions": {plugin_info["version"]: None}, } @@ -63,11 +91,13 @@ def update_plugin_json(plugin_info, category): def extract_plugman(plugins): for plugin in plugins: if "plugins/" in plugin: - debug_print(plugin) - # Split the path and get the part after 'plugins/' - parts = plugin.split("plugins/")[1].split("/") - category = parts[0] # First part after plugins/ - + try: + # Split the path and get the part after 'plugins/' + parts = plugin.split("plugins/")[1].split("/") + category = parts[0] # First part after plugins/ + except ValueError: + if "plugin_manager" in plugin: + continue with open(plugin, "r") as f: tree = ast.parse(f.read()) @@ -87,8 +117,13 @@ def extract_plugman(plugins): result = {} for kw in node.value.keywords: result[kw.arg] = ast.literal_eval(kw.value) - update_plugin_json(result, category=category) - raise ValueError("Variable plugman not found in the file or has unsupported format.") + if category: + update_plugin_json(result, category=category) + else: + update_plugman_json(result) + raise ValueError( + "Variable plugman not found in the file or has unsupported format." + ) if __name__ == "__main__":