From 8c78a6ffbd892f070521067f12bb80f6bb967ee0 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Thu, 7 Jun 2018 17:38:18 +0300 Subject: [PATCH] Refactored bundle tag helpers and bundle manager --- .../Bundling/AbpBundleTagHelperServiceBase.cs | 56 +++++++++++++++++++ .../Bundling/AbpScriptBundleTagHelper.cs | 2 +- .../AbpScriptBundleTagHelperService.cs | 43 ++++---------- .../Bundling/AbpStyleBundleTagHelper.cs | 3 +- .../AbpStyleBundleTagHelperService.cs | 44 ++++----------- .../TagHelpers/Bundling/IBundleTagHelper.cs | 7 +++ .../UI/Bundling/BundleCollection.cs | 39 +++++++------ .../UI/Bundling/BundleManager.cs | 21 +------ .../UI/Bundling/IBundleManager.cs | 6 +- 9 files changed, 114 insertions(+), 107 deletions(-) create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpBundleTagHelperServiceBase.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/IBundleTagHelper.cs diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpBundleTagHelperServiceBase.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpBundleTagHelperServiceBase.cs new file mode 100644 index 0000000000..ad4126abfc --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpBundleTagHelperServiceBase.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Razor.TagHelpers; +using Volo.Abp.AspNetCore.Mvc.UI.Bundling; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling +{ + public abstract class AbpBundleTagHelperServiceBase : AbpTagHelperService + where TTagHelper : TagHelper, IBundleTagHelper + { + protected IBundleManager BundleManager { get; } + + protected AbpBundleTagHelperServiceBase(IBundleManager bundleManager) + { + BundleManager = bundleManager; + } + + public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) + { + output.TagName = null; + + var bundleName = TagHelper.Name; + var files = await GetFileList(context, output); + if (bundleName.IsNullOrEmpty()) + { + bundleName = GenerateBundleName(context, output, files); + } + + CreateBundle(bundleName, files); + + var bundleFiles = GetBundleFiles(bundleName); + await output.GetChildContentAsync(); //TODO: Suppress child execution! + output.Content.Clear(); + AddHtmlTags(context, output, bundleFiles); + } + + protected abstract void CreateBundle(string bundleName, List files); + protected abstract List GetBundleFiles(string bundleName); + + protected abstract void AddHtmlTags(TagHelperContext context, TagHelperOutput output, List files); + + protected virtual string GenerateBundleName(TagHelperContext context, TagHelperOutput output, List fileList) + { + return fileList.JoinAsString("|").ToMd5(); + } + + protected virtual async Task> GetFileList(TagHelperContext context, TagHelperOutput output) + { + var fileList = new List(); + context.Items[AbpBundleFileTagHelperService.ContextFileListKey] = fileList; + await output.GetChildContentAsync(); + return fileList; + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelper.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelper.cs index 5d64230981..029b1e8456 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelper.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelper.cs @@ -3,7 +3,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling { [HtmlTargetElement("abp-script-bundle", TagStructure = TagStructure.NormalOrSelfClosing)] - public class AbpScriptBundleTagHelper : AbpTagHelper + public class AbpScriptBundleTagHelper : AbpTagHelper, IBundleTagHelper { public string Name { get; set; } diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelperService.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelperService.cs index b75c936148..f882d45412 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelperService.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelperService.cs @@ -1,43 +1,32 @@ using System; using System.Collections.Generic; -using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.TagHelpers; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling { - public class AbpScriptBundleTagHelperService : AbpTagHelperService + public class AbpScriptBundleTagHelperService : AbpBundleTagHelperServiceBase { - private readonly IBundleManager _bundleManager; - public AbpScriptBundleTagHelperService(IBundleManager bundleManager) + : base(bundleManager) { - _bundleManager = bundleManager; + } - public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) + protected override void CreateBundle(string bundleName, List files) { - output.TagName = null; - - var bundleName = TagHelper.Name; - var files = await GetFileList(context, output); - if (bundleName.IsNullOrEmpty()) - { - bundleName = GenerateBundleName(context, output, files); - } - - _bundleManager.CreateDynamicScriptBundle( + BundleManager.CreateScriptBundle( bundleName, configuration => configuration.AddFiles(files.ToArray()) ); + } - var bundleFiles = _bundleManager.GetScriptBundleFiles(bundleName); - await output.GetChildContentAsync(); //TODO: Suppress child execution! - output.Content.Clear(); - AddLinkTags(context, output, bundleFiles); + protected override List GetBundleFiles(string bundleName) + { + return BundleManager.GetScriptBundleFiles(bundleName); } - protected virtual void AddLinkTags(TagHelperContext context, TagHelperOutput output, List files) + protected override void AddHtmlTags(TagHelperContext context, TagHelperOutput output, List files) { foreach (var file in files) { @@ -45,17 +34,5 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling } } - protected virtual string GenerateBundleName(TagHelperContext context, TagHelperOutput output, List fileList) - { - return fileList.JoinAsString("|").ToMd5(); - } - - protected virtual async Task> GetFileList(TagHelperContext context, TagHelperOutput output) - { - var fileList = new List(); - context.Items[AbpBundleFileTagHelperService.ContextFileListKey] = fileList; - await output.GetChildContentAsync(); - return fileList; - } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelper.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelper.cs index 866c78a19f..e2c47f4ff5 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelper.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelper.cs @@ -1,12 +1,11 @@ using Microsoft.AspNetCore.Razor.TagHelpers; -using Volo.Abp.Threading; namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling { //TODO: Move to Volo.Abp.AspNetCore.Mvc.UI package. [HtmlTargetElement("abp-style-bundle", TagStructure = TagStructure.NormalOrSelfClosing)] - public class AbpStyleBundleTagHelper : AbpTagHelper + public class AbpStyleBundleTagHelper : AbpTagHelper, IBundleTagHelper { public string Name { get; set; } diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelperService.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelperService.cs index 32758ba1a7..8c4fcfaf66 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelperService.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelperService.cs @@ -1,43 +1,32 @@ using System; using System.Collections.Generic; -using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.TagHelpers; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling { - public class AbpStyleBundleTagHelperService : AbpTagHelperService + public class AbpStyleBundleTagHelperService : AbpBundleTagHelperServiceBase { - private readonly IBundleManager _bundleManager; - public AbpStyleBundleTagHelperService(IBundleManager bundleManager) + : base(bundleManager) { - _bundleManager = bundleManager; + } - public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) + protected override void CreateBundle(string bundleName, List files) { - output.TagName = null; - - var bundleName = TagHelper.Name; - var files = await GetFileList(context, output); - if (bundleName.IsNullOrEmpty()) - { - bundleName = GenerateBundleName(context, output, files); - } - - _bundleManager.CreateDynamicStyleBundle( + BundleManager.CreateStyleBundle( bundleName, configuration => configuration.AddFiles(files.ToArray()) ); + } - var bundleFiles = _bundleManager.GetStyleBundleFiles(bundleName); - await output.GetChildContentAsync(); //TODO: Suppress child execution! - output.Content.Clear(); - AddLinkTags(context, output, bundleFiles); + protected override List GetBundleFiles(string bundleName) + { + return BundleManager.GetStyleBundleFiles(bundleName); } - protected virtual void AddLinkTags(TagHelperContext context, TagHelperOutput output, List files) + protected override void AddHtmlTags(TagHelperContext context, TagHelperOutput output, List files) { foreach (var file in files) { @@ -45,18 +34,5 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling $"{Environment.NewLine}"); } } - - protected virtual string GenerateBundleName(TagHelperContext context, TagHelperOutput output, List fileList) - { - return fileList.JoinAsString("|").ToMd5(); - } - - protected virtual async Task> GetFileList(TagHelperContext context, TagHelperOutput output) - { - var fileList = new List(); - context.Items[AbpBundleFileTagHelperService.ContextFileListKey] = fileList; - await output.GetChildContentAsync(); - return fileList; - } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/IBundleTagHelper.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/IBundleTagHelper.cs new file mode 100644 index 0000000000..226f5a8572 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/IBundleTagHelper.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling +{ + public interface IBundleTagHelper + { + string Name { get; } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs index efbed2dd1d..7b9afd5d08 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs @@ -1,18 +1,18 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling { public class BundleCollection { - private readonly Dictionary _bundleContributors; + private readonly ConcurrentDictionary _bundleContributors; public BundleCollection() { - _bundleContributors = new Dictionary(); + _bundleContributors = new ConcurrentDictionary(); } - //TODO: Seperate to Add and WithFiles/WithContributors methods instead of coupling public BundleConfiguration Add(string bundleName) { if (_bundleContributors.ContainsKey(bundleName)) @@ -20,43 +20,40 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling throw new AbpException($"There is already a bundle added with given {nameof(bundleName)}: {bundleName}"); } - var bundleConfiguration = new BundleConfiguration(bundleName); - _bundleContributors.Add(bundleName, bundleConfiguration); - return bundleConfiguration; + return _bundleContributors.AddOrUpdate(bundleName, new BundleConfiguration(bundleName), (n, c) => c); } public BundleConfiguration Get(string bundleName) { - if (!_bundleContributors.ContainsKey(bundleName)) - { - throw new AbpException($"There is no bundle added with given {nameof(bundleName)}: {bundleName}"); - } + CheckBundle(bundleName); return _bundleContributors[bundleName]; } - public BundleConfiguration GetOrAdd(string bundleName, Action configureAction = null) + public BundleConfiguration GetOrAdd(string bundleName) + { + return GetOrAdd(bundleName, c => { }); + } + + internal BundleConfiguration GetOrAdd(string bundleName, Action configureAction) { return _bundleContributors.GetOrAdd( bundleName, () => { var configuration = new BundleConfiguration(bundleName); - configureAction?.Invoke(configuration); + configureAction.Invoke(configuration); return configuration; }); } public List GetFiles(string bundleName) { - var bundleConfiguration = _bundleContributors.GetOrDefault(bundleName); - if (bundleConfiguration == null) - { - throw new AbpException("Undefined bundle: " + bundleName); - } + CheckBundle(bundleName); var files = new List(); + var bundleConfiguration = _bundleContributors[bundleName]; foreach (var contributor in bundleConfiguration.Contributors) { contributor.Contribute(files); @@ -64,5 +61,13 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling return files; } + + private void CheckBundle(string bundleName) + { + if (!_bundleContributors.ContainsKey(bundleName)) + { + throw new AbpException($"There is no bundle with given {nameof(bundleName)}: {bundleName}"); + } + } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleManager.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleManager.cs index fb15685df6..77070083bf 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleManager.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleManager.cs @@ -34,31 +34,16 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling public List GetScriptBundleFiles(string bundleName) { - //if (_hostingEnvironment.IsDevelopment()) - { - return _options.ScriptBundles.GetFiles(bundleName); - } - - return new List - { - _cache.GetOrAdd( - "SCRIPT:" + bundleName, - () => _bundler.CreateBundle( - _options.ScriptBundles.GetFiles(bundleName) - ) - ) - }; + return _options.ScriptBundles.GetFiles(bundleName); } - public void CreateDynamicStyleBundle(string bundleName, Action configureAction) + public void CreateStyleBundle(string bundleName, Action configureAction) { - //TODO: How to handle concurrency! _options.StyleBundles.GetOrAdd(bundleName, configureAction); } - public void CreateDynamicScriptBundle(string bundleName, Action configureAction) + public void CreateScriptBundle(string bundleName, Action configureAction) { - //TODO: How to handle concurrency! _options.ScriptBundles.GetOrAdd(bundleName, configureAction); } } diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleManager.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleManager.cs index 263eecba84..0e0f9989a3 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleManager.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleManager.cs @@ -3,14 +3,16 @@ using System.Collections.Generic; namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling { + //TODO: Bundle system needs refactoring/redesign + public interface IBundleManager { List GetStyleBundleFiles(string bundleName); List GetScriptBundleFiles(string bundleName); - void CreateDynamicStyleBundle(string bundleName, Action configureAction); + void CreateStyleBundle(string bundleName, Action configureAction); - void CreateDynamicScriptBundle(string bundleName, Action configureAction); + void CreateScriptBundle(string bundleName, Action configureAction); } } \ No newline at end of file