-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-blazor.py
More file actions
141 lines (122 loc) · 4.9 KB
/
build-blazor.py
File metadata and controls
141 lines (122 loc) · 4.9 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
import http.server
import os
import os.path
import shutil
import subprocess
import sys
PORT = 8080
def get_env(args):
env = "local"
if len(args) >= 2:
if args[1] == "deploy":
env = "deploy"
elif args[1] == "deploy-aws":
env = "deploy-aws"
print(f"environment: {env}")
return env
def add_cs_sources(env):
if env == "local":
print("Removing existing generated code...")
shutil.rmtree(path("CREBlazorApp/GeneratedCode"), ignore_errors=True)
print("Adding generated code...")
os.mkdir(path("CREBlazorApp/GeneratedCode"))
print("Adding GeneratedImageSources.cs...")
with open(path("CREBlazorApp/GeneratedCode/GeneratedImageSources.cs"), "x") as image_sources_cs:
image_sources_cs.write("""namespace CREBlazorApp.GeneratedCode;
public static class GeneratedImageSources
{
public static readonly string[] ImageSources =
[
""")
for image_source in os.listdir(path("Files/Types")):
image_sources_cs.write(f" \"{image_source}\",\n")
image_sources_cs.write(""" ];
}""")
print("Adding GeneratedLevels.cs...")
with open(path("CREBlazorApp/GeneratedCode/GeneratedLevels.cs"), "x") as generated_levels_cs:
files = sorted(os.listdir(path("Files/Levels")))
generated_levels_cs.write("""namespace CREBlazorApp.GeneratedCode;
public static class GeneratedLevels
{
public static readonly string[] JsonLevels =
[
""")
for file in files:
if not(file.endswith(".json")):
continue
generated_levels_cs.write('"""\n')
with open(path(f"Files/Levels/{file}")) as json_level:
generated_levels_cs.write(json_level.read())
generated_levels_cs.write('\n""",\n')
generated_levels_cs.write(""" ];
public static readonly string[] Descriptions =
[
""")
for file in files:
if not(file.endswith(".txt")):
continue
generated_levels_cs.write('"""\n')
with open(path(f"Files/Levels/{file}")) as description:
generated_levels_cs.write(description.read())
generated_levels_cs.write('\n""",\n')
generated_levels_cs.write(""" ];
public static readonly string[] Solutions =
[
""")
for file in files:
if not(file.endswith(".cs")):
continue
generated_levels_cs.write('"""\n')
with open(path(f"Files/Levels/{file}")) as solution:
generated_levels_cs.write(solution.read())
generated_levels_cs.write('\n""",\n')
generated_levels_cs.write(""" ];
}""")
def build(env):
if env == "deploy-aws":
dotnet_path = "./dotnet9/dotnet"
else:
dotnet_path = "dotnet"
if env == "local":
# in a live deployment, we don't need to remove these as we are already in a fresh environment
print("Removing existing published data...")
shutil.rmtree(path("code-rooms-exe"), ignore_errors=True)
print("Publishing CREBlazorApp...")
process_resp = subprocess.run([dotnet_path, "publish", path("CREBlazorApp"), "-c", "CREBlazor", "-o", path("publish")])
if process_resp.returncode != 0:
print("Dotnet build failed")
return False
print("Moving published data...")
# we do not remove anything from the publish directory so that for a local build,
# dotnet can use caching to drastically speed up the publishing process
shutil.copytree(path("publish/wwwroot"), path("code-rooms-exe/__blazor/wwwroot"))
shutil.move(path("code-rooms-exe/__blazor/wwwroot/_framework"), path("code-rooms-exe/_framework"))
shutil.move(path("code-rooms-exe/__blazor/wwwroot/index.html"), path("code-rooms-exe/index.html"))
shutil.copy(path("code-rooms-exe/index.html"), path("code-rooms-exe/404.html"))
print("Moving static files...")
shutil.copytree(path("Files"), path("code-rooms-exe/Files"))
shutil.rmtree(path("code-rooms-exe/Files/Save"))
shutil.rmtree(path("code-rooms-exe/Files/Tests"))
os.remove(path("code-rooms-exe/Files/Dummy.cs"))
os.remove(path("code-rooms-exe/Files/Dummy.java"))
print("Build done.")
return True
def path(p):
return os.path.normpath(p)
def run_web_server():
try:
with http.server.ThreadingHTTPServer(("", PORT), http.server.SimpleHTTPRequestHandler) as httpd:
with open(path("code-rooms-exe/404.html")) as html_file:
httpd.RequestHandlerClass.error_message_format = html_file.read()
print(f"Serving at http://127.0.0.1:{PORT}/code-rooms-exe/ (Ctrl+C to stop)")
httpd.serve_forever()
except KeyboardInterrupt:
print("Keyboard interrupt detected")
if __name__ == "__main__":
env = get_env(sys.argv)
add_cs_sources(env)
build_resp = build(env)
if not build_resp:
sys.exit(1)
if env == "local":
run_web_server()