Enhanced IO issue handling when generating output

This commit is contained in:
Thorsten Sommer 2022-11-01 19:24:27 +01:00
parent 53f98d4e3c
commit a19ad7dcdb
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
5 changed files with 67 additions and 15 deletions

View File

@ -8,17 +8,27 @@ public class DotnetBigFile : IGenerator
private static readonly List<string> CULTURE_CODES = new();
private static int DEFAULT_CULTURE_INDEX = -1;
public async Task GenerateAsync()
public async Task<ProcessorResult<long>> GenerateAsync()
{
const string filename = "I18N.cs";
var destPath = await AppSettings.GetGeneratorDotnetDestinationPath();
destPath = Environment.ExpandEnvironmentVariables(destPath);
long destBytesWritten = 0;
var pathFinal = Path.Join(destPath, filename);
var pathTemp = Path.Join(destPath, filename + ".gen");
var issueFinal = string.Empty;
try
{
if(File.Exists(pathTemp))
File.Delete(pathTemp);
}
catch (IOException e)
{
return new ProcessorResult<long>(0, false, $"Cannot delete the temporary file: '{e.Message}'. Hint: Is the ransomware protection enabled in your Windows system? If so, please make sure that the I18N Commander has write permission.");
}
CULTURE_CODES.Clear();
var cultures = await AppSettings.GetCultureInfos();
@ -49,17 +59,41 @@ public class DotnetBigFile : IGenerator
await this.TransformSection(writer, indention, section);
});
}
catch (IOException e)
{
// Happens, e.g. when the ransomware protection on Windows is active and
// the I18N commander is not on the exclusion list.
return new ProcessorResult<long>(0, false, $"Cannot write the generator's result file: '{e.Message}'. Hint: Is the ransomware protection enabled in your Windows system? If so, please make sure that the I18N Commander has write permission.");
}
finally
{
if(new FileInfo(pathTemp).Length > 0)
if (File.Exists(pathTemp))
{
destBytesWritten = new FileInfo(pathTemp).Length;
if (destBytesWritten > 0)
{
try
{
if (File.Exists(pathFinal))
File.Delete(pathFinal);
File.Move(pathTemp, pathFinal);
}
catch (IOException e)
{
// Happens when the file is still in use by the compiler, the IDE, etc.
// Depends on the timing, this happens sometimes. We ignore it, though.
issueFinal = e.Message;
}
}
}
}
if(string.IsNullOrWhiteSpace(issueFinal))
return new ProcessorResult<long>(destBytesWritten, true, string.Empty);
else
return new ProcessorResult<long>(0, false, $"Cannot move the generator's result file to the destination: '{issueFinal}'. Hint: Is the ransomware protection enabled in your Windows system? If so, please make sure that the I18N Commander has write permission.");
}
private string AddIndention(int indention) => new string(' ', indention * 3);

View File

@ -12,15 +12,30 @@ public static class Generator
_ => VOID_GENERATOR,
};
public static async Task TriggerAllAsync()
public static async Task<ProcessorResult<long>> TriggerAllAsync()
{
var dotnetEnabled = await AppSettings.GetGeneratorDotnetEnabled();
var godotEnabled = await AppSettings.GetGeneratorGodotEnabled();
long bytesWritten = 0;
if (dotnetEnabled)
await Generator.Get(Type.DOTNET).GenerateAsync();
{
var result = await Generator.Get(Type.DOTNET).GenerateAsync();
if(!result.Successful)
return result;
bytesWritten += result.Result;
}
if(godotEnabled)
await Generator.Get(Type.GODOT).GenerateAsync();
{
var result = await Generator.Get(Type.GODOT).GenerateAsync();
if(!result.Successful)
return result;
bytesWritten += result.Result;
}
return new ProcessorResult<long>(bytesWritten);
}
}

View File

@ -2,5 +2,5 @@
public interface IGenerator
{
public Task GenerateAsync();
public Task<ProcessorResult<long>> GenerateAsync();
}

View File

@ -331,7 +331,9 @@ public partial class SectionTree : UserControl
}
this.buttonGenerate.Enabled = false;
await Generator.TriggerAllAsync();
var result = await Generator.TriggerAllAsync();
result.ProcessError();
this.buttonGenerate.Enabled = true;
}
}

View File

@ -6,7 +6,8 @@ public static class ExtensionsError
{
public static void ProcessError<TResult>(this ProcessorResult<TResult> result)
{
if (result.Successful) return;
if (result.Successful)
return;
MessageBox.Show(result.ErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}