diff --git a/.gitignore b/.gitignore index 8824726..1177550 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ obj *.nupkg artifacts/ .packages/ +.nuget/ diff --git a/Xamarin.MacDev/RuntimeService.cs b/Xamarin.MacDev/RuntimeService.cs index cf436d6..e8c1091 100644 --- a/Xamarin.MacDev/RuntimeService.cs +++ b/Xamarin.MacDev/RuntimeService.cs @@ -31,7 +31,7 @@ public RuntimeService (ICustomLogger log) /// public List List (bool availableOnly = false) { - var json = simctl.Run ("list", "runtimes", "--json"); + var json = simctl.RunJson ("list", "runtimes"); if (json is null) return new List (); diff --git a/Xamarin.MacDev/SimCtl.cs b/Xamarin.MacDev/SimCtl.cs index 959fc76..517549e 100644 --- a/Xamarin.MacDev/SimCtl.cs +++ b/Xamarin.MacDev/SimCtl.cs @@ -57,4 +57,40 @@ public SimCtl (ICustomLogger log) return null; } } + + /// + /// Runs xcrun simctl {args} with --json --json-output=<file> + /// so that structured JSON output is written to a temp file instead of stdout. + /// Returns the file contents, or null on failure. + /// This is intended for simctl list subcommands that support --json-output. + /// + public string? RunJson (params string [] args) + { + var tempPath = Path.GetTempFileName (); + try { + var jsonArgs = new string [args.Length + 2]; + Array.Copy (args, jsonArgs, args.Length); + jsonArgs [args.Length] = "--json"; + jsonArgs [args.Length + 1] = "--json-output=" + tempPath; + + var result = Run (jsonArgs); + if (result is null) + return null; + + if (!File.Exists (tempPath)) { + log.LogInfo ("simctl did not produce JSON output file at '{0}'.", tempPath); + return null; + } + + var content = File.ReadAllText (tempPath); + if (string.IsNullOrWhiteSpace (content)) { + log.LogInfo ("simctl produced an empty JSON output file at '{0}'.", tempPath); + return null; + } + + return content; + } finally { + try { if (File.Exists (tempPath)) File.Delete (tempPath); } catch { } + } + } } diff --git a/Xamarin.MacDev/SimulatorService.cs b/Xamarin.MacDev/SimulatorService.cs index 2a9624a..322bc82 100644 --- a/Xamarin.MacDev/SimulatorService.cs +++ b/Xamarin.MacDev/SimulatorService.cs @@ -31,7 +31,7 @@ public SimulatorService (ICustomLogger log) /// public List List (bool availableOnly = false) { - var json = simctl.Run ("list", "devices", "--json"); + var json = simctl.RunJson ("list", "devices"); if (json is null) return new List (); diff --git a/eng/Versions.props b/eng/Versions.props index 4d2c6fd..b63fb1e 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -19,5 +19,6 @@ 4.3.0 3.18.1 5.0.0 + 17.12.0 diff --git a/tests/SimctlOutputParserTests.cs b/tests/SimctlOutputParserTests.cs index 3f113d5..b3a4a70 100644 --- a/tests/SimctlOutputParserTests.cs +++ b/tests/SimctlOutputParserTests.cs @@ -379,7 +379,7 @@ public void LiveSimctlList_ParsesWithoutExceptions () // with no exceptions logged — per rolfbjarne review feedback var logger = new TestLogger (); var simctl = new SimCtl (logger); - var json = simctl.Run ("list", "--json"); + var json = simctl.RunJson ("list"); Assert.That (json, Is.Not.Null, "simctl list --json should return output"); var devices = SimctlOutputParser.ParseDevices (json, logger); diff --git a/tests/tests.csproj b/tests/tests.csproj index 5e221d0..4787990 100644 --- a/tests/tests.csproj +++ b/tests/tests.csproj @@ -8,14 +8,18 @@ false true false + true + + $(NoWarn);NETSDK1023;NU1504 + all runtime; build; native; contentfiles; analyzers; buildtransitive -