Skip to content

Commit

Permalink
Add ExecuteAdbCommand to run adb command
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Jan 29, 2024
1 parent f4ca666 commit 5d3462c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
28 changes: 22 additions & 6 deletions AdvancedSharpAdbClient/AdbCommandLineClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ public virtual async Task<AdbCommandLineStatus> GetVersionAsync(CancellationToke
return version;
}

/// <inheritdoc/>
public virtual async Task<List<string>> ExecuteAdbCommandAsync(string command, CancellationToken cancellationToken = default)
{
List<string> errorOutput = [];
List<string> standardOutput = [];
int status = await RunAdbProcessInnerAsync(command, errorOutput, standardOutput, cancellationToken).ConfigureAwait(false);
if (errorOutput.Count > 0)
{
string error = StringExtensions.Join(Environment.NewLine, errorOutput!);
throw new AdbException($"The adb process returned error code {status} when running command {command} with error output:{Environment.NewLine}{error}", error);
}
else if (status != 0) { throw new AdbException($"The adb process returned error code {status} when running command {command}"); }
else { return standardOutput; }
}

/// <inheritdoc/>
public virtual async Task StartServerAsync(CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -147,12 +162,6 @@ protected virtual async Task<int> RunProcessAsync(string filename, string comman

using Process process = Process.Start(psi) ?? throw new AdbException($"The adb process could not be started. The process returned null when starting {filename} {command}");

string standardErrorString = await process.StandardError.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
string standardOutputString = await process.StandardOutput.ReadToEndAsync(cancellationToken).ConfigureAwait(false);

errorOutput?.AddRange(standardErrorString.Split(separator, StringSplitOptions.RemoveEmptyEntries));
standardOutput?.AddRange(standardOutputString.Split(separator, StringSplitOptions.RemoveEmptyEntries));

#if NET5_0_OR_GREATER
using (CancellationTokenSource completionSource = new(TimeSpan.FromMilliseconds(5000)))
{
Expand All @@ -174,6 +183,13 @@ protected virtual async Task<int> RunProcessAsync(string filename, string comman
process.Kill();
}
#endif

string standardErrorString = await process.StandardError.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
string standardOutputString = await process.StandardOutput.ReadToEndAsync(cancellationToken).ConfigureAwait(false);

errorOutput?.AddRange(standardErrorString.Split(separator, StringSplitOptions.RemoveEmptyEntries));
standardOutput?.AddRange(standardOutputString.Split(separator, StringSplitOptions.RemoveEmptyEntries));

// get the return code from the process
return process.ExitCode;
#else
Expand Down
27 changes: 21 additions & 6 deletions AdvancedSharpAdbClient/AdbCommandLineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ public virtual void StartServer()
RunAdbProcess("start-server", null, null);
}

/// <inheritdoc/>
public virtual List<string> ExecuteAdbCommand(string command)
{
List<string> errorOutput = [];
List<string> standardOutput = [];
int status = RunAdbProcessInner(command, errorOutput, standardOutput);
if (errorOutput.Count > 0)
{
string error = StringExtensions.Join(Environment.NewLine, errorOutput!);
throw new AdbException($"The adb process returned error code {status} when running command {command} with error output:{Environment.NewLine}{error}", error);
}
else if (status != 0) { throw new AdbException($"The adb process returned error code {status} when running command {command}"); }
else { return standardOutput; }
}

/// <inheritdoc/>
public virtual bool CheckAdbFileExists(string adbPath) => adbPath == "adb" ||
#if WINDOWS_UWP
Expand Down Expand Up @@ -219,18 +234,18 @@ protected virtual int RunProcess(string filename, string command, ICollection<st

using Process process = Process.Start(psi) ?? throw new AdbException($"The adb process could not be started. The process returned null when starting {filename} {command}");

string standardErrorString = process.StandardError.ReadToEnd();
string standardOutputString = process.StandardOutput.ReadToEnd();

errorOutput?.AddRange(standardErrorString.Split(separator, StringSplitOptions.RemoveEmptyEntries));
standardOutput?.AddRange(standardOutputString.Split(separator, StringSplitOptions.RemoveEmptyEntries));

// get the return code from the process
if (!process.WaitForExit(5000))
{
process.Kill();
}

string standardErrorString = process.StandardError.ReadToEnd();
string standardOutputString = process.StandardOutput.ReadToEnd();

errorOutput?.AddRange(standardErrorString.Split(separator, StringSplitOptions.RemoveEmptyEntries));
standardOutput?.AddRange(standardOutputString.Split(separator, StringSplitOptions.RemoveEmptyEntries));

return process.ExitCode;
#else
throw new PlatformNotSupportedException("This platform is not support System.Diagnostics.Process. You can start adb server by running `adb start-server` manually.");
Expand Down
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient/AdvancedSharpAdbClient.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<FullTargets>False</FullTargets>
<FullTargets>True</FullTargets>
<ImportAsync>True</ImportAsync>
<Nullable>Enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere. All rights reserved.
// </copyright>

using System.Collections.Generic;
using System.Threading;

namespace AdvancedSharpAdbClient
Expand All @@ -23,6 +24,14 @@ public partial interface IAdbCommandLineClient
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
Task StartServerAsync(CancellationToken cancellationToken);

/// <summary>
/// Asynchronously runs the <c>adb.exe</c> process, invoking a specific <paramref name="command"/>, and reads the standard output.
/// </summary>
/// <param name="command">The <c>adb.exe</c> command to invoke, such as <c>version</c> or <c>start-server</c>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.</param>
/// <returns>A <see cref="Task"/> which return a list in which to store the standard output. Each line is added as a new entry.</returns>
Task<List<string>> ExecuteAdbCommandAsync(string command, CancellationToken cancellationToken);

/// <summary>
/// Determines whether the <c>adb.exe</c> file exists.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions AdvancedSharpAdbClient/Interfaces/IAdbCommandLineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere. All rights reserved.
// </copyright>

using System.Collections.Generic;

namespace AdvancedSharpAdbClient
{
/// <summary>
Expand All @@ -20,6 +22,13 @@ public partial interface IAdbCommandLineClient
/// </summary>
void StartServer();

/// <summary>
/// Runs the <c>adb.exe</c> process, invoking a specific <paramref name="command"/>, and reads the standard output.
/// </summary>
/// <param name="command">The <c>adb.exe</c> command to invoke, such as <c>version</c> or <c>start-server</c>.</param>
/// <return>A list in which to store the standard output. Each line is added as a new entry.</return>
List<string> ExecuteAdbCommand(string command);

/// <summary>
/// Determines whether the <c>adb.exe</c> file exists.
/// </summary>
Expand Down

0 comments on commit 5d3462c

Please sign in to comment.