-
Notifications
You must be signed in to change notification settings - Fork 28
Add runtime management APIs #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Xamarin.MacDev.Models; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Xamarin.MacDev; | ||
|
|
||
| /// <summary> | ||
| /// Manages simulator runtimes via <c>xcrun simctl</c> and <c>xcrun xcodebuild</c>. | ||
| /// </summary> | ||
| public class RuntimeService { | ||
|
|
||
| static readonly string XcrunPath = "/usr/bin/xcrun"; | ||
|
|
||
| readonly ICustomLogger log; | ||
| readonly SimCtl simctl; | ||
|
|
||
| public RuntimeService (ICustomLogger log) | ||
| { | ||
| this.log = log ?? throw new ArgumentNullException (nameof (log)); | ||
| simctl = new SimCtl (log); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Lists installed simulator runtimes. Optionally filters by availability. | ||
| /// </summary> | ||
| public List<SimulatorRuntimeInfo> List (bool availableOnly = false) | ||
| { | ||
| var json = simctl.Run ("list", "runtimes", "--json"); | ||
| if (json is null) | ||
| return new List<SimulatorRuntimeInfo> (); | ||
|
|
||
| var runtimes = SimctlOutputParser.ParseRuntimes (json, log); | ||
|
|
||
| if (availableOnly) | ||
| runtimes.RemoveAll (r => !r.IsAvailable); | ||
|
|
||
| log.LogInfo ("Found {0} simulator runtime(s).", runtimes.Count); | ||
| return runtimes; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Lists runtimes for a specific platform (e.g. "iOS", "tvOS", "watchOS", "visionOS"). | ||
| /// </summary> | ||
| public List<SimulatorRuntimeInfo> ListByPlatform (string platform, bool availableOnly = false) | ||
| { | ||
| if (string.IsNullOrEmpty (platform)) | ||
| throw new ArgumentException ("Platform must not be null or empty.", nameof (platform)); | ||
|
|
||
| var all = List (availableOnly); | ||
| return all.Where (r => string.Equals (r.Platform, platform, StringComparison.OrdinalIgnoreCase)).ToList (); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Downloads a platform runtime using <c>xcrun xcodebuild -downloadPlatform</c>. | ||
| /// </summary> | ||
| /// <param name="platform">The platform to download (e.g. "iOS", "tvOS", "watchOS", "visionOS").</param> | ||
| /// <param name="version">Optional specific version to download (e.g. "17.5").</param> | ||
| /// <returns>True if the download command succeeded.</returns> | ||
| public bool DownloadPlatform (string platform, string? version = null) | ||
| { | ||
| if (string.IsNullOrEmpty (platform)) | ||
| throw new ArgumentException ("Platform must not be null or empty.", nameof (platform)); | ||
|
|
||
| log.LogInfo ("Downloading {0} platform runtime via xcodebuild...", platform); | ||
|
|
||
| try { | ||
| var args = string.IsNullOrEmpty (version) | ||
| ? new [] { "xcodebuild", "-downloadPlatform", platform } | ||
| : new [] { "xcodebuild", "-downloadPlatform", platform, "-buildVersion", version! }; | ||
|
|
||
| log.LogInfo ("Executing: {0} {1}", XcrunPath, string.Join (" ", args)); | ||
| var (exitCode, _, stderr) = ProcessUtils.Exec (XcrunPath, args); | ||
| if (exitCode != 0) { | ||
| log.LogInfo ("xcodebuild -downloadPlatform {0} failed (exit {1}): {2}", platform, exitCode, stderr.Trim ()); | ||
| return false; | ||
| } | ||
|
|
||
| log.LogInfo ("Successfully downloaded {0} platform runtime.", platform); | ||
| return true; | ||
| } catch (System.ComponentModel.Win32Exception ex) { | ||
| log.LogInfo ("Could not run xcodebuild: {0}", ex.Message); | ||
| return false; | ||
| } catch (InvalidOperationException ex) { | ||
| log.LogInfo ("Could not run xcodebuild: {0}", ex.Message); | ||
| return false; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using NUnit.Framework; | ||
| using Xamarin.MacDev; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace tests; | ||
|
|
||
| [TestFixture] | ||
| public class RuntimeServiceTests { | ||
|
|
||
| [Test] | ||
| public void Constructor_ThrowsOnNullLogger () | ||
| { | ||
| Assert.Throws<ArgumentNullException> (() => new RuntimeService (null!)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ListByPlatform_ThrowsOnNullPlatform () | ||
| { | ||
| var svc = new RuntimeService (ConsoleLogger.Instance); | ||
| Assert.Throws<ArgumentException> (() => svc.ListByPlatform (null!)); | ||
| Assert.Throws<ArgumentException> (() => svc.ListByPlatform ("")); | ||
| } | ||
|
|
||
| [Test] | ||
| public void DownloadPlatform_ThrowsOnNullPlatform () | ||
| { | ||
| var svc = new RuntimeService (ConsoleLogger.Instance); | ||
| Assert.Throws<ArgumentException> (() => svc.DownloadPlatform (null!)); | ||
| Assert.Throws<ArgumentException> (() => svc.DownloadPlatform ("")); | ||
| } | ||
|
|
||
| [Test] | ||
| [Platform ("MacOsX")] | ||
| public void List_DoesNotThrow () | ||
| { | ||
| var svc = new RuntimeService (ConsoleLogger.Instance); | ||
| Assert.DoesNotThrow (() => svc.List ()); | ||
| } | ||
|
|
||
| [Test] | ||
| [Platform ("MacOsX")] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same — keeping |
||
| public void ListByPlatform_DoesNotThrow () | ||
| { | ||
| var svc = new RuntimeService (ConsoleLogger.Instance); | ||
| Assert.DoesNotThrow (() => svc.ListByPlatform ("iOS")); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct case would be:
MacOSX(ormacOS) - would any of those work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NUnit's
PlatformAttributeusesMacOsXas the official value (case-insensitive per NUnit docs). BothMacOSXandMacOsXresolve to the same thing at runtime. KeepingMacOsXto match NUnit's documented convention.