From 99bdb075fa6bb0bd87e63f3742d8c7f699187652 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Thu, 7 Jun 2018 00:00:05 +0300 Subject: [PATCH] Revised bundling system. --- .../AbpAspNetCoreMvcUiThemeSharedModule.cs | 66 ++++++++++--------- .../UI/Bundling/BundleCollection.cs | 43 ++++++++++-- .../UI/Bundling/BundleCollectionExtensions.cs | 12 ++++ .../UI/Bundling/BundleConfiguration.cs | 15 +++++ .../UI/Bundling/BundleContributorList.cs | 9 +++ .../Contributors/SimpleBundleContributor.cs | 20 ++++++ .../UI/Bundling/IBundleContributor.cs | 9 +++ .../Collections/Generic/AbpListExtensions.cs | 23 +++++++ .../Generic/AbpListExtensions_Tests.cs | 34 +++++++++- 9 files changed, 192 insertions(+), 39 deletions(-) create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollectionExtensions.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleConfiguration.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleContributorList.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/Contributors/SimpleBundleContributor.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleContributor.cs diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs index 4b31078f07..bf77e4e092 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs @@ -20,40 +20,44 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared services.Configure(options => { - options.StyleBundles.Add("GlobalStyles", new[] - { - "/libs/font-awesome/css/font-awesome.css", - "/libs/bootstrap/css/bootstrap.css", - "/libs/datatables.net-bs4/css/dataTables.bootstrap4.css", - "/libs/toastr/toastr.min.css", + options + .StyleBundles + .Add("GlobalStyles") + .AddFiles( + "/libs/font-awesome/css/font-awesome.css", + "/libs/bootstrap/css/bootstrap.css", + "/libs/datatables.net-bs4/css/dataTables.bootstrap4.css", + "/libs/toastr/toastr.min.css", - "/libs/abp/aspnetcore.mvc.ui.theme.shared/datatables/datatables.css" - }); + "/libs/abp/aspnetcore.mvc.ui.theme.shared/datatables/datatables.css" + ); - options.ScriptBundles.Add("GlobalScripts", new[] - { - "/libs/jquery/jquery.js", - "/libs/bootstrap/js/bootstrap.bundle.js", - "/libs/jquery-validation/jquery.validate.js", - "/libs/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js", - "/libs/jquery-form/jquery.form.min.js", - "/libs/datatables.net/js/jquery.dataTables.js", - "/libs/datatables.net-bs4/js/dataTables.bootstrap4.js", - "/libs/sweetalert/sweetalert.min.js", - "/libs/toastr/toastr.min.js", + options + .ScriptBundles + .Add("GlobalScripts") + .AddFiles( + "/libs/jquery/jquery.js", + "/libs/bootstrap/js/bootstrap.bundle.js", + "/libs/jquery-validation/jquery.validate.js", + "/libs/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js", + "/libs/jquery-form/jquery.form.min.js", + "/libs/datatables.net/js/jquery.dataTables.js", + "/libs/datatables.net-bs4/js/dataTables.bootstrap4.js", + "/libs/sweetalert/sweetalert.min.js", + "/libs/toastr/toastr.min.js", - "/libs/abp/core/abp.js", - "/libs/abp/jquery/abp.dom.js", - "/libs/abp/jquery/abp.ajax.js", - "/libs/abp/jquery/abp.resource-loader.js", - "/libs/abp/aspnetcore.mvc.ui.theme.shared/jquery/jquery-extensions.js", - "/libs/abp/aspnetcore.mvc.ui.theme.shared/jquery-form/jquery-form-extensions.js", - "/libs/abp/aspnetcore.mvc.ui.theme.shared/bootstrap/dom-event-handlers.js", - "/libs/abp/aspnetcore.mvc.ui.theme.shared/bootstrap/modal-manager.js", - "/libs/abp/aspnetcore.mvc.ui.theme.shared/datatables/datatables-extensions.js", - "/libs/abp/aspnetcore.mvc.ui.theme.shared/sweetalert/abp-sweetalert.js", - "/libs/abp/aspnetcore.mvc.ui.theme.shared/toastr/abp-toastr.js" - }); + "/libs/abp/core/abp.js", + "/libs/abp/jquery/abp.dom.js", + "/libs/abp/jquery/abp.ajax.js", + "/libs/abp/jquery/abp.resource-loader.js", + "/libs/abp/aspnetcore.mvc.ui.theme.shared/jquery/jquery-extensions.js", + "/libs/abp/aspnetcore.mvc.ui.theme.shared/jquery-form/jquery-form-extensions.js", + "/libs/abp/aspnetcore.mvc.ui.theme.shared/bootstrap/dom-event-handlers.js", + "/libs/abp/aspnetcore.mvc.ui.theme.shared/bootstrap/modal-manager.js", + "/libs/abp/aspnetcore.mvc.ui.theme.shared/datatables/datatables-extensions.js", + "/libs/abp/aspnetcore.mvc.ui.theme.shared/sweetalert/abp-sweetalert.js", + "/libs/abp/aspnetcore.mvc.ui.theme.shared/toastr/abp-toastr.js" + ); }); services.AddAssemblyOf(); 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 bb879c9ed5..ae6b3c8c78 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs @@ -4,27 +4,56 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling { public class BundleCollection { - private readonly Dictionary> _bundles; + private readonly Dictionary _bundleContributors; public BundleCollection() { - _bundles = new Dictionary>(); + _bundleContributors = new Dictionary(); } - public void Add(string bundleName, string[] files) + //TODO: Seperate to Add and WithFiles/WithContributors methods instead of coupling + public BundleConfiguration Add(string bundleName) { - var list = _bundles.GetOrAdd(bundleName, () => new List()); - list.AddRange(files); + if (_bundleContributors.ContainsKey(bundleName)) + { + 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; + } + + public BundleConfiguration Get(string bundleName) + { + if (!_bundleContributors.ContainsKey(bundleName)) + { + throw new AbpException($"There is no bundle added with given {nameof(bundleName)}: {bundleName}"); + } + + return _bundleContributors[bundleName]; + } + + public BundleConfiguration GetOrAdd(string bundleName) + { + return _bundleContributors.GetOrAdd(bundleName, () => new BundleConfiguration(bundleName)); } public List GetFiles(string bundleName) { - var files = _bundles.GetOrDefault(bundleName); - if (files == null) + var bundleConfiguration = _bundleContributors.GetOrDefault(bundleName); + if (bundleConfiguration == null) { throw new AbpException("Undefined bundle: " + bundleName); } + var files = new List(); + + foreach (var contributor in bundleConfiguration.Contributors) + { + contributor.Contribute(files); + } + return files; } } diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollectionExtensions.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollectionExtensions.cs new file mode 100644 index 0000000000..65967a2045 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollectionExtensions.cs @@ -0,0 +1,12 @@ +using Volo.Abp.AspNetCore.Mvc.UI.Bundling.Contributors; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling +{ + public static class BundleConfigurationExtensions + { + public static void AddFiles(this BundleConfiguration bundleConfiguration, params string[] files) + { + bundleConfiguration.Contributors.Add(new SimpleBundleContributor(files)); + } + } +} diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleConfiguration.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleConfiguration.cs new file mode 100644 index 0000000000..9b43a8fb74 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleConfiguration.cs @@ -0,0 +1,15 @@ +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling +{ + public class BundleConfiguration + { + public string Name { get; } + + public BundleContributorList Contributors { get; } + + public BundleConfiguration(string name) + { + Name = name; + Contributors = new BundleContributorList(); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleContributorList.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleContributorList.cs new file mode 100644 index 0000000000..c527ed7972 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleContributorList.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling +{ + public class BundleContributorList : List + { + + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/Contributors/SimpleBundleContributor.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/Contributors/SimpleBundleContributor.cs new file mode 100644 index 0000000000..4ba6e8b4c4 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/Contributors/SimpleBundleContributor.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.Contributors +{ + public class SimpleBundleContributor : IBundleContributor + { + public string[] Files { get; } + + public SimpleBundleContributor(params string[] files) + { + Files = files ?? Array.Empty(); + } + + public void Contribute(List files) + { + files.AddRange(Files); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleContributor.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleContributor.cs new file mode 100644 index 0000000000..78e502fb61 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleContributor.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling +{ + public interface IBundleContributor + { + void Contribute(List files); + } +} diff --git a/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs b/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs index d4711ad7fc..2cc73c2117 100644 --- a/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs +++ b/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs @@ -57,6 +57,29 @@ namespace System.Collections.Generic source.Insert(index, item); } + public static void ReplaceWhile(this IList source, Predicate selector, T item) + { + for (int i = 0; i < source.Count; i++) + { + if (selector(source[i])) + { + source[i] = item; + } + } + } + + public static void ReplaceWhile(this IList source, Predicate selector, Func itemFactory) + { + for (int i = 0; i < source.Count; i++) + { + var item = source[i]; + if (selector(item)) + { + source[i] = itemFactory(item); + } + } + } + public static void MoveItem(this List source, Predicate selector, int targetIndex) { if (!targetIndex.IsBetween(0, source.Count - 1)) diff --git a/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs b/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs index e8ed194314..ceacd5f298 100644 --- a/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs +++ b/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs @@ -10,7 +10,7 @@ namespace System.Collections.Generic public void InsertAfter() { var list = Enumerable.Range(1, 3).ToList(); - + list.InsertAfter(i => i == 2, 42); list.Count.ShouldBe(4); @@ -65,5 +65,37 @@ namespace System.Collections.Generic list[3].ShouldBe(2); list[4].ShouldBe(3); } + + [Fact] + public void ReplaceWhile_WithValue() + { + var list = Enumerable.Range(1, 3).ToList(); + + list[0].ShouldBe(1); + list[1].ShouldBe(2); + list[2].ShouldBe(3); + + list.ReplaceWhile(i => i >= 2, 42); + + list[0].ShouldBe(1); + list[1].ShouldBe(42); + list[2].ShouldBe(42); + } + + [Fact] + public void ReplaceWhile_WithFactory() + { + var list = Enumerable.Range(1, 3).ToList(); + + list[0].ShouldBe(1); + list[1].ShouldBe(2); + list[2].ShouldBe(3); + + list.ReplaceWhile(i => i >= 2, i => i + 1); + + list[0].ShouldBe(1); + list[1].ShouldBe(3); + list[2].ShouldBe(4); + } } }