-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
218 lines (188 loc) Β· 8.03 KB
/
build.py
File metadata and controls
218 lines (188 loc) Β· 8.03 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import subprocess, os, sys, shutil, json, re
from pathlib import Path
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
# =============================================
# π§© EverMod CLI β Build Automation Script
# =============================================
# Features:
# - Detects unchanged version and prompts user
# - Syncs manifest.json β setup.iss & pyproject.toml
# - Cleans old builds
# - Builds PyInstaller (onefile)
# - Builds Inno Setup installer
# =============================================
# Paths
ROOT = Path(__file__).resolve().parent
APPDATA = os.environ["LOCALAPPDATA"]
DIST = ROOT / "dist"
SPEC = ROOT / "evermod.spec"
SETUP_ISS = ROOT / "setup.iss"
MANIFEST = ROOT / "manifest.json"
PYPROJECT = ROOT / "pyproject.toml"
INNO_SETUP_EXE = Path(APPDATA) / "Programs/Inno Setup 6/ISCC.exe"
# -----------------------------------------
# π Optional key generation before build
# -----------------------------------------
def generate_keys():
"""Run generate_keys.py before building."""
print("\nπ Generating EverMod RSA keys...\n")
private_path = Path.home() / ".evermod" / "keys" / "private.pem"
public_path = Path("src/evermod/auth/keys/evermod_public.pem")
public_path.parent.mkdir(parents=True, exist_ok=True)
private_path.parent.mkdir(parents=True, exist_ok=True)
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
with open(private_path, "wb") as f:
f.write(
private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
)
public_key = private_key.public_key()
with open(public_path, "wb") as f:
f.write(
public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
)
print(f"β
Claves generadas correctamente:\n - {private_path}\n - {public_path}")
# -----------------------------------------
# π§ Utilities
# -----------------------------------------
def run_command(cmd: list[str], cwd: Path = ROOT):
"""Run a system command and stream output."""
print(f"\n> {' '.join(cmd)}\n")
process = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
for line in process.stdout:
print(line, end="")
process.wait()
if process.returncode != 0:
print(f"\nβ Command failed with exit code {process.returncode}")
sys.exit(process.returncode)
# -----------------------------------------
# π Synchronization + Validation
# -----------------------------------------
def sync_versions():
"""Sync version from manifest.json β setup.iss & pyproject.toml, ask if unchanged."""
if not MANIFEST.exists():
print("β manifest.json not found.")
sys.exit(1)
manifest = json.loads(MANIFEST.read_text(encoding="utf-8"))
version = manifest.get("version", "").strip()
if not version:
print("β Version not found in manifest.json.")
sys.exit(1)
# Get last recorded version (if any) from setup.iss
last_version = None
if SETUP_ISS.exists():
setup_text = SETUP_ISS.read_text(encoding="utf-8")
match = re.search(r"(?m)^AppVersion=(.*)", setup_text)
if match:
last_version = match.group(1).strip()
if last_version and last_version == version:
print(f"\n β οΈ The version number in manifest.json ({version}) has not changed since last build.")
choice = input("Do you want to keep this version? (y/n): ").strip().lower()
if choice not in ("y", "yes"):
new_version = input("Enter new version number (current: " + version + "): ").strip()
if new_version:
manifest["version"] = new_version
version = new_version
MANIFEST.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
print(f"β
Updated manifest.json β version {version}")
else:
print("β No new version entered. Aborting.")
sys.exit(1)
print(f"\nπ Syncing version {version} across project files...\n")
# --- Update setup.iss ---
if not SETUP_ISS.exists():
print("β setup.iss not found.")
sys.exit(1)
setup_text = SETUP_ISS.read_text(encoding="utf-8")
setup_text = re.sub(r"(?m)^AppVersion=.*", f"AppVersion={version}", setup_text)
SETUP_ISS.write_text(setup_text, encoding="utf-8")
print(f"β
setup.iss updated β AppVersion={version}")
# --- Update pyproject.toml ---
if PYPROJECT.exists():
py_text = PYPROJECT.read_text(encoding="utf-8")
py_text = re.sub(r'(?m)^version\s*=\s*".*"', f'version = "{version}"', py_text)
PYPROJECT.write_text(py_text, encoding="utf-8")
print(f"β
pyproject.toml updated β version = {version}")
else:
print("β οΈ pyproject.toml not found (skipped).")
print()
return version
# -----------------------------------------
# π§Ή Clean previous builds
# -----------------------------------------
def clean_previous_builds():
"""Step 0: Remove previous build artifacts."""
print("\nπ§Ή Cleaning previous build artifacts...\n")
for folder in ("build", "dist", "__pycache__"):
folder_path = ROOT / folder
if folder_path.exists():
shutil.rmtree(folder_path)
print(f" ποΈ Removed {folder_path}")
# Also remove the old Inno Setup output if exists
output_dir = ROOT / "Output"
if output_dir.exists():
shutil.rmtree(output_dir)
print(f" ποΈ Removed {output_dir}")
# -----------------------------------------
# π build
# -----------------------------------------
def build_pyinstaller():
"""Step 1: Build evermod.exe using PyInstaller"""
print("\nπ Building EverMod CLI executable with PyInstaller...\n")
if not SPEC.exists():
print("β evermod.spec not found. Please create it first.")
sys.exit(1)
cmd = ["pyinstaller", str(SPEC), "--noconfirm", "--clean"]
run_command(cmd)
dist_exe = DIST / "evermod.exe"
build_exe = ROOT / "build" / "evermod" / "evermod.exe"
if dist_exe.exists():
print(f"\nβ
PyInstaller build completed successfully: {dist_exe}")
elif build_exe.exists():
print(f"\n β οΈ PyInstaller placed the exe in build/. Moving to dist/...")
DIST.mkdir(parents=True, exist_ok=True)
import shutil
shutil.copy(build_exe, dist_exe)
print(f"β
Copied {build_exe} β {dist_exe}")
else:
print("β PyInstaller did not produce evermod.exe")
print("π Check the 'build' folder manually for output files.")
sys.exit(1)
def build_inno_setup():
"""Step 2: Build installer using Inno Setup"""
print("\nπ¦ Building EverMod installer with Inno Setup...\n")
if not SETUP_ISS.exists():
print("β setup.iss not found in project root.")
sys.exit(1)
if not INNO_SETUP_EXE.exists():
print("β Inno Setup not found. Please update INNO_SETUP_EXE path in build.py.")
sys.exit(1)
cmd = [str(INNO_SETUP_EXE), str(SETUP_ISS)]
run_command(cmd)
output_dir = ROOT / "Output"
if output_dir.exists():
installers = list(output_dir.glob("*.exe"))
if installers:
print(f"\nβ
Inno Setup installer created: {installers[0]}")
else:
print("β οΈ No installer found in Output folder.")
else:
print("β οΈ Output folder not found. Check setup.iss configuration.")
def main():
print("π§© EverMod CLI β Build Automation\n")
args = [a.lower() for a in sys.argv[1:]]
if "--keys" in args or "keys" in args or "-k" in args:
generate_keys()
clean_previous_builds()
build_pyinstaller()
build_inno_setup()
print("\nπ Build process completed successfully!\n")
if __name__ == "__main__":
main()