Refactor some of the CLI tools (#696)

Hi. See commit messages for details 😄
This commit is contained in:
Kirill Volozhanin 2023-07-31 19:46:09 +05:00 committed by GitHub
parent b688c29d08
commit fbad5fb78a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 72 deletions

View file

@ -1,55 +1,57 @@
import os, shutil
import shutil
from pathlib import Path
user_path = os.path.expanduser("~")
cf_path = user_path + "/curseforge/minecraft/Instances/Fabulously Optimized/"
mmc_path = user_path + "/Documents/MultiMC/instances/Fabulously Optimized/minecraft/"
git_path = user_path + "/Documents/GitHub/fabulously-optimized/"
cf_path = Path(Path.home(), "/curseforge/minecraft/Instances/Fabulously Optimized/")
mmc_path = Path(Path.home(), "/Documents/MultiMC/instances/Fabulously Optimized/minecraft/")
git_path = Path(Path.home(), "/Documents/GitHub/fabulously-optimized/")
minecraft_version = "1.20.1"
packwiz_path = git_path + "Packwiz/" + minecraft_version
packwiz_path = Path(git_path, "Packwiz", minecraft_version)
# Functions
def remove_dir(path, description):
if os.path.isdir(path):
def remove_dir(path: Path, description: str) -> None:
if path.is_dir():
shutil.rmtree(path)
print("Deleted " + description)
print(f"Deleted {description}")
else:
print("Skipped " + description + " deletion, didn't exist")
print(f"Skipped {description} deletion, didn't exist")
def remove_file(path, description):
if os.path.isfile(path):
os.remove(path)
print("Deleted " + description)
def remove_file(path: Path, description: str) -> None:
if path.is_file():
path.unlink()
print(f"Deleted {description}")
else:
print("Skipped " + description + " deletion, didn't exist")
print(f"Skipped {description} deletion, didn't exist")
def copy_dir(from_path, to_path, from_desc, to_desc):
if os.path.isdir(from_path):
def copy_dir(from_path: Path, to_path: Path, from_desc: str, to_desc: str) -> None:
if from_path.is_dir():
shutil.copytree(from_path, to_path, dirs_exist_ok=True)
print("Copied " + from_desc + " to " + to_desc)
print(f"Copied {from_desc} to {to_desc}")
else:
print("Skipped " + from_desc + " copying to " + to_desc + ", didn't exist")
print(f"Skipped {from_desc} copying to {to_desc}, didn't exist")
def copy_file(from_path, to_path, from_desc, to_desc):
if os.path.isfile(from_path):
def copy_file(from_path: Path, to_path: Path, from_desc: str, to_desc: str) -> None:
if from_path.is_file():
shutil.copy2(from_path, to_path)
print("Copied " + from_desc + " to " + to_desc)
print(f"Copied {from_desc} to {to_desc}")
else:
print("Skipped " + from_desc + " copying to " + to_desc + ", didn't exist")
print(f"Skipped {from_desc} copying to {to_desc}, didn't exist")
# CurseForge to MultiMC
remove_dir(Path(mmc_path, "mods"), "MultiMC mods")
remove_dir(Path(mmc_path, "config"), "MultiMC configs")
remove_dir(Path(mmc_path, "resourcepacks"), "MultiMC resourcepacks")
remove_file(Path(mmc_path, "options.txt"), "MultiMC options.txt")
copy_dir(Path(cf_path, "mods"), Path(mmc_path, "mods"), "CurseForge mods", "MultiMC")
copy_dir(Path(cf_path, "config"), Path(mmc_path, "config"), "CurseForge configs", "MultiMC")
copy_dir(Path(cf_path, "resourcepacks"), Path(mmc_path, "resourcepacks"), "CurseForge resource packs", "MultiMC")
remove_dir(mmc_path + "config/", "MultiMC configs")
remove_file(mmc_path + "options.txt", "MultiMC options.txt")
remove_dir(mmc_path + "mods/", "MultiMC mods")
remove_dir(mmc_path + "resourcepacks/", "MultiMC resourcepacks")
copy_dir(cf_path + "config/", mmc_path + "config/", "CurseForge configs", "MultiMC")
copy_dir(cf_path + "mods/", mmc_path + "mods/", "CurseForge mods", "MultiMC")
copy_dir(cf_path + "resourcepacks/", mmc_path + "resourcepacks/", "CurseForge resource packs", "MultiMC")
# CurseForge to Git
# CurseForge to Git
# Manifest and json copying moved to "CurseForge to Packwiz.py" to make sure they are always newest
remove_dir(packwiz_path + "/config/", "Packwiz configs in Git")
copy_dir(cf_path + "config/", packwiz_path + "/config/", "CurseForge configs", "Git (Packwiz)")
copy_dir(cf_path + "resourcepacks/", packwiz_path + "/resourcepacks/", "CurseForge resource packs", "Git (Packwiz)")
remove_dir(Path(packwiz_path, "config"), "Packwiz configs in Git")
copy_dir(Path(cf_path, "config"), Path(packwiz_path, "config"), "CurseForge configs", "Git (Packwiz)")
copy_dir(Path(cf_path, "resourcepacks"), Path(packwiz_path, "resourcepacks"), "CurseForge resource packs", "Git (Packwiz)")

View file

@ -1,46 +1,64 @@
import os, shutil, re, zipfile
import re
import shutil
import zipfile
from pathlib import Path
user_path = os.path.expanduser("~")
mmc_path = user_path + "/Documents/MultiMC/instances/Fabulously Optimized/"
git_path = user_path + "/Documents/GitHub/fabulously-optimized/"
output_path = user_path + "/Desktop/"
# Functions
mmc_path = Path(Path.home(), "/Documents/MultiMC/instances/Fabulously Optimized/")
git_path = Path(Path.home(), "/Documents/GitHub/fabulously-optimized/")
output_path = Path(Path.home(), "/Desktop/")
def copy_file(from_path, to_path, from_desc, to_desc):
if os.path.isfile(from_path):
def copy_file(from_path: Path, to_path: Path, from_desc: str, to_desc: str) -> None:
if from_path.is_file():
shutil.copy2(from_path, to_path)
print("Copied " + from_desc + " to " + to_desc)
print(f"Copied {from_desc} to {to_desc}")
else:
print("Skipped " + from_desc + " copying to " + to_desc + ", didn't exist")
print(f"Skipped {from_desc} copying to {to_desc}, didn't exist")
def copy_to_archive(from_path, to_path, archive_path):
def copy_to_archive(from_path: Path, to_path: Path, archive_path: Path) -> None:
files = []
with zipfile.ZipFile(archive_path) as archive:
for zipinfo in archive.infolist():
if zipinfo.filename != to_path:
files.append((zipinfo.filename, archive.read(zipinfo.filename)))
with zipfile.ZipFile(archive_path, "r") as archive:
for info in archive.infolist():
if info.filename != to_path:
files.append((info.filename, archive.read(info.filename)))
with zipfile.ZipFile(archive_path, "w") as archive:
for filename, content in files:
archive.writestr(filename, content)
archive.write(from_path, to_path)
# MultiMC to Git
# MultiMC to Git
version = "0.0.0"
pattern = re.compile(r"\d+\.\d+\.\d+-?\w*\.?\d*")
with open(mmc_path + "instance.cfg") as file:
with open(Path(mmc_path, "instance.cfg"), "r") as file:
if match := pattern.search(file.read()):
version = match.group()
with open(git_path + "/MultiMC/Fabulously Optimized x.y.z/instance.cfg", "r+") as file:
with open(Path(git_path, "/MultiMC/Fabulously Optimized x.y.z/instance.cfg"), "r+") as file:
data = pattern.sub(version, file.read())
file.seek(0); file.truncate(); file.write(data)
file.seek(0)
file.truncate()
file.write(data)
copy_to_archive(git_path + "/MultiMC/Fabulously Optimized x.y.z/instance.cfg", f"Fabulously Optimized {version}/instance.cfg", output_path + f"Fabulously Optimized {version}.zip")
copy_file(mmc_path + "mmc-pack.json", git_path + "MultiMC/Fabulously Optimized x.y.z/mmc-pack.json", "MultiMC mmc-pack.json", "Git")
copy_file(mmc_path + "pack.png", git_path + "MultiMC/Fabulously Optimized x.y.z/pack.png", "MultiMC pack.png", "Git")
copy_to_archive(
Path(git_path, "/MultiMC/Fabulously Optimized x.y.z/instance.cfg"),
Path(f"Fabulously Optimized {version}/instance.cfg"),
Path(output_path, f"Fabulously Optimized {version}.zip"),
)
copy_file(
Path(mmc_path, "mmc-pack.json"),
Path(git_path, "MultiMC/Fabulously Optimized x.y.z/mmc-pack.json"),
"MultiMC mmc-pack.json",
"Git",
)
copy_file(
Path(mmc_path, "pack.png"),
Path(git_path, "MultiMC/Fabulously Optimized x.y.z/pack.png"),
"MultiMC pack.png",
"Git",
)

View file

@ -1,26 +1,31 @@
import os, json
import json
from pathlib import Path
user_path = os.path.expanduser("~")
cf_path = user_path + "/curseforge/minecraft/Instances/Fabulously Optimized/"
title_screen_path = cf_path + "config/isxander-main-menu-credits.json"
warning_path = cf_path + "config/fabric_loader_dependencies.json"
def load_json(path):
return json.load(open(path))
cf_path = Path(Path.home(), "/curseforge/minecraft/Instances/Fabulously Optimized/")
title_screen_path = Path(cf_path, "config/isxander-main-menu-credits.json")
warning_path = Path(cf_path, "config/fabric_loader_dependencies.json")
def save_file(path, obj):
def load_json(path: Path):
with open(path, "r") as f:
return json.load(f)
def save_file(path: Path, obj) -> None:
with open(path, "w") as f:
json.dump(obj, f, separators=(',', ':'))
json.dump(obj, f, separators=(",", ":"))
title_screen_obj = load_json(title_screen_path)
existing_version = title_screen_obj["main_menu"]["bottom_right"][0]["text"]
print("Current version: " + existing_version)
print(f"Current version: {existing_version}")
new_version = input("Enter new version: Fabulously Optimized ")
title_screen_obj["main_menu"]["bottom_right"][0]["text"] = "Fabulously Optimized " + new_version
title_screen_obj["main_menu"]["bottom_right"][0]["text"] = f"Fabulously Optimized {new_version}"
save_file(title_screen_path, title_screen_obj)
warning_file_obj = load_json(warning_path)
warning_file_obj["overrides"]["minecraft"]["+recommends"]["Fabulously Optimized"] = ">" + new_version
save_file(warning_path, warning_file_obj)
warning_file_obj["overrides"]["minecraft"]["+recommends"]["Fabulously Optimized"] = f">{new_version}"
save_file(warning_path, warning_file_obj)