This repository was archived by the owner on Mar 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.py
More file actions
294 lines (230 loc) · 8.12 KB
/
build.py
File metadata and controls
294 lines (230 loc) · 8.12 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
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
import argparse
import itertools
import json
import logging
import os
import os.path
import shutil
import subprocess
from collections.abc import Callable, Iterable, Iterator
from glob import iglob
from typing import TypeVar, cast
import semver
log = logging.getLogger(__name__)
AddonData = dict # Dict[str, Any]
# TypedDict(
# "AddonData",
# {
# # Built in gmod
# "title": str,
# "type": str,
# "tags": List[str],
# "ignore": List[str],
# # Stuff I sometimes put in anyway
# "author": Optional[str],
# "contact": Optional[str],
# "license": Optional[str],
# # Stuff for this tool
# "workshopid": int, # From gmosh
# "version": str,
# "include": List[str],
# "import": List[str],
# "versionables": List[str],
# },
# )
BUILD_DIR = "_build"
ADDON_DIR = "addons"
ALL_ADDONS = [
os.path.splitext(os.path.basename(file))[0] for file in iglob(f"{ADDON_DIR}/*.json")
]
VALID_BUMPS = [
"major",
"minor",
"patch",
"pre",
"premajor",
"preminor",
"prepatch",
"prerelease",
]
TEXT_EXTENSIONS = [".lua", ".properties"]
T = TypeVar("T")
class Addon:
name: str
datafile: str
data: AddonData
version: semver.SemVer
sub_addons: list[Addon]
def __init__(self, name: str) -> None:
self.name = name
self._load_data()
self.build_dir = os.path.join(BUILD_DIR, name)
self.version = semver.parse(self.data.get("version", "0.0.0"), loose=True)
self._load_subaddons()
# self.data = {}
def _load_data(self) -> None:
self.datafile = os.path.normpath(f"{ADDON_DIR}/{self.name}.json")
# Throw an error if the file doesn't exist
os.stat(self.datafile)
with open(self.datafile) as f:
self.data = cast(AddonData, json.load(f))
def _load_subaddons(self) -> None:
self.sub_addons = [Addon(name) for name in self.data.get("import", [])]
def _listiculate(self, action: Callable[[Addon], Iterable[T]]) -> Iterator[T]:
return itertools.chain(
action(self),
itertools.chain.from_iterable(
addon._listiculate(action) for addon in self.sub_addons
),
)
def _get_versionables(self) -> Iterator[tuple[str, str]]:
return self._listiculate(
lambda addon: (
(name, f"{name}_v{addon.version.format().replace('.', '_')}")
for name in addon.data.get("versionables", [])
),
)
pass
def _get_files(self) -> Iterator[str]:
return itertools.chain.from_iterable(
iglob(path)
for path in self._listiculate(lambda addon: addon.data["include"])
)
def copy_to_build(self) -> None:
log.info("%s: Copying to build directory", self.name)
if os.path.isdir(self.build_dir):
log.debug("Build directory %s already exists, deleting", self.build_dir)
shutil.rmtree(self.build_dir)
versionables = list(self._get_versionables())
for file in self._get_files():
source = os.path.relpath(file)
dest = source
for old, new in versionables:
dest = dest.replace(old, new)
dest = os.path.join(self.build_dir, dest)
dirname = os.path.dirname(dest)
log.debug("Creating build directory %s", dirname)
os.makedirs(dirname, exist_ok=True)
(_, ext) = os.path.splitext(dest)
if versionables and ext in TEXT_EXTENSIONS:
log.debug("Copying %s to %s via sed", source, dest)
with open(source) as f:
filedata = f.read()
for old, new in versionables:
filedata = filedata.replace(old, new)
with open(dest, "w") as f:
f.write(filedata)
else:
log.debug("Copying %s to %s", source, dest)
shutil.copyfile(source, dest)
dest = os.path.join(self.build_dir, "addon.json")
log.debug("Copying %s to %s", self.datafile, dest)
shutil.copyfile(self.datafile, dest)
def _target_gma_name(self) -> str:
name = f"{self.name}_v{self.version.format()}"
wsid = self.data.get("workshopid", None)
if wsid:
name += f"_{wsid}"
return os.path.join(BUILD_DIR, name + ".gma")
def build(self) -> None:
log.info("%s: Building GMA", self.name)
target = self._target_gma_name()
log.debug("Creating GMA %s from %s", target, self.build_dir)
res = subprocess.run(
["gmad", "create", "-folder", self.build_dir, "-out", target],
stdout=subprocess.PIPE,
# Garry doesn't belive in stderr but some day he might
stderr=subprocess.STDOUT,
encoding="utf-8",
)
if res.returncode != 0:
log.critical("Unable to create GMA")
log.critical(res.stdout)
# ?
res.check_returncode()
def write_data(self) -> None:
with open(self.datafile, mode="w") as f:
json.dump(self.data, f, indent="\t")
def bump_version(self, release: str, identifier: str | None = None) -> None:
log.info("%s: Building version by %s", self.name, release)
self.version.inc(release, identifier)
self.data["version"] = self.version.format()
log.debug("Version is now %s", self.data["version"])
def gma_exists(self) -> bool:
return os.path.isfile(self._target_gma_name())
def update(self, changes: str) -> None:
log.info("%s: Uploading %s to the workshop", self.name, self.version)
target = self._target_gma_name()
workshopid = str(self.data["workshopid"])
res = subprocess.run(
[
"gmpublish",
"update",
"-id",
workshopid,
"-addon",
target,
"-changes",
changes,
],
stdout=subprocess.PIPE,
# Garry doesn't belive in stderr but some day he might
stderr=subprocess.STDOUT,
encoding="utf-8",
)
if res.returncode != 0:
log.critical("Unable to update to the workshop")
log.critical(res.stdout)
# ?
res.check_returncode()
else:
log.info(res.stdout)
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
actions = parser.add_subparsers(dest="action")
parser.add_argument("addon", choices=ALL_ADDONS)
actions.add_parser("build")
bump_args = actions.add_parser("bump")
bump_args.add_argument("release", choices=VALID_BUMPS)
bump_args.add_argument("pretag", nargs="?")
release_args = actions.add_parser("release")
release_args.add_argument("--bump", choices=VALID_BUMPS, const="minor", nargs="?")
release_args.add_argument("--changes")
return parser.parse_args()
def get_changes() -> str:
lines = []
print("Enter changes, . to stop") # noqa: T201
while True:
try:
line = input().strip()
except EOFError:
break
if line == ".":
break
lines.append(line)
return "\n".join(lines).strip()
def main():
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s")
args = get_args()
log.debug("Doing a %s", args.action)
addon = Addon(args.addon)
if args.action == "build":
addon.copy_to_build()
addon.build()
elif args.action == "bump":
addon.bump_version(args.release, args.pretag)
addon.write_data()
elif args.action == "release":
if args.bump:
addon.bump_version(args.bump)
addon.write_data()
changes = args.changes
if not changes:
changes = get_changes()
changes = f"[h1][b]v{addon.version}[/b][/h1]\n{changes}"
if not addon.gma_exists():
addon.copy_to_build()
addon.build()
addon.update(changes)
if __name__ == "__main__":
main()