Minor code refactoring

Spmplify/relayer things a little bit

--HG--
branch : 1.x
This commit is contained in:
Renaud Paquay
2011-05-29 02:42:57 -07:00
parent 933b625bf0
commit a281658f9a
6 changed files with 54 additions and 96 deletions

View File

@@ -74,7 +74,7 @@ namespace Orchard.Environment.Extensions {
// And finally save the new entries in the dependencies folder
_dependenciesFolder.StoreDescriptors(context.NewDependencies);
_extensionDependenciesManager.StoreDependencies(context.NewDependencies, path => GetFileHash(context, path));
_extensionDependenciesManager.StoreDependencies(context.NewDependencies, desc => GetExtensionHash(context, desc));
Logger.Information("Done loading extensions...");
@@ -85,35 +85,18 @@ namespace Orchard.Environment.Extensions {
}
}
private string GetFileHash(ExtensionLoadingContext context, string extensionId) {
private string GetExtensionHash(ExtensionLoadingContext context, DependencyDescriptor dependencyDescriptor) {
var hash = new Hash();
hash.AddString(extensionId);
hash.AddString(dependencyDescriptor.Name);
{
ExtensionProbeEntry extensionProbe;
if (context.ProcessedExtensions.TryGetValue(extensionId, out extensionProbe)) {
if (extensionProbe != null) {
var virtualPathDependencies = extensionProbe.VirtualPathDependencies;
foreach (var virtualpathDependency in virtualPathDependencies) {
DateTime dateTime;
if (context.VirtualPathModficationDates.TryGetValue(virtualpathDependency, out dateTime)) {
hash.AddDateTime(dateTime);
}
}
}
}
foreach (var virtualpathDependency in context.ProcessedExtensions[dependencyDescriptor.Name].VirtualPathDependencies) {
hash.AddDateTime(GetVirtualPathModificationTimeUtc(context.VirtualPathModficationDates, virtualpathDependency));
}
{
ExtensionReferenceProbeEntry extensionReferenceProbe;
if (context.ProcessedReferences.TryGetValue(extensionId, out extensionReferenceProbe)) {
if (extensionReferenceProbe != null) {
DateTime dateTime;
if (context.VirtualPathModficationDates.TryGetValue(extensionReferenceProbe.VirtualPath, out dateTime)) {
hash.AddDateTime(dateTime);
}
}
}
foreach (var reference in dependencyDescriptor.References) {
hash.AddString(reference.Name);
hash.AddString(reference.LoaderName);
hash.AddDateTime(GetVirtualPathModificationTimeUtc(context.VirtualPathModficationDates, reference.VirtualPath));
}
return hash.Value;
@@ -215,7 +198,7 @@ namespace Orchard.Environment.Extensions {
.Where(probe => probe != null))
.GroupBy(e => e.Descriptor.Id)
.ToDictionary(g => g.Key, g => g.AsEnumerable()
.OrderByDescending(probe => GetLatestModificationTimeUtc(virtualPathModficationDates, probe))
.OrderByDescending(probe => GetVirtualPathDepedenciesModificationTimeUtc(virtualPathModficationDates, probe))
.ThenBy(probe => probe.Loader.Order), StringComparer.OrdinalIgnoreCase);
var deletedDependencies = previousDependencies
@@ -254,25 +237,27 @@ namespace Orchard.Environment.Extensions {
};
}
private DateTime GetLatestModificationTimeUtc(Dictionary<string, DateTime> virtualPathDependencies, ExtensionProbeEntry probe) {
private DateTime GetVirtualPathDepedenciesModificationTimeUtc(IDictionary<string, DateTime> virtualPathDependencies, ExtensionProbeEntry probe) {
if (!probe.VirtualPathDependencies.Any())
return DateTime.MinValue;
Logger.Information("Retrieving modification dates of dependencies of extension '{0}'", probe.Descriptor.Id);
var result = probe.VirtualPathDependencies.Max(path => {
DateTime dateTime;
if (!virtualPathDependencies.TryGetValue(path, out dateTime)) {
dateTime = _virtualPathProvider.GetFileLastWriteTimeUtc(path);
virtualPathDependencies.Add(path, dateTime);
}
return dateTime;
});
var result = probe.VirtualPathDependencies.Max(path => GetVirtualPathModificationTimeUtc(virtualPathDependencies, path));
Logger.Information("Done retrieving modification dates of dependencies of extension '{0}'", probe.Descriptor.Id);
return result;
}
private DateTime GetVirtualPathModificationTimeUtc(IDictionary<string, DateTime> virtualPathDependencies, string path) {
DateTime dateTime;
if (!virtualPathDependencies.TryGetValue(path, out dateTime)) {
dateTime = _virtualPathProvider.GetFileLastWriteTimeUtc(path);
virtualPathDependencies.Add(path, dateTime);
}
return dateTime;
}
IEnumerable<DependencyReferenceDescriptor> ProcessExtensionReferences(ExtensionLoadingContext context, ExtensionProbeEntry activatedExtension) {
if (activatedExtension == null)
return Enumerable.Empty<DependencyReferenceDescriptor>();

View File

@@ -14,7 +14,7 @@ namespace Orchard.FileSystems.Dependencies {
/// </summary>
public class DefaultExtensionDependenciesManager : IExtensionDependenciesManager {
private const string BasePath = "Dependencies";
private const string FileName = "Dependencies.ModuleCompilation.xml";
private const string FileName = "dependencies.compiled.xml";
private readonly ICacheManager _cacheManager;
private readonly IAppDataFolder _appDataFolder;
private readonly InvalidationToken _writeThroughToken;
@@ -33,12 +33,12 @@ namespace Orchard.FileSystems.Dependencies {
get { return _appDataFolder.Combine(BasePath, FileName); }
}
public void StoreDependencies(IEnumerable<DependencyDescriptor> dependencyDescriptors, Func<string, string> fileHashProvider) {
public void StoreDependencies(IEnumerable<DependencyDescriptor> dependencyDescriptors, Func<DependencyDescriptor, string> fileHashProvider) {
Logger.Information("Storing module dependency file.");
var newDocument = CreateDocument(dependencyDescriptors, fileHashProvider);
var previousDocument = ReadDocument(PersistencePath);
if (CompareXmlDocuments(newDocument, previousDocument)) {
if (XNode.DeepEquals(newDocument.Root, previousDocument.Root)) {
Logger.Debug("Existing document is identical to new one. Skipping save.");
}
else {
@@ -48,8 +48,9 @@ namespace Orchard.FileSystems.Dependencies {
Logger.Information("Done storing module dependency file.");
}
public IEnumerable<string> GetVirtualPathDependencies(DependencyDescriptor descriptor) {
if (IsSupportedLoader(descriptor.LoaderName)) {
public IEnumerable<string> GetVirtualPathDependencies(string extensionId) {
var descriptor = GetDescriptor(extensionId);
if (descriptor != null && IsSupportedLoader(descriptor.LoaderName)) {
// Currently, we return the same file for every module. An improvement would be to return
// a specific file per module (this would decrease the number of recompilations needed
// when modules change on disk).
@@ -62,38 +63,31 @@ namespace Orchard.FileSystems.Dependencies {
}
public IEnumerable<ActivatedExtensionDescriptor> LoadDescriptors() {
return _cacheManager.Get(PersistencePath,
ctx => {
_appDataFolder.CreateDirectory(BasePath);
ctx.Monitor(_appDataFolder.WhenPathChanges(ctx.Key));
return _cacheManager.Get(PersistencePath, ctx => {
_appDataFolder.CreateDirectory(BasePath);
ctx.Monitor(_appDataFolder.WhenPathChanges(ctx.Key));
_writeThroughToken.IsCurrent = true;
ctx.Monitor(_writeThroughToken);
_writeThroughToken.IsCurrent = true;
ctx.Monitor(_writeThroughToken);
return ReadDescriptors(ctx.Key).ToList();
});
return ReadDescriptors(ctx.Key).ToList();
});
}
private XDocument CreateDocument(IEnumerable<DependencyDescriptor> dependencies, Func<string, string> fileHashProvider) {
private XDocument CreateDocument(IEnumerable<DependencyDescriptor> dependencies, Func<DependencyDescriptor, string> fileHashProvider) {
Func<string, XName> ns = (name => XName.Get(name));
var document = new XDocument();
document.Add(new XElement(ns("Dependencies")));
var elements = FilterDependencies(dependencies).Select(
d => new XElement("Dependency",
new XElement(ns("ExtensionId"), d.Name),
new XElement(ns("LoaderName"), d.LoaderName),
new XElement(ns("VirtualPath"), d.VirtualPath),
new XElement(ns("FileHash"), fileHashProvider(d.Name)),
new XElement(ns("References"), FilterReferences(d.References)
.Select(r => new XElement(ns("Reference"),
new XElement(ns("ReferenceId"), r.Name),
new XElement(ns("LoaderName"), r.LoaderName),
new XElement(ns("VirtualPath"), r.VirtualPath),
new XElement(ns("FileHash"), fileHashProvider(r.Name)))).ToArray())));
var elements = dependencies
.Where(dep => IsSupportedLoader(dep.LoaderName))
.OrderBy(dep => dep.Name, StringComparer.OrdinalIgnoreCase)
.Select(descriptor =>
new XElement(ns("Dependency"),
new XElement(ns("ExtensionId"), descriptor.Name),
new XElement(ns("LoaderName"), descriptor.LoaderName),
new XElement(ns("VirtualPath"), descriptor.VirtualPath),
new XElement(ns("Hash"), fileHashProvider(descriptor))));
document.Root.Add(elements);
return document;
return new XDocument(new XElement(ns("Dependencies"), elements.ToArray()));
}
private IEnumerable<ActivatedExtensionDescriptor> ReadDescriptors(string persistancePath) {
@@ -108,23 +102,10 @@ namespace Orchard.FileSystems.Dependencies {
ExtensionId = elem(e, "ExtensionId"),
VirtualPath = elem(e, "VirtualPath"),
LoaderName = elem(e, "LoaderName"),
FileHash = elem(e, "FileHash"),
//References = e.Elements(ns("References")).Elements(ns("Reference")).Select(r => new DependencyReferenceDescriptor {
// Name = elem(r, "Name"),
// LoaderName = elem(r, "LoaderName"),
// VirtualPath = elem(r, "VirtualPath")
//})
Hash = elem(e, "Hash"),
}).ToList();
}
private IEnumerable<DependencyDescriptor> FilterDependencies(IEnumerable<DependencyDescriptor> dependencies) {
return dependencies.Where(dep => IsSupportedLoader(dep.LoaderName));
}
private IEnumerable<DependencyReferenceDescriptor> FilterReferences(IEnumerable<DependencyReferenceDescriptor> references) {
return references.Where(dep => IsSupportedLoader(dep.LoaderName));
}
private bool IsSupportedLoader(string loaderName) {
//Note: this is hard-coded for now, to avoid adding more responsibilities to the IExtensionLoader
// implementations.
@@ -151,15 +132,11 @@ namespace Orchard.FileSystems.Dependencies {
}
}
catch (Exception e) {
Logger.Information(e, "Error reading file '{0}'", persistancePath);
Logger.Information(e, "Error reading file '{0}'. Assuming empty.", persistancePath);
return new XDocument();
}
}
private bool CompareXmlDocuments(XDocument doc1, XDocument doc2) {
return XNode.DeepEquals(doc1.Root, doc2.Root);
}
private class InvalidationToken : IVolatileToken {
public bool IsCurrent { get; set; }
}

View File

@@ -36,12 +36,8 @@ namespace Orchard.FileSystems.Dependencies {
var desc = GetExtensionDescriptor(virtualPath);
if (desc != null) {
// We are only interested in ".csproj" files loaded from "DynamicExtensionLoader"
var dynamicExtensionLoader = _loaders.Where(l => l.Name == desc.LoaderName).FirstOrDefault() as DynamicExtensionLoader;
if (dynamicExtensionLoader != null) {
if (virtualPath.Equals(desc.VirtualPath, StringComparison.OrdinalIgnoreCase)) {
return desc.FileHash;
}
if (desc.VirtualPath.Equals(virtualPath, StringComparison.OrdinalIgnoreCase)) {
return desc.Hash;
}
}
return base.GetFileHash(virtualPath, virtualPathDependencies);

View File

@@ -7,13 +7,13 @@ namespace Orchard.FileSystems.Dependencies {
public string ExtensionId { get; set; }
public string LoaderName { get; set; }
public string VirtualPath { get; set; }
public string FileHash { get; set; }
public string Hash { get; set; }
}
public interface IExtensionDependenciesManager : IVolatileProvider {
void StoreDependencies(IEnumerable<DependencyDescriptor> dependencyDescriptors, Func<string, string> fileHashProvider);
void StoreDependencies(IEnumerable<DependencyDescriptor> dependencyDescriptors, Func<DependencyDescriptor, string> fileHashProvider);
IEnumerable<string> GetVirtualPathDependencies(DependencyDescriptor descriptor);
IEnumerable<string> GetVirtualPathDependencies(string extensionId);
ActivatedExtensionDescriptor GetDescriptor(string extensionId);
}
}

View File

@@ -62,7 +62,7 @@ namespace Orchard.FileSystems.Dependencies {
var dependencies =
virtualPathDependencies
.OfType<string>()
.Concat(file.Loaders.SelectMany(dl => _extensionDependenciesManager.GetVirtualPathDependencies(dl.Descriptor)))
.Concat(file.Loaders.SelectMany(dl => _extensionDependenciesManager.GetVirtualPathDependencies(dl.Descriptor.Name)))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();

View File

@@ -77,7 +77,7 @@ namespace Orchard.Mvc.ViewEngines.Razor {
loader,
descriptor,
references = loader.GetCompilationReferences(descriptor),
dependencies = _extensionDependenciesManager.GetVirtualPathDependencies(descriptor)
dependencies = _extensionDependenciesManager.GetVirtualPathDependencies(descriptor.Name)
}));
// Add assemblies