Merge pull request 'feature/release-creation' (#2) from feature/release-creation into main

Reviewed-on: #2
This commit is contained in:
IsWaffle 2022-06-08 00:09:46 +00:00
commit fec4063cf9
16 changed files with 451 additions and 2 deletions

View File

@ -8,12 +8,21 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FubarCoder.RestSharp.Portable.Core" Version="4.0.8" />
<PackageReference Include="FubarCoder.RestSharp.Portable.HttpClient" Version="4.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Spectre.Console" Version="0.44.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="Gitea">
<HintPath>Resources\Gitea.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Update="settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

View File

@ -0,0 +1,18 @@
using Spectre.Console;
namespace EftPatchHelper.Extensions
{
public static class ObjectExtensions
{
public static T ValidateOrExit<T>(this object? toValidate)
{
if (toValidate == null || toValidate is not T)
{
AnsiConsole.Prompt(new TextPrompt<string>("Press [blue]enter[/] to close ...").AllowEmpty());
Environment.Exit(0);
}
return (T)toValidate;
}
}
}

View File

@ -0,0 +1,6 @@
namespace EftPatchHelper.Interfaces
{
public interface IFileProcessingTasks : ITaskable
{
}
}

View File

@ -0,0 +1,6 @@
namespace EftPatchHelper.Interfaces
{
public interface IPatchGenTasks : ITaskable
{
}
}

View File

@ -0,0 +1,6 @@
namespace EftPatchHelper.Interfaces
{
public interface IPatchTestingTasks : ITaskable
{
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EftPatchHelper.Interfaces
{
public interface IReleaseCreator : ITaskable
{
}
}

View File

@ -4,9 +4,29 @@ namespace EftPatchHelper.Model
{
public class Options
{
/// <summary>
/// Whether or not the user opted to ignore existing directories.
/// </summary>
public bool IgnoreExistingDirectories = false;
/// <summary>
/// The target client used to generate patches
/// </summary>
public EftClient TargetClient = null;
/// <summary>
/// The source client used to generate patches
/// </summary>
public EftClient SourceClient = null;
/// <summary>
/// The path to the patch folder containing the patches that were generated
/// </summary>
public string OutputPatchPath = null;
/// <summary>
/// Whether or not the user opted to create a release on gitea
/// </summary>
public bool CreateRelease = false;
}
}

View File

@ -30,6 +30,18 @@ namespace EftPatchHelper.Model
[JsonPropertyName("patcherExePath")]
public string PatcherEXEPath { get; set; } = "";
[JsonPropertyName("giteaApiBasePath")]
public string GiteaApiBasePath { get; set; } = "";
[JsonPropertyName("giteaApiKey")]
public string GiteaApiKey { get; set; } = "";
[JsonPropertyName("giteaReleaseRepoOwner")]
public string GiteaReleaseRepoOwner { get; set; } = "";
[JsonPropertyName("giteaReleaseRepoName")]
public string GiteaReleaseRepoName { get; set; } = "";
public bool Save()
{
try
@ -53,6 +65,19 @@ namespace EftPatchHelper.Model
}
}
public bool UsingGitea()
{
if (string.IsNullOrWhiteSpace(GiteaApiBasePath)) return false;
if (string.IsNullOrWhiteSpace(GiteaReleaseRepoOwner)) return false;
if (string.IsNullOrWhiteSpace(GiteaReleaseRepoName)) return false;
if (string.IsNullOrWhiteSpace(GiteaApiKey)) return false;
return true;
}
public bool Validate()
{
if (string.IsNullOrWhiteSpace(TargetEftVersion)) return false;

View File

@ -17,6 +17,7 @@ namespace EftPatchHelper
ITaskable _fileProcessingTasks;
ITaskable _patchGenTasks;
ITaskable _patchTestingTasks;
ITaskable _createReleaseTasks;
public static void Main(string[] args)
{
@ -34,7 +35,8 @@ namespace EftPatchHelper
IClientSelectionTask clientSelectionTasks,
IFileProcessingTasks fileProcessingTasks,
IPatchGenTasks patchGenTasks,
IPatchTestingTasks patchTestingTasks
IPatchTestingTasks patchTestingTasks,
IReleaseCreator createReleaseTasks
)
{
_settingsTasks = settingsTasks;
@ -42,6 +44,7 @@ namespace EftPatchHelper
_fileProcessingTasks = fileProcessingTasks;
_patchGenTasks = patchGenTasks;
_patchTestingTasks = patchTestingTasks;
_createReleaseTasks = createReleaseTasks;
}
public void Run()
@ -51,6 +54,7 @@ namespace EftPatchHelper
_fileProcessingTasks.Run();
_patchGenTasks.Run();
_patchTestingTasks.Run();
_createReleaseTasks.Run();
}
private static IHost ConfigureHost(string[] args)
@ -73,6 +77,7 @@ namespace EftPatchHelper
services.AddTransient<IFileProcessingTasks, FileProcessingTasks>();
services.AddTransient<IPatchGenTasks, PatchGenTasks>();
services.AddTransient<IPatchTestingTasks, PatchTestingTasks>();
services.AddTransient<IReleaseCreator, CreateReleaseTasks>();
services.AddTransient<Program>();
})
.ConfigureAppConfiguration((_, config) =>

Binary file not shown.

View File

@ -0,0 +1,103 @@
using EftPatchHelper.Interfaces;
using EftPatchHelper.Model;
using Gitea.Api;
using Gitea.Model;
using Gitea.Client;
using Spectre.Console;
using EftPatchHelper.Extensions;
namespace EftPatchHelper.Tasks
{
public class CreateReleaseTasks : IReleaseCreator
{
private Settings _settings;
private Options _options;
public CreateReleaseTasks(Settings settings, Options options)
{
_settings = settings;
_options = options;
}
private bool UploadAsset(FileInfo file, Release release, RepositoryApi repo)
{
return AnsiConsole.Status().Start("Uploading Asset", (StatusContext context) =>
{
AnsiConsole.MarkupLine($"[blue]Adding release asset: {file.Name.EscapeMarkup()}[/]");
file.Refresh();
if (!file.Exists)
{
AnsiConsole.MarkupLine($"[red]File does not exist: {file.FullName}[/]");
}
using var fileStream = file.OpenRead();
try
{
var attachment = repo.RepoCreateReleaseAttachment(_settings.GiteaReleaseRepoOwner, _settings.GiteaReleaseRepoName, release.Id, fileStream, file.Name);
AnsiConsole.MarkupLine("[green]Upload Complete[/]");
return true;
}
catch (Exception ex)
{
AnsiConsole.MarkupLine("[red]Failed to upload asset[/]");
AnsiConsole.WriteException(ex);
return false;
}
});
}
private Release? MakeRelease(RepositoryApi repo)
{
AnsiConsole.Write("Adding release to gitea ... ");
string sourceTail = _options.SourceClient.Version.Split('.').Last();
string targetTail = _options.TargetClient.Version.Split('.').Last();
string releaseName = $"{sourceTail} to {targetTail}";
try
{
var release = repo.RepoCreateRelease(_settings.GiteaReleaseRepoOwner, _settings.GiteaReleaseRepoName, new CreateReleaseOption(null, false, releaseName, false, sourceTail, null));
AnsiConsole.MarkupLine($"[green]Release added: {release.Name.EscapeMarkup()}[/]");
return release;
}
catch(Exception ex)
{
AnsiConsole.MarkupLine($"[red]Failed to create release[/]");
AnsiConsole.WriteException(ex);
return null;
}
}
public void Run()
{
AnsiConsole.WriteLine();
if (!_options.CreateRelease) return;
Configuration.Default.BasePath = _settings.GiteaApiBasePath;
Configuration.Default.AddApiKey("token", _settings.GiteaApiKey);
var repo = new RepositoryApi(Configuration.Default);
var release = MakeRelease(repo).ValidateOrExit<Release>();
var fileInfo = new FileInfo(_options.OutputPatchPath + ".zip");
UploadAsset(fileInfo, release, repo);
}
}
}

View File

@ -0,0 +1,55 @@
using EftPatchHelper.EftInfo;
using EftPatchHelper.Extensions;
using EftPatchHelper.Helpers;
using EftPatchHelper.Interfaces;
using EftPatchHelper.Model;
using Spectre.Console;
namespace EftPatchHelper.Tasks
{
public class FileProcessingTasks : IFileProcessingTasks
{
Settings _settings;
Options _options;
public FileProcessingTasks(Settings settings, Options options)
{
_settings = settings;
_options = options;
}
private bool BackupClients()
{
bool targetOK = _options.TargetClient.Backup(_settings, _options.IgnoreExistingDirectories);
bool sourceOK = _options.SourceClient.Backup(_settings, _options.IgnoreExistingDirectories);
return targetOK && sourceOK;
}
private bool CopyData(EftClient client, string message)
{
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine(message);
var folderCopy = new FolderCopy(client.FolderPath, client.PrepPath);
return folderCopy.Start(_options.IgnoreExistingDirectories);
}
private void CleanPrepFolders()
{
FolderCleaner.Clean(_options.TargetClient.PrepPath);
FolderCleaner.Clean(_options.SourceClient.PrepPath);
}
public void Run()
{
BackupClients().ValidateOrExit();
CopyData(_options.SourceClient, "[gray]Copying[/] [blue]source[/][gray] to prep area ...[/]").ValidateOrExit();
CopyData(_options.TargetClient, "[gray]Copying[/] [blue]target[/][gray] to prep area ...[/]").ValidateOrExit();
CleanPrepFolders();
}
}
}

View File

@ -0,0 +1,92 @@
using EftPatchHelper.Extensions;
using EftPatchHelper.Interfaces;
using EftPatchHelper.Model;
using Spectre.Console;
using System.Diagnostics;
namespace EftPatchHelper.Tasks
{
internal class PatchGenTasks : IPatchGenTasks
{
private Settings _settings;
private Options _options;
public PatchGenTasks(Settings settings, Options options)
{
_settings = settings;
_options = options;
}
private bool RunPatchGenerator()
{
if (!File.Exists(_settings.PatcherEXEPath))
{
AnsiConsole.WriteLine("Could not find patch generator exe");
return false;
}
string patcherOutputName = $"Patcher_{_options.SourceClient.Version}_to_{_options.TargetClient.Version}";
var patcherPath = new FileInfo(_settings.PatcherEXEPath)?.Directory?.FullName;
if(patcherPath == null)
{
AnsiConsole.WriteLine("Could not find patch generator folder");
return false;
}
_options.OutputPatchPath = Path.Join(patcherPath, patcherOutputName);
return AnsiConsole.Status().Start("Staring Patch Generator ...", (StatusContext context) =>
{
var genProc = Process.Start(new ProcessStartInfo()
{
FileName = _settings.PatcherEXEPath,
WorkingDirectory = new FileInfo(_settings.PatcherEXEPath).Directory?.FullName ?? Directory.GetCurrentDirectory(),
ArgumentList =
{
$"OutputFolderName::{patcherOutputName}",
$"SourceFolderPath::{_options.SourceClient.PrepPath}",
$"TargetFolderPath::{_options.TargetClient.PrepPath}",
$"AutoZip::{_settings.AutoZip}",
$"AutoClose::{_settings.AutoClose}"
}
});
context.Status = "Generating patches ...";
genProc?.WaitForExit();
switch ((PatcherExitCode)genProc.ExitCode)
{
case PatcherExitCode.ProgramClosed:
{
AnsiConsole.MarkupLine("[red]Program was closed[/]");
return false;
}
case PatcherExitCode.Success:
{
AnsiConsole.MarkupLine("[green]Patches Generated[/]");
return true;
}
case PatcherExitCode.MissingDir:
{
AnsiConsole.MarkupLine("[red]Missing Dir[/]");
return false;
}
default:
{
AnsiConsole.MarkupLine("[red]Something went wrong[/]");
return false;
}
}
});
}
public void Run()
{
RunPatchGenerator().ValidateOrExit();
}
}
}

View File

@ -0,0 +1,84 @@
using EftPatchHelper.Extensions;
using EftPatchHelper.Helpers;
using EftPatchHelper.Interfaces;
using EftPatchHelper.Model;
using Spectre.Console;
using System.Diagnostics;
namespace EftPatchHelper.Tasks
{
public class PatchTestingTasks : IPatchTestingTasks
{
private Settings _settings;
private Options _options;
public PatchTestingTasks(Settings settings, Options options)
{
_settings = settings;
_options = options;
}
private bool CopySourceToPrep()
{
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine($"Re-copying [blue]source[/] to prep area for testing ...");
var sourceCopy = new FolderCopy(_options.SourceClient.FolderPath, _options.SourceClient.PrepPath);
return sourceCopy.Start(false, true);
}
private bool RunPatcher()
{
var patcherCopy = new FolderCopy(_options.OutputPatchPath, _options.SourceClient.PrepPath);
patcherCopy.Start(false, true);
return AnsiConsole.Status().Start("Starting Patcher ...", (StatusContext context) =>
{
var patchProcess = Process.Start(new ProcessStartInfo()
{
FileName = Path.Join(_options.SourceClient.PrepPath, "patcher.exe"),
ArgumentList = { "autoclose" },
WorkingDirectory = _options.SourceClient.PrepPath
});
context.Status = "Running Patcher Test ...";
patchProcess.WaitForExit();
switch ((PatcherExitCode)patchProcess.ExitCode)
{
case PatcherExitCode.Success:
{
AnsiConsole.MarkupLine("[green]Patch Test Succeeded[/]");
return true;
}
case PatcherExitCode.NoPatchFolder:
{
AnsiConsole.MarkupLine("[red]No patch folder found[/]");
return false;
}
case PatcherExitCode.MissingFile:
{
AnsiConsole.MarkupLine("[red]Missing file[/]");
return false;
}
default:
{
AnsiConsole.MarkupLine("[red]Something went wrong[/]");
return false;
}
}
});
}
public void Run()
{
CopySourceToPrep().ValidateOrExit();
RunPatcher().ValidateOrExit();
}
}
}

View File

@ -61,6 +61,10 @@ namespace EftPatchHelper.Tasks
private void ConfirmOptions()
{
_options.IgnoreExistingDirectories = new ConfirmationPrompt("Skip existing directories? (you will be prompted if no)").Show(AnsiConsole.Console);
if (!_settings.UsingGitea()) return;
_options.CreateRelease = new ConfirmationPrompt("Create a release on gitea?").Show(AnsiConsole.Console);
}
public void Run()

View File

@ -5,5 +5,9 @@
"liveEftPath": "",
"patcherExePath": "",
"autoZip": true,
"autoClose": false
"autoClose": false,
"giteaApiBasePath": "", //You can leave the gitea settings blank if you don't need to create releases on gitea
"giteaApiKey": "",
"giteaReleaseRepoOwner": "",
"giteaReleaseRepoName": ""
}