-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathtasks.py
More file actions
454 lines (365 loc) · 14.8 KB
/
tasks.py
File metadata and controls
454 lines (365 loc) · 14.8 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
from invoke import task, context, Exit
import os
import subprocess
import re
from colorama import *
import glob
import shutil
from shutil import copy2, rmtree, copytree
from datetime import datetime
import pathlib
from typing import *
import time
from pathlib import Path
init()
class BuildConfig:
"""Configuration for the build process"""
def __init__(self):
self.releases_path = "releases"
self.output = "bin"
self.output_folder = "" # defined at runtime
self.version = "DEV"
# Project root directory (where tasks.py is located)
self.project_root = os.path.dirname(os.path.abspath(__file__))
self.seven_zip = os.path.join(self.project_root, "7z.exe")
@property
def output_folder_path(self):
return os.path.join(self.releases_path, self.version)
# Global config instance
config = BuildConfig()
delphi_versions = [
{"version": "10.0", "path": "17.0", "desc": "Delphi 10 Seattle"},
{"version": "10.1", "path": "18.0", "desc": "Delphi 10.1 Berlin"},
{"version": "10.2", "path": "19.0", "desc": "Delphi 10.2 Tokyo"},
{"version": "10.3", "path": "20.0", "desc": "Delphi 10.3 Rio"},
{"version": "10.4", "path": "21.0", "desc": "Delphi 10.4 Sydney"},
{"version": "11.0", "path": "22.0", "desc": "Delphi 11 Alexandria"},
{"version": "11.1", "path": "22.0", "desc": "Delphi 11.1 Alexandria"},
{"version": "11.2", "path": "22.0", "desc": "Delphi 11.2 Alexandria"},
{"version": "11.3", "path": "22.0", "desc": "Delphi 11.3 Alexandria"},
{"version": "12.0", "path": "23.0", "desc": "Delphi 12 Athens"},
{"version": "13.0", "path": "37.0", "desc": "Delphi 13 Florence"},
]
def get_package_folders():
"""Get list of package folders by scanning the packages directory.
Returns folders that match the pattern 'd*' (e.g., d100, d110, d130)"""
packages_dir = "packages"
if not os.path.isdir(packages_dir):
return []
folders = []
for item in os.listdir(packages_dir):
item_path = os.path.join(packages_dir, item)
if os.path.isdir(item_path) and item.startswith("d") and item[1:].isdigit():
folders.append(item)
return sorted(folders)
def get_delphi_projects_to_build(which=""):
"""Get list of Delphi projects to build based on type filter"""
projects = []
delphi_version, _ = get_best_delphi_version_available()
dversion = "d" + delphi_version["version"].replace(".", "")
if not which or which == "core":
# Build package for the current Delphi version
projects += glob.glob(rf"packages\{dversion}\*.dproj")
if not which or which == "tests":
projects += glob.glob(r"unittests\*.dproj")
if not which or which == "samples":
projects += glob.glob(r"samples\**\*.dproj", recursive=True)
# Exclude Android-only samples from Win32 build
projects = [p for p in projects if "130_simple_console_appender" not in p]
return sorted(projects)
def get_best_delphi_version_available() -> tuple[dict, str]:
global delphi_version
found = False
rsvars_path = None
i = len(delphi_versions)
while (not found) and (i >= 0):
i -= 1
delphi_version = delphi_versions[i]
version_path = delphi_version["path"]
rsvars_path = f"C:\\Program Files (x86)\\Embarcadero\\Studio\\{version_path}\\bin\\rsvars.bat"
if os.path.isfile(rsvars_path):
found = True
else:
rsvars_path = f"D:\\Program Files (x86)\\Embarcadero\\Studio\\{version_path}\\bin\\rsvars.bat"
if os.path.isfile(rsvars_path):
found = True
if found:
return delphi_version, rsvars_path
else:
raise Exception("Cannot find a Delphi compiler")
def build_delphi_project(
ctx: context.Context, project_filename, config="DEBUG", platform="Win32"
):
delphi_version, rsvars_path = get_best_delphi_version_available()
print("\nBUILD WITH: " + delphi_version["desc"])
cmdline = (
'"'
+ rsvars_path
+ '"'
+ " & msbuild /t:Build /p:Config="
+ config
+ f' /p:Platform={platform} "'
+ project_filename
+ '"'
)
r = ctx.run(cmdline, hide=True, warn=True)
if r.failed:
print(r.stdout)
print(r.stderr)
raise Exit("Build failed for " + delphi_version["desc"])
def create_zip(ctx, version):
print("CREATING ZIP")
archive_name = "..\\" + version + ".zip"
cmdline = f'"{config.seven_zip}" a {archive_name} *'
print(cmdline)
with ctx.cd(config.output_folder):
result = ctx.run(cmdline, hide=False, warn=True)
if result.failed:
print(Fore.RED + "ERROR: Failed to create zip" + Fore.RESET)
raise Exit("Failed to create zip archive")
def copy_sources():
"""Copy source files to output folder"""
os.makedirs(config.output_folder, exist_ok=True)
# Copy main source files
print("Copying LoggerPro Sources...")
src_files = glob.glob("*.pas") + glob.glob("*.inc")
for file in src_files:
if os.path.isfile(file):
print(f"Copying {file} to {config.output_folder}")
copy2(file, config.output_folder)
# Copy packages
folders = get_package_folders()
if not folders:
raise Exit("No package folders found in packages directory")
for folder in folders:
print(f"Copying LoggerPro packages {folder}...")
src_folder = os.path.join("packages", folder)
dest_folder = os.path.join(config.output_folder, "packages", folder)
os.makedirs(dest_folder, exist_ok=True)
for ext in ["*.dpk", "*.dproj"]:
for file in glob.glob(os.path.join(src_folder, ext)):
print(f"Copying {file}")
copy2(file, dest_folder)
# Copy samples
print("Copying samples...")
samples_dest = os.path.join(config.output_folder, "samples")
ignore_patterns = shutil.ignore_patterns(
"*.identcache", "*.dcu", "*.exe", "*.res", "*.stat",
"*.local", "*.log", "__history", "__recovery", "Win32", "Win64",
"nul" # Windows reserved filename
)
copytree("samples", samples_dest, ignore=ignore_patterns)
def ensure_dir_exists(path, description=""):
"""Validate that a directory exists, raise Exit if not"""
if not os.path.isdir(path):
desc = f" ({description})" if description else ""
raise Exit(f"Source directory not found{desc}: {path}")
def printkv(key, value):
print(Fore.RESET + key + ": " + Fore.GREEN + value.rjust(60) + Fore.RESET)
def show_version():
"""Display current version at build start"""
version = get_version_from_file()
print()
print(Fore.CYAN + "=" * 80 + Fore.RESET)
print(Fore.CYAN + f" LOGGERPRO VERSION: {version}" + Fore.RESET)
print(Fore.CYAN + "=" * 80 + Fore.RESET)
print()
return version
def init_build(version, clean_releases=False):
"""Required by all tasks"""
config.version = version
config.output_folder = config.releases_path + "\\" + config.version
print()
print(Fore.RESET + Fore.RED + "*" * 80)
print(Fore.RESET + Fore.RED + " BUILD VERSION: " + config.version + Fore.RESET)
print(Fore.RESET + Fore.RED + " OUTPUT PATH : " + config.output_folder + Fore.RESET)
print(Fore.RESET + Fore.RED + "*" * 80)
if clean_releases:
print("Cleaning releases folder...")
rmtree(config.releases_path, True)
else:
rmtree(config.output_folder, True)
os.makedirs(config.output_folder, exist_ok=True)
f = open(config.output_folder + "\\version.txt", "w")
f.write("VERSION " + config.version + "\n")
f.write("BUILD DATETIME " + datetime.now().isoformat() + "\n")
f.close()
copy2("README.md", config.output_folder)
copy2("License.txt", config.output_folder)
def build_delphi_project_list(ctx, projects, build_config="DEBUG", filter=""):
ret = True
for delphi_project in projects:
if filter and (filter not in delphi_project):
print(f"Skipped {os.path.basename(delphi_project)}")
continue
msg = f"Building: {os.path.basename(delphi_project)} ({build_config})"
print(Fore.RESET + msg.ljust(90, "."), end="")
try:
build_delphi_project(ctx, delphi_project, build_config)
print(Fore.GREEN + "OK" + Fore.RESET)
except Exception as e:
print(Fore.RED + "\n\nBUILD ERROR")
print(Fore.RESET)
print(e)
ret = False
return ret
@task
def clean(ctx, folder=None):
"""Clean build artifacts"""
if folder is None:
folder = "."
if not os.path.isdir(folder):
print(f"Folder does not exist, nothing to clean: {folder}")
return
print(f"Cleaning folder {folder}")
to_delete = []
to_delete += glob.glob(folder + r"\**\*.exe", recursive=True)
to_delete += glob.glob(folder + r"\**\*.dcu", recursive=True)
to_delete += glob.glob(folder + r"\**\*.stat", recursive=True)
to_delete += glob.glob(folder + r"\**\*.res", recursive=True)
to_delete += glob.glob(folder + r"\**\*.map", recursive=True)
to_delete += glob.glob(folder + r"\**\*.~*", recursive=True)
to_delete += glob.glob(folder + r"\**\*.rsm", recursive=True)
to_delete += glob.glob(folder + r"\**\*.drc", recursive=True)
to_delete += glob.glob(folder + r"\**\*.log", recursive=True)
to_delete += glob.glob(folder + r"\**\*.local", recursive=True)
to_delete += glob.glob(folder + r"\**\*.identcache", recursive=True)
for f in to_delete:
print(f"Deleting {f}")
try:
os.remove(f)
except Exception as e:
print(f"Warning: could not delete {f}: {e}")
# Clean __history and __recovery folders
for pattern in [r"**\__history", r"**\__recovery"]:
for d in glob.glob(folder + "\\" + pattern, recursive=True):
print(f"Removing directory {d}")
rmtree(d, True)
@task()
def tests(ctx):
"""Builds and execute the unit tests"""
show_version()
testclient = r"unittests\UnitTests.dproj"
print("\nBuilding Unit Tests")
build_delphi_project(ctx, testclient, config="CI")
print("\nExecuting tests...")
r = subprocess.run([r"unittests\Win32\CI\UnitTests.exe"])
if r.returncode != 0:
raise Exit("Cannot run unit tests: \n" + str(r.stdout))
if r.returncode > 0:
print(r)
print("Unit Tests Failed")
raise Exit("Unit tests failed")
def get_version_from_file():
with open(r".\loggerprobuildconsts.inc") as f:
lines = f.readlines()
# Find line with LOGGERPRO_VERSION or DMVCFRAMEWORK_VERSION
res = [x for x in lines if "_VERSION" in x and "=" in x]
if len(res) != 1:
raise Exception("Cannot find version constant in loggerprobuildconsts.inc")
version_line = res[0].strip(" ;\t\n")
pieces = version_line.split("=")
if len(pieces) != 2:
raise Exception("Version line in wrong format: " + version_line)
version = pieces[1].strip("' ")
if not "loggerpro" in version.lower():
version = "loggerpro-" + version
print(Fore.RESET + Fore.GREEN + "BUILDING VERSION: " + version + Fore.RESET)
return version
def inc_version():
"""Increment patch version number in loggerprobuildconsts.inc"""
global config
home = Path(__file__).parent
inc_file = home.joinpath("loggerprobuildconsts.inc")
with open(inc_file, "r") as f:
content = f.read()
# Find current version
match = re.search(r"_VERSION\s*=\s*'([^']+)'", content)
if not match:
raise Exception("Cannot find version in loggerprobuildconsts.inc")
v = match.group(1)
# Remove "loggerpro-" prefix if present for parsing
v_clean = v.replace("loggerpro-", "")
pieces = v_clean.split(".")
if len(pieces) != 2:
# Try with 3 parts
if len(pieces) != 3:
raise Exception(f"Invalid version format: {v}")
if len(pieces) == 2:
new_version = pieces[0] + "." + str(int(pieces[1]) + 1)
else:
new_version = ".".join(pieces[:-1]) + "." + str(int(pieces[2]) + 1)
config.version = new_version
print(f"INC VERSION [{v}] => [{new_version}]")
# Replace version in file
new_content = re.sub(r"(_VERSION\s*=\s*')[^']+(')", rf"\g<1>{new_version}\g<2>", content)
with open(inc_file, "w") as f:
f.write(new_content)
return new_version
@task()
def release(ctx, skip_build=False, skip_tests=False):
"""Builds all the projects, executes tests and prepares the release"""
version = show_version()
init_build(version, clean_releases=False) # Only clean current version folder
if not skip_tests:
tests(ctx)
if not skip_build:
delphi_projects = get_delphi_projects_to_build("")
if not _build_projects(ctx, delphi_projects, "DEBUG", ""):
return False
print(Fore.RESET)
copy_sources()
clean(ctx, config.output_folder)
create_zip(ctx, version)
return True
def _build_projects(ctx, delphi_projects, version, filter):
return build_delphi_project_list(ctx, delphi_projects, version, filter)
@task
def build_samples(ctx, version="DEBUG", filter=""):
"""Builds samples"""
show_version()
init_build(version)
delphi_projects = get_delphi_projects_to_build("samples")
return _build_projects(ctx, delphi_projects, version, filter)
@task
def build_core(ctx, version="DEBUG"):
"""Builds core package"""
show_version()
init_build(version)
delphi_projects = get_delphi_projects_to_build("core")
if not _build_projects(ctx, delphi_projects, version, ""):
raise Exit("Build failed")
@task(post=[tests])
def build(ctx, version="DEBUG"):
"""Builds LoggerPro and runs tests"""
show_version()
delphi_projects = get_delphi_projects_to_build("")
ret = build_delphi_project_list(ctx, delphi_projects, version)
if not ret:
raise Exit("Build failed")
@task()
def tag_release(ctx):
"""Creates a git tag for the current version and pushes it"""
version = get_version_from_file()
tag_name = "v" + version.replace("loggerpro-", "").replace(" ", "_")
print("Creating Git tag " + tag_name)
result = ctx.run("git add -u", warn=True)
if result.failed:
raise Exception("Cannot add files to git")
result = ctx.run(f"git tag {tag_name}", warn=True)
if result.failed:
raise Exception("Cannot create git tag")
result = ctx.run(f'git commit -m "Release {tag_name}"', warn=True)
if result.failed:
raise Exception("Cannot commit on git")
result = ctx.run("git push origin", warn=True)
if result.failed:
raise Exception("Cannot push")
result = ctx.run(f"git push origin {tag_name}", warn=True)
if result.failed:
raise Exception("Cannot push tag")
print(Fore.GREEN + f"Successfully created and pushed tag {tag_name}" + Fore.RESET)
@task()
def bump_version(ctx):
"""Increments the patch version number"""
inc_version()