-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateNewProject.py
263 lines (197 loc) · 7.98 KB
/
GenerateNewProject.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# Copyright (C) Avnish Kirnalli 2024.
print("Copyright (C) Avnish Kirnalli 2024.")
import platform
import sys
import os
from io import BytesIO
def is_windows():
return platform.system() == 'Windows'
def is_mac():
return platform.system() == 'Darwin'
def in_venv():
return sys.prefix != sys.base_prefix
def restart():
print('Restarting Script')
os.system(f"{ 'call ' if is_windows() else '' }{os.getcwd()}/.python/{ 'Scripts' if is_windows() else 'bin' }/python {__file__}")
exit()
def create_venv():
if not os.path.exists(f'{os.getcwd()}/.python'):
print(f'Creating Python venv at {os.getcwd()}\\.python')
os.system('python3 -m venv ./.python')
print(f'Activating Python venv at {os.getcwd()}\\.python')
if is_windows():
os.system('call .python/Scripts/activate')
if is_mac():
os.system('source ./.python/bin/activate')
restart()
if not in_venv():
create_venv()
# Module checks with Install option
def yes_or_no(question):
reply = str(input(question + ' (y/n): ')).lower().strip()
if reply[0] == 'y' or reply[0] == 'yes':
return True
if reply[0] == 'n' or reply[0] == 'no':
return False
return yes_or_no(question)
def InstallModule(package):
if yes_or_no(f'Package {package} not found. Do you want to install Python Package {package}?'):
os.system(f"{os.getcwd()}/.python/{ 'Scripts' if is_windows() else 'bin' }/pip3 install {package}")
restart()
else:
print('Exiting')
exit()
try:
import shutil
except ImportError as e:
InstallModule('shutil')
try:
import requests
except ImportError as e:
InstallModule('requests')
try:
from tqdm import tqdm
except ImportError as e:
InstallModule('tqdm')
try:
import zipfile
except ImportError as e:
InstallModule('zipfile')
if is_mac():
try:
import tarfile
except ImportError as e:
InstallModule('tarfile')
def DownloadPremake():
if os.path.exists(f'{os.getcwd()}/premake'):
print('Premake Up-to date')
return
print('Downloading Premake.')
url = f'https://github.com/premake/premake-core/releases/download/v5.0.0-beta2/premake-5.0.0-beta2-windows.zip' if is_windows() else f'https://github.com/premake/premake-core/releases/download/v5.0.0-beta3/premake-5.0.0-beta3-macosx.tar.gz'
req = requests.get(url)
filename = url.split('/')[-1]
with open(filename, 'wb') as output_file:
output_file.write(req.content)
print('Downloaded Premake.')
print('Starting Premake Extraction')
if is_windows():
with zipfile.ZipFile(BytesIO(req.content)) as zf:
zf.extract('premake5.exe', 'premake/')
if is_mac():
archive = tarfile.open(filename)
archive.extract('premake5', 'premake/', filter='data')
archive.close()
os.system(f'chmod +x {os.getcwd()}/premake/premake5')
print('Premake extracted Successfully')
print('Deleting Premake Residual Files')
os.remove(f'{os.getcwd()}/{filename}')
print('Downloading Premake License')
url = f'https://raw.githubusercontent.com/premake/premake-core/master/LICENSE.txt'
req = requests.get(url)
with open('premake/LICENSE.txt', 'wb') as output_file:
output_file.write(req.content)
print('Premake License downloaded.')
print('Premake installed successfully!')
def DownloadImGui():
response = requests.get("https://api.github.com/repos/ocornut/imgui/releases/latest")
versionNo = response.json()["name"]
versionNo = versionNo.replace('v', '').strip()
if os.path.exists(f'{os.getcwd()}/ImGuiBorderlessWindow/Gui/ThirdParty/ImGui'):
f = open(f'{os.getcwd()}/ImGuiBorderlessWindow/Gui/ThirdParty/ImGui/version.txt')
if f.read() == versionNo:
print('ImGui Up-to date')
return
else:
if yes_or_no(f'ImGui is outdated, current version: {f.read()}, latest version: {versionNo}, do you want to update it?'):
print(f'Deleting current version of ImGui ({f.read()})')
shutil.rmtree(f'{os.getcwd()}/ImGuiBorderlessWindow/Gui/ThirdParty/ImGui')
print(f'Downloading latest version of ImGui ({versionNo})')
DownloadImGui()
return
else:
print(f'Continuing with current version of ImGui ({f.read()})')
return
print('Downloading ImGui.')
print(f"ImGui Version: {versionNo}")
url = f'https://github.com/ocornut/imgui/archive/refs/tags/v{versionNo}.zip'
req = requests.get(url)
filename = url.split('/')[-1]
with open(filename, 'wb') as output_file:
output_file.write(req.content)
print('Downloaded ImGui.')
print('Starting ImGui Extraction')
with zipfile.ZipFile(BytesIO(req.content)) as zf:
for member in tqdm(zf.infolist(), desc='Extracting ImGui '):
try:
zf.extract(member, os.getcwd())
except zipfile.error as e:
print(f'Error while extracting ImGui: {e}')
pass
print('ImGui extracted Successfully')
print('Deleting ImGui Residual Files')
os.remove(f'{os.getcwd()}/{filename}')
print('Finalizing ImGui Setup')
if os.path.exists(f'{os.getcwd()}/ImGui'):
shutil.rmtree(f'{os.getcwd()}/ImGui')
os.rename(f'imgui-{versionNo}', 'ImGui')
# writing version.txt
with open('ImGui/version.txt', "w") as f:
f.write(versionNo)
shutil.copy('ImGui/misc/cpp/imgui_stdlib.h', 'ImGui/imgui_stdlib.h')
shutil.copy('ImGui/misc/cpp/imgui_stdlib.cpp', 'ImGui/imgui_stdlib.cpp')
shutil.move(f'{os.getcwd()}/ImGui', f'{os.getcwd()}/ImGuiBorderlessWindow/Gui/ThirdParty/ImGui')
print('ImGui installed successfully!')
def replace_in_file(file_path, target_string, replacement_string):
# Read the file content
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Replace occurrences of the target string
modified_content = content.replace(target_string, replacement_string)
# Write the modified content back to the file
with open(file_path, 'w', encoding='utf-8') as file:
file.write(modified_content)
def main():
projName = input("Enter project name: ")
if projName == 'default':
print(f'Generating default project at {os.getcwd()}')
if is_windows():
os.system(f'{os.getcwd()}/premake/premake5 vs2022')
print('Generated Visual Studio solution')
if is_mac():
os.system(f'{os.getcwd()}/premake/premake5 xcode4')
print('Generated XCode project files.')
input('Press any key to continue....')
return
print("Generating project")
shutil.copytree("ImGuiBorderlessWindow", f'{projName}/{projName}')
shutil.copy('premake5.lua', f'{projName}/premake5.lua')
replace_in_file(f'{projName}/premake5.lua', 'ImGuiBorderlessWindow', projName)
os.chdir(f'{os.getcwd()}/{projName}')
if is_windows():
os.system(f'{os.getcwd()}/../premake/premake5 vs2022')
print('Generated Visual Studio solution')
if is_mac():
os.system(f'{os.getcwd()}/../premake/premake5 xcode4')
print('Generated XCode project files.')
os.chdir(f'{os.getcwd()}/..')
# os.remove(f'{projName}/premake5.lua')
# Set App Name in Info-macOS.plist
replace_in_file(f'{projName}/{projName}/Gui/Platform/Mac/Info-macOS.plist', "APP_NAME", projName)
print(f'Project generated successfully at {os.getcwd()}\\{projName}')
input('Press any key to continue....')
def DownloadDependencies():
print('Fetching Dependencies')
DownloadImGui()
DownloadPremake()
def CheckInternetConnection():
try:
requests.get('https://google.com')
except Exception as e:
return False
return True
if not CheckInternetConnection():
print('No Internet Connection detected! Internet is required to run this script.')
input('Press any key to continue....')
exit()
DownloadDependencies()
main()