#!/usr/bin/env python3 """Build the opt-in saturation observer without changing product dependencies.""" import argparse import json from pathlib import Path import subprocess import xml.etree.ElementTree as ET def build(output, libraries, linux): output = output.resolve() output.mkdir(parents=True, exist_ok=False) project = ET.Element("Project", Sdk="Microsoft.NET.Sdk") properties = ET.SubElement(project, "PropertyGroup") for key, value in {"OutputType": "Exe", "TargetFramework": "net10.0", "ImplicitUsings": "enable", "TreatWarningsAsErrors": "true", "EnableDefaultCompileItems": "false"}.items(): ET.SubElement(properties, key).text = value items = ET.SubElement(project, "ItemGroup") ET.SubElement(items, "FrameworkReference", Include="Microsoft.AspNetCore.App") ET.SubElement(items, "Compile", Include=str(Path(__file__).resolve().parent / "SaturationObserver.cs")) for name in ["Microsoft.Diagnostics.NETCore.Client", "Microsoft.Diagnostics.Tracing.TraceEvent", "Microsoft.Diagnostics.FastSerialization"]: path = (libraries / (name + ".dll")).resolve(strict=True) reference = ET.SubElement(items, "Reference", Include=name) ET.SubElement(reference, "HintPath").text = str(path) project_path = output / "observer.csproj" ET.indent(project) ET.ElementTree(project).write(project_path, encoding="unicode") command = ["dotnet", "publish" if linux else "build", str(project_path), "-c", "Release", "-m:2", "--nologo"] if linux: command += ["-r", "linux-arm64", "--self-contained", "true", "-p:RuntimeFrameworkVersion=10.0.14", "-p:PublishSingleFile=false", "-p:PublishTrimmed=false", "-p:UseAppHost=true", "-o", str(output / "published")] with (output / "build.log").open("w") as log: result = subprocess.run(command, stdout=log, stderr=subprocess.STDOUT) (output / "build-command.json").write_text(json.dumps(command, indent=2) + "\n") if result.returncode: raise subprocess.CalledProcessError(result.returncode, command) return output / ("published" if linux else "bin/Release/net10.0") if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output", required=True, type=Path) parser.add_argument("--libraries", required=True, type=Path) parser.add_argument("--linux-self-contained", action="store_true") args = parser.parse_args() print(build(args.output, args.libraries, args.linux_self_contained))