Skip to content

Commit d3c269d

Browse files
rmarinhoCopilotjonathanpeppers
authored
Add AdbRunner for adb CLI operations (#283)
## Summary Wraps `adb` CLI operations for device management. Addresses #279. Parsing/formatting/merging logic ported from `dotnet/android` `GetAvailableAndroidDevices` MSBuild task, enabling code sharing via the `external/xamarin-android-tools` submodule. See draft PR: dotnet/android#10880 ## Public API ```csharp public class AdbRunner { // Constructor — requires full path to adb executable public AdbRunner(string adbPath, IDictionary<string, string>? environmentVariables = null); // Instance methods (async, invoke adb process) public Task<IReadOnlyList<AdbDeviceInfo>> ListDevicesAsync(CancellationToken ct = default); public Task WaitForDeviceAsync(string? serial = null, TimeSpan? timeout = null, CancellationToken ct = default); public Task StopEmulatorAsync(string serial, CancellationToken ct = default); // Static helpers — public so dotnet/android can call without instantiating AdbRunner public static List<AdbDeviceInfo> ParseAdbDevicesOutput(IEnumerable<string> lines); public static AdbDeviceStatus MapAdbStateToStatus(string adbState); public static string BuildDeviceDescription(AdbDeviceInfo device, Action<TraceLevel, string>? logger = null); public static string FormatDisplayName(string avdName); public static List<AdbDeviceInfo> MergeDevicesAndEmulators( IReadOnlyList<AdbDeviceInfo> adbDevices, IReadOnlyList<string> availableEmulators, Action<TraceLevel, string>? logger = null); } ``` **Internal methods** (not part of public API): - `GetEmulatorAvdNameAsync` — queries AVD name via `adb emu avd name` with TCP console fallback - `ProcessUtils.ThrowIfFailed` — shared exit code validation (string and StringWriter overloads) ## Key Design Decisions - **Constructor takes `string adbPath`**: Callers pass the resolved path; no lazy `Func<>` indirection. Optional `environmentVariables` dictionary for ANDROID_HOME/JAVA_HOME/PATH. - **Static parsing methods** are `public static` so `dotnet/android` can call them without instantiating `AdbRunner` (e.g., `GetAvailableAndroidDevices` MSBuild task passes `List<string>` to `ParseAdbDevicesOutput`) - **`IEnumerable<string>` overload**: `dotnet/android` passes `List<string>` directly from output lines - **Logger parameter**: `BuildDeviceDescription` and `MergeDevicesAndEmulators` accept `Action<TraceLevel, string>?` — `dotnet/android` passes `this.CreateTaskLogger()` for MSBuild trace output - **Regex with explicit state list**: Uses `\s+` separator to match one or more whitespace characters (spaces or tabs). Matches explicit known states with `IgnoreCase`. Daemon startup lines (`*`) are pre-filtered. - **Exit code checking**: `ListDevicesAsync`, `WaitForDeviceAsync`, and `StopEmulatorAsync` throw `InvalidOperationException` with stderr context on non-zero exit via `ProcessUtils.ThrowIfFailed` (internal) - **`MapAdbStateToStatus` as switch expression**: Simple value mapping uses C# switch expression for conciseness - **Property patterns instead of null-forgiving**: Uses `is { Length: > 0 }` patterns throughout for null checks on `netstandard2.0` where `string.IsNullOrEmpty()` lacks `[NotNullWhen(false)]` - **FormatDisplayName**: Lowercases before `ToTitleCase` to normalize mixed-case input (e.g., "PiXeL" → "Pixel") - **Environment variables via `StartProcess`**: Runners pass env vars dictionary to `ProcessUtils.StartProcess`. `AndroidEnvironmentHelper.GetEnvironmentVariables()` builds the dict. ## Tests **45 unit tests** (`AdbRunnerTests.cs`): - **ParseAdbDevicesOutput**: real-world data, empty output, single/multiple devices, mixed states, daemon messages, IP:port, Windows newlines, recovery/sideload, tab-separated output - **FormatDisplayName**: underscores, title case, API capitalization, mixed case, special chars, empty - **MapAdbStateToStatus**: all known states + unknown (recovery, sideload) - **MergeDevicesAndEmulators**: no emulators, no running, mixed, case-insensitive dedup, sorting - **Constructor**: valid path, null/empty throws - **WaitForDeviceAsync**: timeout validation (negative, zero) **4 integration tests** (`RunnerIntegrationTests.cs`): - Run only when `TF_BUILD=True` or `CI=true` (case-insensitive truthy check), skipped locally - Require pre-installed JDK (`JAVA_HOME`) and Android SDK (`ANDROID_HOME`) on CI agent - `Assert.Ignore` when `ANDROID_HOME` missing (no bootstrap/network dependency) - Cover: constructor, `ListDevicesAsync`, `WaitForDeviceAsync` timeout, tool discovery Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
1 parent f4d2f2d commit d3c269d

File tree

12 files changed

+1244
-19
lines changed

12 files changed

+1244
-19
lines changed

.github/copilot-instructions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ When setting environment variables for SDK tools (e.g. `sdkmanager`, `avdmanager
4949
- **File-scoped namespaces**: all new files should use file-scoped namespaces (`namespace Foo;` instead of `namespace Foo { ... }`).
5050
- **Static `HttpClient`**: `HttpClient` instances must be `static` to avoid socket exhaustion. See [HttpClient guidelines](https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use). Do not create per-instance `HttpClient` fields or dispose them in `IDisposable`.
5151
- [Mono Coding Guidelines](http://www.mono-project.com/community/contributing/coding-guidelines/): tabs, K&R braces, `PascalCase` public members.
52+
- **No null-forgiving operator (`!`)**: do not use the null-forgiving operator after null checks. Instead, use C# property patterns (e.g. `if (value is { Length: > 0 } v)`) which give the compiler proper non-null flow analysis on all target frameworks including `netstandard2.0`.
53+
- **Prefer switch expressions**: use C# switch expressions over switch statements for simple value mappings (e.g. `return state switch { "x" => A, _ => B };`). Use switch statements only when the body has side effects or complex logic.
5254
- Nullable enabled in `AndroidSdk`. `NullableAttributes.cs` excluded on `net10.0+`.
5355
- Strong-named via `product.snk`. In the AndroidSdk project, tests use `InternalsVisibleTo` with full public key (`Properties/AssemblyInfo.cs`).
5456
- Assembly names support `$(VendorPrefix)`/`$(VendorSuffix)` for branding forks.

src/Xamarin.Android.Tools.AndroidSdk/EnvironmentVariableNames.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,17 @@ internal static class EnvironmentVariableNames
4040
/// Executable file extensions (Windows).
4141
/// </summary>
4242
public const string PathExt = "PATHEXT";
43+
44+
/// <summary>
45+
/// Overrides the default location for Android user-specific data
46+
/// (AVDs, preferences, etc.). Defaults to $HOME/.android.
47+
/// </summary>
48+
public const string AndroidUserHome = "ANDROID_USER_HOME";
49+
50+
/// <summary>
51+
/// Overrides the AVD storage directory. Takes precedence over
52+
/// <see cref="AndroidUserHome"/>/avd.
53+
/// </summary>
54+
public const string AndroidAvdHome = "ANDROID_AVD_HOME";
4355
}
4456
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Xamarin.Android.Tools;
5+
6+
/// <summary>
7+
/// Represents an Android device or emulator from 'adb devices -l' output.
8+
/// Mirrors the metadata produced by dotnet/android's GetAvailableAndroidDevices task.
9+
/// </summary>
10+
public class AdbDeviceInfo
11+
{
12+
/// <summary>
13+
/// Serial number of the device (e.g., "emulator-5554", "0A041FDD400327").
14+
/// For non-running emulators, this is the AVD name.
15+
/// </summary>
16+
public string Serial { get; set; } = string.Empty;
17+
18+
/// <summary>
19+
/// Human-friendly description of the device (e.g., "Pixel 7 API 35", "Pixel 6 Pro").
20+
/// </summary>
21+
public string Description { get; set; } = string.Empty;
22+
23+
/// <summary>
24+
/// Device type: Device or Emulator.
25+
/// </summary>
26+
public AdbDeviceType Type { get; set; }
27+
28+
/// <summary>
29+
/// Device status: Online, Offline, Unauthorized, NoPermissions, NotRunning, Unknown.
30+
/// </summary>
31+
public AdbDeviceStatus Status { get; set; }
32+
33+
/// <summary>
34+
/// AVD name for emulators (e.g., "pixel_7_api_35"). Null for physical devices.
35+
/// </summary>
36+
public string? AvdName { get; set; }
37+
38+
/// <summary>
39+
/// Device model from adb properties (e.g., "Pixel_6_Pro").
40+
/// </summary>
41+
public string? Model { get; set; }
42+
43+
/// <summary>
44+
/// Product name from adb properties (e.g., "raven").
45+
/// </summary>
46+
public string? Product { get; set; }
47+
48+
/// <summary>
49+
/// Device code name from adb properties (e.g., "raven").
50+
/// </summary>
51+
public string? Device { get; set; }
52+
53+
/// <summary>
54+
/// Transport ID from adb properties.
55+
/// </summary>
56+
public string? TransportId { get; set; }
57+
58+
/// <summary>
59+
/// Whether this device is an emulator.
60+
/// </summary>
61+
public bool IsEmulator => Type == AdbDeviceType.Emulator;
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Xamarin.Android.Tools;
5+
6+
/// <summary>
7+
/// Represents the status of an Android device.
8+
/// </summary>
9+
public enum AdbDeviceStatus
10+
{
11+
Online,
12+
Offline,
13+
Unauthorized,
14+
NoPermissions,
15+
NotRunning,
16+
Unknown
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Xamarin.Android.Tools;
5+
6+
/// <summary>
7+
/// Represents the type of an Android device.
8+
/// </summary>
9+
public enum AdbDeviceType
10+
{
11+
Device,
12+
Emulator
13+
}

src/Xamarin.Android.Tools.AndroidSdk/ProcessUtils.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,33 @@ static string JoinArguments (string[] args)
203203
}
204204
#endif
205205

206+
/// <summary>
207+
/// Throws <see cref="InvalidOperationException"/> when <paramref name="exitCode"/> is non-zero.
208+
/// Includes stderr/stdout context in the message when available.
209+
/// </summary>
210+
internal static void ThrowIfFailed (int exitCode, string command, string? stderr = null, string? stdout = null)
211+
{
212+
if (exitCode == 0)
213+
return;
214+
215+
var message = $"'{command}' failed with exit code {exitCode}.";
216+
217+
if (stderr is { Length: > 0 })
218+
message += $" stderr:{Environment.NewLine}{stderr.Trim ()}";
219+
if (stdout is { Length: > 0 })
220+
message += $" stdout:{Environment.NewLine}{stdout.Trim ()}";
221+
222+
throw new InvalidOperationException (message);
223+
}
224+
225+
/// <summary>
226+
/// Overload that accepts <see cref="StringWriter"/> directly so callers don't need to call ToString().
227+
/// </summary>
228+
internal static void ThrowIfFailed (int exitCode, string command, StringWriter? stderr = null, StringWriter? stdout = null)
229+
{
230+
ThrowIfFailed (exitCode, command, stderr?.ToString (), stdout?.ToString ());
231+
}
232+
206233
internal static IEnumerable<string> FindExecutablesInPath (string executable)
207234
{
208235
var path = Environment.GetEnvironmentVariable (EnvironmentVariableNames.Path) ?? "";

0 commit comments

Comments
 (0)