V9 - Include models folder in the project (#10250)

* fixes #10213. When models folder is included in project the models have a Build Action of C# Compiler

* models directory comment indicates correct models location

* added PureLive files to App_Data/Temp

* changed PureLive directory

* include generated models in project

* https://github.com/umbraco/Umbraco-CMS/pull/10250/ - Cleanup in csproj file and fix the generated template too

Co-authored-by: Mario Lopez <mario@monkii.com>
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
Mario Lopez
2021-05-17 17:50:58 +10:00
committed by GitHub
parent 0ae3a2d7ef
commit 69a507e5db
4 changed files with 36 additions and 32 deletions

View File

@@ -18,16 +18,13 @@
<Compile Remove="umbraco\Data\**" />
<Compile Remove="umbraco\logs\**" />
<Compile Remove="umbraco\MediaCache\**" />
<Compile Remove="umbraco\models\**" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Remove="umbraco\Data\**" />
<EmbeddedResource Remove="umbraco\logs\**" />
<EmbeddedResource Remove="umbraco\MediaCache\**" />
<EmbeddedResource Remove="umbraco\models\**" />
</ItemGroup>
<ItemGroup>
<None Remove="umbraco\models\**" />
<None Remove="umbraco\Data\**" />
<None Remove="umbraco\logs\**" />
<None Remove="umbraco\MediaCache\**" />
@@ -44,7 +41,6 @@
<Content Remove="umbraco\Data\**" />
<Content Remove="umbraco\logs\**" />
<Content Remove="umbraco\MediaCache\**" />
<Content Remove="umbraco\models\**" />
</ItemGroup>
<!--Set this to true ModelsBuilder mode is not PureLive -->

View File

@@ -30,7 +30,7 @@ namespace Umbraco.Cms.Core.Configuration.Models
/// </summary>
/// <remarks>
/// Models become out-of-date when data types or content types are updated. When this
/// setting is activated the ~/App_Data/Models/ood.txt file is then created. When models are
/// setting is activated the ~/umbraco/models/PureLive/ood.txt file is then created. When models are
/// generated through the dashboard, the files is cleared. Default value is <c>false</c>.
/// </remarks>
public bool FlagOutOfDateModels
@@ -51,9 +51,10 @@ namespace Umbraco.Cms.Core.Configuration.Models
/// <summary>
/// Gets or sets a value for the models directory.
/// </summary>
/// <remarks>Default is ~/App_Data/Models but that can be changed.</remarks>
/// <remarks>Default is ~/umbraco/models but that can be changed.</remarks>
public string ModelsDirectory { get; set; } = DefaultModelsDirectory;
/// <summary>
/// Gets or sets a value indicating whether to accept an unsafe value for ModelsDirectory.
/// </summary>

View File

@@ -50,6 +50,9 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
private readonly ApplicationPartManager _applicationPartManager;
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);
private readonly Lazy<string> _pureLiveDirectory;
public PureLiveModelFactory(
Lazy<UmbracoServices> umbracoServices,
@@ -77,17 +80,18 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
return;
}
var modelsDirectory = _config.ModelsDirectoryAbsolute(_hostingEnvironment);
if (!Directory.Exists(modelsDirectory))
_pureLiveDirectory = new Lazy<string>(PureLiveDirectoryAbsolute);
if (!Directory.Exists(_pureLiveDirectory.Value))
{
Directory.CreateDirectory(modelsDirectory);
Directory.CreateDirectory(_pureLiveDirectory.Value);
}
// 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
_hostingLifetime.RegisterObject(this);
_watcher = new FileSystemWatcher(modelsDirectory);
_watcher = new FileSystemWatcher(_pureLiveDirectory.Value);
_watcher.Changed += WatcherOnChanged;
_watcher.EnableRaisingEvents = true;
@@ -218,15 +222,14 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
_hasModels = false;
_pendingRebuild = true;
var modelsDirectory = _config.ModelsDirectoryAbsolute(_hostingEnvironment);
if (!Directory.Exists(modelsDirectory))
if (!Directory.Exists(_pureLiveDirectory.Value))
{
Directory.CreateDirectory(modelsDirectory);
Directory.CreateDirectory(_pureLiveDirectory.Value);
}
// clear stuff
var modelsHashFile = Path.Combine(modelsDirectory, "models.hash");
var dllPathFile = Path.Combine(modelsDirectory, "all.dll.path");
var modelsHashFile = Path.Combine(_pureLiveDirectory.Value, "models.hash");
var dllPathFile = Path.Combine(_pureLiveDirectory.Value, "all.dll.path");
if (File.Exists(dllPathFile))
{
@@ -340,6 +343,10 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
}
}
public string PureLiveDirectoryAbsolute() => _hostingEnvironment.MapPathContentRoot("~/umbraco/Data/TEMP/PureLive");
// This is NOT thread safe but it is only called from within a lock
private Assembly ReloadAssembly(string pathToAssembly)
{
@@ -389,18 +396,18 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
// This is NOT thread safe but it is only called from within a lock
private Assembly GetModelsAssembly(bool forceRebuild)
{
var modelsDirectory = _config.ModelsDirectoryAbsolute(_hostingEnvironment);
if (!Directory.Exists(modelsDirectory))
if (!Directory.Exists(_pureLiveDirectory.Value))
{
Directory.CreateDirectory(modelsDirectory);
Directory.CreateDirectory(_pureLiveDirectory.Value);
}
IList<TypeModel> typeModels = UmbracoServices.GetAllTypes();
var currentHash = TypeModelHasher.Hash(typeModels);
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");
var modelsHashFile = Path.Combine(_pureLiveDirectory.Value, "models.hash");
var modelsSrcFile = Path.Combine(_pureLiveDirectory.Value, "models.generated.cs");
var projFile = Path.Combine(_pureLiveDirectory.Value, "all.generated.cs");
var dllPathFile = Path.Combine(_pureLiveDirectory.Value, "all.dll.path");
// caching the generated models speeds up booting
// currentHash hashes both the types & the user's partials
@@ -569,7 +576,7 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
private string GetOutputAssemblyPath(string currentHash)
{
var dirInfo = new DirectoryInfo(Path.Combine(_config.ModelsDirectoryAbsolute(_hostingEnvironment), "Compiled"));
var dirInfo = new DirectoryInfo(Path.Combine(_pureLiveDirectory.Value, "Compiled"));
if (!dirInfo.Exists)
{
Directory.CreateDirectory(dirInfo.FullName);
@@ -578,6 +585,7 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
return Path.Combine(dirInfo.FullName, $"generated.cs{currentHash}.dll");
}
private void ClearOnFailingToCompile(string dllPathFile, string modelsHashFile, string projFile)
{
_logger.LogDebug("Failed to compile.");
@@ -666,13 +674,13 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
private string GenerateModelsCode(IList<TypeModel> typeModels)
{
var modelsDirectory = _config.ModelsDirectoryAbsolute(_hostingEnvironment);
if (!Directory.Exists(modelsDirectory))
if (!Directory.Exists(_pureLiveDirectory.Value))
{
Directory.CreateDirectory(modelsDirectory);
Directory.CreateDirectory(_pureLiveDirectory.Value);
}
foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs"))
foreach (var file in Directory.GetFiles(_pureLiveDirectory.Value, "*.generated.cs"))
{
File.Delete(file);
}
@@ -686,6 +694,8 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
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

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
@@ -28,7 +28,6 @@
<Compile Remove="umbraco\Data\**" />
<Compile Remove="umbraco\logs\**" />
<Compile Remove="umbraco\MediaCache\**" />
<Compile Remove="umbraco\models\**" />
<Compile Remove="wwwroot\Umbraco\**" />
<Compile Remove="App_Data\**" />
</ItemGroup>
@@ -38,7 +37,6 @@
<EmbeddedResource Remove="umbraco\Data\**" />
<EmbeddedResource Remove="umbraco\logs\**" />
<EmbeddedResource Remove="umbraco\MediaCache\**" />
<EmbeddedResource Remove="umbraco\models\**" />
</ItemGroup>
<ItemGroup>
@@ -46,7 +44,6 @@
<Content Remove="umbraco\Data\**" />
<Content Remove="umbraco\logs\**" />
<Content Remove="umbraco\MediaCache\**" />
<Content Remove="umbraco\models\**" />
<Content Remove="wwwroot\Web.config" />
</ItemGroup>
@@ -64,9 +61,9 @@
<None Remove="umbraco\Data\**" />
<None Remove="umbraco\logs\**" />
<None Remove="umbraco\MediaCache\**" />
<None Remove="umbraco\models\**" />
<None Include="umbraco\UmbracoWebsite\NoNodes.cshtml" />
<None Remove="scripts\aaa\fc75309db05f41609a9e1adb8cf0998c.tmp" />
<None Remove="umbraco\models\**" />
</ItemGroup>
<!-- We don't want to include the generated files, they will throw a lot of errors -->