Make even more amazing

This commit is contained in:
Maarceeli 2025-05-24 10:57:44 +02:00
parent 69260388ef
commit 34d8e2516c
Signed by: Marceeli
GPG key ID: ABC955B3D1495923

View file

@ -1,5 +1,5 @@
import flet as ft import flet as ft
import tempfile, subprocess, os, re, uuid import tempfile, subprocess, os, re, uuid, threading
import PyInstaller.__main__ import PyInstaller.__main__
@ -11,68 +11,134 @@ def main(page: ft.Page):
ruuid = uuid.uuid4().hex ruuid = uuid.uuid4().hex
base_temp_dir = os.path.join(os.getcwd(), "temp") base_temp_dir = os.path.join(os.getcwd(), "temp")
temp_dir = os.path.join(base_temp_dir, f"clone-{ruuid}") temp_dir = os.path.join(base_temp_dir, f"clone-{ruuid}")
versionprompt = ft.TextField(label="Version", expand=True)
repoprompt = ft.TextField(label="Fuji Repository", expand=True, value="https://git.marceeli.ovh/Fuji/Fuji")
log_output = ft.TextField(
value="",
multiline=True,
read_only=True,
expand=True,
min_lines=10,
max_lines=20,
border=ft.InputBorder.NONE,
text_style=ft.TextStyle(size=12),
)
progress_bar = ft.ProgressBar(width=500, value=None, visible=False)
def clone_and_set_version(version: str, repo_url: str): def clone_and_set_version(version: str, repo_url: str):
if not (version and repo_url): if not (version and repo_url):
return return
# Create a temp directory inside the current project directory
os.makedirs(base_temp_dir, exist_ok=True) os.makedirs(base_temp_dir, exist_ok=True)
os.makedirs(temp_dir) os.makedirs(temp_dir)
subprocess.run(["git", "clone", repo_url, temp_dir], check=True) subprocess.run(["git", "clone", repo_url, temp_dir], check=True)
version_file = os.path.join(temp_dir, "src", "version.py") version_file = os.path.join(temp_dir, "src", "version.py")
with open(version_file, "r", encoding="utf-8") as f: with open(version_file, "r", encoding="utf-8") as f:
contents = f.read() contents = f.read()
new_contents = re.sub( new_contents = re.sub(
r'^version\s*=\s*["\'].*?["\']', r'^version\s*=\s*["\'].*?["\']',
f'version = "{version}"', f'version = "{version}"',
contents, contents,
flags=re.MULTILINE, flags=re.MULTILINE,
) )
with open(version_file, "w", encoding="utf-8") as f: with open(version_file, "w", encoding="utf-8") as f:
f.write(new_contents) f.write(new_contents)
print(f"Updated {version_file} to version = \"{version}\"") log_output.value += f"✔ Updated version.py to version = \"{version}\"\n"
print(f"Cloned project located at: {temp_dir}") log_output.value += f"✔ Cloned repo to: {temp_dir}\n"
page.update()
versionprompt = ft.TextField(label="Version", expand=True) requirements_path = os.path.join(temp_dir, "requirements.txt")
repoprompt = ft.TextField(label="Fuji Repository", expand=True, value="https://git.marceeli.ovh/Fuji/Fuji") if os.path.isfile(requirements_path):
try:
def build(e): log_output.value += "📦 Installing requirements...\n"
clone_and_set_version(versionprompt.value.strip(), repoprompt.value.strip()) page.update()
subprocess.run(
["pip", "install", "-r", requirements_path],
check=True
)
log_output.value += "✔ Requirements installed successfully.\n"
except subprocess.CalledProcessError as e:
log_output.value += f"❌ Failed to install requirements: {e}\n"
else:
log_output.value += "⚠ No requirements.txt file found.\n"
page.update()
def run_pyinstaller():
main_file = os.path.join(temp_dir, "src", "main.py") main_file = os.path.join(temp_dir, "src", "main.py")
locales_dir = os.path.join(temp_dir, "src", "locales") locales_dir = os.path.join(temp_dir, "src", "locales")
assets_dir = os.path.join(temp_dir, "src", "assets") assets_dir = os.path.join(temp_dir, "src", "assets")
PyInstaller.__main__.run([ command = [
"pyinstaller",
main_file, main_file,
'--clean', "--clean",
'--noconfirm', "--noconfirm",
f"--add-data={locales_dir}:locales", f"--add-data={locales_dir}{os.pathsep}locales",
f"--add-data={assets_dir}:assets" f"--add-data={assets_dir}{os.pathsep}assets"
]) ]
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
progress_bar.visible = True
page.update()
for line in iter(process.stdout.readline, ''):
log_output.value += line
page.update()
button = ft.ElevatedButton(text="Build", on_click=build, height=40, expand=True, bgcolor=ft.Colors.with_opacity(0.4, ft.Colors.BLUE)) process.stdout.close()
process.wait()
log_output.value += "🏁 PyInstaller build finished.\n"
progress_bar.visible = False
page.update()
def build(e):
def task():
log_output.value = "" # Clear previous logs
page.update()
try:
clone_and_set_version(versionprompt.value.strip(), repoprompt.value.strip())
run_pyinstaller()
except Exception as ex:
log_output.value += f"❌ Build failed: {ex}\n"
progress_bar.visible = False
page.update()
threading.Thread(target=task).start()
button = ft.ElevatedButton(
text="Build",
on_click=build,
height=40,
expand=True,
bgcolor=ft.Colors.with_opacity(0.4, ft.Colors.BLUE)
)
card = ft.Card( card = ft.Card(
content=ft.Container( content=ft.Container(
content=ft.Column( content=ft.Column(
controls=[versionprompt, repoprompt, button], controls=[
spacing=10, expand=True, versionprompt,
repoprompt,
button,
progress_bar,
log_output
],
spacing=10,
expand=True,
horizontal_alignment=ft.CrossAxisAlignment.STRETCH horizontal_alignment=ft.CrossAxisAlignment.STRETCH
), ),
alignment=ft.alignment.center, alignment=ft.alignment.center,
border_radius=20, border_radius=20,
padding=25, padding=25,
width=500 width=500
) )
) )