2019-06-24 11:58:36 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
2020-03-03 13:42:07 +01:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2020-03-17 16:26:56 +01:00
|
|
|
|
using Umbraco.Core.IO;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
2019-10-29 00:25:03 +11:00
|
|
|
|
namespace Umbraco.ModelsBuilder.Embedded
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
2019-10-28 19:08:42 +11:00
|
|
|
|
public sealed class ModelsGenerationError
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
2019-10-28 18:02:52 +11:00
|
|
|
|
private readonly IModelsBuilderConfig _config;
|
2020-03-17 16:26:56 +01:00
|
|
|
|
private readonly IIOHelper _ioHelper;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
2020-03-17 16:26:56 +01:00
|
|
|
|
public ModelsGenerationError(IModelsBuilderConfig config, IIOHelper ioHelper)
|
2019-10-28 18:02:52 +11:00
|
|
|
|
{
|
|
|
|
|
|
_config = config;
|
2020-03-17 16:26:56 +01:00
|
|
|
|
_ioHelper = ioHelper;
|
2019-10-28 18:02:52 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
var errFile = GetErrFile();
|
|
|
|
|
|
if (errFile == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
// "If the file to be deleted does not exist, no exception is thrown."
|
|
|
|
|
|
File.Delete(errFile);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-28 18:02:52 +11:00
|
|
|
|
public void Report(string message, Exception e)
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
var errFile = GetErrFile();
|
|
|
|
|
|
if (errFile == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
sb.Append(message);
|
|
|
|
|
|
sb.Append("\r\n");
|
|
|
|
|
|
sb.Append(e.Message);
|
|
|
|
|
|
sb.Append("\r\n\r\n");
|
|
|
|
|
|
sb.Append(e.StackTrace);
|
|
|
|
|
|
sb.Append("\r\n");
|
|
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(errFile, sb.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-28 18:02:52 +11:00
|
|
|
|
public string GetLastError()
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
var errFile = GetErrFile();
|
|
|
|
|
|
if (errFile == null) return null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return File.ReadAllText(errFile);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch // accepted
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-28 18:02:52 +11:00
|
|
|
|
private string GetErrFile()
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
2020-03-17 16:26:56 +01:00
|
|
|
|
var modelsDirectory = _config.ModelsDirectoryAbsolute(_ioHelper);
|
2019-06-24 11:58:36 +02:00
|
|
|
|
if (!Directory.Exists(modelsDirectory))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
return Path.Combine(modelsDirectory, "models.err");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|