"""Standalone desktop client artifacts; never installs or restarts a server.""" import hashlib import json import os from pathlib import Path import platform import shutil import subprocess import tarfile import tempfile import time from urllib.parse import urlsplit def _run(runner, command, **kwargs): try: result = runner(command, **kwargs) except subprocess.TimeoutExpired as error: raise RuntimeError(f"{command[0]} timed out") from error if result.returncode: raise RuntimeError(f"{command[0]} failed; see its build output") return result def _arguments(server_url): if not server_url: return () parsed = urlsplit(server_url) if parsed.scheme not in ("ws", "wss", "http", "https") or not parsed.hostname or parsed.username or parsed.password or parsed.fragment: raise ValueError("--server-url must be a server address without embedded credentials or fragment") return (f"--dart-define=PIPELINES_SERVER_URL={server_url}",) def _linux_archive(entry): archive = entry / "pipelines-linux-x64.tar.gz" if archive.is_file(): return archive staged = archive.with_suffix(".gz.next") try: with tarfile.open(staged, "w:gz") as output: for child in sorted(entry.iterdir()): if child.name not in (".complete", archive.name, staged.name): output.add(child, arcname=f"pipelines-linux-x64/{child.name}") os.replace(staged, archive) finally: staged.unlink(missing_ok=True) return archive def _macos_build(*, app, cache, source_digest, runner, arguments, flutter, log): if platform.system() != "Darwin": raise ValueError("macOS clients must be built natively on macOS with Xcode") selected = flutter or os.environ.get("PIPELINES_FLUTTER") or shutil.which("flutter") if not selected: raise ValueError("Flutter is required; pass --flutter /path/to/flutter or set PIPELINES_FLUTTER") selected = str(Path(selected).expanduser()) version = _run(runner, [selected, "--version", "--machine"], capture_output=True, text=True, timeout=60) try: actual = json.loads(version.stdout)["frameworkVersion"] except (ValueError, KeyError, TypeError) as error: raise ValueError("The selected Flutter SDK did not report its version") from error pinned = (app / ".flutter-version").read_text().strip() if actual != pinned: raise ValueError(f"Flutter {pinned} is required; selected SDK is {actual}. No SDK was changed.") xcode = _run(runner, ["xcodebuild", "-version"], capture_output=True, text=True, timeout=30) identity = ("native-macos-v1", platform.machine(), xcode.stdout.strip(), actual) key = source_digest("macos", app=app, extra=arguments + identity) entry = cache / f"macos-{key}" bundle = entry / "Pipelines.app" archive = entry / "Pipelines-macos.zip" if (entry / ".complete").is_file() and (bundle / "Contents/MacOS/Pipelines").is_file() and archive.is_file(): log(f"Reusing native macOS build {key}.") return entry, bundle, archive cache.mkdir(parents=True, exist_ok=True) staging = Path(tempfile.mkdtemp(prefix="macos-", dir=cache)) try: with tempfile.TemporaryDirectory(prefix="pipelines-macos-source-") as temporary: work = Path(temporary) / "app" shutil.copytree(app, work, symlinks=True, ignore=shutil.ignore_patterns( "build", ".dart_tool", "Pods", "ephemeral", ".symlinks", ".gradle", ".cxx", "local.properties", ".DS_Store")) _run(runner, [selected, "pub", "get"], cwd=work, timeout=300) built = int(time.time()) _run(runner, [selected, "build", "macos", "--release", "--no-pub", *arguments, f"--build-number={built // 60}", f"--dart-define=PIPELINES_NATIVE_BUILD_CODE={built // 60}"], cwd=work, timeout=1200, env={**os.environ, "FLUTTER_XCODE_ARCHS": {"aarch64": "arm64", "x86_64": "x86_64"}.get(platform.machine(), platform.machine())}) native = work / "build/macos/Build/Products/Release/Pipelines.app" if not (native / "Contents/MacOS/Pipelines").is_file(): raise RuntimeError("Flutter did not produce a complete Pipelines.app") arches = _run(runner, ["lipo", "-archs", str(native / "Contents/MacOS/Pipelines")], capture_output=True, text=True, timeout=30).stdout.split() architectures = sorted({{"x86_64": "x64", "arm64": "arm64"}.get(a, a) for a in arches}) if not architectures or any(a not in ("x64", "arm64") for a in architectures): raise RuntimeError("macOS bundle has unsupported or missing architectures") # Framework symlinks and executable modes are part of the bundle. shutil.copytree(native, staging / "Pipelines.app", symlinks=True) _run(runner, ["ditto", "-c", "-k", "--keepParent", str(staging / "Pipelines.app"), str(staging / archive.name)], timeout=120) if not (staging / archive.name).is_file(): raise RuntimeError("macOS archive was not created") (staging / ".complete").write_text(json.dumps({"key": key, "built": built, "architectures": architectures}) + "\n") if entry.exists(): previous = entry.with_name(entry.name + ".previous") if previous.exists(): raise RuntimeError(f"A previous build is retained at {previous}; no files were replaced") entry.rename(previous) try: staging.rename(entry) except BaseException: previous.rename(entry) raise shutil.rmtree(previous) else: staging.rename(entry) finally: if staging.exists(): shutil.rmtree(staging) return entry, bundle, archive def build_desktop(kind, *, app, cache, runner, source_digest, docker_build, docker_ready, server_url=None, flutter=None, output=None, log=print, prune_cache=None, update_keys=()): arguments = _arguments(server_url) + tuple(update_keys) if kind == "macos": entry, bundle, archive = _macos_build(app=app, cache=cache, runner=runner, source_digest=source_digest, arguments=arguments, flutter=flutter, log=log) elif kind == "linux": if not docker_ready(runner): raise RuntimeError("Docker is required for the Linux x64 client; start Docker and retry") entry = docker_build("linux", app=app, cache=cache, runner=runner, arguments=arguments, log=log) if not (entry / "pipelines-app").is_file() or not (entry / "data/flutter_assets").is_dir() or not (entry / "lib").is_dir(): raise RuntimeError("Linux build is missing its executable, data or libraries") bundle, archive = entry, _linux_archive(entry) else: raise ValueError(f"Unknown desktop target: {kind}") if prune_cache is not None: prune_cache(cache, kind, keep=entry) if output is not None: output = Path(output).expanduser() output.mkdir(parents=True, exist_ok=True) published = output / archive.name descriptor, temporary = tempfile.mkstemp(prefix=archive.name + ".", dir=output) os.close(descriptor) try: shutil.copy2(archive, temporary) os.replace(temporary, published) finally: Path(temporary).unlink(missing_ok=True) archive = published log(f"Desktop client bundle: {bundle}") log(f"Desktop client archive: {archive}") log("No server was installed or restarted. Choose your server in the app.") return bundle, archive def publish_desktop(kind, bundle, archive, updates, *, signer, notes="", log=print): """Publish archive before its feed; versioned names preserve in-flight reads. [signer] returns the detached `version.json.sig` for the feed bytes.""" entry = bundle.parent if kind == "macos" else bundle complete = json.loads((entry / ".complete").read_text()) code = int(complete["built"]) // 60 architectures = complete.get("architectures") if kind == "macos" else ["x64"] if not architectures or any(a not in ("x64", "arm64") for a in architectures): raise ValueError("Build metadata has no supported architecture") digest = hashlib.sha256(archive.read_bytes()).hexdigest() suffix = ".zip" if kind == "macos" else ".tar.gz" stem = "Pipelines-macos" if kind == "macos" else "pipelines-linux-x64" filename = f"{stem}-{code}-{digest[:8]}{suffix}" target = updates / ("macos" if kind == "macos" else "linux-x64") target.mkdir(parents=True, exist_ok=True) metadata = {"versionCode": code, "versionName": f"0.1.{code}", "platform": kind, "architectures": architectures, "file": filename, "sha256": digest, "notes": notes} feed = target / "version.json" if feed.exists(): previous = json.loads(feed.read_text()) if previous.get("versionCode", 0) > code: raise ValueError("Refusing to publish an older build over a newer update") data = (json.dumps(metadata) + "\n").encode() # The signature lands before the feed it covers, after the archive. for destination, content in ((target / filename, archive.read_bytes()), (target / "version.json.sig", signer(data)), (feed, data)): descriptor, temporary = tempfile.mkstemp(prefix=destination.name + ".", dir=target) try: with os.fdopen(descriptor, "wb") as output: output.write(content) output.flush() os.fsync(output.fileno()) os.chmod(temporary, 0o644) os.replace(temporary, destination) finally: Path(temporary).unlink(missing_ok=True) log(f"Published {kind} update {code} at {feed}") return metadata