using System.Text.Json; using System.Xml.Linq; namespace HealthApp.Provisioning; public static class LaunchdWriter { public static void Write(string service, string root, string dotnet, string output) { if (!Deployment.Names.Contains(service)) throw new InvalidOperationException("Unknown service."); var directory = Path.Combine(root, "services", service); var assembly = service switch { "api-gateway" => "ApiGateway", "patient-profile" => "PatientProfileService", "observation" => "ObservationService", "insights" => "InsightsService", _ => "HealthOverviewService" }; var environment = new XElement("dict"); foreach (var line in File.ReadAllLines(Path.Combine(root, "config", service, "service.env"))) { var position = line.IndexOf('='); environment.Add(new XElement("key", line[..position]), new XElement("string", JsonSerializer.Deserialize(line[(position + 1)..]))); } var document = new XDocument(new XDeclaration("1.0", "utf-8", null), new XDocumentType("plist", "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null), new XElement("plist", new XAttribute("version", "1.0"), new XElement("dict", new XElement("key", "Label"), new XElement("string", "healthapp-" + service), new XElement("key", "ProgramArguments"), new XElement("array", new XElement("string", dotnet), new XElement("string", Path.Combine(directory, assembly + ".dll"))), new XElement("key", "WorkingDirectory"), new XElement("string", directory), new XElement("key", "UserName"), new XElement("string", "_healthapp_" + service.Replace('-', '_')), new XElement("key", "EnvironmentVariables"), environment, new XElement("key", "RunAtLoad"), new XElement("true"), new XElement("key", "KeepAlive"), new XElement("true")))); document.Save(output); if (!OperatingSystem.IsWindows()) File.SetUnixFileMode(output, UnixFileMode.UserRead | UnixFileMode.UserWrite); } }