Skip to content

Commit 19ecf8a

Browse files
committed
Add ExecuteShellCommandAsync in DeviceExtensions
1 parent d46f4bb commit 19ecf8a

File tree

11 files changed

+86
-50
lines changed

11 files changed

+86
-50
lines changed

AdvancedSharpAdbClient.Tests/DeviceCommands/DeviceExtensionsTests.Async.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public async void ExecuteServerCommandAsyncTest()
1414
{
1515
const string command = nameof(command);
1616
IAdbSocket socket = Substitute.For<IAdbSocket>();
17-
IShellOutputReceiver receiver = Substitute.For<IShellOutputReceiver>();
17+
static bool predicate(string x) => true;
18+
IShellOutputReceiver receiver = new FunctionOutputReceiver(predicate);
1819
List<string> result = ["Hello", "World", "!"];
1920

2021
IAdbClient client = Substitute.For<IAdbClient>();
@@ -39,6 +40,7 @@ public async void ExecuteServerCommandAsyncTest()
3940

4041
await client.ExecuteShellCommandAsync(Device, command);
4142
await client.ExecuteShellCommandAsync(Device, command, receiver);
43+
await client.ExecuteShellCommandAsync(Device, command, predicate);
4244
}
4345

4446
/// <summary>

AdvancedSharpAdbClient.Tests/DeviceCommands/DeviceExtensionsTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public partial class DeviceExtensionsTests
2020
public void ExecuteServerCommandTest()
2121
{
2222
const string command = nameof(command);
23-
IShellOutputReceiver receiver = Substitute.For<IShellOutputReceiver>();
23+
static bool predicate(string x) => true;
24+
IShellOutputReceiver receiver = new FunctionOutputReceiver(predicate);
2425

2526
IAdbClient client = Substitute.For<IAdbClient>();
2627
client.When(x => x.ExecuteRemoteCommand(Arg.Any<string>(), Arg.Any<DeviceData>()))
@@ -40,6 +41,7 @@ public void ExecuteServerCommandTest()
4041

4142
client.ExecuteShellCommand(Device, command);
4243
client.ExecuteShellCommand(Device, command, receiver);
44+
client.ExecuteShellCommand(Device, command, predicate);
4345
}
4446

4547
/// <summary>

AdvancedSharpAdbClient/DeviceCommands/DeviceExtensions.Async.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,21 @@ public static Task ExecuteShellCommandAsync(this IAdbClient client, DeviceData d
3636
/// <param name="receiver">Optionally, a <see cref="IShellOutputReceiver"/> that processes the command output.</param>
3737
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.</param>
3838
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
39-
public static Task ExecuteShellCommandAsync(this IAdbClient client, DeviceData device, string command, IShellOutputReceiver receiver, CancellationToken cancellationToken = default) =>
39+
public static Task ExecuteShellCommandAsync(this IAdbClient client, DeviceData device, string command, IShellOutputReceiver? receiver, CancellationToken cancellationToken = default) =>
4040
client.ExecuteRemoteCommandAsync(command, device, receiver, AdbClient.Encoding, cancellationToken);
4141

42+
/// <summary>
43+
/// Asynchronously executes a shell command on the device.
44+
/// </summary>
45+
/// <param name="client">The <see cref="IAdbClient"/> to use when executing the command.</param>
46+
/// <param name="device">The device on which to run the command.</param>
47+
/// <param name="command">The command to execute.</param>
48+
/// <param name="predicate">Optionally, a <see cref="Func{String, Boolean}"/> that processes the command output.</param>
49+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.</param>
50+
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
51+
public static Task ExecuteShellCommandAsync(this IAdbClient client, DeviceData device, string command, Func<string, bool>? predicate, CancellationToken cancellationToken = default) =>
52+
client.ExecuteRemoteCommandAsync(command, device, predicate.AsShellOutputReceiver(), AdbClient.Encoding, cancellationToken);
53+
4254
/// <summary>
4355
/// Asynchronously gets the current device screen snapshot asynchronously.
4456
/// </summary>
@@ -387,7 +399,7 @@ public static async Task PullAsync(this IAdbClient client, DeviceData device,
387399
CancellationToken cancellationToken = default)
388400
{
389401
using ISyncService service = Factories.SyncServiceFactory(client, device);
390-
await service.PullAsync(remotePath, stream, progress == null ? null : progress.Report, cancellationToken).ConfigureAwait(false);
402+
await service.PullAsync(remotePath, stream, progress.AsAction(), cancellationToken).ConfigureAwait(false);
391403
}
392404

393405
/// <summary>
@@ -408,7 +420,7 @@ public static async Task PushAsync(this IAdbClient client, DeviceData device,
408420
CancellationToken cancellationToken = default)
409421
{
410422
using ISyncService service = Factories.SyncServiceFactory(client, device);
411-
await service.PushAsync(stream, remotePath, permissions, timestamp, progress == null ? null : progress.Report, cancellationToken).ConfigureAwait(false);
423+
await service.PushAsync(stream, remotePath, permissions, timestamp, progress.AsAction(), cancellationToken).ConfigureAwait(false);
412424
}
413425

414426
/// <summary>

AdvancedSharpAdbClient/DeviceCommands/DeviceExtensions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,19 @@ public static void ExecuteShellCommand(this IAdbClient client, DeviceData device
3333
/// <param name="device">The device on which to run the command.</param>
3434
/// <param name="command">The command to execute.</param>
3535
/// <param name="receiver">Optionally, a <see cref="IShellOutputReceiver"/> that processes the command output.</param>
36-
public static void ExecuteShellCommand(this IAdbClient client, DeviceData device, string command, IShellOutputReceiver receiver) =>
36+
public static void ExecuteShellCommand(this IAdbClient client, DeviceData device, string command, IShellOutputReceiver? receiver) =>
3737
client.ExecuteRemoteCommand(command, device, receiver, AdbClient.Encoding);
3838

39+
/// <summary>
40+
/// Executes a shell command on the device.
41+
/// </summary>
42+
/// <param name="client">The <see cref="IAdbClient"/> to use when executing the command.</param>
43+
/// <param name="device">The device on which to run the command.</param>
44+
/// <param name="command">The command to execute.</param>
45+
/// <param name="predicate">Optionally, a <see cref="Func{String, Boolean}"/> that processes the command output.</param>
46+
public static void ExecuteShellCommand(this IAdbClient client, DeviceData device, string command, Func<string, bool>? predicate) =>
47+
client.ExecuteRemoteCommand(command, device, predicate.AsShellOutputReceiver(), AdbClient.Encoding);
48+
3949
/// <summary>
4050
/// Gets the current device screen snapshot.
4151
/// </summary>
@@ -334,7 +344,7 @@ public static void Pull(this IAdbClient client, DeviceData device,
334344
in bool isCancelled = false)
335345
{
336346
using ISyncService service = Factories.SyncServiceFactory(client, device);
337-
service.Pull(remotePath, stream, progress == null ? null : progress.Report, in isCancelled);
347+
service.Pull(remotePath, stream, progress.AsAction(), in isCancelled);
338348
}
339349

340350
/// <summary>
@@ -354,7 +364,7 @@ public static void Push(this IAdbClient client, DeviceData device,
354364
in bool isCancelled = false)
355365
{
356366
using ISyncService service = Factories.SyncServiceFactory(client, device);
357-
service.Push(stream, remotePath, permissions, timestamp, progress == null ? null : progress.Report, in isCancelled);
367+
service.Push(stream, remotePath, permissions, timestamp, progress.AsAction(), in isCancelled);
358368
}
359369

360370
/// <summary>

AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ await splitRemoteFilePaths.Select(async splitRemoteFilePath =>
353353
/// <param name="arguments">The arguments to pass to <c>adb install</c>.</param>
354354
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
355355
public virtual async Task InstallPackageAsync(string packageFilePath, IProgress<InstallProgressEventArgs>? progress = null, CancellationToken cancellationToken = default, params string[] arguments) =>
356-
await InstallPackageAsync(packageFilePath, progress == null ? null : progress.Report, cancellationToken, arguments).ConfigureAwait(false);
356+
await InstallPackageAsync(packageFilePath, progress.AsAction(), cancellationToken, arguments).ConfigureAwait(false);
357357

358358
/// <summary>
359359
/// Asynchronously installs the application package that was pushed to a temporary location on the device.
@@ -365,7 +365,7 @@ public virtual async Task InstallPackageAsync(string packageFilePath, IProgress<
365365
/// <param name="arguments">The arguments to pass to <c>pm install</c>.</param>
366366
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
367367
public virtual async Task InstallRemotePackageAsync(string remoteFilePath, IProgress<InstallProgressEventArgs>? progress = null, CancellationToken cancellationToken = default, params string[] arguments) =>
368-
await InstallRemotePackageAsync(remoteFilePath, progress == null ? null : progress.Report, cancellationToken, arguments).ConfigureAwait(false);
368+
await InstallRemotePackageAsync(remoteFilePath, progress.AsAction(), cancellationToken, arguments).ConfigureAwait(false);
369369

370370
/// <summary>
371371
/// Asynchronously installs Android multiple application on device.
@@ -378,7 +378,7 @@ public virtual async Task InstallRemotePackageAsync(string remoteFilePath, IProg
378378
/// <param name="arguments">The arguments to pass to <c>pm install-create</c>.</param>
379379
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
380380
public virtual async Task InstallMultiplePackageAsync(string basePackageFilePath, IEnumerable<string> splitPackageFilePaths, IProgress<InstallProgressEventArgs>? progress = null, CancellationToken cancellationToken = default, params string[] arguments) =>
381-
await InstallMultiplePackageAsync(basePackageFilePath, splitPackageFilePaths, progress == null ? null : progress.Report, cancellationToken, arguments).ConfigureAwait(false);
381+
await InstallMultiplePackageAsync(basePackageFilePath, splitPackageFilePaths, progress.AsAction(), cancellationToken, arguments).ConfigureAwait(false);
382382

383383
/// <summary>
384384
/// Asynchronously installs Android multiple application on device.
@@ -391,7 +391,7 @@ public virtual async Task InstallMultiplePackageAsync(string basePackageFilePath
391391
/// <param name="arguments">The arguments to pass to <c>pm install-create</c>.</param>
392392
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
393393
public virtual async Task InstallMultiplePackageAsync(IEnumerable<string> splitPackageFilePaths, string packageName, IProgress<InstallProgressEventArgs>? progress = null, CancellationToken cancellationToken = default, params string[] arguments) =>
394-
await InstallMultiplePackageAsync(splitPackageFilePaths, packageName, progress == null ? null : progress.Report, cancellationToken, arguments).ConfigureAwait(false);
394+
await InstallMultiplePackageAsync(splitPackageFilePaths, packageName, progress.AsAction(), cancellationToken, arguments).ConfigureAwait(false);
395395

396396
/// <summary>
397397
/// Asynchronously installs the multiple application package that was pushed to a temporary location on the device.
@@ -404,7 +404,7 @@ public virtual async Task InstallMultiplePackageAsync(IEnumerable<string> splitP
404404
/// <param name="arguments">The arguments to pass to <c>pm install-create</c>.</param>
405405
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
406406
public virtual async Task InstallMultipleRemotePackageAsync(string baseRemoteFilePath, IEnumerable<string> splitRemoteFilePaths, IProgress<InstallProgressEventArgs>? progress = null, CancellationToken cancellationToken = default, params string[] arguments) =>
407-
await InstallMultipleRemotePackageAsync(baseRemoteFilePath, splitRemoteFilePaths, progress == null ? null : progress.Report, cancellationToken, arguments).ConfigureAwait(false);
407+
await InstallMultipleRemotePackageAsync(baseRemoteFilePath, splitRemoteFilePaths, progress.AsAction(), cancellationToken, arguments).ConfigureAwait(false);
408408

409409
/// <summary>
410410
/// Asynchronously installs the multiple application package that was pushed to a temporary location on the device.
@@ -417,7 +417,7 @@ public virtual async Task InstallMultipleRemotePackageAsync(string baseRemoteFil
417417
/// <param name="arguments">The arguments to pass to <c>pm install-create</c>.</param>
418418
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
419419
public virtual async Task InstallMultipleRemotePackageAsync(IEnumerable<string> splitRemoteFilePaths, string packageName, IProgress<InstallProgressEventArgs>? progress = null, CancellationToken cancellationToken = default, params string[] arguments) =>
420-
await InstallMultipleRemotePackageAsync(splitRemoteFilePaths, packageName, progress == null ? null : progress.Report, cancellationToken, arguments).ConfigureAwait(false);
420+
await InstallMultipleRemotePackageAsync(splitRemoteFilePaths, packageName, progress.AsAction(), cancellationToken, arguments).ConfigureAwait(false);
421421
#endif
422422

423423
/// <summary>

AdvancedSharpAdbClient/DeviceCommands/PackageManager.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public virtual void InstallMultipleRemotePackage(IEnumerable<string> splitRemote
401401
/// The progress is reported as <see cref="InstallProgressEventArgs"/>, representing the state of installation.</param>
402402
/// <param name="arguments">The arguments to pass to <c>adb install</c>.</param>
403403
public virtual void InstallPackage(string packageFilePath, IProgress<InstallProgressEventArgs>? progress = null, params string[] arguments) =>
404-
InstallPackage(packageFilePath, progress == null ? null : progress.Report, arguments);
404+
InstallPackage(packageFilePath, progress.AsAction(), arguments);
405405

406406
/// <summary>
407407
/// Installs the application package that was pushed to a temporary location on the device.
@@ -411,7 +411,7 @@ public virtual void InstallPackage(string packageFilePath, IProgress<InstallProg
411411
/// The progress is reported as <see cref="InstallProgressEventArgs"/>, representing the state of installation.</param>
412412
/// <param name="arguments">The arguments to pass to <c>adb install</c>.</param>
413413
public virtual void InstallRemotePackage(string remoteFilePath, IProgress<InstallProgressEventArgs>? progress = null, params string[] arguments) =>
414-
InstallRemotePackage(remoteFilePath, progress == null ? null : progress.Report, arguments);
414+
InstallRemotePackage(remoteFilePath, progress.AsAction(), arguments);
415415

416416
/// <summary>
417417
/// Installs Android multiple application on device.
@@ -422,7 +422,7 @@ public virtual void InstallRemotePackage(string remoteFilePath, IProgress<Instal
422422
/// The progress is reported as <see cref="InstallProgressEventArgs"/>, representing the state of installation.</param>
423423
/// <param name="arguments">The arguments to pass to <c>pm install-create</c>.</param>
424424
public virtual void InstallMultiplePackage(string basePackageFilePath, IEnumerable<string> splitPackageFilePaths, IProgress<InstallProgressEventArgs>? progress = null, params string[] arguments) =>
425-
InstallMultiplePackage(basePackageFilePath, splitPackageFilePaths, progress == null ? null : progress.Report, arguments);
425+
InstallMultiplePackage(basePackageFilePath, splitPackageFilePaths, progress.AsAction(), arguments);
426426

427427
/// <summary>
428428
/// Installs Android multiple application on device.
@@ -433,7 +433,7 @@ public virtual void InstallMultiplePackage(string basePackageFilePath, IEnumerab
433433
/// The progress is reported as <see cref="InstallProgressEventArgs"/>, representing the state of installation.</param>
434434
/// <param name="arguments">The arguments to pass to <c>pm install-create</c>.</param>
435435
public virtual void InstallMultiplePackage(IEnumerable<string> splitPackageFilePaths, string packageName, IProgress<InstallProgressEventArgs>? progress = null, params string[] arguments) =>
436-
InstallMultiplePackage(splitPackageFilePaths, packageName, progress == null ? null : progress.Report, arguments);
436+
InstallMultiplePackage(splitPackageFilePaths, packageName, progress.AsAction(), arguments);
437437

438438
/// <summary>
439439
/// Installs the multiple application package that was pushed to a temporary location on the device.
@@ -444,7 +444,7 @@ public virtual void InstallMultiplePackage(IEnumerable<string> splitPackageFileP
444444
/// The progress is reported as <see cref="InstallProgressEventArgs"/>, representing the state of installation.</param>
445445
/// <param name="arguments">The arguments to pass to <c>pm install-create</c>.</param>
446446
public virtual void InstallMultipleRemotePackage(string baseRemoteFilePath, IEnumerable<string> splitRemoteFilePaths, IProgress<InstallProgressEventArgs>? progress = null, params string[] arguments) =>
447-
InstallMultipleRemotePackage(baseRemoteFilePath, splitRemoteFilePaths, progress == null ? null : progress.Report, arguments);
447+
InstallMultipleRemotePackage(baseRemoteFilePath, splitRemoteFilePaths, progress.AsAction(), arguments);
448448

449449
/// <summary>
450450
/// Installs the multiple application package that was pushed to a temporary location on the device.
@@ -455,7 +455,7 @@ public virtual void InstallMultipleRemotePackage(string baseRemoteFilePath, IEnu
455455
/// The progress is reported as <see cref="InstallProgressEventArgs"/>, representing the state of installation.</param>
456456
/// <param name="arguments">The arguments to pass to <c>pm install-create</c>.</param>
457457
public virtual void InstallMultipleRemotePackage(IEnumerable<string> splitRemoteFilePaths, string packageName, IProgress<InstallProgressEventArgs>? progress = null, params string[] arguments) =>
458-
InstallMultipleRemotePackage(splitRemoteFilePaths, packageName, progress == null ? null : progress.Report, arguments);
458+
InstallMultipleRemotePackage(splitRemoteFilePaths, packageName, progress.AsAction(), arguments);
459459
#endif
460460

461461
/// <summary>

0 commit comments

Comments
 (0)