|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +using Xamarin.MacDev.Models; |
| 9 | + |
| 10 | +#nullable enable |
| 11 | + |
| 12 | +namespace Xamarin.MacDev { |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Performs a comprehensive check of the Apple development environment. |
| 16 | + /// Aggregates results from <see cref="CommandLineTools"/>, |
| 17 | + /// <see cref="XcodeManager"/>, and <see cref="RuntimeService"/>. |
| 18 | + /// Also validates Xcode license acceptance and first-launch state, |
| 19 | + /// patterns from ClientTools.Platform iOSSshCommandsExtensions. |
| 20 | + /// </summary> |
| 21 | + public class EnvironmentChecker { |
| 22 | + |
| 23 | + readonly ICustomLogger log; |
| 24 | + |
| 25 | + public EnvironmentChecker (ICustomLogger log) |
| 26 | + { |
| 27 | + this.log = log ?? throw new ArgumentNullException (nameof (log)); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Runs a full environment check and returns the results. |
| 32 | + /// </summary> |
| 33 | + public EnvironmentCheckResult Check () |
| 34 | + { |
| 35 | + var result = new EnvironmentCheckResult (); |
| 36 | + |
| 37 | + // 1. Check Xcode |
| 38 | + var xcodeManager = new XcodeManager (log); |
| 39 | + var xcode = xcodeManager.GetBest (); |
| 40 | + result.Xcode = xcode; |
| 41 | + |
| 42 | + if (xcode is not null) { |
| 43 | + log.LogInfo ("Xcode {0} found at '{1}'.", xcode.Version, xcode.Path); |
| 44 | + |
| 45 | + // Check license acceptance (pattern from ClientTools.Platform) |
| 46 | + if (IsXcodeLicenseAccepted (xcode.Path)) |
| 47 | + log.LogInfo ("Xcode license is accepted."); |
| 48 | + else |
| 49 | + log.LogInfo ("Xcode license may not be accepted. Run 'sudo xcodebuild -license accept'."); |
| 50 | + |
| 51 | + // Collect platform SDKs |
| 52 | + result.Platforms = GetPlatforms (xcode.Path); |
| 53 | + } else { |
| 54 | + log.LogInfo ("No Xcode installation found."); |
| 55 | + } |
| 56 | + |
| 57 | + // 2. Check Command Line Tools |
| 58 | + var clt = new CommandLineTools (log); |
| 59 | + result.CommandLineTools = clt.Check (); |
| 60 | + |
| 61 | + // 3. Check runtimes |
| 62 | + var runtimeService = new RuntimeService (log); |
| 63 | + result.Runtimes = runtimeService.List (availableOnly: true); |
| 64 | + |
| 65 | + // 4. Derive overall status |
| 66 | + result.DeriveStatus (); |
| 67 | + |
| 68 | + log.LogInfo ("Environment check complete. Status: {0}.", result.Status); |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Checks whether the Xcode license has been accepted by running |
| 74 | + /// <c>xcodebuild -license check</c>. |
| 75 | + /// Pattern from ClientTools.Platform iOSSshCommandsExtensions.CheckXcodeLicenseAsync. |
| 76 | + /// </summary> |
| 77 | + public bool IsXcodeLicenseAccepted (string xcodePath) |
| 78 | + { |
| 79 | + var xcodebuildPath = Path.Combine (xcodePath, "Contents", "Developer", "usr", "bin", "xcodebuild"); |
| 80 | + if (!File.Exists (xcodebuildPath)) |
| 81 | + return false; |
| 82 | + |
| 83 | + try { |
| 84 | + var (exitCode, _, _) = ProcessUtils.Exec (xcodebuildPath, "-license", "check"); |
| 85 | + return exitCode == 0; |
| 86 | + } catch (System.ComponentModel.Win32Exception) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Runs <c>xcodebuild -runFirstLaunch</c> to ensure packages are installed. |
| 93 | + /// Pattern from ClientTools.Platform iOSSshCommandsExtensions.RunXcodeBuildFirstLaunchAsync. |
| 94 | + /// Returns true if the command succeeded. |
| 95 | + /// </summary> |
| 96 | + public bool RunFirstLaunch (string xcodePath) |
| 97 | + { |
| 98 | + var xcodebuildPath = Path.Combine (xcodePath, "Contents", "Developer", "usr", "bin", "xcodebuild"); |
| 99 | + if (!File.Exists (xcodebuildPath)) { |
| 100 | + log.LogInfo ("xcodebuild not found at '{0}'.", xcodebuildPath); |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + log.LogInfo ("Running xcodebuild -runFirstLaunch..."); |
| 106 | + var (exitCode, _, stderr) = ProcessUtils.Exec (xcodebuildPath, "-runFirstLaunch"); |
| 107 | + if (exitCode != 0) { |
| 108 | + log.LogInfo ("xcodebuild -runFirstLaunch failed (exit {0}): {1}", exitCode, stderr.Trim ()); |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + log.LogInfo ("xcodebuild -runFirstLaunch completed successfully."); |
| 113 | + return true; |
| 114 | + } catch (System.ComponentModel.Win32Exception ex) { |
| 115 | + log.LogInfo ("Could not run xcodebuild: {0}", ex.Message); |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Gets the list of available platform SDK directories in the Xcode bundle. |
| 122 | + /// </summary> |
| 123 | + System.Collections.Generic.List<string> GetPlatforms (string xcodePath) |
| 124 | + { |
| 125 | + var platforms = new System.Collections.Generic.List<string> (); |
| 126 | + var platformsDir = Path.Combine (xcodePath, "Contents", "Developer", "Platforms"); |
| 127 | + |
| 128 | + if (!Directory.Exists (platformsDir)) |
| 129 | + return platforms; |
| 130 | + |
| 131 | + try { |
| 132 | + foreach (var dir in Directory.GetDirectories (platformsDir, "*.platform")) { |
| 133 | + var name = Path.GetFileNameWithoutExtension (dir); |
| 134 | + // Convert "iPhoneOS" to "iOS", "AppleTVOS" to "tvOS", etc. |
| 135 | + var friendly = MapPlatformName (name); |
| 136 | + if (!platforms.Contains (friendly)) |
| 137 | + platforms.Add (friendly); |
| 138 | + } |
| 139 | + } catch (UnauthorizedAccessException ex) { |
| 140 | + log.LogInfo ("Could not read platforms directory: {0}", ex.Message); |
| 141 | + } |
| 142 | + |
| 143 | + return platforms; |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Maps Apple platform directory names to friendly names. |
| 148 | + /// </summary> |
| 149 | + public static string MapPlatformName (string sdkName) |
| 150 | + { |
| 151 | + switch (sdkName) { |
| 152 | + case "iPhoneOS": |
| 153 | + case "iPhoneSimulator": |
| 154 | + return "iOS"; |
| 155 | + case "AppleTVOS": |
| 156 | + case "AppleTVSimulator": |
| 157 | + return "tvOS"; |
| 158 | + case "WatchOS": |
| 159 | + case "WatchSimulator": |
| 160 | + return "watchOS"; |
| 161 | + case "XROS": |
| 162 | + case "XRSimulator": |
| 163 | + return "visionOS"; |
| 164 | + case "MacOSX": |
| 165 | + return "macOS"; |
| 166 | + default: |
| 167 | + return sdkName; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments