-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpack_tool.py
374 lines (329 loc) · 16 KB
/
pack_tool.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from colorama import Fore, Back, Style, init
import os, shutil
import subprocess, shlex
from pathlib import Path
from shutil import which
import winreg
import itertools
import argparse
init()
PROJECT_GIT_DIR = str(Path(__file__).parent.resolve())
TARGET_BASE_DIR = os.path.join(PROJECT_GIT_DIR, "Publish")
SOLUTION_PATH = os.path.join(PROJECT_GIT_DIR, "Fo76ini\\Fo76ini.sln")
PROGRAM_BIN_DIR = os.path.join(PROJECT_GIT_DIR, "Fo76ini\\bin\\")
EXECUTABLE_NAME = "Fo76ini.exe"
EXECUTABLE_PATH = os.path.join(PROGRAM_BIN_DIR, "Release", EXECUTABLE_NAME)
UPDATER_BIN_DIR = os.path.join(PROJECT_GIT_DIR, "Fo76ini_Updater\\bin\\")
UPDATER_SOLUTION_PATH = os.path.join(PROJECT_GIT_DIR, "Fo76ini_Updater\\Fo76ini_Updater.sln")
DEPENDENCIES_DIR = os.path.join(PROJECT_GIT_DIR, "Additional files")
VERSION_PATH = os.path.join(PROJECT_GIT_DIR, "VERSION")
SETUP_ISS_PATH = os.path.join(PROJECT_GIT_DIR, "setup.iss")
VERSION = "x.x.x"
def get_binaries_path():
return os.path.join(TARGET_BASE_DIR, "v" + VERSION)
def get_msbuild_path():
"""Attempts to run 'which', then check common paths, and if all else fails, reads the registry and returns the path to MSBuild.exe as string or None."""
path = None
if which("msbuild") is not None:
return which("msbuild")
for drive in ["C:", "D:"]:
if os.path.isfile(drive + "\\Program Files\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe"):
return drive + "\\Program Files\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe"
elif os.path.isfile(drive + "\\Program Files\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe"):
return drive + "\\Program Files\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe"
elif os.path.isfile(drive + "\\Program Files (x86)\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe"):
return drive + "\\Program Files (x86)\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe"
elif os.path.isfile(drive + "\\Program Files (x86)\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe"):
return drive + "\\Program Files (x86)\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe"
elif os.path.isfile(drive + "\\Program Files\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe"):
return drive + "\\Program Files\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe"
elif os.path.isfile(drive + "\\Program Files\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe"):
return drive + "\\Program Files\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe"
elif os.path.isfile(drive + "\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe"):
return drive + "\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe"
elif os.path.isfile(drive + "\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe"):
return drive + "\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe"
# https://stackoverflow.com/questions/328017/path-to-msbuild
with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as reg:
with winreg.OpenKey(reg, "SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0") as key:
try:
for i in itertools.count(start = 0, step = 1):
name, value, type = winreg.EnumValue(key, i)
if name == "MSBuildToolsPath" and type == winreg.REG_SZ:
path = value
break
except OSError:
# No more values
pass
if path is not None:
path = os.path.join(path, "MSBuild.exe")
if os.path.isfile(path):
return path
return None
def get_7zip_path():
sevenzip = which("7z")
if sevenzip is not None:
return sevenzip
sevenzip_alt = which("7za")
if sevenzip_alt is not None:
return sevenzip_alt
return None
def get_version():
global VERSION
try:
with open(VERSION_PATH, "r") as f:
VERSION = f.read().strip()
except:
print(Fore.RED + "ERROR: Couldn't read VERSION" + Fore.RESET)
def set_version():
global VERSION
try:
VERSION = input("VERSION: ")
with open(VERSION_PATH, "w") as f:
f.write(VERSION + "\n")
print("Version set.")
except KeyboardInterrupt:
print("\nAbort.")
return
def restore_nuget():
os.system("{0} restore \"{1}\"".format(which("nuget"), SOLUTION_PATH, VERSION))
os.system("{0} restore \"{1}\"".format(which("nuget"), UPDATER_SOLUTION_PATH, VERSION))
def build_updater(debug = False):
print("Building updater...")
configuration = "Debug" if debug else "Release"
subprocess.run(shlex.split(f"\"{get_msbuild_path()}\" \"{SOLUTION_PATH}\" /p:Configuration={configuration} /t:Fo76ini_Updater"))
if debug:
copytree(UPDATER_BIN_DIR + configuration, PROGRAM_BIN_DIR + configuration)
else:
copytree(UPDATER_BIN_DIR + configuration, get_binaries_path())
def build_app(debug = False):
print("Building app...")
configuration = "Debug" if debug else "Release"
subprocess.run(shlex.split(f"\"{get_msbuild_path()}\" \"{SOLUTION_PATH}\" /p:Configuration={configuration} /t:Fo76ini"))
if not debug:
copytree(PROGRAM_BIN_DIR + "Release", get_binaries_path())
def copy_additions(debug = False):
print("Copying additional files...")
if debug:
copytree(DEPENDENCIES_DIR, PROGRAM_BIN_DIR + "Debug")
else:
copytree(DEPENDENCIES_DIR, get_binaries_path())
def pack_release():
print("Packing to v{0}.zip...".format(VERSION))
os.system("{0} a \"{1}\" \"{2}\\*\"".format(get_7zip_path(), os.path.join(TARGET_BASE_DIR, "v" + VERSION + ".zip"), get_binaries_path()))
print("Done.")
def use_rcedit():
print("Setting executable version to '{0}'...".format(VERSION))
os.system("{0} \"{1}\" --set-file-version {2} --set-product-version {2}".format(which("rcedit"), EXECUTABLE_PATH, VERSION))
os.system("{0} \"{1}\" --set-file-version {2} --set-product-version {2}".format(which("rcedit"), os.path.join(get_binaries_path(), EXECUTABLE_NAME), VERSION))
def update_inno():
print("Changing version number in setup.iss ...")
content = ""
with open(SETUP_ISS_PATH, "r") as f:
for line in f:
if line.startswith("#define ProjectVersion"):
line = "#define ProjectVersion \"" + VERSION + "\"\n"
print("Line changed: " + line, end="")
if line.startswith("#define MyAppExeName"):
line = "#define MyAppExeName \"" + EXECUTABLE_NAME + "\"\n"
print("Line changed: " + line, end="")
if line.startswith("#define ProjectGitDir"):
line = "#define ProjectGitDir \"" + PROJECT_GIT_DIR.rstrip("\\") + "\"\n"
print("Line changed: " + line, end="")
#if line.startswith("#define ProjectPackTargetDir"):
# line = "#define ProjectPackTargetDir \"" + TARGET_BASE_DIR.rstrip("\\") + "\"\n"
# print("Line changed: " + line, end="")
content += line
with open(SETUP_ISS_PATH, "w") as f:
f.write(content)
def build_inno():
print("Building setup using ISCC...")
subprocess.run(shlex.split("\"" + which("iscc") + "\" \"" + SETUP_ISS_PATH + "\""))
def convert_md():
print("Converting Markdown to HTML and RTF")
subprocess.run(shlex.split("\"" + which("pandoc") + "\" --standalone -f gfm \"What's new.md\" -o \"whatsnew.html\" --css=Pandoc/pandoc-style.css -H Pandoc/pandoc-header.html"))
subprocess.run(shlex.split("\"" + which("pandoc") + "\" --standalone -f gfm \"What's new.md\" -o \"whatsnewdark.html\" --css=Pandoc/pandoc-style-dark.css -H Pandoc/pandoc-header.html"))
subprocess.run(shlex.split("\"" + which("pandoc") + "\" --standalone \"What's new.md\" -o \"What's new.rtf\""))
def open_dir():
if os.path.exists(TARGET_BASE_DIR):
os.system("explorer.exe \"{0}\"".format(TARGET_BASE_DIR))
else:
print("ERROR: Path does not exist.")
# https://stackoverflow.com/a/7550424
def mkdir(newdir):
"""works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make them as well
"""
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired " \
"dir, '%s', already exists." % newdir)
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
mkdir(head)
if tail:
os.mkdir(newdir)
# https://stackoverflow.com/a/7550424
def copytree(src, dst, symlinks=False):
"""Recursively copy a directory tree using copy2().
The destination directory must not already exist.
If exception(s) occur, an Error is raised with a list of reasons.
If the optional symlinks flag is true, symbolic links in the
source tree result in symbolic links in the destination tree; if
it is false, the contents of the files pointed to by symbolic
links are copied.
XXX Consider this example code rather than the ultimate tool.
"""
names = os.listdir(src)
mkdir(dst)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
else:
shutil.copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error) as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Exception as err:
errors.extend(err.args[0])
try:
shutil.copystat(src, dst)
except WindowsError:
# can't copy file access times on Windows
pass
def run_interactive():
print("""-----------------------------------------
Pack Tool""")
warn_text = get_warn_text()
if warn_text:
print("-----------------------------------------\n" + warn_text + Fore.RESET, end="")
else:
print("-----------------------------------------\n" + Fore.GREEN + "All requirements found!\n" + Fore.RESET, end="")
while True:
print("-----------------------------------------")
print("You can also use command line arguments!\nSee: " + Fore.MAGENTA + "$ " + Fore.BLUE + "python pack_tool.py --help" + Fore.RESET)
print("-----------------------------------------")
print(f"""{Fore.BLUE}Set version
{Fore.MAGENTA}(1){Fore.RESET} Set "VERSION" (current: {Fore.GREEN}{VERSION}{Fore.RESET})
{Fore.BLUE}Building
{Fore.MAGENTA}(2){Fore.RESET} Restore NuGet packages
{Fore.MAGENTA}(3){Fore.RESET} Build app (Debug)
{Fore.MAGENTA}(4){Fore.RESET} Build app (Release)
{Fore.MAGENTA}(5){Fore.RESET} Pack app to *.zip
{Fore.MAGENTA}(6){Fore.RESET} Build setup
{Fore.BLUE}What's new.md
{Fore.MAGENTA}(7){Fore.RESET} Convert Markdown to HTML and RTF using Pandoc
{Fore.BLUE}Others
{Fore.MAGENTA}(8){Fore.RESET} Open target folder
{Fore.MAGENTA}(0){Fore.RESET} Exit (Ctrl+C)
-----------------------------------------""")
try:
i = input(">>> ").strip()
except KeyboardInterrupt:
print("""^C - Bye bye!
-----------------------------------------""")
break
if i == "1":
set_version()
# use_rcedit()
elif i == "2":
restore_nuget()
elif i == "3":
build_updater(debug=True)
build_app(debug=True)
copy_additions(debug=True)
elif i == "4":
build_updater()
build_app()
copy_additions()
use_rcedit()
elif i == "5":
pack_release()
elif i == "6":
update_inno()
build_inno()
elif i == "7":
convert_md()
elif i == "8":
open_dir()
elif i == "0" or i == "":
print("""Bye bye!
-----------------------------------------""")
break
else:
print("Input not recognized.")
def run_args(args):
if args.set_version:
set_version()
# use_rcedit()
if args.restore:
restore_nuget()
if args.build_debug:
build_updater(debug=True)
build_app(debug=True)
copy_additions(debug=True)
if args.build:
build_updater()
build_app()
copy_additions()
use_rcedit()
if args.pack:
pack_release()
if args.build_setup:
update_inno()
build_inno()
if args.whatsnew:
convert_md()
def get_warn_text():
warn_text = ""
if not os.path.exists(PROJECT_GIT_DIR):
warn_text += Fore.YELLOW + "WARN: Project folder doesn't exist!\n"
if not os.path.isdir(os.path.join(PROJECT_GIT_DIR, "Fo76ini")):
warn_text += Fore.YELLOW + "WARN: " + Fore.RESET + "\"Fo76ini\" folder doesn't exist!\n Please run the script within the git repo folder."
if which("rcedit") is None:
warn_text += Fore.YELLOW + "WARN: " + Fore.RESET + "rcedit not found!\n"
if get_7zip_path() is None:
warn_text += Fore.YELLOW + "WARN: " + Fore.RESET + "7-Zip not found!\n"
if which("iscc") is None:
warn_text += Fore.YELLOW + "WARN: " + Fore.RESET + "ISCC (Inno Setup Compiler) not found!\n"
if which("pandoc") is None:
warn_text += Fore.YELLOW + "WARN: " + Fore.RESET + "Pandoc not found!\n"
if get_msbuild_path() is None:
warn_text += Fore.YELLOW + "WARN: " + Fore.RESET + "MSBuild not found!\n"
if which("nuget") is None:
warn_text += Fore.YELLOW + "WARN: " + Fore.RESET + "NuGet not found!\n"
if warn_text:
warn_text += Fore.YELLOW + "\nBuilding might fail if requirements are missing.\nMake sure you installed them properly and added them to your PATH.\n\nYou can install most of them with scoop like so:\n> " + Fore.BLUE + "scoop install 7zip git rcedit inno-setup pandoc\n"
return warn_text
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Helper script for building Fallout 76 Quick Configuration')
parser.add_argument('-v', '--set-version', help='set the current version', required=False, action='store_true')
parser.add_argument('-r', '--restore', help='restore nuget packages', required=False, action='store_true')
parser.add_argument('-b', '--build', help='build the app and updater', required=False, action='store_true')
parser.add_argument('-d', '--build-debug', help='build the app and updater (Debug configuration)', required=False, action='store_true')
parser.add_argument('-p', '--pack', help='pack the app into a zip archive', required=False, action='store_true')
parser.add_argument('-s', '--build-setup', help='build the setup', required=False, action='store_true')
parser.add_argument('-w', '--whatsnew', help='update the "What\'s new?" files', required=False, action='store_true')
args = parser.parse_args()
mkdir(TARGET_BASE_DIR)
get_version()
args_list = [args.restore, args.build_debug, args.build, args.build_setup, args.pack, args.set_version, args.whatsnew]
#if args_list.count(True) > 1:
# print("ERROR: Only one argument allowed")
if args_list.count(True) >= 1:
run_args(args)
else:
run_interactive()