diff --git a/external/Xamarin.MacDev b/external/Xamarin.MacDev index f1300986199f..10a0c3c8ecc5 160000 --- a/external/Xamarin.MacDev +++ b/external/Xamarin.MacDev @@ -1 +1 @@ -Subproject commit f1300986199f5489191d2c9712e57bf8a0a3d84a +Subproject commit 10a0c3c8ecc5d0f509881c49cb0b4078b0a3a116 diff --git a/msbuild/Xamarin.MacDev.Tasks/Tasks/GetAvailableDevices.cs b/msbuild/Xamarin.MacDev.Tasks/Tasks/GetAvailableDevices.cs index eda5380ab2d9..57ecb577164d 100644 --- a/msbuild/Xamarin.MacDev.Tasks/Tasks/GetAvailableDevices.cs +++ b/msbuild/Xamarin.MacDev.Tasks/Tasks/GetAvailableDevices.cs @@ -2,13 +2,13 @@ using System.IO; using System.Collections.Generic; using System.Linq; -using System.Text.Json; using System.Threading; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using Xamarin.Localization.MSBuild; +using Xamarin.MacDev.Models; using Xamarin.Messaging.Build.Client; using Xamarin.Utils; @@ -161,265 +161,218 @@ protected virtual async System.Threading.Tasks.Task ExecuteCtlAsync (par } } - async System.Threading.Tasks.Task ExecuteCtlToJsonAsync (params string [] args) - { - var json = await ExecuteCtlAsync (args); - var options = new JsonDocumentOptions { - AllowTrailingCommas = true, - CommentHandling = JsonCommentHandling.Skip, - }; - return JsonDocument.Parse (string.IsNullOrEmpty (json) ? "{}" : json, options); - } - async System.Threading.Tasks.Task> RunDeviceCtlAsync () { - var doc = await ExecuteCtlToJsonAsync ("devicectl", "list", "devices"); - var array = doc.FindProperty ("result", "devices")?.EnumerateIfArray (); + var json = await ExecuteCtlAsync ("devicectl", "list", "devices"); var rv = new List (); - if (array is not null) { - foreach (var device in array) { - var name = device.GetStringPropertyOrEmpty ("deviceProperties", "name"); - var udid = device.GetStringPropertyOrEmpty ("hardwareProperties", "udid"); - var identifier = device.GetStringPropertyOrEmpty ("identifier"); - - var deviceProperties = device.GetNullableProperty ("deviceProperties"); - var buildVersion = deviceProperties.GetStringPropertyOrEmpty ("osBuildUpdate"); - var productVersion = deviceProperties.GetStringPropertyOrEmpty ("osVersionNumber"); - - var hardwareProperties = device.GetNullableProperty ("hardwareProperties"); - var deviceClass = hardwareProperties.GetStringPropertyOrEmpty ("deviceType"); - var hardwareModel = hardwareProperties.GetStringPropertyOrEmpty ("hardwareModel"); - var hardwarePlatform = hardwareProperties.GetStringPropertyOrEmpty ("platform"); - var productType = hardwareProperties.GetStringPropertyOrEmpty ("productType"); - var serialNumber = hardwareProperties.GetStringPropertyOrEmpty ("serialNumber"); - var uniqueChipID = hardwareProperties.GetUInt64Property ("ecid"); - - var cpuType = hardwareProperties.GetNullableProperty ("cpuType"); - var cpuArchitecture = cpuType.GetStringPropertyOrEmpty ("name"); - - var connectionProperties = device.GetNullableProperty ("connectionProperties"); - var transportType = connectionProperties.GetStringPropertyOrEmpty ("transportType"); - var pairingState = connectionProperties.GetStringPropertyOrEmpty ("pairingState"); - - if (string.IsNullOrEmpty (udid)) - udid = identifier; - - if (string.IsNullOrEmpty (udid)) - udid = $""; - - var item = new TaskItem (udid); - item.SetMetadata ("Name", name); - item.SetMetadata ("BuildVersion", buildVersion); - item.SetMetadata ("DeviceClass", deviceClass); - item.SetMetadata ("HardwareModel", hardwareModel); - item.SetMetadata ("Platform", hardwarePlatform); - item.SetMetadata ("ProductType", productType); - item.SetMetadata ("SerialNumber", serialNumber); - item.SetMetadata ("UniqueChipID", uniqueChipID?.ToString () ?? string.Empty); - item.SetMetadata ("CPUArchitecture", cpuArchitecture); - item.SetMetadata ("TransportType", transportType); - item.SetMetadata ("PairingState", pairingState); - - // we provide the following metadata for both simulator and device - item.SetMetadata ("Description", name); - item.SetMetadata ("Type", "Device"); - item.SetMetadata ("OSVersion", productVersion); - item.SetMetadata ("UDID", udid); - - // compute the platform and runtime identifier - var runtimeIdentifier = ""; - ApplePlatform platform; - IPhoneDeviceType deviceType; - var discardedReason = ""; - switch (deviceClass.ToLowerInvariant ()) { - case "iphone": - case "ipod": - runtimeIdentifier += "ios-"; - platform = ApplePlatform.iOS; - deviceType = IPhoneDeviceType.IPhone; - break; - case "ipad": - runtimeIdentifier += "ios-"; - platform = ApplePlatform.iOS; - deviceType = IPhoneDeviceType.IPad; - break; - case "appletv": - runtimeIdentifier += "tvos-"; - platform = ApplePlatform.TVOS; - deviceType = IPhoneDeviceType.TV; + + // Use shared parser from Xamarin.MacDev for devicectl JSON extraction + var parsedDevices = DeviceCtlOutputParser.ParseDevices (json); + + foreach (var device in parsedDevices) { + var udid = device.Udid; + + if (string.IsNullOrEmpty (udid)) + udid = $""; + + var item = new TaskItem (udid); + item.SetMetadata ("Name", device.Name); + item.SetMetadata ("BuildVersion", device.BuildVersion); + item.SetMetadata ("DeviceClass", device.DeviceClass); + item.SetMetadata ("HardwareModel", device.HardwareModel); + item.SetMetadata ("Platform", device.Platform); + item.SetMetadata ("ProductType", device.ProductType); + item.SetMetadata ("SerialNumber", device.SerialNumber); + item.SetMetadata ("UniqueChipID", device.UniqueChipID?.ToString () ?? string.Empty); + item.SetMetadata ("CPUArchitecture", device.CpuArchitecture); + item.SetMetadata ("TransportType", device.TransportType); + item.SetMetadata ("PairingState", device.PairingState); + + // we provide the following metadata for both simulator and device + item.SetMetadata ("Description", device.Name); + item.SetMetadata ("Type", "Device"); + item.SetMetadata ("OSVersion", device.OSVersion); + item.SetMetadata ("UDID", udid); + + // compute the platform and runtime identifier + var runtimeIdentifier = ""; + ApplePlatform platform; + IPhoneDeviceType deviceType; + var discardedReason = ""; + switch (device.DeviceClass.ToLowerInvariant ()) { + case "iphone": + case "ipod": + runtimeIdentifier += "ios-"; + platform = ApplePlatform.iOS; + deviceType = IPhoneDeviceType.IPhone; + break; + case "ipad": + runtimeIdentifier += "ios-"; + platform = ApplePlatform.iOS; + deviceType = IPhoneDeviceType.IPad; + break; + case "appletv": + runtimeIdentifier += "tvos-"; + platform = ApplePlatform.TVOS; + deviceType = IPhoneDeviceType.TV; + break; + case "applewatch": + case "visionos": + default: + platform = ApplePlatform.None; + deviceType = IPhoneDeviceType.NotSet; + discardedReason = $"'{device.DeviceClass}' devices are not supported"; + break; + } + + if (string.IsNullOrEmpty (discardedReason)) { + switch (device.CpuArchitecture.ToLowerInvariant ()) { + case "arm64": + case "arm64e": + // arm64 and arm64e are both arm64 for our purposes + runtimeIdentifier += "arm64"; break; - case "applewatch": - case "visionos": default: - platform = ApplePlatform.None; - deviceType = IPhoneDeviceType.NotSet; - discardedReason = $"'{deviceClass}' devices are not supported"; + discardedReason = $"Unknown CPU architecture '{device.CpuArchitecture}'"; break; } + } - if (string.IsNullOrEmpty (discardedReason)) { - switch (cpuArchitecture.ToLowerInvariant ()) { - case "arm64": - case "arm64e": - // arm64 and arm64e are both arm64 for our purposes - runtimeIdentifier += "arm64"; - break; - default: - discardedReason = $"Unknown CPU architecture '{cpuArchitecture}'"; - break; - } - } - - Version.TryParse (productVersion, out var minimumOSVersion); - var maximumOSVersion = new Version (65535, 255, 255); + Version.TryParse (device.OSVersion, out var minimumOSVersion); + var maximumOSVersion = new Version (65535, 255, 255); - rv.Add (new DeviceInfo (item, [runtimeIdentifier], platform, deviceType, minimumOSVersion ?? new Version (0, 0), maximumOSVersion, discardedReason)); - } + rv.Add (new DeviceInfo (item, [runtimeIdentifier], platform, deviceType, minimumOSVersion ?? new Version (0, 0), maximumOSVersion, discardedReason)); } return rv; } async System.Threading.Tasks.Task> RunSimCtlAsync () { - var doc = await ExecuteCtlToJsonAsync ("simctl", "list", "--json"); + var json = await ExecuteCtlAsync ("simctl", "list", "--json"); var rv = new List (); - var runtimes = new Dictionary (); - if (doc.TryGetProperty ("runtimes", out var runtimesElement)) { - foreach (var runtime in runtimesElement.EnumerateIfArray ()) { - var name = runtime.GetStringProperty ("identifier") ?? string.Empty; - runtimes [name] = runtime; - } - } - - var deviceTypes = new Dictionary (); - if (doc.TryGetProperty ("devicetypes", out var deviceTypesElement)) { - foreach (var deviceType in deviceTypesElement.EnumerateIfArray ()) { - var name = deviceType.GetStringProperty ("identifier") ?? string.Empty; - deviceTypes [name] = deviceType; - } - } + // Use shared parser from Xamarin.MacDev for device and runtime extraction + var parsedDevices = SimctlOutputParser.ParseDevices (json); + var parsedRuntimes = SimctlOutputParser.ParseRuntimes (json); + + // Index runtimes by identifier for SupportedArchitectures lookup + var runtimesByIdentifier = new Dictionary (); + foreach (var rt in parsedRuntimes) + runtimesByIdentifier [rt.Identifier] = rt; + + // Use shared parser for devicetypes (productFamily, min/maxRuntime) + var parsedDeviceTypes = SimctlOutputParser.ParseDeviceTypes (json); + var deviceTypes = new Dictionary (); + foreach (var dt in parsedDeviceTypes) + deviceTypes [dt.Identifier] = dt; + + foreach (var device in parsedDevices) { + var hasRuntime = runtimesByIdentifier.TryGetValue (device.RuntimeIdentifier, out var runtimeInfo); + var runtimePlatform = hasRuntime ? runtimeInfo!.Platform : string.Empty; + var runtimeVersion = hasRuntime ? runtimeInfo!.Version : device.OSVersion; + var supportedArchitectures = hasRuntime ? runtimeInfo!.SupportedArchitectures : new List (); + + var item = new TaskItem (device.Udid); + item.SetMetadata ("Runtime", device.RuntimeIdentifier); + item.SetMetadata ("IsAvailable", device.IsAvailable.ToString ()); + item.SetMetadata ("AvailabilityError", device.AvailabilityError); + item.SetMetadata ("DeviceTypeIdentifier", device.DeviceTypeIdentifier); + item.SetMetadata ("State", device.State); + item.SetMetadata ("Name", device.Name); + item.SetMetadata ("SupportedArchitectures", string.Join (",", supportedArchitectures)); + + // we provide the following metadata for both simulator and device + item.SetMetadata ("Description", device.Name); + item.SetMetadata ("Type", "Simulator"); + item.SetMetadata ("OSVersion", runtimeVersion); + item.SetMetadata ("UDID", device.Udid); + + var discardedReason = ""; + var runtimeIdentifier = ""; + var runtimeIdentifiers = new List (); + if (device.IsAvailable) { + switch (runtimePlatform.ToLowerInvariant ()) { + case "ios": + runtimeIdentifier += "iossimulator-"; + break; + case "tvos": + runtimeIdentifier += "tvossimulator-"; + break; + default: + discardedReason = $"'{runtimePlatform}' simulators are not supported"; + break; + } - if (doc.TryGetProperty ("devices", out var devicesElement)) { - foreach (var runtime in devicesElement.EnumerateObject ()) { - var runtimeName = runtime.Name; - var hasRuntime = runtimes.TryGetValue (runtimeName, out var runtimeElement); - var runtimePlatform = hasRuntime ? runtimeElement.GetStringProperty ("platform") ?? string.Empty : string.Empty; - var runtimeVersion = hasRuntime ? runtimeElement.GetStringProperty ("version") ?? string.Empty : string.Empty; - var supportedArchitectures = hasRuntime ? runtimeElement.GetProperty ("supportedArchitectures").EnumerateIfArray ().Select (v => v.GetString () ?? "") : Enumerable.Empty (); - foreach (var element in runtime.Value.EnumerateIfArray ()) { - var udid = element.GetStringProperty ("udid") ?? string.Empty; - var isAvailable = element.GetBooleanProperty ("isAvailable") ?? false; - var availabilityError = element.GetStringProperty ("availabilityError") ?? string.Empty; - var deviceTypeIdentifier = element.GetStringProperty ("deviceTypeIdentifier") ?? string.Empty; - var state = element.GetStringProperty ("state") ?? string.Empty; - var name = element.GetStringProperty ("name") ?? string.Empty; - - var item = new TaskItem (udid); - item.SetMetadata ("Runtime", runtimeName); - item.SetMetadata ("IsAvailable", isAvailable.ToString ()); - item.SetMetadata ("AvailabilityError", availabilityError); - item.SetMetadata ("DeviceTypeIdentifier", deviceTypeIdentifier); - item.SetMetadata ("State", state); - item.SetMetadata ("Name", name); - item.SetMetadata ("SupportedArchitectures", string.Join (",", supportedArchitectures)); - - // we provide the following metadata for both simulator and device - item.SetMetadata ("Description", name); - item.SetMetadata ("Type", "Simulator"); - item.SetMetadata ("OSVersion", runtimeVersion); - item.SetMetadata ("UDID", udid); - - var discardedReason = ""; - var runtimeIdentifier = ""; - var runtimeIdentifiers = new List (); - if (isAvailable) { - switch (runtimePlatform.ToLowerInvariant ()) { - case "ios": - runtimeIdentifier += "iossimulator-"; + // pick the first architecture as the simulator architecture + if (string.IsNullOrEmpty (discardedReason)) { + foreach (var arch in supportedArchitectures) { + switch (arch.ToLowerInvariant ()) { + case "x64": + case "x86_64": + runtimeIdentifiers.Add (runtimeIdentifier + "x64"); break; - case "tvos": - runtimeIdentifier += "tvossimulator-"; + case "arm64": + runtimeIdentifiers.Add (runtimeIdentifier + "arm64"); + if (!CanRunArm64) + discardedReason = $"Can't run an arm64 simulator on an x86_64 macOS desktop."; break; default: - discardedReason = $"'{runtimePlatform}' simulators are not supported"; + discardedReason = $"Unknown CPU architecture '{arch}'"; break; } - - // pick the first architecture as the simulator architecture - if (string.IsNullOrEmpty (discardedReason)) { - foreach (var arch in supportedArchitectures) { - switch (arch.ToLowerInvariant ()) { - case "x64": - case "x86_64": - runtimeIdentifiers.Add (runtimeIdentifier + "x64"); - break; - case "arm64": - runtimeIdentifiers.Add (runtimeIdentifier + "arm64"); - if (!CanRunArm64) - discardedReason = $"Can't run an arm64 simulator on an x86_64 macOS desktop."; - break; - default: - discardedReason = $"Unknown CPU architecture '{arch}'"; - break; - } - } - } - } else { - discardedReason = $"Device is not available: {availabilityError}"; } + } + } else { + discardedReason = $"Device is not available: {device.AvailabilityError}"; + } - var platformName = runtimeName.Replace ("com.apple.CoreSimulator.SimRuntime.", "").Split ('-') [0]; - var platform = ApplePlatform.None; - if (string.IsNullOrEmpty (discardedReason)) { - switch (platformName.ToLowerInvariant ()) { - case "ios": - platform = ApplePlatform.iOS; - break; - case "tvos": - platform = ApplePlatform.TVOS; - break; - case "watchos": - case "visionos": - default: - discardedReason = $"'{platformName}' simulators are not supported"; - break; - } - } - var deviceType = IPhoneDeviceType.NotSet; - var minimumOSVersion = new Version (0, 0); - var maximumOSVersion = new Version (65535, 255, 255); - if (string.IsNullOrEmpty (discardedReason)) { - if (deviceTypes.TryGetValue (deviceTypeIdentifier, out var deviceTypeElement)) { - var productFamily = deviceTypeElement.GetStringProperty ("productFamily") ?? string.Empty; - switch (productFamily.ToLowerInvariant ()) { - case "iphone": - case "ipod": - deviceType = IPhoneDeviceType.IPhone; - break; - case "ipad": - deviceType = IPhoneDeviceType.IPad; - break; - case "appletv": - case "apple tv": - deviceType = IPhoneDeviceType.TV; - break; - default: - discardedReason = $"Unknown product family '{productFamily}'"; - break; - } - if (Version.TryParse (deviceTypeElement.GetStringProperty ("minRuntimeVersionString"), out var parsedMinimumOSVersion)) - minimumOSVersion = parsedMinimumOSVersion; - if (Version.TryParse (deviceTypeElement.GetStringProperty ("maxRuntimeVersionString"), out var parsedMaximumOSVersion)) - maximumOSVersion = parsedMaximumOSVersion; - } else { - discardedReason = $"Unknown device type identifier '{deviceTypeIdentifier}'"; - } + var platform = ApplePlatform.None; + if (string.IsNullOrEmpty (discardedReason)) { + switch (device.Platform.ToLowerInvariant ()) { + case "ios": + platform = ApplePlatform.iOS; + break; + case "tvos": + platform = ApplePlatform.TVOS; + break; + case "watchos": + case "visionos": + default: + discardedReason = $"'{device.Platform}' simulators are not supported"; + break; + } + } + var deviceType = IPhoneDeviceType.NotSet; + var minimumOSVersion = new Version (0, 0); + var maximumOSVersion = new Version (65535, 255, 255); + if (string.IsNullOrEmpty (discardedReason)) { + if (deviceTypes.TryGetValue (device.DeviceTypeIdentifier, out var deviceTypeInfo)) { + switch (deviceTypeInfo.ProductFamily.ToLowerInvariant ()) { + case "iphone": + case "ipod": + deviceType = IPhoneDeviceType.IPhone; + break; + case "ipad": + deviceType = IPhoneDeviceType.IPad; + break; + case "appletv": + case "apple tv": + deviceType = IPhoneDeviceType.TV; + break; + default: + discardedReason = $"Unknown product family '{deviceTypeInfo.ProductFamily}'"; + break; } - - rv.Add (new DeviceInfo (item, runtimeIdentifiers, platform, deviceType, minimumOSVersion, maximumOSVersion, discardedReason)); + if (Version.TryParse (deviceTypeInfo.MinRuntimeVersionString, out var parsedMinimumOSVersion)) + minimumOSVersion = parsedMinimumOSVersion; + if (Version.TryParse (deviceTypeInfo.MaxRuntimeVersionString, out var parsedMaximumOSVersion)) + maximumOSVersion = parsedMaximumOSVersion; + } else { + discardedReason = $"Unknown device type identifier '{device.DeviceTypeIdentifier}'"; } } + + rv.Add (new DeviceInfo (item, runtimeIdentifiers, platform, deviceType, minimumOSVersion, maximumOSVersion, discardedReason)); } return rv; } diff --git a/msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinTask.cs b/msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinTask.cs index f25ef2fd388c..f5470c4cc104 100644 --- a/msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinTask.cs +++ b/msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinTask.cs @@ -347,24 +347,24 @@ protected static string GetExecutable (List arguments, string toolName, } #region Xamarin.MacDev.ICustomLogger - void ICustomLogger.LogError (string message, Exception ex) + void ICustomLogger.LogError (string message, Exception? ex) { Log.LogError (message); if (ex is not null) Log.LogErrorFromException (ex); } - void ICustomLogger.LogWarning (string messageFormat, params object [] args) + void ICustomLogger.LogWarning (string messageFormat, params object? [] args) { Log.LogWarning (messageFormat, args); } - void ICustomLogger.LogInfo (string messageFormat, object [] args) + void ICustomLogger.LogInfo (string messageFormat, params object? [] args) { Log.LogMessage (MessageImportance.Normal, messageFormat, args); } - void ICustomLogger.LogDebug (string messageFormat, params object [] args) + void ICustomLogger.LogDebug (string messageFormat, params object? [] args) { Log.LogMessage (MessageImportance.Low, messageFormat, args); } diff --git a/msbuild/Xamarin.MacDev.Tasks/Xamarin.MacDev.Tasks.csproj b/msbuild/Xamarin.MacDev.Tasks/Xamarin.MacDev.Tasks.csproj index 899dfdeb2160..e30b3f4f22d1 100644 --- a/msbuild/Xamarin.MacDev.Tasks/Xamarin.MacDev.Tasks.csproj +++ b/msbuild/Xamarin.MacDev.Tasks/Xamarin.MacDev.Tasks.csproj @@ -14,6 +14,7 @@ $(NoWarn);NU1701 $(NoWarn);MSB3277 $(NoWarn);8002 + $(NoWarn);CS0618 enable @@ -21,7 +22,7 @@ - +