-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
147 lines (133 loc) · 5.03 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import sys
import os
import re
import shutil
from cx_Freeze import setup, Executable, build_exe as _build_exe
# If no command is provided, default based on the OS.
# This will auto-select the proper build command if the user runs "python3 setup.py" without arguments.
if len(sys.argv) == 1:
if sys.platform.startswith("win"):
sys.argv.append("bdist_msi")
elif sys.platform == "darwin":
sys.argv.append("bdist_dmg")
elif sys.platform.startswith("linux"):
sys.argv.append("build_linux")
# Custom build command that moves settings.py into the build folder.
class CustomBuildExe(_build_exe):
def run(self):
# Run the standard build_exe process.
super().run()
# Determine the build directory used by cx_Freeze.
build_dir = self.build_exe
settings_path = "settings.py"
if os.path.exists(settings_path):
dest_path = os.path.join(build_dir, "settings.py")
try:
shutil.move(settings_path, dest_path)
print(f"Moved '{settings_path}' to '{dest_path}'")
except Exception as e:
print(f"Warning: Could not move '{settings_path}' into build folder: {e}")
# Remove the existing build folder if it exists.
build_folder = "build"
if os.path.exists(build_folder):
try:
shutil.rmtree(build_folder)
print(f"Deleted existing '{build_folder}' folder.")
except Exception as e:
print(f"Warning: could not delete '{build_folder}' folder: {e}")
# Ensure high_score.txt exists and is empty.
with open("high_score.txt", "w") as f:
pass
# Check if we are building a DMG (for macOS)
is_dmg = any("bdist_dmg" in arg for arg in sys.argv)
# Read the first line from TetraFusion.py to extract version.
with open("TetraFusion.py", "r") as f:
first_line = f.readline().strip()
match = re.search(r'#\s*Game Ver:?\s*([\d\.]+)', first_line)
version = match.group(1) if match else "0.0.0"
# Determine the executable extension based on platform and build command.
if sys.platform.startswith("win"):
exe_extension = ".exe"
elif sys.platform == "darwin":
# For macOS: if building a DMG, the app bundle should have no extension.
exe_extension = "" if is_dmg else ".exe"
else:
exe_extension = ""
exe_name = f"TetraFusion {version}{exe_extension}"
# Include additional files (assets, audio, etc.)
include_files = [
("assets", "assets"),
("Audio", "Audio"),
"high_score.txt",
"ICON1.ico",
"LICENSE.txt",
]
# On Windows, use the Win32 GUI base.
base = "Win32GUI" if sys.platform.startswith("win") else None
exe = Executable(
script="TetraFusion.py",
base=base,
target_name=exe_name,
icon="ICON1.ico", # Set the application icon
)
# Configure MSI options and shortcuts only for Windows.
if sys.platform.startswith("win"):
# Define the shortcut table for the MSI installer.
shortcut_table = [
("DesktopShortcut", # Shortcut
"DesktopFolder", # Directory_
f"TetraFusion {version}", # Shortcut name
"TARGETDIR", # Component_
f"[TARGETDIR]{exe_name}", # Target executable
None, # Arguments
None, # Description
None, # Hotkey
None, # Icon (explicit icon reference removed)
None, # IconIndex
None, # ShowCmd
"TARGETDIR", # Working directory
)
]
msi_data = {"Shortcut": shortcut_table}
bdist_msi_options = {
"data": msi_data,
"upgrade_code": "{E1234567-1234-5678-1234-56789ABCDEF0}", # Replace with a unique GUID
}
else:
bdist_msi_options = {}
# For Linux, register a new command name "build_linux" that uses our custom build class.
cmdclass = {}
if sys.platform.startswith("linux"):
class BuildLinux(CustomBuildExe):
description = "Build executable for Linux"
cmdclass["build_linux"] = BuildLinux
else:
# For non-Linux platforms, use the standard build_exe command.
cmdclass["build_exe"] = CustomBuildExe
# Determine modules to include based on platform.
# Always include "pygame" and "mutagen".
includes = ["pygame", "mutagen"]
if sys.platform == "darwin":
try:
# game uses NSOpenPanel and NSApplication from AppKit,
# include the AppKit module.
from AppKit import NSOpenPanel, NSApplication
includes.append("AppKit")
except ImportError:
print("Warning: pyobjc's AppKit module is not installed. Skipping inclusion of AppKit.")
# Setup configuration.
setup(
name="TetraFusion",
version=version,
description="A Tetris-inspired game with custom features",
options={
# The build options apply to the command that will be run.
("build_exe" if not sys.platform.startswith("linux") else "build_linux"): {
"include_files": include_files,
"includes": includes,
},
"bdist_msi": bdist_msi_options,
},
executables=[exe],
cmdclass=cmdclass,
)