# import subprocess # import sys # def generate_executable(): # """Prompts for a version and generates a single-file executable.""" # # 1. Ask the user for the version number # version = "" # while not version: # version = input("Enter the version for the executable (e.g., 4.1): ") # if not version: # print("Version cannot be empty. Please try again.") # executable_name = f"SwapStationDashboard_v{version}" # print(f"Generating executable with name: {executable_name}") # # Check if pyinstaller is installed # try: # subprocess.run([sys.executable, "-m", "PyInstaller", "--version"], check=True, capture_output=True) # except (subprocess.CalledProcessError, FileNotFoundError): # print("PyInstaller is not found. Please install it using: pip install pyinstaller") # return # print("Starting executable generation...") # # 2. Use the version to create the command # command = [ # sys.executable, # "-m", "PyInstaller", # f"--name={executable_name}", # "--onefile", # "--icon=assets/icon.ico", # "--add-data=logo;logo", # "--add-data=assets;assets", # "--add-data=proto;proto", # "--hidden-import=paho.mqtt", # "--hidden-import=google.protobuf", # "--hidden-import=PyQt6", # "--hidden-import=PyQt6.Qt6", # "--hidden-import=PyQt6.sip", # "--hidden-import=setuptools", # "main.py" # ] # try: # # 3. Execute the command # subprocess.run(command, check=True) # print("\nāœ… Executable generated successfully!") # print(f"Look for '{executable_name}.exe' in the 'dist' folder.") # except subprocess.CalledProcessError as e: # print("\nāŒ An error occurred during executable generation.") # print(f"Command failed with return code: {e.returncode}") # except FileNotFoundError: # print("\nāŒ Error: The 'main.py' file was not found. Please run this script from the project's root directory.") # if __name__ == "__main__": # generate_executable() import subprocess import sys from pathlib import Path def generate_executable(): """Prompts for a version, writes VERSION.txt, and builds a single-file exe that shows the same version inside the app.""" # --- Ask version --- version = "" while not version: version = input("Enter the version for the executable (e.g., 4.1): ").strip() if not version: print("Version cannot be empty. Please try again.") # Normalize how you want to display it in the app: display_version = f"v{version}" # --- Paths --- project_root = Path(__file__).resolve().parent version_file = project_root / "VERSION.txt" executable_name = f"SwapStationDashboard_{display_version}" # --- Persist version for the app --- version_file.write_text(display_version, encoding="utf-8") print(f"šŸ“¦ Wrote {version_file} with '{display_version}'") # --- Check PyInstaller --- try: subprocess.run([sys.executable, "-m", "PyInstaller", "--version"], check=True, capture_output=True) except (subprocess.CalledProcessError, FileNotFoundError): print("PyInstaller is not found. Please install it using: pip install pyinstaller") return print(f"šŸš€ Building: {executable_name}") # NOTE: On Windows, --add-data uses 'src;dst' (semicolon). (You already follow this.) command = [ sys.executable, "-m", "PyInstaller", f"--name={executable_name}", "--onefile", "--icon=assets/icon.ico", "--add-data=logo;logo", "--add-data=assets;assets", "--add-data=proto;proto", "--add-data=VERSION.txt;.", # <-- bundle VERSION.txt at app root inside the exe "--hidden-import=paho.mqtt", "--hidden-import=google.protobuf", "--hidden-import=PyQt6", "--hidden-import=PyQt6.Qt6", "--hidden-import=PyQt6.sip", "--hidden-import=setuptools", "main.py", ] try: subprocess.run(command, check=True) print("\nāœ… Executable generated successfully!") print(f"šŸ“ Find '{executable_name}.exe' in the 'dist' folder.") except subprocess.CalledProcessError as e: print("\nāŒ An error occurred during executable generation.") print(f"Command failed with return code: {e.returncode}") except FileNotFoundError: print("\nāŒ Error: The 'main.py' file was not found. Please run this script from the project's root directory.") if __name__ == "__main__": generate_executable()