|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | + |
| 7 | +using Xamarin.MacDev.Models; |
| 8 | + |
| 9 | +#nullable enable |
| 10 | + |
| 11 | +namespace Xamarin.MacDev { |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Orchestrates Apple development environment setup. Checks the current |
| 15 | + /// state via <see cref="EnvironmentChecker"/> and installs missing |
| 16 | + /// components (Command Line Tools, Xcode first-launch packages, and |
| 17 | + /// simulator runtimes). |
| 18 | + /// </summary> |
| 19 | + public class AppleInstaller { |
| 20 | + |
| 21 | + readonly ICustomLogger log; |
| 22 | + |
| 23 | + public AppleInstaller (ICustomLogger log) |
| 24 | + { |
| 25 | + this.log = log ?? throw new ArgumentNullException (nameof (log)); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Ensures the Apple development environment is ready. |
| 30 | + /// When <paramref name="dryRun"/> is true, reports what would be |
| 31 | + /// installed without making any changes. |
| 32 | + /// Returns the final <see cref="EnvironmentCheckResult"/>. |
| 33 | + /// </summary> |
| 34 | + /// <param name="requestedPlatforms"> |
| 35 | + /// Platforms to ensure runtimes for (e.g. "iOS", "tvOS"). |
| 36 | + /// If null or empty, only existing runtimes are verified. |
| 37 | + /// </param> |
| 38 | + /// <param name="dryRun"> |
| 39 | + /// When true, logs planned actions but does not execute them. |
| 40 | + /// </param> |
| 41 | + public EnvironmentCheckResult Install (IEnumerable<string>? requestedPlatforms = null, bool dryRun = false) |
| 42 | + { |
| 43 | + var checker = new EnvironmentChecker (log); |
| 44 | + |
| 45 | + // 1. Initial check |
| 46 | + log.LogInfo ("Running initial environment check..."); |
| 47 | + var result = checker.Check (); |
| 48 | + |
| 49 | + // 2. Ensure Command Line Tools |
| 50 | + EnsureCommandLineTools (result.CommandLineTools, dryRun); |
| 51 | + |
| 52 | + // 3. Ensure Xcode first-launch packages |
| 53 | + if (result.Xcode is not null) |
| 54 | + EnsureFirstLaunch (checker, result.Xcode.Path, dryRun); |
| 55 | + else |
| 56 | + log.LogInfo ("No Xcode found — skipping first-launch check."); |
| 57 | + |
| 58 | + // 4. Ensure requested runtimes |
| 59 | + if (requestedPlatforms is not null) |
| 60 | + EnsureRuntimes (result, requestedPlatforms, dryRun); |
| 61 | + |
| 62 | + // 5. Re-check and return |
| 63 | + if (!dryRun) { |
| 64 | + log.LogInfo ("Running final environment check..."); |
| 65 | + result = checker.Check (); |
| 66 | + } |
| 67 | + |
| 68 | + log.LogInfo ("Install complete. Status: {0}.", result.Status); |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Triggers CLT installation if not already present. |
| 74 | + /// Uses <c>xcode-select --install</c> to launch the macOS installer UI. |
| 75 | + /// </summary> |
| 76 | + void EnsureCommandLineTools (CommandLineToolsInfo clt, bool dryRun) |
| 77 | + { |
| 78 | + if (clt.IsInstalled) { |
| 79 | + log.LogInfo ("Command Line Tools already installed (v{0}).", clt.Version); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (dryRun) { |
| 84 | + log.LogInfo ("[DRY RUN] Would trigger Command Line Tools installation."); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + log.LogInfo ("Command Line Tools not found. Triggering installation..."); |
| 89 | + try { |
| 90 | + // xcode-select --install triggers the macOS installer dialog |
| 91 | + var (exitCode, _, stderr) = ProcessUtils.Exec ("xcode-select", "--install"); |
| 92 | + if (exitCode == 0) |
| 93 | + log.LogInfo ("Command Line Tools installer triggered. Complete the dialog to continue."); |
| 94 | + else |
| 95 | + log.LogInfo ("xcode-select --install failed (exit {0}): {1}", exitCode, stderr.Trim ()); |
| 96 | + } catch (System.ComponentModel.Win32Exception ex) { |
| 97 | + log.LogInfo ("Could not run xcode-select: {0}", ex.Message); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Ensures Xcode first-launch packages are installed. |
| 103 | + /// </summary> |
| 104 | + void EnsureFirstLaunch (EnvironmentChecker checker, string xcodePath, bool dryRun) |
| 105 | + { |
| 106 | + if (dryRun) { |
| 107 | + log.LogInfo ("[DRY RUN] Would run xcodebuild -runFirstLaunch."); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + checker.RunFirstLaunch (xcodePath); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Ensures simulator runtimes are available for the requested platforms. |
| 116 | + /// Downloads missing runtimes via <see cref="RuntimeService.DownloadPlatform"/>. |
| 117 | + /// </summary> |
| 118 | + void EnsureRuntimes (EnvironmentCheckResult result, IEnumerable<string> requestedPlatforms, bool dryRun) |
| 119 | + { |
| 120 | + // Build a set of available runtime platforms |
| 121 | + var available = new HashSet<string> (StringComparer.OrdinalIgnoreCase); |
| 122 | + foreach (var rt in result.Runtimes) { |
| 123 | + if (!string.IsNullOrEmpty (rt.Platform)) |
| 124 | + available.Add (rt.Platform); |
| 125 | + } |
| 126 | + |
| 127 | + var runtimeService = new RuntimeService (log); |
| 128 | + |
| 129 | + foreach (var platform in requestedPlatforms) { |
| 130 | + if (available.Contains (platform)) { |
| 131 | + log.LogInfo ("Runtime for '{0}' is already available.", platform); |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + if (dryRun) { |
| 136 | + log.LogInfo ("[DRY RUN] Would download runtime for '{0}'.", platform); |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + log.LogInfo ("Downloading runtime for '{0}'...", platform); |
| 141 | + runtimeService.DownloadPlatform (platform); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments