forked from SIsilicon/WorldEdit-BE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
202 lines (181 loc) · 6.18 KB
/
build.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
import argparse
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
parser = argparse.ArgumentParser(description="Build and package the addon.")
parser.add_argument(
"--watch",
"-w",
choices=["stable", "preview", "server"],
help="Whether to continually build and where to sync the project while editing it.",
)
parser.add_argument(
"--target",
choices=["release", "debug", "server"],
default="debug",
help="Whether to build the addon in debug or release mode.",
)
parser.add_argument(
"--clean",
"-c",
action="store_true",
help='Clean "BP/scripts" folder before building.',
)
parser.add_argument(
"--package-only",
"-p",
action="store_true",
help="Only package what's already there.",
)
args = parser.parse_args()
build_pack_name = "WorldEdit"
def handleError(err):
if err:
exit(err)
def regExpSub(regEx, replace, file):
with open(file, "r") as f:
content = f.read()
contentNew = re.sub(regEx, replace, content, flags=re.M)
with open(file, "w") as f:
f.write(contentNew)
if not args.package_only:
# Check for input and output folder
if not os.path.isdir("src"):
sys.exit("The src folder does not exist in the current working directory!")
elif not os.path.isdir("BP/scripts"):
sys.exit(
"The output scripts folder does not exist in the current working directory!"
)
# Clean script output folder
if args.clean:
print("cleaning script output folder...")
folder = "BP/scripts"
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if file_path.endswith(".txt"):
continue
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print("Failed to delete %s. Reason: %s" % (file_path, e))
# Build manifests
handleError(
subprocess.call(
[sys.executable, "tools/process_manifest.py", f"--target={args.target}"],
stdout=subprocess.DEVNULL,
)
)
if args.watch:
print("syncing com.mojang folder...")
watch_target = "server" if args.watch == "server" else "debug"
subprocess.call(
[sys.executable, "tools/process_config.py", f"--target={watch_target}"],
stdout=subprocess.DEVNULL,
)
subprocess.call(
[sys.executable, "tools/sync2com-mojang.py", f"--dest={args.watch}"],
stdout=subprocess.DEVNULL,
)
print("Watch mode: press control-C to stop.")
tsc = subprocess.Popen("tsc -w", shell=True)
# Remap absolute imports
remap_imports = subprocess.Popen(
[sys.executable, "tools/remap_imports.py", "-w"], stdout=subprocess.DEVNULL
)
# Convert po to lang files
po2lang = subprocess.Popen(
[sys.executable, "tools/po2lang.py", "-w"], stdout=subprocess.DEVNULL
)
# Build settings file
process_cfg = subprocess.Popen(
[
sys.executable,
"tools/process_config.py",
"-w",
f"--target={watch_target}",
],
stdout=subprocess.DEVNULL,
)
# Sync to com.mojang
sync_mojang = subprocess.Popen(
[
sys.executable,
"tools/sync2com-mojang.py",
"-w",
"--init=False",
f"--dest={args.watch}",
],
stdout=subprocess.DEVNULL,
)
from time import sleep
try:
while True:
sleep(1)
except KeyboardInterrupt:
tsc.kill()
remap_imports.kill()
po2lang.kill()
process_cfg.kill()
sync_mojang.kill()
exit()
else:
print("building scripts...")
handleError(subprocess.call(["tsc", "-b"], shell=True))
# Remap absolute imports
handleError(subprocess.call([sys.executable, "tools/remap_imports.py"]))
# Convert po to lang files
handleError(subprocess.call([sys.executable, "tools/po2lang.py"]))
# Build manifests
handleError(
subprocess.call(
[sys.executable, "tools/process_manifest.py", f"--target={args.target}"]
)
)
# Build settings files
handleError(
subprocess.call(
[sys.executable, "tools/process_config.py", f"--target={args.target}"]
)
)
if args.target == "server":
handleError(
subprocess.call(
[sys.executable, "tools/process_config.py", "--generateConfigJSON"]
)
)
if not os.path.isdir("builds"):
os.makedirs("builds")
if os.path.exists(f"builds/{build_pack_name}BP"):
shutil.rmtree(f"builds/{build_pack_name}BP")
if os.path.exists(f"builds/{build_pack_name}RP"):
shutil.rmtree(f"builds/{build_pack_name}RP")
try:
shutil.copytree("BP", f"builds/{build_pack_name}BP")
except:
pass
try:
shutil.copytree("RP", f"builds/{build_pack_name}RP")
except:
pass
if args.target != "debug":
from zipfile import ZipFile
def zipWriteDir(zip, dirname, arcname):
for folderName, _, filenames in os.walk(dirname):
for filename in filenames:
filePath = os.path.join(folderName, filename)
zip.write(filePath, arcname / Path(filePath).relative_to(dirname))
if args.target == "release":
with ZipFile(f"builds/{build_pack_name}.mcaddon", "w") as zip:
zipWriteDir(zip, f"builds/{build_pack_name}BP", f"{build_pack_name}BP")
zipWriteDir(zip, f"builds/{build_pack_name}RP", f"{build_pack_name}RP")
elif args.target == "server":
with ZipFile(f"builds/{build_pack_name}.server.zip", "w") as zip:
zip.write("builds/variables.json", "variables.json")
zipWriteDir(zip, f"builds/{build_pack_name}BP", f"{build_pack_name}BP")
zipWriteDir(zip, f"builds/{build_pack_name}RP", f"{build_pack_name}RP")