From 8865ddc7ba79920e2b8415e817182fd9b368247a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=93=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BC=D1=8E=D0=BA?= Date: Wed, 21 Feb 2024 15:24:48 +0300 Subject: [PATCH] feature: install command --- .../Commands/Engine/ConfigureEngineCommand.cs | 2 + .../Engine/Install/ConfigureInstallCommand.cs | 57 +++++++++++++++++++ .../Commands/Engine/Install/InstallCommand.cs | 31 ++++++++++ UEScript.CLI/Program.cs | 1 + .../Services/IFileDownloaderService.cs | 9 +++ .../Impl/FileDownloaderServiceService.cs | 28 +++++++++ 6 files changed, 128 insertions(+) create mode 100644 UEScript.CLI/Commands/Engine/Install/ConfigureInstallCommand.cs create mode 100644 UEScript.CLI/Commands/Engine/Install/InstallCommand.cs create mode 100644 UEScript.CLI/Services/IFileDownloaderService.cs create mode 100644 UEScript.CLI/Services/Impl/FileDownloaderServiceService.cs diff --git a/UEScript.CLI/Commands/Engine/ConfigureEngineCommand.cs b/UEScript.CLI/Commands/Engine/ConfigureEngineCommand.cs index ada650d..06d4b0e 100644 --- a/UEScript.CLI/Commands/Engine/ConfigureEngineCommand.cs +++ b/UEScript.CLI/Commands/Engine/ConfigureEngineCommand.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Logging; using UEScript.CLI.Commands.Engine.Add; using UEScript.CLI.Commands.Engine.Delete; +using UEScript.CLI.Commands.Engine.Install; using UEScript.CLI.Commands.Engine.List; using UEScript.CLI.Common; using UEScript.CLI.Services; @@ -27,6 +28,7 @@ public static void AddEngineCommand(this CliRootCommand rootCommand) engineCommand.AddListCommand(); engineCommand.AddAddCommand(); engineCommand.AddDeleteCommand(); + engineCommand.AddInstallCommand(); engineCommand.Action = CommandHandler.Create((file, host) => { diff --git a/UEScript.CLI/Commands/Engine/Install/ConfigureInstallCommand.cs b/UEScript.CLI/Commands/Engine/Install/ConfigureInstallCommand.cs new file mode 100644 index 0000000..973e02a --- /dev/null +++ b/UEScript.CLI/Commands/Engine/Install/ConfigureInstallCommand.cs @@ -0,0 +1,57 @@ +using System.CommandLine; +using System.CommandLine.NamingConventionBinder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using UEScript.CLI.Services; +using UEScript.Utils.Extensions; + +namespace UEScript.CLI.Commands.Engine.Install; + +public static class ConfigureInstallCommand +{ + public static void AddInstallCommand(this CliCommand command) + { + var installCommand = new CliCommand("install", "Install Unreal ENGINE from url") + { + new CliArgument("name") + { + Description = "Name of the engine", + }, + new CliArgument("path") + { + Description = "Path to the engine", + }, + new CliArgument("url") + { + Description = "Url to the engine", + }, + new CliOption("--isDefault") + { + Description = "Set this engine as default", + Required = false, + Aliases = { "-d" } + }, + }; + + installCommand.Action = CommandHandler.Create + ((name, path,url,isDefault, host) => + { + var serviceProvider = host.Services; + + var loggerFactory = serviceProvider.GetRequiredService(); + var logger = loggerFactory.CreateLogger("Engine.DeleteCommand"); + + var engineAssociationRepository = serviceProvider.GetRequiredService(); + var fileDownloader = serviceProvider.GetRequiredService(); + + var act = async () => await InstallCommand.Execute(name, path, url, logger, fileDownloader, engineAssociationRepository); + + var result = Task.Run(act).Result; + + logger.LogResult(result); + }); + + command.Add(installCommand); + } +} \ No newline at end of file diff --git a/UEScript.CLI/Commands/Engine/Install/InstallCommand.cs b/UEScript.CLI/Commands/Engine/Install/InstallCommand.cs new file mode 100644 index 0000000..e896d13 --- /dev/null +++ b/UEScript.CLI/Commands/Engine/Install/InstallCommand.cs @@ -0,0 +1,31 @@ +using Microsoft.Extensions.Logging; +using UEScript.CLI.Commands.Engine.Add; +using UEScript.CLI.Services; +using UEScript.Utils.Results; + +namespace UEScript.CLI.Commands.Engine.Install; + +public static class InstallCommand +{ + public static async Task> Execute + (string name, FileInfo filePath, string url, ILogger logger, + IFileDownloaderService fileDownloaderService, IUnrealEngineAssociationRepository repository) + { + logger.LogTrace("Install command start execution..."); + + var directory = filePath.Directory; + + logger.LogInformation("Starting download engine zip from {url}...", url); + + var downloadResult = await fileDownloaderService.DownloadFile(url, directory); + + if (!downloadResult.IsSuccess) + { + return Result.Error(downloadResult); + } + + logger.LogInformation("Download success"); + + return AddCommand.Execute(name, filePath, true, repository, logger);; + } +} \ No newline at end of file diff --git a/UEScript.CLI/Program.cs b/UEScript.CLI/Program.cs index b64fd9e..2478c1a 100644 --- a/UEScript.CLI/Program.cs +++ b/UEScript.CLI/Program.cs @@ -82,6 +82,7 @@ public static Task Main(string[] args) services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); }); }); diff --git a/UEScript.CLI/Services/IFileDownloaderService.cs b/UEScript.CLI/Services/IFileDownloaderService.cs new file mode 100644 index 0000000..0e46d3b --- /dev/null +++ b/UEScript.CLI/Services/IFileDownloaderService.cs @@ -0,0 +1,9 @@ +using UEScript.CLI.Commands; +using UEScript.Utils.Results; + +namespace UEScript.CLI.Services; + +public interface IFileDownloaderService +{ + public Task> DownloadFile(string url, DirectoryInfo filePath); +} \ No newline at end of file diff --git a/UEScript.CLI/Services/Impl/FileDownloaderServiceService.cs b/UEScript.CLI/Services/Impl/FileDownloaderServiceService.cs new file mode 100644 index 0000000..1b110e4 --- /dev/null +++ b/UEScript.CLI/Services/Impl/FileDownloaderServiceService.cs @@ -0,0 +1,28 @@ +using System.IO.Compression; +using UEScript.CLI.Commands; +using UEScript.Utils.Results; + +namespace UEScript.CLI.Services.Impl; + +public class FileDownloaderServiceService : IFileDownloaderService +{ + public async Task> DownloadFile(string url, DirectoryInfo filePath) + { + try + { + var uri = new Uri(url); + + using var httpClient = new HttpClient(); + var responseMessage = await httpClient.GetStreamAsync(uri); + + using var zip = new ZipArchive(responseMessage); + zip.ExtractToDirectory(filePath.ToString()); + } + catch (Exception ex) + { + return Result.Error(new CommandError(ex.Message)); + } + + return Result.Ok("File downloaded"); + } +} \ No newline at end of file