-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from hugocasser/refactor/v3
feature: install command
- Loading branch information
Showing
6 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
UEScript.CLI/Commands/Engine/Install/ConfigureInstallCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / build
Check warning on line 20 in UEScript.CLI/Commands/Engine/Install/InstallCommand.cs GitHub Actions / build
Check warning on line 20 in UEScript.CLI/Commands/Engine/Install/InstallCommand.cs GitHub Actions / build, pack & publish
|
||
|
||
if (!downloadResult.IsSuccess) | ||
{ | ||
return Result<string, CommandError>.Error(downloadResult); | ||
} | ||
|
||
logger.LogInformation("Download success"); | ||
|
||
return AddCommand.Execute(name, filePath, true, repository, logger);; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
UEScript.CLI/Services/Impl/FileDownloaderServiceService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |