Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Xamarin.MacDev/RuntimeService.cs
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;
}
}
}
52 changes: 52 additions & 0 deletions tests/RuntimeServiceTests.cs
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")]
Copy link
Copy Markdown
Member

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 (or macOS) - would any of those work?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NUnit's PlatformAttribute uses MacOsX as the official value (case-insensitive per NUnit docs). Both MacOSX and MacOsX resolve to the same thing at runtime. Keeping MacOsX to match NUnit's documented convention.

public void List_DoesNotThrow ()
{
var svc = new RuntimeService (ConsoleLogger.Instance);
Assert.DoesNotThrow (() => svc.List ());
}

[Test]
[Platform ("MacOsX")]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same — keeping MacOsX per NUnit convention (case-insensitive matching).

public void ListByPlatform_DoesNotThrow ()
{
var svc = new RuntimeService (ConsoleLogger.Instance);
Assert.DoesNotThrow (() => svc.ListByPlatform ("iOS"));
}
}