mirror of
https://github.com/bombsquad-community/plugin-manager.git
synced 2025-10-08 14:54:36 +00:00
Add support for plugman and getting latest version
This commit is contained in:
parent
8194012fa6
commit
e7476a6ae2
1 changed files with 69 additions and 34 deletions
|
|
@ -1,56 +1,84 @@
|
||||||
import ast
|
import ast
|
||||||
import json
|
import json
|
||||||
import sys
|
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
|
||||||
|
|
||||||
|
|
||||||
def debug_print(*args, **kwargs):
|
except Exception as e:
|
||||||
if DEBUG:
|
raise e
|
||||||
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):
|
def update_plugin_json(plugin_info, category):
|
||||||
name = plugin_info["plugin_name"]
|
name = plugin_info["plugin_name"]
|
||||||
import os
|
|
||||||
|
|
||||||
print(os.getcwd())
|
|
||||||
|
|
||||||
with open(f"plugins/{category}/{category}.json", "r+") as file:
|
with open(f"plugins/{category}/{category}.json", "r+") as file:
|
||||||
data = json.load(file)
|
data = json.load(file)
|
||||||
try:
|
try:
|
||||||
# Check if plugin is already in the json
|
# Check if plugin is already in the json
|
||||||
plugin = data["plugins"][name]
|
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(".", ""))
|
current_version = int(plugin_info["version"].replace(".", ""))
|
||||||
# `or` In case another change was made on the plugin while still on pr
|
# Ensure the version is always greater from the already released version
|
||||||
if current_version > plugman_version or current_version == plugman_version:
|
if current_version > plugman_version:
|
||||||
plugin["versions"][plugin_info["version"]] = None
|
plugin["versions"][plugin_info["version"]] = None
|
||||||
# Ensure latest version appears first
|
# 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["description"] = plugin_info["description"]
|
||||||
plugin["external_url"] = plugin_info["external_url"]
|
plugin["external_url"] = plugin_info["external_url"]
|
||||||
plugin["authors"] = [
|
plugin["authors"] = plugin_info["authors"]
|
||||||
{
|
|
||||||
"name": ", ".join(plugin_info["author"]),
|
|
||||||
"discord": plugin_info["discord"],
|
|
||||||
"email": plugin_info["email"],
|
|
||||||
}
|
|
||||||
]
|
|
||||||
elif current_version < plugman_version:
|
elif current_version < plugman_version:
|
||||||
raise Exception("Version cant be lower than the previous")
|
raise Exception("Version cant be lower than the previous")
|
||||||
except KeyError:
|
except KeyError:
|
||||||
data["plugins"][name] = {
|
data["plugins"][name] = {
|
||||||
"description": plugin_info["description"],
|
"description": plugin_info["description"],
|
||||||
"external_url": plugin_info["external_url"],
|
"external_url": plugin_info["external_url"],
|
||||||
"authors": [
|
"authors": plugin_info["authors"],
|
||||||
{
|
|
||||||
"name": ", ".join(plugin_info["author"]),
|
|
||||||
"discord": plugin_info["discord"],
|
|
||||||
"email": plugin_info["email"],
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"versions": {plugin_info["version"]: None},
|
"versions": {plugin_info["version"]: None},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,11 +91,13 @@ def update_plugin_json(plugin_info, category):
|
||||||
def extract_plugman(plugins):
|
def extract_plugman(plugins):
|
||||||
for plugin in plugins:
|
for plugin in plugins:
|
||||||
if "plugins/" in plugin:
|
if "plugins/" in plugin:
|
||||||
debug_print(plugin)
|
try:
|
||||||
# Split the path and get the part after 'plugins/'
|
# Split the path and get the part after 'plugins/'
|
||||||
parts = plugin.split("plugins/")[1].split("/")
|
parts = plugin.split("plugins/")[1].split("/")
|
||||||
category = parts[0] # First part after plugins/
|
category = parts[0] # First part after plugins/
|
||||||
|
except ValueError:
|
||||||
|
if "plugin_manager" in plugin:
|
||||||
|
continue
|
||||||
with open(plugin, "r") as f:
|
with open(plugin, "r") as f:
|
||||||
tree = ast.parse(f.read())
|
tree = ast.parse(f.read())
|
||||||
|
|
||||||
|
|
@ -87,8 +117,13 @@ def extract_plugman(plugins):
|
||||||
result = {}
|
result = {}
|
||||||
for kw in node.value.keywords:
|
for kw in node.value.keywords:
|
||||||
result[kw.arg] = ast.literal_eval(kw.value)
|
result[kw.arg] = ast.literal_eval(kw.value)
|
||||||
update_plugin_json(result, category=category)
|
if category:
|
||||||
raise ValueError("Variable plugman not found in the file or has unsupported format.")
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue