Reuse compiled assembly again if no changes are detected

It's not pretty, but it works...
This commit is contained in:
Nikolaj
2020-09-03 10:31:45 +02:00
parent 624e57f304
commit aa1807bd5a
2 changed files with 39 additions and 20 deletions

View File

@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Umbraco.ModelsBuilder.Embedded.Building
{
@@ -7,38 +10,53 @@ namespace Umbraco.ModelsBuilder.Embedded.Building
{
public static string Hash(IEnumerable<TypeModel> typeModels)
{
var hash = new HashCombiner();
var builder = new StringBuilder();
// see Umbraco.ModelsBuilder.Umbraco.Application for what's important to hash
// ie what comes from Umbraco (not computed by ModelsBuilder) and makes a difference
foreach (var typeModel in typeModels.OrderBy(x => x.Alias))
{
hash.Add("--- CONTENT TYPE MODEL ---");
hash.Add(typeModel.Id);
hash.Add(typeModel.Alias);
hash.Add(typeModel.ClrName);
hash.Add(typeModel.ParentId);
hash.Add(typeModel.Name);
hash.Add(typeModel.Description);
hash.Add(typeModel.ItemType.ToString());
hash.Add("MIXINS:" + string.Join(",", typeModel.MixinTypes.OrderBy(x => x.Id).Select(x => x.Id)));
builder.Append("--- CONTENT TYPE MODEL ---");
builder.Append(typeModel.Id);
builder.Append(typeModel.Alias);
builder.Append(typeModel.ClrName);
builder.Append(typeModel.ParentId);
builder.Append(typeModel.Name);
builder.Append(typeModel.Description);
builder.Append(typeModel.ItemType.ToString());
builder.Append("MIXINS:" + string.Join(",", typeModel.MixinTypes.OrderBy(x => x.Id).Select(x => x.Id)));
foreach (var prop in typeModel.Properties.OrderBy(x => x.Alias))
{
hash.Add("--- PROPERTY ---");
hash.Add(prop.Alias);
hash.Add(prop.ClrName);
hash.Add(prop.Name);
hash.Add(prop.Description);
hash.Add(prop.ModelClrType.ToString()); // see ModelType tests, want ToString() not FullName
builder.Append("--- PROPERTY ---");
builder.Append(prop.Alias);
builder.Append(prop.ClrName);
builder.Append(prop.Name);
builder.Append(prop.Description);
builder.Append(prop.ModelClrType.ToString()); // see ModelType tests, want ToString() not FullName
}
}
// Include the MB version in the hash so that if the MB version changes, models are rebuilt
hash.Add(ApiVersion.Current.Version.ToString());
builder.Append(ApiVersion.Current.Version.ToString());
return hash.GetCombinedHashCode();
return GenerateHash(builder.ToString());
}
private static string GenerateHash(string input)
{
using (var md5 = MD5.Create())
{
var hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
var builder = new StringBuilder();
foreach(var b in hashBytes)
{
builder.Append(b.ToString("X"));
}
return builder.ToString();
}
}
}
}