2019-06-24 11:58:36 +02:00
|
|
|
|
using System.IO;
|
2020-09-11 21:13:18 +02:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2020-03-03 13:42:07 +01:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2020-09-02 14:44:01 +02:00
|
|
|
|
using Umbraco.Core.Hosting;
|
2020-08-23 15:58:37 +02:00
|
|
|
|
using Umbraco.Core.Configuration.Models;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
using Umbraco.Web.Cache;
|
|
|
|
|
|
|
2019-10-29 00:25:03 +11:00
|
|
|
|
namespace Umbraco.ModelsBuilder.Embedded
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
public sealed class OutOfDateModelsStatus
|
|
|
|
|
|
{
|
2020-09-18 12:53:10 +02:00
|
|
|
|
private readonly ModelsBuilderSettings _config;
|
2020-09-02 14:44:01 +02:00
|
|
|
|
private readonly IHostingEnvironment _hostingEnvironment;
|
2019-10-28 18:02:52 +11:00
|
|
|
|
|
2020-09-18 12:53:10 +02:00
|
|
|
|
public OutOfDateModelsStatus(IOptions<ModelsBuilderSettings> config, IHostingEnvironment hostingEnvironment)
|
2019-10-28 19:08:42 +11:00
|
|
|
|
{
|
2020-09-11 21:13:18 +02:00
|
|
|
|
_config = config.Value;
|
2020-09-02 14:44:01 +02:00
|
|
|
|
_hostingEnvironment = hostingEnvironment;
|
2019-10-28 18:02:52 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-28 19:08:42 +11:00
|
|
|
|
internal void Install()
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
// just be sure
|
2019-10-28 19:08:42 +11:00
|
|
|
|
if (_config.FlagOutOfDateModels == false)
|
2019-06-24 11:58:36 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
ContentTypeCacheRefresher.CacheUpdated += (sender, args) => Write();
|
|
|
|
|
|
DataTypeCacheRefresher.CacheUpdated += (sender, args) => Write();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-28 19:08:42 +11:00
|
|
|
|
private string GetFlagPath()
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
2020-09-02 14:44:01 +02:00
|
|
|
|
var modelsDirectory = _config.ModelsDirectoryAbsolute(_hostingEnvironment);
|
2019-06-24 11:58:36 +02:00
|
|
|
|
if (!Directory.Exists(modelsDirectory))
|
|
|
|
|
|
Directory.CreateDirectory(modelsDirectory);
|
|
|
|
|
|
return Path.Combine(modelsDirectory, "ood.flag");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-28 19:08:42 +11:00
|
|
|
|
private void Write()
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
var path = GetFlagPath();
|
|
|
|
|
|
if (path == null || File.Exists(path)) return;
|
|
|
|
|
|
File.WriteAllText(path, "THIS FILE INDICATES THAT MODELS ARE OUT-OF-DATE\n\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-28 19:08:42 +11:00
|
|
|
|
public void Clear()
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
2019-10-28 19:08:42 +11:00
|
|
|
|
if (_config.FlagOutOfDateModels == false) return;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
var path = GetFlagPath();
|
|
|
|
|
|
if (path == null || !File.Exists(path)) return;
|
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-28 19:08:42 +11:00
|
|
|
|
public bool IsEnabled => _config.FlagOutOfDateModels;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
2019-10-28 19:08:42 +11:00
|
|
|
|
public bool IsOutOfDate
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2019-10-28 19:08:42 +11:00
|
|
|
|
if (_config.FlagOutOfDateModels == false) return false;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
var path = GetFlagPath();
|
|
|
|
|
|
return path != null && File.Exists(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|