Skip to content
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

Use PolySharp to generate attributes #87

Merged
merged 10 commits into from
Dec 14, 2023
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ body:
description: Specify the version of AdvancedSharpAdbClient you're using.
options:
- "Latest Source"
- "2.5.8"
- "2.5.7"
- "2.5.6"
- "2.5.5"
Expand All @@ -49,6 +50,7 @@ body:
multiple: true
options:
- ".NET Framework 2.0"
- ".NET Framework 3.0"
- ".NET Framework 3.5"
- ".NET Framework 4.0"
- ".NET Framework 4.5.x"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void AddRangeTest()
public async void TaskToArrayTest()
{
int[] array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Task<IEnumerable<int>> arrayTask = Extensions.Delay(10).ContinueWith(_ => array.Select(x => x));
IEnumerable<Task<int>> taskArray = array.Select(x => Extensions.Delay(x).ContinueWith(_ => x));
Task<IEnumerable<int>> arrayTask = TaskExExtensions.Delay(10).ContinueWith(_ => array.Select(x => x));
IEnumerable<Task<int>> taskArray = array.Select(x => TaskExExtensions.Delay(x).ContinueWith(_ => x));
Assert.Equal(array, await taskArray.ToArrayAsync());
Assert.Equal(array, await arrayTask.ToArrayAsync());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public void GetCommandInvalidCommandTest() =>

[Fact]
public void GetBytesInvalidCommandTest() =>
_ = Assert.Throws<ArgumentOutOfRangeException>(() => SyncCommandConverter.GetBytes((SyncCommand)99));
_ = Assert.Throws<ArgumentOutOfRangeException>(() => ((SyncCommand)99).GetBytes());

[Fact]
public void SyncCommandConverterTest()
{
SyncCommand[] commands = Enum.GetValues<SyncCommand>();
foreach (SyncCommand command in commands)
{
byte[] bytes = SyncCommandConverter.GetBytes(command);
byte[] bytes = command.GetBytes();
Assert.Equal(4, bytes.Length);
Assert.Equal(command, SyncCommandConverter.GetCommand(bytes));
}
Expand Down
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public async void PushAsyncTest()
byte[] content = await File.ReadAllBytesAsync("Assets/Fstab.bin");
byte[] contentMessage =
[
.. SyncCommandConverter.GetBytes(SyncCommand.DATA),
.. SyncCommand.DATA.GetBytes(),
.. BitConverter.GetBytes(content.Length),
.. content,
];
Expand Down
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient.Tests/SyncServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void PushTest()
byte[] content = File.ReadAllBytes("Assets/Fstab.bin");
byte[] contentMessage =
[
.. SyncCommandConverter.GetBytes(SyncCommand.DATA),
.. SyncCommand.DATA.GetBytes(),
.. BitConverter.GetBytes(content.Length),
.. content,
];
Expand Down
69 changes: 34 additions & 35 deletions AdvancedSharpAdbClient/AdbClient.Async.cs

Large diffs are not rendered by default.

75 changes: 39 additions & 36 deletions AdvancedSharpAdbClient/AdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ namespace AdvancedSharpAdbClient
/// </remarks>
public partial class AdbClient : IAdbClient
{
/// <summary>
/// The <see cref="Array"/> of <see cref="char"/>s that represent a new line.
/// </summary>
private static readonly char[] separator = Extensions.NewLineSeparator;

/// <summary>
Expand All @@ -52,7 +55,7 @@ public partial class AdbClient : IAdbClient
/// <summary>
/// The <see cref="Func{EndPoint, IAdbSocket}"/> to create <see cref="IAdbSocket"/>.
/// </summary>
protected readonly Func<EndPoint, IAdbSocket> adbSocketFactory;
protected readonly Func<EndPoint, IAdbSocket> AdbSocketFactory;

/// <summary>
/// Initializes a new instance of the <see cref="AdbClient"/> class.
Expand Down Expand Up @@ -96,7 +99,7 @@ public AdbClient(EndPoint endPoint, Func<EndPoint, IAdbSocket> adbSocketFactory)
}

EndPoint = endPoint;
this.adbSocketFactory = adbSocketFactory ?? throw new ArgumentNullException(nameof(adbSocketFactory));
this.AdbSocketFactory = adbSocketFactory ?? throw new ArgumentNullException(nameof(adbSocketFactory));
}

/// <summary>
Expand All @@ -120,7 +123,7 @@ public AdbClient(Func<EndPoint, IAdbSocket> adbSocketFactory)
}

/// <summary>
/// Get or set default encoding.
/// Gets or sets default encoding.
/// </summary>
public static Encoding Encoding { get; set; } = Encoding.UTF8;

Expand Down Expand Up @@ -169,7 +172,7 @@ public static byte[] CreateAdbForwardRequest(string address, int port)
/// <inheritdoc/>
public virtual int GetAdbVersion()
{
using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);

socket.SendAdbRequest("host:version");
_ = socket.ReadAdbResponse();
Expand All @@ -181,7 +184,7 @@ public virtual int GetAdbVersion()
/// <inheritdoc/>
public virtual void KillAdb()
{
using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SendAdbRequest("host:kill");

// The host will immediately close the connection after the kill
Expand All @@ -191,7 +194,7 @@ public virtual void KillAdb()
/// <inheritdoc/>
public virtual IEnumerable<DeviceData> GetDevices()
{
using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);

socket.SendAdbRequest("host:devices-l");
_ = socket.ReadAdbResponse();
Expand All @@ -206,7 +209,7 @@ public virtual int CreateForward(DeviceData device, string local, string remote,
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
string rebind = allowRebind ? string.Empty : "norebind:";

socket.SendAdbRequest($"host-serial:{device.Serial}:forward:{rebind}{local};{remote}");
Expand All @@ -222,7 +225,7 @@ public virtual int CreateReverseForward(DeviceData device, string remote, string
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);

socket.SetDevice(device);
string rebind = allowRebind ? string.Empty : "norebind:";
Expand All @@ -240,7 +243,7 @@ public virtual void RemoveReverseForward(DeviceData device, string remote)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest($"reverse:killforward:{remote}");
Expand All @@ -252,7 +255,7 @@ public virtual void RemoveAllReverseForwards(DeviceData device)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest($"reverse:killforward-all");
Expand All @@ -264,7 +267,7 @@ public virtual void RemoveForward(DeviceData device, int localPort)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SendAdbRequest($"host-serial:{device.Serial}:killforward:tcp:{localPort}");
_ = socket.ReadAdbResponse();
}
Expand All @@ -274,7 +277,7 @@ public virtual void RemoveAllForwards(DeviceData device)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SendAdbRequest($"host-serial:{device.Serial}:killforward-all");
_ = socket.ReadAdbResponse();
}
Expand All @@ -284,7 +287,7 @@ public IEnumerable<ForwardData> ListForward(DeviceData device)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SendAdbRequest($"host-serial:{device.Serial}:list-forward");
_ = socket.ReadAdbResponse();
string data = socket.ReadString();
Expand All @@ -298,7 +301,7 @@ public IEnumerable<ForwardData> ListReverseForward(DeviceData device)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest($"reverse:list-forward");
Expand All @@ -313,7 +316,7 @@ public IEnumerable<ForwardData> ListReverseForward(DeviceData device)
public virtual void ExecuteServerCommand(string target, string command, Encoding encoding)
{
ExceptionExtensions.ThrowIfNull(encoding);
using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
ExecuteServerCommand(target, command, socket, encoding);
}

Expand All @@ -339,7 +342,7 @@ public virtual void ExecuteRemoteCommand(string command, DeviceData device, Enco
EnsureDevice(device);
ExceptionExtensions.ThrowIfNull(encoding);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

ExecuteServerCommand("shell", command, socket, encoding);
Expand All @@ -349,7 +352,7 @@ public virtual void ExecuteRemoteCommand(string command, DeviceData device, Enco
public virtual void ExecuteServerCommand(string target, string command, IShellOutputReceiver receiver, Encoding encoding)
{
ExceptionExtensions.ThrowIfNull(encoding);
using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
ExecuteServerCommand(target, command, socket, receiver, encoding);
}

Expand Down Expand Up @@ -397,7 +400,7 @@ public virtual void ExecuteRemoteCommand(string command, DeviceData device, IShe
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

ExecuteServerCommand("shell", command, socket, receiver, encoding);
Expand All @@ -407,7 +410,7 @@ public virtual void ExecuteRemoteCommand(string command, DeviceData device, IShe
public Framebuffer CreateRefreshableFramebuffer(DeviceData device)
{
EnsureDevice(device);
return new Framebuffer(device, this, adbSocketFactory);
return new Framebuffer(device, this, AdbSocketFactory);
}

/// <inheritdoc/>
Expand All @@ -430,7 +433,7 @@ public virtual void RunLogService(DeviceData device, Action<LogEntry> messageSin

// The 'log' service has been deprecated, see
// https://android.googlesource.com/platform/system/core/+/7aa39a7b199bb9803d3fd47246ee9530b4a96177
using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

StringBuilder request = new StringBuilder().Append("shell:logcat -B");
Expand Down Expand Up @@ -475,7 +478,7 @@ public virtual void Reboot(string into, DeviceData device)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest($"reboot:{into}");
Expand All @@ -487,7 +490,7 @@ public string Pair(DnsEndPoint endpoint, string code)
{
ExceptionExtensions.ThrowIfNull(endpoint);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SendAdbRequest($"host:pair:{code}:{endpoint.Host}:{endpoint.Port}");
_ = socket.ReadAdbResponse();

Expand All @@ -499,7 +502,7 @@ public string Connect(DnsEndPoint endpoint)
{
ExceptionExtensions.ThrowIfNull(endpoint);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SendAdbRequest($"host:connect:{endpoint.Host}:{endpoint.Port}");
_ = socket.ReadAdbResponse();

Expand All @@ -511,7 +514,7 @@ public string Disconnect(DnsEndPoint endpoint)
{
ExceptionExtensions.ThrowIfNull(endpoint);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SendAdbRequest($"host:disconnect:{endpoint.Host}:{endpoint.Port}");
_ = socket.ReadAdbResponse();

Expand All @@ -533,7 +536,7 @@ protected void Root(string request, DeviceData device)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);
socket.SendAdbRequest(request);
_ = socket.ReadAdbResponse();
Expand Down Expand Up @@ -562,7 +565,7 @@ protected void Root(string request, DeviceData device)
#if HAS_PROCESS && !WINDOWS_UWP
Thread.Sleep(3000);
#else
Extensions.Delay(3000).Wait();
TaskExExtensions.Delay(3000).AwaitByTaskCompleteSource();
#endif
}
}
Expand Down Expand Up @@ -594,14 +597,14 @@ public void Install(DeviceData device, Stream apk, IProgress<InstallProgressEven
// do last to override any user specified value
_ = requestBuilder.AppendFormat(" -S {0}", apk.Length);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest(requestBuilder.ToString());
_ = socket.ReadAdbResponse();

byte[] buffer = new byte[32 * 1024];
int read = 0;
int read;

long totalBytesToProcess = apk.Length;
long totalBytesRead = 0;
Expand Down Expand Up @@ -760,7 +763,7 @@ public string InstallCreate(DeviceData device, string? packageName = null, param
}
}

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest(requestBuilder.ToString());
Expand Down Expand Up @@ -801,14 +804,14 @@ public void InstallWrite(DeviceData device, Stream apk, string apkName, string s
.AppendFormat(" -S {0}", apk.Length)
.AppendFormat(" {0} {1}.apk", session, apkName);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest(requestBuilder.ToString());
_ = socket.ReadAdbResponse();

byte[] buffer = new byte[32 * 1024];
int read = 0;
int read;

long totalBytesToProcess = apk.Length;
long totalBytesRead = 0;
Expand Down Expand Up @@ -870,14 +873,14 @@ protected virtual void InstallWrite(DeviceData device, Stream apk, string apkNam
.AppendFormat(" -S {0}", apk.Length)
.AppendFormat(" {0} {1}.apk", session, apkName);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest(requestBuilder.ToString());
_ = socket.ReadAdbResponse();

byte[] buffer = new byte[32 * 1024];
int read = 0;
int read;

long totalBytesToProcess = apk.Length;
long totalBytesRead = 0;
Expand Down Expand Up @@ -916,7 +919,7 @@ public virtual void InstallCommit(DeviceData device, string session)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest($"exec:cmd package 'install-commit' {session}");
Expand Down Expand Up @@ -947,7 +950,7 @@ public virtual void Uninstall(DeviceData device, string packageName, params stri

_ = requestBuilder.AppendFormat(" {0}", packageName);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SetDevice(device);

socket.SendAdbRequest(requestBuilder.ToString());
Expand All @@ -966,7 +969,7 @@ public virtual IEnumerable<string> GetFeatureSet(DeviceData device)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
using IAdbSocket socket = AdbSocketFactory(EndPoint);
socket.SendAdbRequest($"host-serial:{device.Serial}:features");
_ = socket.ReadAdbResponse();
string features = socket.ReadString();
Expand Down
Loading