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