2021-01-13 15:18:59 +11:00
using System ;
2019-06-24 11:58:36 +02:00
using System.Collections ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Reflection ;
using System.Reflection.Emit ;
2021-01-14 23:14:35 +11:00
using System.Runtime.Loader ;
2019-06-24 11:58:36 +02:00
using System.Text ;
using System.Text.RegularExpressions ;
using System.Threading ;
2021-01-14 23:14:35 +11:00
using Microsoft.AspNetCore.Mvc.ApplicationParts ;
2020-09-21 09:27:54 +02:00
using Microsoft.Extensions.Logging ;
2021-01-13 15:18:59 +11:00
using Microsoft.Extensions.Options ;
2019-06-24 11:58:36 +02:00
using Umbraco.Core ;
2021-01-13 15:18:59 +11:00
using Umbraco.Core.Configuration ;
using Umbraco.Core.Configuration.Models ;
2020-01-08 09:01:58 +01:00
using Umbraco.Core.Hosting ;
2019-06-24 11:58:36 +02:00
using Umbraco.Core.Logging ;
using Umbraco.Core.Models.PublishedContent ;
2021-02-18 08:26:08 +01:00
using Umbraco.Infrastructure.ModelsBuilder ;
using Umbraco.Infrastructure.ModelsBuilder.Building ;
2019-06-24 11:58:36 +02:00
2021-02-18 08:26:08 +01:00
namespace Umbraco.Web.Common.ModelsBuilder
2019-06-24 11:58:36 +02:00
{
internal class PureLiveModelFactory : ILivePublishedModelFactory , IRegisteredObject
{
private Infos _infos = new Infos { ModelInfos = null , ModelTypeMap = new Dictionary < string , Type > ( ) } ;
private readonly ReaderWriterLockSlim _locker = new ReaderWriterLockSlim ( ) ;
private volatile bool _hasModels ; // volatile 'cos reading outside lock
private bool _pendingRebuild ;
2020-09-15 08:45:40 +02:00
private readonly IProfilingLogger _profilingLogger ;
2020-09-28 08:26:21 +02:00
private readonly ILogger < PureLiveModelFactory > _logger ;
2019-06-24 11:58:36 +02:00
private readonly FileSystemWatcher _watcher ;
2021-01-13 15:18:59 +11:00
private int _ver ;
private int _skipver ;
2019-06-24 11:58:36 +02:00
private readonly int _debugLevel ;
2020-09-02 14:44:01 +02:00
private RoslynCompiler _roslynCompiler ;
2021-01-15 18:17:13 +11:00
private UmbracoAssemblyLoadContext _currentAssemblyLoadContext ;
2019-10-28 19:08:42 +11:00
private readonly Lazy < UmbracoServices > _umbracoServices ; // fixme: this is because of circular refs :(
2021-01-13 15:18:59 +11:00
private static readonly Regex s_assemblyVersionRegex = new Regex ( "AssemblyVersion\\(\"[0-9]+.[0-9]+.[0-9]+.[0-9]+\"\\)" , RegexOptions . Compiled ) ;
private static readonly string [ ] s_ourFiles = { "models.hash" , "models.generated.cs" , "all.generated.cs" , "all.dll.path" , "models.err" , "Compiled" } ;
2020-09-18 12:53:10 +02:00
private readonly ModelsBuilderSettings _config ;
2020-09-02 14:44:01 +02:00
private readonly IHostingEnvironment _hostingEnvironment ;
2020-03-26 15:39:20 +11:00
private readonly IApplicationShutdownRegistry _hostingLifetime ;
2019-10-28 19:08:42 +11:00
private readonly ModelsGenerationError _errors ;
2020-09-02 14:44:01 +02:00
private readonly IPublishedValueFallback _publishedValueFallback ;
2021-01-14 23:14:35 +11:00
private readonly ApplicationPartManager _applicationPartManager ;
2021-01-13 15:18:59 +11:00
private static readonly Regex s_usingRegex = new Regex ( "^using(.*);" , RegexOptions . Compiled | RegexOptions . Multiline ) ;
private static readonly Regex s_aattrRegex = new Regex ( "^\\[assembly:(.*)\\]" , RegexOptions . Compiled | RegexOptions . Multiline ) ;
2019-06-24 11:58:36 +02:00
2020-03-17 16:26:56 +01:00
public PureLiveModelFactory (
Lazy < UmbracoServices > umbracoServices ,
2020-09-15 08:45:40 +02:00
IProfilingLogger profilingLogger ,
2020-09-28 08:26:21 +02:00
ILogger < PureLiveModelFactory > logger ,
2020-09-18 12:53:10 +02:00
IOptions < ModelsBuilderSettings > config ,
2020-03-17 16:26:56 +01:00
IHostingEnvironment hostingEnvironment ,
2020-03-26 15:39:20 +11:00
IApplicationShutdownRegistry hostingLifetime ,
2021-01-14 23:14:35 +11:00
IPublishedValueFallback publishedValueFallback ,
ApplicationPartManager applicationPartManager )
2019-06-24 11:58:36 +02:00
{
_umbracoServices = umbracoServices ;
2020-09-15 08:45:40 +02:00
_profilingLogger = profilingLogger ;
2019-06-24 11:58:36 +02:00
_logger = logger ;
2020-08-23 15:58:37 +02:00
_config = config . Value ;
2020-09-02 14:44:01 +02:00
_hostingEnvironment = hostingEnvironment ;
2020-03-25 15:06:22 +11:00
_hostingLifetime = hostingLifetime ;
2020-09-02 14:44:01 +02:00
_publishedValueFallback = publishedValueFallback ;
2021-01-14 23:14:35 +11:00
_applicationPartManager = applicationPartManager ;
2020-09-02 14:44:01 +02:00
_errors = new ModelsGenerationError ( config , _hostingEnvironment ) ;
2019-06-24 11:58:36 +02:00
_ver = 1 ; // zero is for when we had no version
_skipver = - 1 ; // nothing to skip
2021-01-13 15:18:59 +11:00
if ( ! hostingEnvironment . IsHosted )
{
return ;
}
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 ) )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
Directory . CreateDirectory ( modelsDirectory ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
// BEWARE! if the watcher is not properly released then for some reason the
// BuildManager will start confusing types - using a 'registered object' here
// though we should probably plug into Umbraco's MainDom - which is internal
2020-03-25 15:06:22 +11:00
_hostingLifetime . RegisterObject ( this ) ;
2019-06-24 11:58:36 +02:00
_watcher = new FileSystemWatcher ( modelsDirectory ) ;
_watcher . Changed + = WatcherOnChanged ;
_watcher . EnableRaisingEvents = true ;
// get it here, this need to be fast
_debugLevel = _config . DebugLevel ;
2021-01-15 18:17:13 +11:00
AssemblyLoadContext . Default . Resolving + = OnResolvingDefaultAssemblyLoadContext ;
2019-06-24 11:58:36 +02:00
}
2021-01-15 18:17:13 +11:00
public event EventHandler ModelsChanged ;
2021-01-13 15:18:59 +11:00
private UmbracoServices UmbracoServices = > _umbracoServices . Value ;
2019-06-24 11:58:36 +02:00
2021-01-14 23:14:35 +11:00
/// <summary>
/// Gets the currently loaded pure live models assembly
/// </summary>
/// <remarks>
/// Can be null
/// </remarks>
public Assembly CurrentModelsAssembly { get ; private set ; }
2019-06-24 11:58:36 +02:00
/// <inheritdoc />
public object SyncRoot { get ; } = new object ( ) ;
2021-01-14 23:14:35 +11:00
/// <summary>
/// Gets the RoslynCompiler
/// </summary>
2021-01-13 15:18:59 +11:00
private RoslynCompiler RoslynCompiler
{
get
{
if ( _roslynCompiler ! = null )
{
return _roslynCompiler ;
}
2021-01-19 15:46:25 +11:00
_roslynCompiler = new RoslynCompiler ( AssemblyLoadContext . All . SelectMany ( x = > x . Assemblies ) ) ;
2021-01-13 15:18:59 +11:00
return _roslynCompiler ;
}
}
/// <inheritdoc />
2021-01-19 18:29:14 +11:00
public bool Enabled = > _config . ModelsMode = = ModelsMode . PureLive ;
2021-01-13 15:18:59 +11:00
2021-01-15 18:17:13 +11:00
/// <summary>
/// Handle the event when a reference cannot be resolved from the default context and return our custom MB assembly reference if we have one
/// </summary>
/// <remarks>
/// This is required because the razor engine will only try to load things from the default context, it doesn't know anything
/// about our context so we need to proxy.
/// </remarks>
private Assembly OnResolvingDefaultAssemblyLoadContext ( AssemblyLoadContext assemblyLoadContext , AssemblyName assemblyName )
= > _currentAssemblyLoadContext ? . LoadFromAssemblyName ( assemblyName ) ;
2019-06-24 11:58:36 +02:00
public IPublishedElement CreateModel ( IPublishedElement element )
{
// get models, rebuilding them if needed
2021-01-13 15:18:59 +11:00
Dictionary < string , ModelInfo > infos = EnsureModels ( ) ? . ModelInfos ;
2019-06-24 11:58:36 +02:00
if ( infos = = null )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
return element ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
// be case-insensitive
var contentTypeAlias = element . ContentType . Alias ;
// lookup model constructor (else null)
2019-10-28 21:53:32 +11:00
infos . TryGetValue ( contentTypeAlias , out var info ) ;
2019-06-24 11:58:36 +02:00
// create model
2020-09-02 14:44:01 +02:00
return info = = null ? element : info . Ctor ( element , _publishedValueFallback ) ;
2019-06-24 11:58:36 +02:00
}
// this runs only once the factory is ready
// NOT when building models
public Type MapModelType ( Type type )
{
2021-01-13 15:18:59 +11:00
Infos infos = EnsureModels ( ) ;
2019-06-24 11:58:36 +02:00
return ModelType . Map ( type , infos . ModelTypeMap ) ;
}
// this runs only once the factory is ready
// NOT when building models
public IList CreateModelList ( string alias )
{
2021-01-13 15:18:59 +11:00
Infos infos = EnsureModels ( ) ;
2019-06-24 11:58:36 +02:00
// fail fast
if ( infos = = null )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
return new List < IPublishedElement > ( ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
2021-01-13 15:18:59 +11:00
if ( ! infos . ModelInfos . TryGetValue ( alias , out ModelInfo modelInfo ) )
{
2019-06-24 11:58:36 +02:00
return new List < IPublishedElement > ( ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
2021-01-13 15:18:59 +11:00
Func < IList > ctor = modelInfo . ListCtor ;
if ( ctor ! = null )
{
return ctor ( ) ;
}
2019-06-24 11:58:36 +02:00
2021-01-13 15:18:59 +11:00
Type listType = typeof ( List < > ) . MakeGenericType ( modelInfo . ModelType ) ;
2019-06-24 11:58:36 +02:00
ctor = modelInfo . ListCtor = ReflectionUtilities . EmitConstructor < Func < IList > > ( declaring : listType ) ;
return ctor ( ) ;
}
2020-04-20 22:25:05 +10:00
/// <inheritdoc />
public void Reset ( )
{
2021-01-15 18:17:13 +11:00
if ( Enabled )
2021-01-13 15:18:59 +11:00
{
2020-04-20 22:25:05 +10:00
ResetModels ( ) ;
2021-01-13 15:18:59 +11:00
}
2020-04-20 22:25:05 +10:00
}
2019-06-24 11:58:36 +02:00
// tells the factory that it should build a new generation of models
private void ResetModels ( )
{
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Resetting models." ) ;
2019-06-24 11:58:36 +02:00
try
{
_locker . EnterWriteLock ( ) ;
_hasModels = false ;
_pendingRebuild = true ;
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 ) )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
Directory . CreateDirectory ( modelsDirectory ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
// clear stuff
var modelsHashFile = Path . Combine ( modelsDirectory , "models.hash" ) ;
var dllPathFile = Path . Combine ( modelsDirectory , "all.dll.path" ) ;
2021-01-13 15:18:59 +11:00
if ( File . Exists ( dllPathFile ) )
{
File . Delete ( dllPathFile ) ;
}
if ( File . Exists ( modelsHashFile ) )
{
File . Delete ( modelsHashFile ) ;
}
2019-06-24 11:58:36 +02:00
}
finally
{
if ( _locker . IsWriteLockHeld )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
_locker . ExitWriteLock ( ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
}
}
// ensure that the factory is running with the lastest generation of models
internal Infos EnsureModels ( )
{
if ( _debugLevel > 0 )
2021-01-13 15:18:59 +11:00
{
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Ensuring models." ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
// don't use an upgradeable lock here because only 1 thread at a time could enter it
try
{
_locker . EnterReadLock ( ) ;
if ( _hasModels )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
return _infos ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
}
finally
{
if ( _locker . IsReadLockHeld )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
_locker . ExitReadLock ( ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
}
try
{
_locker . EnterUpgradeableReadLock ( ) ;
2021-01-13 15:18:59 +11:00
if ( _hasModels )
{
return _infos ;
}
2019-06-24 11:58:36 +02:00
_locker . EnterWriteLock ( ) ;
// we don't have models,
// either they haven't been loaded from the cache yet
// or they have been reseted and are pending a rebuild
2020-09-15 08:45:40 +02:00
using ( _profilingLogger . DebugDuration < PureLiveModelFactory > ( "Get models." , "Got models." ) )
2019-06-24 11:58:36 +02:00
{
try
{
2021-01-19 18:44:19 +11:00
Assembly assembly = GetModelsAssembly ( _pendingRebuild ) ;
2021-01-14 23:14:35 +11:00
CurrentModelsAssembly = assembly ;
2019-06-24 11:58:36 +02:00
2021-01-21 17:58:44 +11:00
// Raise the model changing event.
// NOTE: That on first load, if there is content, this will execute before the razor view engine
// has loaded which means it hasn't yet bound to this event so there's no need to worry about if
// it will be eagerly re-generated unecessarily on first render. BUT we should be aware that if we
// change this to use the event aggregator that will no longer be the case.
ModelsChanged ? . Invoke ( this , new EventArgs ( ) ) ;
2021-01-15 18:17:13 +11:00
2021-01-13 15:18:59 +11:00
IEnumerable < Type > types = assembly . ExportedTypes . Where ( x = > x . Inherits < PublishedContentModel > ( ) | | x . Inherits < PublishedElementModel > ( ) ) ;
2019-06-24 11:58:36 +02:00
_infos = RegisterModels ( types ) ;
2019-10-28 19:08:42 +11:00
_errors . Clear ( ) ;
2019-06-24 11:58:36 +02:00
}
catch ( Exception e )
{
try
{
2020-10-06 14:21:41 +02:00
_logger . LogError ( e , "Failed to build models." ) ;
2020-09-15 08:45:40 +02:00
_logger . LogWarning ( "Running without models." ) ; // be explicit
2019-10-28 19:08:42 +11:00
_errors . Report ( "Failed to build PureLive models." , e ) ;
2019-06-24 11:58:36 +02:00
}
finally
{
2021-01-14 23:14:35 +11:00
CurrentModelsAssembly = null ;
2019-06-24 11:58:36 +02:00
_infos = new Infos { ModelInfos = null , ModelTypeMap = new Dictionary < string , Type > ( ) } ;
}
}
// don't even try again
_hasModels = true ;
}
return _infos ;
}
finally
{
if ( _locker . IsWriteLockHeld )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
_locker . ExitWriteLock ( ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
if ( _locker . IsUpgradeableReadLockHeld )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
_locker . ExitUpgradeableReadLock ( ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
}
}
2021-01-15 18:17:13 +11:00
// This is NOT thread safe but it is only called from within a lock
2021-01-19 18:44:19 +11:00
private Assembly ReloadAssembly ( string pathToAssembly )
2021-01-14 23:14:35 +11:00
{
2021-01-15 18:17:13 +11:00
// If there's a current AssemblyLoadContext, unload it before creating a new one.
if ( ! ( _currentAssemblyLoadContext is null ) )
{
_currentAssemblyLoadContext . Unload ( ) ;
2021-01-14 23:14:35 +11:00
2021-01-15 18:17:13 +11:00
// we need to remove the current part too
ApplicationPart currentPart = _applicationPartManager . ApplicationParts . FirstOrDefault ( x = > x . Name = = RoslynCompiler . GeneratedAssemblyName ) ;
if ( currentPart ! = null )
{
_applicationPartManager . ApplicationParts . Remove ( currentPart ) ;
}
}
2020-09-07 10:19:25 +02:00
2021-01-15 18:17:13 +11:00
// We must create a new assembly load context
// as long as theres a reference to the assembly load context we can't delete the assembly it loaded
_currentAssemblyLoadContext = new UmbracoAssemblyLoadContext ( ) ;
2021-01-14 23:14:35 +11:00
2021-01-19 15:46:25 +11:00
// NOTE: We cannot use in-memory assemblies due to the way the razor engine works which must use
2021-01-15 18:17:13 +11:00
// application parts in order to add references to it's own CSharpCompiler.
// These parts must have real paths since that is how the references are loaded. In that
// case we'll need to work on temp files so that the assembly isn't locked.
// Get a temp file path
2021-01-19 15:46:25 +11:00
// NOTE: We cannot use Path.GetTempFileName(), see this issue:
// https://github.com/dotnet/AspNetCore.Docs/issues/3589 which can cause issues, this is recommended instead
var tempFile = Path . Combine ( Path . GetTempPath ( ) , Path . GetRandomFileName ( ) ) ;
2021-01-14 23:14:35 +11:00
File . Copy ( pathToAssembly , tempFile , true ) ;
2021-01-15 18:17:13 +11:00
2021-01-14 23:14:35 +11:00
// Load it in
2021-01-15 18:17:13 +11:00
Assembly assembly = _currentAssemblyLoadContext . LoadFromAssemblyPath ( tempFile ) ;
2021-01-14 23:14:35 +11:00
// Add the assembly to the application parts - this is required because this is how
// the razor ReferenceManager resolves what to load, see
// https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.Razor.RuntimeCompilation/src/RazorReferenceManager.cs#L53
var partFactory = ApplicationPartFactory . GetApplicationPartFactory ( assembly ) ;
foreach ( ApplicationPart applicationPart in partFactory . GetApplicationParts ( assembly ) )
2020-09-07 09:03:09 +02:00
{
2021-01-14 23:14:35 +11:00
_applicationPartManager . ApplicationParts . Add ( applicationPart ) ;
2020-09-07 10:19:25 +02:00
}
2021-01-14 23:14:35 +11:00
return assembly ;
2020-09-04 10:38:37 +02:00
}
2021-01-15 18:17:13 +11:00
// This is NOT thread safe but it is only called from within a lock
2021-01-19 18:44:19 +11:00
private Assembly GetModelsAssembly ( bool forceRebuild )
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 ) )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
Directory . CreateDirectory ( modelsDirectory ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
2021-01-13 15:18:59 +11:00
IList < TypeModel > typeModels = UmbracoServices . GetAllTypes ( ) ;
2019-10-28 21:53:32 +11:00
var currentHash = TypeModelHasher . Hash ( typeModels ) ;
2019-06-24 11:58:36 +02:00
var modelsHashFile = Path . Combine ( modelsDirectory , "models.hash" ) ;
var modelsSrcFile = Path . Combine ( modelsDirectory , "models.generated.cs" ) ;
var projFile = Path . Combine ( modelsDirectory , "all.generated.cs" ) ;
var dllPathFile = Path . Combine ( modelsDirectory , "all.dll.path" ) ;
// caching the generated models speeds up booting
// currentHash hashes both the types & the user's partials
if ( ! forceRebuild )
{
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Looking for cached models." ) ;
2019-06-24 11:58:36 +02:00
if ( File . Exists ( modelsHashFile ) & & File . Exists ( projFile ) )
{
var cachedHash = File . ReadAllText ( modelsHashFile ) ;
if ( currentHash ! = cachedHash )
{
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Found obsolete cached models." ) ;
2019-06-24 11:58:36 +02:00
forceRebuild = true ;
}
// else cachedHash matches currentHash, we can try to load an existing dll
}
else
{
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Could not find cached models." ) ;
2019-06-24 11:58:36 +02:00
forceRebuild = true ;
}
}
Assembly assembly ;
if ( ! forceRebuild )
{
// try to load the dll directly (avoid rebuilding)
//
// ensure that the .dll file does not have a corresponding .dll.delete file
// as that would mean the the .dll file is going to be deleted and should not
// be re-used - that should not happen in theory, but better be safe
if ( File . Exists ( dllPathFile ) )
{
var dllPath = File . ReadAllText ( dllPathFile ) ;
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( $"Cached models dll at {dllPath}." ) ;
2019-06-24 11:58:36 +02:00
2020-09-03 14:18:09 +02:00
if ( File . Exists ( dllPath ) & & ! File . Exists ( dllPath + ".delete" ) )
2019-06-24 11:58:36 +02:00
{
2021-01-19 18:44:19 +11:00
assembly = ReloadAssembly ( dllPath ) ;
2020-09-09 12:46:23 +02:00
2021-01-13 15:18:59 +11:00
ModelsBuilderAssemblyAttribute attr = assembly . GetCustomAttribute < ModelsBuilderAssemblyAttribute > ( ) ;
2019-06-24 11:58:36 +02:00
if ( attr ! = null & & attr . PureLive & & attr . SourceHash = = currentHash )
{
// if we were to resume at that revision, then _ver would keep increasing
// and that is probably a bad idea - so, we'll always rebuild starting at
// ver 1, but we remember we want to skip that one - so we never end up
// with the "same but different" version of the assembly in memory
_skipver = assembly . GetName ( ) . Version . Revision ;
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Loading cached models (dll)." ) ;
2019-06-24 11:58:36 +02:00
return assembly ;
}
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Cached models dll cannot be loaded (invalid assembly)." ) ;
2019-06-24 11:58:36 +02:00
}
else if ( ! File . Exists ( dllPath ) )
2021-01-13 15:18:59 +11:00
{
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Cached models dll does not exist." ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
else if ( File . Exists ( dllPath + ".delete" ) )
2021-01-13 15:18:59 +11:00
{
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Cached models dll is marked for deletion." ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
else
2021-01-13 15:18:59 +11:00
{
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Cached models dll cannot be loaded (why?)." ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
}
// must reset the version in the file else it would keep growing
// loading cached modules only happens when the app restarts
var text = File . ReadAllText ( projFile ) ;
2021-01-13 15:18:59 +11:00
Match match = s_assemblyVersionRegex . Match ( text ) ;
2019-06-24 11:58:36 +02:00
if ( match . Success )
{
text = text . Replace ( match . Value , "AssemblyVersion(\"0.0.0." + _ver + "\")" ) ;
File . WriteAllText ( projFile , text ) ;
}
_ver + + ;
try
{
2020-09-07 11:47:46 +02:00
var assemblyPath = GetOutputAssemblyPath ( currentHash ) ;
2020-11-19 03:07:05 -08:00
RoslynCompiler . CompileToFile ( projFile , assemblyPath ) ;
2021-01-19 18:44:19 +11:00
assembly = ReloadAssembly ( assemblyPath ) ;
2019-06-24 11:58:36 +02:00
File . WriteAllText ( dllPathFile , assembly . Location ) ;
2020-09-08 11:23:27 +02:00
File . WriteAllText ( modelsHashFile , currentHash ) ;
2020-09-03 14:53:36 +02:00
TryDeleteUnusedAssemblies ( dllPathFile ) ;
2019-06-24 11:58:36 +02:00
}
catch
{
ClearOnFailingToCompile ( dllPathFile , modelsHashFile , projFile ) ;
throw ;
}
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Loading cached models (source)." ) ;
2019-06-24 11:58:36 +02:00
return assembly ;
}
// need to rebuild
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Rebuilding models." ) ;
2019-06-24 11:58:36 +02:00
// generate code, save
var code = GenerateModelsCode ( typeModels ) ;
// add extra attributes,
// PureLiveAssembly helps identifying Assemblies that contain PureLive models
// AssemblyVersion is so that we have a different version for each rebuild
var ver = _ver = = _skipver ? + + _ver : _ver ;
_ver + + ;
2021-01-13 15:18:59 +11:00
string mbAssemblyDirective = $@"[assembly:ModelsBuilderAssembly(PureLive = true, SourceHash = ""{currentHash}" ")]
[assembly:System.Reflection.AssemblyVersion(""0.0.0.{ver}"")] ";
code = code . Replace ( "//ASSATTR" , mbAssemblyDirective ) ;
2019-06-24 11:58:36 +02:00
File . WriteAllText ( modelsSrcFile , code ) ;
// generate proj, save
var projFiles = new Dictionary < string , string >
{
{ "models.generated.cs" , code }
} ;
var proj = GenerateModelsProj ( projFiles ) ;
File . WriteAllText ( projFile , proj ) ;
// compile and register
try
{
2020-09-07 11:47:46 +02:00
var assemblyPath = GetOutputAssemblyPath ( currentHash ) ;
2020-11-19 03:07:05 -08:00
RoslynCompiler . CompileToFile ( projFile , assemblyPath ) ;
2021-01-19 18:44:19 +11:00
assembly = ReloadAssembly ( assemblyPath ) ;
2020-09-07 10:19:25 +02:00
File . WriteAllText ( dllPathFile , assemblyPath ) ;
2019-06-24 11:58:36 +02:00
File . WriteAllText ( modelsHashFile , currentHash ) ;
2020-09-03 14:53:36 +02:00
TryDeleteUnusedAssemblies ( dllPathFile ) ;
2019-06-24 11:58:36 +02:00
}
catch
{
ClearOnFailingToCompile ( dllPathFile , modelsHashFile , projFile ) ;
throw ;
}
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Done rebuilding." ) ;
2019-06-24 11:58:36 +02:00
return assembly ;
}
2020-09-04 10:38:37 +02:00
private void TryDeleteUnusedAssemblies ( string dllPathFile )
2020-09-03 14:18:09 +02:00
{
if ( File . Exists ( dllPathFile ) )
{
var dllPath = File . ReadAllText ( dllPathFile ) ;
2021-01-13 15:18:59 +11:00
DirectoryInfo dirInfo = new DirectoryInfo ( dllPath ) . Parent ;
IEnumerable < FileInfo > files = dirInfo . GetFiles ( ) . Where ( f = > f . FullName ! = dllPath ) ;
foreach ( FileInfo file in files )
2020-09-03 14:53:36 +02:00
{
try
{
File . Delete ( file . FullName ) ;
}
2021-01-13 15:18:59 +11:00
catch ( UnauthorizedAccessException )
2020-09-03 14:53:36 +02:00
{
// The file is in use, we'll try again next time...
2020-09-07 10:19:25 +02:00
// This shouldn't happen anymore.
2020-09-03 14:53:36 +02:00
}
}
2020-09-03 14:18:09 +02:00
}
}
2020-09-02 15:35:05 +02:00
private string GetOutputAssemblyPath ( string currentHash )
{
2020-09-08 10:21:11 +02:00
var dirInfo = new DirectoryInfo ( Path . Combine ( _config . ModelsDirectoryAbsolute ( _hostingEnvironment ) , "Compiled" ) ) ;
2020-09-02 15:35:05 +02:00
if ( ! dirInfo . Exists )
2021-01-13 15:18:59 +11:00
{
2020-09-03 15:03:02 +02:00
Directory . CreateDirectory ( dirInfo . FullName ) ;
2021-01-13 15:18:59 +11:00
}
2020-09-02 15:46:48 +02:00
return Path . Combine ( dirInfo . FullName , $"generated.cs{currentHash}.dll" ) ;
2020-09-02 15:35:05 +02:00
}
2019-06-24 11:58:36 +02:00
private void ClearOnFailingToCompile ( string dllPathFile , string modelsHashFile , string projFile )
{
2020-09-15 08:45:40 +02:00
_logger . LogDebug ( "Failed to compile." ) ;
2019-06-24 11:58:36 +02:00
// the dll file reference still points to the previous dll, which is obsolete
// now and will be deleted by ASP.NET eventually, so better clear that reference.
// also touch the proj file to force views to recompile - don't delete as it's
// useful to have the source around for debugging.
try
{
2021-01-13 15:18:59 +11:00
if ( File . Exists ( dllPathFile ) )
{
File . Delete ( dllPathFile ) ;
}
if ( File . Exists ( modelsHashFile ) )
{
File . Delete ( modelsHashFile ) ;
}
if ( File . Exists ( projFile ) )
{
File . SetLastWriteTime ( projFile , DateTime . Now ) ;
}
2019-06-24 11:58:36 +02:00
}
catch { /* enough */ }
}
private static Infos RegisterModels ( IEnumerable < Type > types )
{
2021-01-13 15:18:59 +11:00
Type [ ] ctorArgTypes = new [ ] { typeof ( IPublishedElement ) , typeof ( IPublishedValueFallback ) } ;
2019-06-24 11:58:36 +02:00
var modelInfos = new Dictionary < string , ModelInfo > ( StringComparer . InvariantCultureIgnoreCase ) ;
var map = new Dictionary < string , Type > ( ) ;
2021-01-13 15:18:59 +11:00
foreach ( Type type in types )
2019-06-24 11:58:36 +02:00
{
ConstructorInfo constructor = null ;
Type parameterType = null ;
2021-01-13 15:18:59 +11:00
foreach ( ConstructorInfo ctor in type . GetConstructors ( ) )
2019-06-24 11:58:36 +02:00
{
2021-01-13 15:18:59 +11:00
ParameterInfo [ ] parms = ctor . GetParameters ( ) ;
2020-09-02 14:44:01 +02:00
if ( parms . Length = = 2 & & typeof ( IPublishedElement ) . IsAssignableFrom ( parms [ 0 ] . ParameterType ) & & typeof ( IPublishedValueFallback ) . IsAssignableFrom ( parms [ 1 ] . ParameterType ) )
2019-06-24 11:58:36 +02:00
{
if ( constructor ! = null )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
throw new InvalidOperationException ( $"Type {type.FullName} has more than one public constructor with one argument of type, or implementing, IPropertySet." ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
constructor = ctor ;
parameterType = parms [ 0 ] . ParameterType ;
}
}
if ( constructor = = null )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
throw new InvalidOperationException ( $"Type {type.FullName} is missing a public constructor with one argument of type, or implementing, IPropertySet." ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
2021-01-13 15:18:59 +11:00
PublishedModelAttribute attribute = type . GetCustomAttribute < PublishedModelAttribute > ( false ) ;
2019-06-24 11:58:36 +02:00
var typeName = attribute = = null ? type . Name : attribute . ContentTypeAlias ;
2019-10-28 21:53:32 +11:00
if ( modelInfos . TryGetValue ( typeName , out var modelInfo ) )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
throw new InvalidOperationException ( $"Both types {type.FullName} and {modelInfo.ModelType.FullName} want to be a model type for content type with alias \" { typeName } \ "." ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
2020-07-14 10:08:59 +10:00
// TODO: use Core's ReflectionUtilities.EmitCtor !!
2019-10-28 19:08:42 +11:00
// Yes .. DynamicMethod is uber slow
2020-07-14 10:08:59 +10:00
// TODO: But perhaps https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.constructorbuilder?view=netcore-3.1 is better still?
// See CtorInvokeBenchmarks
2019-10-28 21:53:32 +11:00
var meth = new DynamicMethod ( string . Empty , typeof ( IPublishedElement ) , ctorArgTypes , type . Module , true ) ;
2021-01-13 15:18:59 +11:00
ILGenerator gen = meth . GetILGenerator ( ) ;
2019-06-24 11:58:36 +02:00
gen . Emit ( OpCodes . Ldarg_0 ) ;
2020-09-02 15:18:22 +02:00
gen . Emit ( OpCodes . Ldarg_1 ) ;
2019-06-24 11:58:36 +02:00
gen . Emit ( OpCodes . Newobj , constructor ) ;
gen . Emit ( OpCodes . Ret ) ;
2020-09-02 14:44:01 +02:00
var func = ( Func < IPublishedElement , IPublishedValueFallback , IPublishedElement > ) meth . CreateDelegate ( typeof ( Func < IPublishedElement , IPublishedValueFallback , IPublishedElement > ) ) ;
2019-06-24 11:58:36 +02:00
modelInfos [ typeName ] = new ModelInfo { ParameterType = parameterType , Ctor = func , ModelType = type } ;
map [ typeName ] = type ;
}
return new Infos { ModelInfos = modelInfos . Count > 0 ? modelInfos : null , ModelTypeMap = map } ;
}
private string GenerateModelsCode ( IList < TypeModel > typeModels )
{
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 ) )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
Directory . CreateDirectory ( modelsDirectory ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
foreach ( var file in Directory . GetFiles ( modelsDirectory , "*.generated.cs" ) )
2021-01-13 15:18:59 +11:00
{
2019-06-24 11:58:36 +02:00
File . Delete ( file ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
2019-10-28 18:02:52 +11:00
var builder = new TextBuilder ( _config , typeModels ) ;
2019-06-24 11:58:36 +02:00
var codeBuilder = new StringBuilder ( ) ;
builder . Generate ( codeBuilder , builder . GetModelsToGenerate ( ) ) ;
var code = codeBuilder . ToString ( ) ;
return code ;
}
private static string GenerateModelsProj ( IDictionary < string , string > files )
{
// ideally we would generate a CSPROJ file but then we'd need a BuildProvider for csproj
// trying to keep things simple for the time being, just write everything to one big file
// group all 'using' at the top of the file (else fails)
var usings = new List < string > ( ) ;
2021-01-13 15:18:59 +11:00
foreach ( string k in files . Keys . ToList ( ) )
{
files [ k ] = s_usingRegex . Replace ( files [ k ] , m = >
2019-06-24 11:58:36 +02:00
{
usings . Add ( m . Groups [ 1 ] . Value ) ;
return string . Empty ;
} ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
// group all '[assembly:...]' at the top of the file (else fails)
var aattrs = new List < string > ( ) ;
2021-01-13 15:18:59 +11:00
foreach ( string k in files . Keys . ToList ( ) )
{
files [ k ] = s_aattrRegex . Replace ( files [ k ] , m = >
2019-06-24 11:58:36 +02:00
{
aattrs . Add ( m . Groups [ 1 ] . Value ) ;
return string . Empty ;
} ) ;
2021-01-13 15:18:59 +11:00
}
2019-06-24 11:58:36 +02:00
var text = new StringBuilder ( ) ;
foreach ( var u in usings . Distinct ( ) )
{
text . Append ( "using " ) ;
text . Append ( u ) ;
text . Append ( ";\r\n" ) ;
}
2021-01-13 15:18:59 +11:00
2019-06-24 11:58:36 +02:00
foreach ( var a in aattrs )
{
text . Append ( "[assembly:" ) ;
text . Append ( a ) ;
text . Append ( "]\r\n" ) ;
}
2021-01-13 15:18:59 +11:00
2019-06-24 11:58:36 +02:00
text . Append ( "\r\n\r\n" ) ;
2021-01-13 15:18:59 +11:00
foreach ( KeyValuePair < string , string > f in files )
2019-06-24 11:58:36 +02:00
{
text . Append ( "// FILE: " ) ;
text . Append ( f . Key ) ;
text . Append ( "\r\n\r\n" ) ;
text . Append ( f . Value ) ;
text . Append ( "\r\n\r\n\r\n" ) ;
}
2021-01-13 15:18:59 +11:00
2019-06-24 11:58:36 +02:00
text . Append ( "// EOF\r\n" ) ;
return text . ToString ( ) ;
}
private void WatcherOnChanged ( object sender , FileSystemEventArgs args )
{
var changed = args . Name ;
// don't reset when our files change because we are building!
//
// comment it out, and always ignore our files, because it seems that some
// race conditions can occur on slow Cloud filesystems and then we keep
// rebuilding
//if (_building && OurFiles.Contains(changed))
//{
2020-09-15 08:45:40 +02:00
// //_logger.LogInformation<PureLiveModelFactory>("Ignoring files self-changes.");
2019-06-24 11:58:36 +02:00
// return;
//}
// always ignore our own file changes
2021-01-13 15:18:59 +11:00
if ( s_ourFiles . Contains ( changed ) )
2021-01-14 23:14:35 +11:00
{
2019-06-24 11:58:36 +02:00
return ;
2021-01-14 23:14:35 +11:00
}
2019-06-24 11:58:36 +02:00
2020-09-15 08:45:40 +02:00
_logger . LogInformation ( "Detected files changes." ) ;
2019-06-24 11:58:36 +02:00
lock ( SyncRoot ) // don't reset while being locked
2021-01-14 23:14:35 +11:00
{
2019-06-24 11:58:36 +02:00
ResetModels ( ) ;
2021-01-14 23:14:35 +11:00
}
2019-06-24 11:58:36 +02:00
}
public void Stop ( bool immediate )
{
_watcher . EnableRaisingEvents = false ;
_watcher . Dispose ( ) ;
2020-03-25 15:06:22 +11:00
_hostingLifetime . UnregisterObject ( this ) ;
2019-06-24 11:58:36 +02:00
}
2021-01-13 15:18:59 +11:00
internal class Infos
{
public Dictionary < string , Type > ModelTypeMap { get ; set ; }
public Dictionary < string , ModelInfo > ModelInfos { get ; set ; }
}
internal class ModelInfo
{
public Type ParameterType { get ; set ; }
public Func < IPublishedElement , IPublishedValueFallback , IPublishedElement > Ctor { get ; set ; }
public Type ModelType { get ; set ; }
public Func < IList > ListCtor { get ; set ; }
}
2019-06-24 11:58:36 +02:00
}
}