Skip to content

Commit

Permalink
Merge pull request #2 from hugocasser/refactor/v3
Browse files Browse the repository at this point in the history
feature: install command
  • Loading branch information
jejikeh authored Feb 21, 2024
2 parents c1218ec + 3ae035e commit b4e6065
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 2 deletions.
2 changes: 2 additions & 0 deletions UEScript.CLI/Commands/Engine/ConfigureEngineCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,7 @@ public static void AddEngineCommand(this CliRootCommand rootCommand)
engineCommand.AddListCommand();
engineCommand.AddAddCommand();
engineCommand.AddDeleteCommand();
engineCommand.AddInstallCommand();

engineCommand.Action = CommandHandler.Create<FileInfo, IHost>((file, host) =>
{
Expand Down
57 changes: 57 additions & 0 deletions UEScript.CLI/Commands/Engine/Install/ConfigureInstallCommand.cs
Original file line number Diff line number Diff line change
@@ -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<string>("name")
{
Description = "Name of the engine",
},
new CliArgument<FileInfo>("path")
{
Description = "Path to the engine",
},
new CliArgument<string>("url")
{
Description = "Url to the engine",
},
new CliOption<bool>("--isDefault")
{
Description = "Set this engine as default",
Required = false,
Aliases = { "-d" }
},
};

installCommand.Action = CommandHandler.Create<string, FileInfo, string, bool, IHost>
((name, path,url,isDefault, host) =>
{
var serviceProvider = host.Services;
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger("Engine.DeleteCommand");
var engineAssociationRepository = serviceProvider.GetRequiredService<IUnrealEngineAssociationRepository>();
var fileDownloader = serviceProvider.GetRequiredService<IFileDownloaderService>();
var act = async () => await InstallCommand.Execute(name, path, url, logger, fileDownloader, engineAssociationRepository);
var result = Task.Run(act).Result;
logger.LogResult(result);
});

command.Add(installCommand);
}
}
31 changes: 31 additions & 0 deletions UEScript.CLI/Commands/Engine/Install/InstallCommand.cs
Original file line number Diff line number Diff line change
@@ -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<Result<string, CommandError>> 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);

Check warning on line 20 in UEScript.CLI/Commands/Engine/Install/InstallCommand.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'filePath' in 'Task<Result<string, CommandError>> IFileDownloaderService.DownloadFile(string url, DirectoryInfo filePath)'.

Check warning on line 20 in UEScript.CLI/Commands/Engine/Install/InstallCommand.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'filePath' in 'Task<Result<string, CommandError>> IFileDownloaderService.DownloadFile(string url, DirectoryInfo filePath)'.

Check warning on line 20 in UEScript.CLI/Commands/Engine/Install/InstallCommand.cs

View workflow job for this annotation

GitHub Actions / build, pack & publish

Possible null reference argument for parameter 'filePath' in 'Task<Result<string, CommandError>> IFileDownloaderService.DownloadFile(string url, DirectoryInfo filePath)'.

if (!downloadResult.IsSuccess)
{
return Result<string, CommandError>.Error(downloadResult);
}

logger.LogInformation("Download success");

return AddCommand.Execute(name, filePath, true, repository, logger);;
}
}
4 changes: 2 additions & 2 deletions UEScript.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;
using System.Reflection;
using UEScript.CLI.Configurations;

[assembly: AssemblyVersion("3.1.*")]
Expand All @@ -12,7 +12,7 @@ public static Task Main(string[] args)
var cliConfiguration = ProgramConfiguration
.BuildCommandLine()
.Configure(args);

return cliConfiguration.InvokeAsync(args);
}
}
9 changes: 9 additions & 0 deletions UEScript.CLI/Services/IFileDownloaderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UEScript.CLI.Commands;
using UEScript.Utils.Results;

namespace UEScript.CLI.Services;

public interface IFileDownloaderService
{
public Task<Result<string, CommandError>> DownloadFile(string url, DirectoryInfo filePath);
}
28 changes: 28 additions & 0 deletions UEScript.CLI/Services/Impl/FileDownloaderServiceService.cs
Original file line number Diff line number Diff line change
@@ -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<Result<string, CommandError>> 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<string, CommandError>.Error(new CommandError(ex.Message));
}

return Result<string, CommandError>.Ok("File downloaded");
}
}

0 comments on commit b4e6065

Please sign in to comment.