The installed app then depends only on Windows libraries, whatever Visual Studio redistributable the build machine has. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
6.8 KiB
Python
Executable File
146 lines
6.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Build a signed Windows release from a Git snapshot. Run from the repository root."""
|
|
import argparse
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import zipfile
|
|
|
|
VPK_VERSION = "1.2.161"
|
|
VPK_SHA256 = "2b56ce117f803fc70c103cb423bd040e395e40370f6ff6e10818e9ff9c26a323"
|
|
|
|
|
|
def output(*args, **kw):
|
|
return subprocess.check_output(args, **kw)
|
|
|
|
|
|
def windows_path(path):
|
|
return output("wslpath", "-w", path.resolve()).decode().strip()
|
|
|
|
|
|
def digest(path):
|
|
with path.open("rb") as stream:
|
|
return hashlib.file_digest(stream, "sha256")
|
|
|
|
|
|
def wsl_path(win_path):
|
|
return Path(output("wslpath", "-u", win_path).decode().strip())
|
|
|
|
|
|
def build(args):
|
|
number = r"(0|[1-9][0-9]*)"
|
|
suffix = r"-beta\.([1-9][0-9]*)" if args.channel == "beta" else ""
|
|
match = re.fullmatch(r"\.".join([number] * 3) + suffix, args.version)
|
|
if not match or any(int(n) > 65535 for n in match.groups()[:3]):
|
|
raise ValueError("Version attendue : X.Y.Z (stable) ou X.Y.Z-beta.N (beta), composantes ≤ 65535")
|
|
root = Path(output("git", "rev-parse", "--show-toplevel").decode().strip()).resolve()
|
|
commit = output("git", "rev-parse", "--verify", "--end-of-options", args.commit + "^{commit}").decode().strip()
|
|
if output("git", "show", f"{commit}:scripts/package") != Path(__file__).read_bytes():
|
|
raise ValueError("Lancer le script de packaging du commit choisi")
|
|
dest = args.output.resolve()
|
|
if dest.exists():
|
|
raise ValueError("Le dossier de sortie existe déjà")
|
|
key = Path(os.environ["ARENA_SIGNING_KEY"]).resolve(strict=True)
|
|
if key.is_relative_to(root):
|
|
raise ValueError("La clé de signature doit rester hors du dépôt")
|
|
spki = output("openssl", "pkey", "-in", key, "-pubout", "-outform", "DER")
|
|
if len(spki) != 44 or not spki.startswith(bytes.fromhex("302a300506032b6570032100")):
|
|
raise ValueError("Une clé de signature Ed25519 est requise")
|
|
tool = Path(os.environ["VPK_NUPKG"])
|
|
|
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
with tempfile.TemporaryDirectory() as work, tempfile.TemporaryDirectory(dir=dest.parent) as staging:
|
|
tmp = Path(work)
|
|
local_tool = tmp / "vpk.nupkg"
|
|
shutil.copyfile(tool, local_tool)
|
|
if digest(local_tool).hexdigest() != VPK_SHA256:
|
|
raise ValueError(f"Archive NuGet officielle de vpk {VPK_VERSION} attendue")
|
|
src = tmp / "src"
|
|
src.mkdir()
|
|
archive = tmp / "source.tar"
|
|
subprocess.run(["git", "archive", "--output", archive, commit], check=True)
|
|
# -m: fresh mtimes, so Cargo never reuses a workspace crate built from other sources in the shared target.
|
|
subprocess.run(["tar", "-xmf", archive, "-C", src], check=True)
|
|
assets = src / "crates/app/assets"
|
|
(assets / "release-version.h").write_text(
|
|
f'#define ARENA_VERSION_TEXT "{args.version}"\n'
|
|
f'#define ARENA_VERSION_NUMBERS {",".join(match.groups()[:3])},0\n'
|
|
)
|
|
rc = assets / "app.rc"
|
|
rc.write_text("#define ARENA_RELEASE\n" + rc.read_text())
|
|
|
|
identity = {
|
|
"ARENA_RELEASE_VERSION": args.version, "ARENA_RELEASE_CHANNEL": args.channel,
|
|
"ARENA_RELEASE_COMMIT": commit, "ARENA_RELEASE_PUBLIC_KEY": base64.b64encode(spki[-32:]).decode(),
|
|
}
|
|
env = os.environ.copy()
|
|
del env["ARENA_SIGNING_KEY"]
|
|
env.update(identity)
|
|
env["WSLENV"] = ":".join(filter(None, [env.get("WSLENV", ""), *identity]))
|
|
messages = output(src / "scripts/prod", "build", "--message-format=json-render-diagnostics", env=env)
|
|
executables = []
|
|
for line in messages.splitlines():
|
|
artifact = json.loads(line)
|
|
if artifact.get("executable") and artifact["target"]["name"] == "arena-tracker":
|
|
executables.append(artifact["executable"])
|
|
if len(executables) != 1:
|
|
raise ValueError("Cargo devait construire un seul exécutable arena-tracker")
|
|
package_dir = tmp / "app"
|
|
package_dir.mkdir()
|
|
app = package_dir / "arena-tracker.exe"
|
|
shutil.copyfile(wsl_path(executables[0]), app)
|
|
app.chmod(0o755)
|
|
# The pinned packer labels its input with --runtime. Check the actual PE machine first.
|
|
with app.open("rb") as pe:
|
|
pe.seek(0x3C)
|
|
pe.seek(int.from_bytes(pe.read(4), "little"))
|
|
if pe.read(6) != b"PE\0\0\x64\x86":
|
|
raise ValueError("Exécutable Windows x64 attendu")
|
|
with zipfile.ZipFile(local_tool) as archive:
|
|
for member in archive.namelist():
|
|
if member.startswith(("tools/net10.0/any/", "vendor/")):
|
|
archive.extract(member, tmp / "vpk")
|
|
packed = tmp / "packed"
|
|
subprocess.run(["dotnet.exe", windows_path(tmp / "vpk/tools/net10.0/any/vpk.dll"), "pack",
|
|
"--skip-updates", "--runtime", "win-x64", "--packId", "ArenaTracker",
|
|
"--packTitle", "Arena Tracker", "--packAuthors", "Arena Tracker",
|
|
"--packVersion", args.version, "--channel", args.channel,
|
|
"--packDir", windows_path(package_dir), "--mainExe", app.name,
|
|
"--icon", windows_path(assets / "icon.ico"), "--outputDir", windows_path(packed),
|
|
"--shortcuts", "StartMenuRoot", "--delta", "None", "--noPortable"], check=True)
|
|
stage = Path(staging) / "release"
|
|
stage.mkdir()
|
|
package = stage / f"ArenaTracker-{args.version}-{args.channel}-full.nupkg"
|
|
shutil.move(packed / package.name, package)
|
|
installer = f"ArenaTracker-{args.channel}-Setup.exe"
|
|
shutil.move(packed / installer, stage / installer)
|
|
manifest = stage / f"release-{args.channel}.json"
|
|
manifest.write_text(json.dumps({
|
|
"app": "ArenaTracker", "target": "win-x64", "channel": args.channel,
|
|
"version": args.version, "commit": commit, "package": package.name,
|
|
"sha256": base64.b64encode(digest(package).digest()).decode(),
|
|
}) + "\n")
|
|
subprocess.run(["openssl", "pkeyutl", "-sign", "-rawin", "-inkey", key,
|
|
"-in", manifest, "-out", manifest.with_suffix(".sig")], check=True)
|
|
subprocess.run([app, "--verify-release", windows_path(stage)], check=True)
|
|
stage.rename(dest)
|
|
print(dest)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("commit")
|
|
parser.add_argument("version")
|
|
parser.add_argument("channel", choices=("stable", "beta"))
|
|
parser.add_argument("output", type=Path)
|
|
try:
|
|
build(parser.parse_args())
|
|
except (ValueError, OSError, KeyError, subprocess.CalledProcessError) as err:
|
|
raise SystemExit(f"Erreur : {err}")
|