Skip to content

Commit ba4a874

Browse files
authored
Models Builder: Make Models Builder better at not performing "rude edits" (#20394)
Make Models Builder better at not performing rude edits
1 parent 1f35124 commit ba4a874

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

src/Umbraco.Core/Configuration/Models/ModelsBuilderSettings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ModelsBuilderSettings
1717
internal const bool StaticAcceptUnsafeModelsDirectory = false;
1818
internal const int StaticDebugLevel = 0;
1919
internal const bool StaticIncludeVersionNumberInGeneratedModels = true;
20+
internal const bool StaticGenerateVirtualProperties = true;
2021
private bool _flagOutOfDateModels = true;
2122

2223
/// <summary>
@@ -77,4 +78,13 @@ public bool FlagOutOfDateModels
7778
/// </remarks>
7879
[DefaultValue(StaticIncludeVersionNumberInGeneratedModels)]
7980
public bool IncludeVersionNumberInGeneratedModels { get; set; } = StaticIncludeVersionNumberInGeneratedModels;
81+
82+
/// <summary>
83+
/// Gets or sets a value indicating whether to mark all properties in the generated models as virtual.
84+
/// </summary>
85+
/// <remarks>
86+
/// Virtual properties will not work with Hot Reload when running dotnet watch.
87+
/// </remarks>
88+
[DefaultValue(StaticGenerateVirtualProperties)]
89+
public bool GenerateVirtualProperties { get; set; } = StaticGenerateVirtualProperties;
8090
}

src/Umbraco.Infrastructure/ModelsBuilder/Building/ModelsGenerator.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,37 @@ public void GenerateModels()
3131
Directory.CreateDirectory(modelsDirectory);
3232
}
3333

34-
foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs"))
35-
{
36-
File.Delete(file);
37-
}
38-
3934
IList<TypeModel> typeModels = _umbracoService.GetAllTypes();
4035

4136
var builder = new TextBuilder(_config, typeModels);
4237

38+
var generatedFiles = new List<string>();
4339
foreach (TypeModel typeModel in builder.GetModelsToGenerate())
4440
{
4541
var sb = new StringBuilder();
4642
builder.Generate(sb, typeModel);
4743
var filename = Path.Combine(modelsDirectory, typeModel.ClrName + ".generated.cs");
48-
File.WriteAllText(filename, sb.ToString());
44+
generatedFiles.Add(filename);
45+
46+
var code = sb.ToString();
47+
48+
// leave the file alone if its contents is identical to the generated model
49+
if (File.Exists(filename) && File.ReadAllText(filename).Equals(code))
50+
{
51+
continue;
52+
}
53+
54+
// overwrite the file
55+
File.WriteAllText(filename, code);
56+
}
57+
58+
// clean up old/leftover generated files
59+
foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs"))
60+
{
61+
if (generatedFiles.InvariantContains(file) is false)
62+
{
63+
File.Delete(file);
64+
}
4965
}
5066

5167
// the idea was to calculate the current hash and to add it as an extra file to the compilation,

src/Umbraco.Infrastructure/ModelsBuilder/Building/TextBuilder.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,11 @@ private void WriteMixinProperty(StringBuilder sb, PropertyModel property, string
384384

385385
sb.AppendFormat("\t\t[ImplementPropertyType(\"{0}\")]\n", property.Alias);
386386

387-
sb.Append("\t\tpublic virtual ");
387+
sb.Append("\t\tpublic ");
388+
if (Config.GenerateVirtualProperties)
389+
{
390+
sb.Append("virtual ");
391+
}
388392
WriteClrType(sb, property.ClrTypeName);
389393

390394
sb.AppendFormat(
@@ -460,15 +464,23 @@ private void WriteProperty(StringBuilder sb, TypeModel type, PropertyModel prope
460464

461465
if (mixinStatic)
462466
{
463-
sb.Append("\t\tpublic virtual ");
467+
sb.Append("\t\tpublic ");
468+
if (Config.GenerateVirtualProperties)
469+
{
470+
sb.Append("virtual ");
471+
}
464472
WriteClrType(sb, property.ClrTypeName);
465473
sb.AppendFormat(
466474
" {0} => {1}(this, _publishedValueFallback);\n",
467475
property.ClrName, MixinStaticGetterName(property.ClrName));
468476
}
469477
else
470478
{
471-
sb.Append("\t\tpublic virtual ");
479+
sb.Append("\t\tpublic ");
480+
if (Config.GenerateVirtualProperties)
481+
{
482+
sb.Append("virtual ");
483+
}
472484
WriteClrType(sb, property.ClrTypeName);
473485
sb.AppendFormat(
474486
" {0} => this.Value",

0 commit comments

Comments
 (0)