diff --git a/UET/Redpoint.ProgressMonitor/ProgressMonitorExtensions.cs b/UET/Redpoint.ProgressMonitor/ProgressMonitorExtensions.cs index 0ef5d694..6e0b86d1 100644 --- a/UET/Redpoint.ProgressMonitor/ProgressMonitorExtensions.cs +++ b/UET/Redpoint.ProgressMonitor/ProgressMonitorExtensions.cs @@ -2,6 +2,9 @@ { using Microsoft.Extensions.DependencyInjection; using Redpoint.ProgressMonitor.Implementations; +#if NETCOREAPP + using Redpoint.ProgressMonitor.Utils; +#endif /// /// Extensions for registering progress monitoring services. @@ -17,6 +20,9 @@ public static void AddProgressMonitor(this IServiceCollection services) services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); +#if NETCOREAPP + services.AddSingleton(); +#endif } } } diff --git a/UET/Redpoint.ProgressMonitor/Utils/DefaultSimpleDownloadProgress.cs b/UET/Redpoint.ProgressMonitor/Utils/DefaultSimpleDownloadProgress.cs new file mode 100644 index 00000000..39db0773 --- /dev/null +++ b/UET/Redpoint.ProgressMonitor/Utils/DefaultSimpleDownloadProgress.cs @@ -0,0 +1,101 @@ +#if NETCOREAPP + +namespace Redpoint.ProgressMonitor.Utils +{ + using Redpoint.ProgressMonitor; + using System; + using System.IO; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; + + internal class DefaultSimpleDownloadProgress : ISimpleDownloadProgress + { + private readonly IProgressFactory _progressFactory; + private readonly IMonitorFactory _monitorFactory; + + public DefaultSimpleDownloadProgress( + IProgressFactory progressFactory, + IMonitorFactory monitorFactory) + { + _progressFactory = progressFactory; + _monitorFactory = monitorFactory; + } + + private class DidOutput + { + public bool Did; + } + + public async Task DownloadAndCopyToStreamAsync( + HttpClient client, + Uri downloadUrl, + Func copier, + CancellationToken cancellationToken) + { + var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + + using var stream = new PositionAwareStream( + await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false), + response.Content.Headers.ContentLength!.Value); + + using var cts = new CancellationTokenSource(); + var progress = _progressFactory.CreateProgressForStream(stream); + var outputTrack = new DidOutput(); + var monitorTask = Task.Run(async () => + { + var consoleWidth = 0; + try + { + consoleWidth = Console.BufferWidth; + } + catch { } + + var monitor = _monitorFactory.CreateByteBasedMonitor(); + await monitor.MonitorAsync( + progress, + null, + (message, count) => + { + if (consoleWidth != 0) + { + Console.Write($"\r {message}".PadRight(consoleWidth)); + outputTrack.Did = true; + } + else if (count % 50 == 0) + { + Console.WriteLine($" {message}"); + outputTrack.Did = true; + } + }, + cts.Token).ConfigureAwait(false); + }, cts.Token); + + await copier(stream).ConfigureAwait(false); + + cts.Cancel(); + try + { + await monitorTask.ConfigureAwait(false); + } + catch (OperationCanceledException) + { + } + if (outputTrack.Did) + { + var consoleWidth = 0; + try + { + consoleWidth = Console.BufferWidth; + } + catch { } + if (consoleWidth != 0) + { + Console.WriteLine(); + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/UET/Redpoint.ProgressMonitor/Utils/ISimpleDownloadProgress.cs b/UET/Redpoint.ProgressMonitor/Utils/ISimpleDownloadProgress.cs new file mode 100644 index 00000000..9fdf6a37 --- /dev/null +++ b/UET/Redpoint.ProgressMonitor/Utils/ISimpleDownloadProgress.cs @@ -0,0 +1,32 @@ +#if NETCOREAPP + +namespace Redpoint.ProgressMonitor.Utils +{ + using System; + using System.IO; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; + + /// + /// A utility interface which downloads a URL to a stream and emits progress information to the console. + /// + public interface ISimpleDownloadProgress + { + /// + /// Download the target URL using the specified , passing the download stream to (which is typically a lambda that calls ). + /// + /// The to use for the download. + /// The URL to download. + /// The asynchronous function that the download stream will be passed to. When the returned task completes, the stream should have been fully written or consumed. + /// A token which can be used to cancel the download. + /// An awaitable task. + Task DownloadAndCopyToStreamAsync( + HttpClient client, + Uri downloadUrl, + Func copier, + CancellationToken cancellationToken); + } +} + +#endif \ No newline at end of file diff --git a/UET/Redpoint.Uet.SdkManagement/DownloadProgress/ISimpleDownloadProgress.cs b/UET/Redpoint.Uet.SdkManagement/DownloadProgress/ISimpleDownloadProgress.cs deleted file mode 100644 index f776ec49..00000000 --- a/UET/Redpoint.Uet.SdkManagement/DownloadProgress/ISimpleDownloadProgress.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Redpoint.Uet.SdkManagement -{ - using System; - using System.Threading; - using System.Threading.Tasks; - - public interface ISimpleDownloadProgress - { - Task DownloadAndCopyToStreamAsync( - HttpClient client, - Uri downloadUrl, - Func copier, - CancellationToken cancellationToken); - } -} diff --git a/UET/Redpoint.Uet.SdkManagement/DownloadProgress/SimpleDownloadProgress.cs b/UET/Redpoint.Uet.SdkManagement/DownloadProgress/SimpleDownloadProgress.cs deleted file mode 100644 index 22578f48..00000000 --- a/UET/Redpoint.Uet.SdkManagement/DownloadProgress/SimpleDownloadProgress.cs +++ /dev/null @@ -1,95 +0,0 @@ -namespace Redpoint.Uet.SdkManagement -{ - using Redpoint.ProgressMonitor; - using System; - using System.Threading; - using System.Threading.Tasks; - - internal class SimpleDownloadProgress : ISimpleDownloadProgress - { - private readonly IProgressFactory _progressFactory; - private readonly IMonitorFactory _monitorFactory; - - public SimpleDownloadProgress( - IProgressFactory progressFactory, - IMonitorFactory monitorFactory) - { - _progressFactory = progressFactory; - _monitorFactory = monitorFactory; - } - - private class DidOutput - { - public bool Did; - } - - public async Task DownloadAndCopyToStreamAsync( - HttpClient client, - Uri downloadUrl, - Func copier, - CancellationToken cancellationToken) - { - var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); - using (var stream = new PositionAwareStream( - await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false), - response.Content.Headers.ContentLength!.Value)) - { - using var cts = new CancellationTokenSource(); - var progress = _progressFactory.CreateProgressForStream(stream); - var outputTrack = new DidOutput(); - var monitorTask = Task.Run(async () => - { - var consoleWidth = 0; - try - { - consoleWidth = Console.BufferWidth; - } - catch { } - - var monitor = _monitorFactory.CreateByteBasedMonitor(); - await monitor.MonitorAsync( - progress, - null, - (message, count) => - { - if (consoleWidth != 0) - { - Console.Write($"\r {message}".PadRight(consoleWidth)); - outputTrack.Did = true; - } - else if (count % 50 == 0) - { - Console.WriteLine($" {message}"); - outputTrack.Did = true; - } - }, - cts.Token).ConfigureAwait(false); - }, cts.Token); - - await copier(stream).ConfigureAwait(false); - - cts.Cancel(); - try - { - await monitorTask.ConfigureAwait(false); - } - catch (OperationCanceledException) - { - } - if (outputTrack.Did) - { - var consoleWidth = 0; - try - { - consoleWidth = Console.BufferWidth; - } - catch { } - if (consoleWidth != 0) - { - Console.WriteLine(); - } - } - } - } - } -} diff --git a/UET/Redpoint.Uet.SdkManagement/Sdk/LinuxSdkSetup.cs b/UET/Redpoint.Uet.SdkManagement/Sdk/LinuxSdkSetup.cs index 9f87804e..46ca8cbc 100644 --- a/UET/Redpoint.Uet.SdkManagement/Sdk/LinuxSdkSetup.cs +++ b/UET/Redpoint.Uet.SdkManagement/Sdk/LinuxSdkSetup.cs @@ -11,6 +11,7 @@ using System.Reflection; using System.Text.RegularExpressions; using Redpoint.Uet.SdkManagement.Sdk.VersionNumbers; + using Redpoint.ProgressMonitor.Utils; [SupportedOSPlatform("windows")] public class LinuxSdkSetup : ISdkSetup diff --git a/UET/Redpoint.Uet.SdkManagement/Sdk/WindowsSdk/WindowsSdkInstaller.cs b/UET/Redpoint.Uet.SdkManagement/Sdk/WindowsSdk/WindowsSdkInstaller.cs index 07751b3b..574f18c7 100644 --- a/UET/Redpoint.Uet.SdkManagement/Sdk/WindowsSdk/WindowsSdkInstaller.cs +++ b/UET/Redpoint.Uet.SdkManagement/Sdk/WindowsSdk/WindowsSdkInstaller.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging; using Redpoint.IO; using Redpoint.ProcessExecution; + using Redpoint.ProgressMonitor.Utils; using System; using System.Collections.Generic; using System.IO.Compression; diff --git a/UET/Redpoint.Uet.SdkManagement/SdkManagementServiceExtensions.cs b/UET/Redpoint.Uet.SdkManagement/SdkManagementServiceExtensions.cs index 0115d11f..cb6db821 100644 --- a/UET/Redpoint.Uet.SdkManagement/SdkManagementServiceExtensions.cs +++ b/UET/Redpoint.Uet.SdkManagement/SdkManagementServiceExtensions.cs @@ -15,7 +15,6 @@ public static class SdkManagementServiceExtensions { public static void AddSdkManagement(this IServiceCollection services) { - services.AddSingleton(); services.AddSingleton(); services.AddSingleton();