-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
163 lines (139 loc) · 5.03 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
# Copy the strutcture of the project to the build directory
# Only include files in root folders that does not start with a dot
# Only include *.lua, *.html, *.toc and *.xml files
import os
import shutil
import sys
import json
base_build_dir = "./.build"
def copy_files(src, dest):
files_to_copy = {}
files_copied = 0
for root, dirs, files in os.walk("."):
if root.startswith(".\\."):
continue
if "cli" in root:
continue
if ".wowhead" in root:
continue
if ".translator" in root:
continue
if ".git" in root:
continue
if ".vscode" in root:
continue
if ".generate_database" in root:
continue
if ".tests" in root:
continue
# Python venv
if "venv" in root:
continue
# Github actions
if ".lua" in root:
continue
if ".luarocks" in root:
continue
for file in files:
if file.endswith(".lua") or file.endswith(".html") or file.endswith(".toc") or file.endswith(".xml") or file.endswith("LICENSE") or file.endswith("README.md"):
# print(file)
filepath = os.path.join(root, file).replace("\\", "/")
# Replace first dot character with .build
destpath = filepath.replace(".", dest, 1)
# Create directories if they do not exist
os.makedirs(os.path.dirname(destpath), exist_ok=True)
files_to_copy[destpath] = filepath
files_copied += 1
print(f"{len(files_to_copy)} files to copy")
copied = 0
for destpath, filepath in files_to_copy.items():
shutil.copy(filepath, destpath)
copied += 1
if copied % 100 == 0:
print(f"Copied {copied}/{len(files_to_copy)} files")
print(f"Copied {files_copied} files")
return files_copied
def get_versions_from_toc(path="."):
classic_version = None
tbc_version = None
wotlk_version = None
with open(f"{path}/QuestieDB-Classic.toc", "r") as f:
for line in f:
if "## Version:" in line:
classic_version = line.split(":")[1].strip()
with open(f"{path}/QuestieDB-BCC.toc", "r") as f:
for line in f:
if "## Version:" in line:
tbc_version = line.split(":")[1].strip()
with open(f"{path}/QuestieDB-WOTLKC.toc", "r") as f:
for line in f:
if "## Version:" in line:
wotlk_version = line.split(":")[1].strip()
versions = {"classic": classic_version, "tbc": tbc_version, "wotlk": wotlk_version}
return versions
def validate_same_version(versions):
if versions["classic"] == versions["tbc"] == versions["wotlk"]:
return True
else:
return False
def get_versionstring_from_toc():
versions = get_versions_from_toc()
if validate_same_version(versions):
return versions["classic"]
else:
raise Exception("Version mismatch")
def export_version_github_actions(versions):
if "GITHUB_ACTIONS" in os.environ and os.environ["GITHUB_ACTIONS"] == "true":
print("::set-output name=toc_version::" + get_versionstring_from_toc())
versions = get_versions_from_toc()
split_version = versions["classic"].split(".")
print("::set-output name=major_toc_version::" + split_version[0])
print("::set-output name=minor_toc_version::" + split_version[1])
print("::set-output name=patch_toc_version::" + split_version[2])
def main():
# Arg 1 Command
if len(sys.argv) > 1:
command = sys.argv[1]
else:
command = "build"
if command == "build":
# Arg1 Build Directory Name
# Directory Name from args
if len(sys.argv) > 2:
build_output = sys.argv[2]
else:
build_output = "QuestieDB." + get_versionstring_from_toc()
if not os.path.exists(base_build_dir):
os.mkdir(base_build_dir)
build_dir = f"{base_build_dir}/{build_output}"
if not os.path.exists(build_dir):
os.mkdir(build_dir)
print(f"Copying files to build directory '{build_dir}'...")
copy_files(".", build_dir)
# If we are in github actions we output the toc version
versions = get_versions_from_toc()
export_version_github_actions(versions)
if "GITHUB_SHA" in os.environ and os.environ["GITHUB_SHA"] and len(os.environ["GITHUB_SHA"]) >= 7:
short_commit_hash = os.environ["GITHUB_SHA"][:7]
for toc_file in [f"{build_dir}/QuestieDB-Classic.toc", f"{build_dir}/QuestieDB-BCC.toc", f"{build_dir}/QuestieDB-WOTLKC.toc"]:
print(f"Adding sha {short_commit_hash} commit hash to toc file: {toc_file}")
with open(toc_file, "r") as f:
full_file = f.readlines()
with open(toc_file, "w") as f:
for line in full_file:
if "## Version:" in line:
version = line.split(":")[1].strip()
f.write(f"## Version: {version}-{short_commit_hash}\n")
else:
f.write(line)
print("Done")
elif command == "version":
# If we are in github actions we output the toc version
versions = get_versions_from_toc()
export_version_github_actions(versions)
print(json.dumps(get_versionstring_from_toc(), ensure_ascii=False))
return get_versionstring_from_toc()
else:
raise Exception(f"Unknown command: {command}")
if __name__ == "__main__":
main()