Browse Source

Implement ConfigureDynamicResources

pull/301/head
Halil ibrahim Kalkan 8 years ago
parent
commit
6907a34070
  1. 6
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributor.cs
  2. 44
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs
  3. 19
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs
  4. 7
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs
  5. 7
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs
  6. 12
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Resources/IWebRequestResources.cs
  7. 25
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Resources/WebRequestResources.cs
  8. 35
      src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/JQueryValidation/JQueryValidationScriptContributor.cs
  9. 16
      src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs

6
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributor.cs

@ -16,5 +16,11 @@
{
}
//TODO: Reconsider naming and usage!
public virtual void ConfigureDynamicResources(BundleConfigurationContext context)
{
}
}
}

44
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs

@ -4,7 +4,6 @@ using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
@ -68,16 +67,17 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
protected virtual IReadOnlyList<string> GetBundleFiles(BundleConfigurationCollection bundles, string bundleName, IBundler bundler)
{
var contributors = GetContributors(bundles, bundleName);
var files = RequestResources.Filter(CreateFileList(contributors));
var bundleFiles = RequestResources.TryAdd(GetBundleFiles(contributors));
var dynamicResources = RequestResources.TryAdd(GetDynamicResources(contributors));
if (!IsBundlingEnabled())
{
return files;
return bundleFiles.Union(dynamicResources).ToImmutableList();
}
var bundleRelativePath =
Options.BundleFolderName.EnsureEndsWith('/') +
bundleName + "." + files.JoinAsString("|").ToMd5() + "." + bundler.FileExtension;
bundleName + "." + bundleFiles.JoinAsString("|").ToMd5() + "." + bundler.FileExtension;
var cacheItem = BundleCache.GetOrAdd(bundleRelativePath, () =>
{
@ -88,12 +88,12 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
}
);
WatchChanges(cacheValue, files, bundleRelativePath);
WatchChanges(cacheValue, bundleFiles, bundleRelativePath);
var bundleResult = bundler.Bundle(
new BundlerContext(
bundleRelativePath,
files,
bundleFiles,
IsMinficationEnabled()
)
);
@ -103,7 +103,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
return cacheValue;
});
return cacheItem.Files.ToImmutableList();
return cacheItem.Files.Union(dynamicResources).ToImmutableList();
}
private void WatchChanges(BundleCacheItem cacheValue, List<string> files, string bundleRelativePath)
@ -184,17 +184,29 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
}
}
protected virtual List<string> CreateFileList(List<BundleContributor> contributors)
protected virtual List<string> GetBundleFiles(List<BundleContributor> contributors)
{
using (var scope = ServiceProvider.CreateScope())
{
var context = new BundleConfigurationContext(scope.ServiceProvider, scope.ServiceProvider.GetRequiredService<IVirtualFileProvider>());
contributors.ForEach(c => c.PreConfigureBundle(context));
contributors.ForEach(c => c.ConfigureBundle(context));
contributors.ForEach(c => c.PostConfigureBundle(context));
var context = CreateBundleConfigurationContext();
return context.Files;
}
contributors.ForEach(c => c.PreConfigureBundle(context));
contributors.ForEach(c => c.ConfigureBundle(context));
contributors.ForEach(c => c.PostConfigureBundle(context));
return context.Files;
}
protected virtual List<string> GetDynamicResources(List<BundleContributor> contributors)
{
var context = CreateBundleConfigurationContext();
contributors.ForEach(c => c.ConfigureDynamicResources(context));
return context.Files;
}
protected virtual BundleConfigurationContext CreateBundleConfigurationContext()
{
return new BundleConfigurationContext(ServiceProvider, WebRootFileProvider);
}
protected virtual List<BundleContributor> GetContributors(BundleConfigurationCollection bundles, string bundleName)

19
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs

@ -2,8 +2,10 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc.UI.Resources;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.AspNetCore.VirtualFileSystem;
using Volo.Abp.DependencyInjection;
@ -11,20 +13,20 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
public abstract class AbpTagHelperResourceService : ITransientDependency
{
public ILogger<AbpTagHelperResourceService> Logger { get; set; }
protected IBundleManager BundleManager { get; }
protected IHybridWebRootFileProvider WebRootFileProvider { get; }
protected IWebRequestResources WebRequestResources { get; }
protected AbpTagHelperResourceService(
IBundleManager bundleManager,
IHybridWebRootFileProvider webRootFileProvider,
IWebRequestResources webRequestResources)
IHybridWebRootFileProvider webRootFileProvider)
{
BundleManager = bundleManager;
WebRootFileProvider = webRootFileProvider;
WebRequestResources = webRequestResources;
Logger = NullLogger<AbpTagHelperResourceService>.Instance;
}
public virtual Task ProcessAsync(
@ -37,6 +39,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
Check.NotNull(output, nameof(output));
Check.NotNull(bundleItems, nameof(bundleItems));
var stopwatch = Stopwatch.StartNew();
output.TagName = null;
if (bundleName.IsNullOrEmpty())
@ -61,7 +65,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
AddHtmlTag(context, output, bundleFile + "?_v=" + file.LastModified.UtcTicks);
}
WebRequestResources.Add(bundleFiles);
stopwatch.Stop();
Logger.LogDebug($"Added bundle '{bundleName}' to the page in {stopwatch.Elapsed.TotalMilliseconds:0.00} ms.");
return Task.CompletedTask;
}

7
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Resources;
using Volo.Abp.AspNetCore.VirtualFileSystem;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
@ -10,12 +9,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
public AbpTagHelperScriptService(
IBundleManager bundleManager,
IHybridWebRootFileProvider webRootFileProvider,
IWebRequestResources webRequestResources
IHybridWebRootFileProvider webRootFileProvider
) : base(
bundleManager,
webRootFileProvider,
webRequestResources)
webRootFileProvider)
{
}

7
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Resources;
using Volo.Abp.AspNetCore.VirtualFileSystem;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
@ -10,12 +9,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
public AbpTagHelperStyleService(
IBundleManager bundleManager,
IHybridWebRootFileProvider webRootFileProvider,
IWebRequestResources webRequestResources
IHybridWebRootFileProvider webRootFileProvider
) : base(
bundleManager,
webRootFileProvider,
webRequestResources)
webRootFileProvider)
{
}

12
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Resources/IWebRequestResources.cs

@ -4,16 +4,6 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Resources
{
public interface IWebRequestResources
{
/// <summary>
/// Filters given resources and returns a list of resources those are not
/// added to the page for current web request.
/// </summary>
List<string> Filter(IEnumerable<string> resources);
bool IsAddedBefore(string resource);
void Add(IEnumerable<string> resources);
IReadOnlyList<string> GetAll();
List<string> TryAdd(IEnumerable<string> resources);
}
}

25
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Resources/WebRequestResources.cs

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Volo.Abp.DependencyInjection;
@ -14,27 +13,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Resources
Resources = new List<string>();
}
public List<string> Filter(IEnumerable<string> resources)
public List<string> TryAdd(IEnumerable<string> resources)
{
return resources.Except(Resources).ToList();
}
public bool IsAddedBefore(string resource)
{
return Resources.Contains(resource);
}
public void Add(IEnumerable<string> resources)
{
foreach (var resource in resources)
{
Resources.AddIfNotContains(resource);
}
}
public IReadOnlyList<string> GetAll()
{
return Resources.ToImmutableList();
var resourceToBeAdded = resources.Except(Resources).ToList();
Resources.AddRange(resourceToBeAdded);
return resourceToBeAdded;
}
}
}

35
src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/JQueryValidation/JQueryValidationScriptContributor.cs

@ -16,31 +16,40 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.JQueryValidation
context.Files.AddIfNotContains("/libs/jquery-validation/jquery.validate.js");
}
public void ConfigureDynamic(BundleConfigurationContext context)
public override void ConfigureDynamicResources(BundleConfigurationContext context)
{
//TODO: Can we optimize this? Also refactor a bit to reduce duplication
//TODO: Can we optimize these points:
// - Can we get rid of context.FileProvider.GetFileInfo call?
// - What if the same contributer is used twice for a page.
// Duplication is prevented by the bundle manager, however the logic below will execute twice
var currentCultureName = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Replace('-', '_');
var filePath = DefaultLocalizationFolder + "messages_" + currentCultureName + ".js";
var fileInfo = context.FileProvider.GetFileInfo(filePath);
if (fileInfo != null)
var cultureName = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Replace('-', '_');
if (TryAddCultureFile(context, cultureName))
{
context.Files.AddIfNotContains(filePath);
return;
}
if (!currentCultureName.Contains("_"))
if (!cultureName.Contains("_"))
{
return;
}
currentCultureName = currentCultureName.Substring(0, currentCultureName.IndexOf('_'));
filePath = DefaultLocalizationFolder + "messages_" + currentCultureName + ".js";
fileInfo = context.FileProvider.GetFileInfo(filePath);
if (fileInfo != null)
TryAddCultureFile(context, cultureName.Substring(0, cultureName.IndexOf('_')));
}
protected virtual bool TryAddCultureFile(BundleConfigurationContext context, string cultureName)
{
var filePath = DefaultLocalizationFolder + "messages_" + cultureName + ".js";
var fileInfo = context.FileProvider.GetFileInfo(filePath);
if (!fileInfo.Exists)
{
context.Files.AddIfNotContains(filePath);
return false;
}
context.Files.AddIfNotContains(filePath);
return true;
}
}
}

16
src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs

@ -104,12 +104,18 @@ namespace Volo.Abp.AutoMapper
}
).ToArray();
logger.LogDebug($"Found {types.Length} classes define auto mapping attributes:");
foreach (var type in types)
if (types.Length <= 0)
{
logger.LogDebug($"No class found with auto mapping attributes.");
}
else
{
logger.LogDebug(type.FullName);
context.MapperConfiguration.CreateAutoAttributeMaps(type);
logger.LogDebug($"Found {types.Length} classes define auto mapping attributes.");
foreach (var type in types)
{
logger.LogDebug(type.FullName);
context.MapperConfiguration.CreateAutoAttributeMaps(type);
}
}
}
}

Loading…
Cancel
Save