Browse Source

Revised bundling system.

pull/301/head
Halil ibrahim Kalkan 8 years ago
parent
commit
99bdb075fa
  1. 66
      src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs
  2. 43
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs
  3. 12
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollectionExtensions.cs
  4. 15
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleConfiguration.cs
  5. 9
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleContributorList.cs
  6. 20
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/Contributors/SimpleBundleContributor.cs
  7. 9
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleContributor.cs
  8. 23
      src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs
  9. 34
      test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs

66
src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs

@ -20,40 +20,44 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared
services.Configure<BundlingOptions>(options => services.Configure<BundlingOptions>(options =>
{ {
options.StyleBundles.Add("GlobalStyles", new[] options
{ .StyleBundles
"/libs/font-awesome/css/font-awesome.css", .Add("GlobalStyles")
"/libs/bootstrap/css/bootstrap.css", .AddFiles(
"/libs/datatables.net-bs4/css/dataTables.bootstrap4.css", "/libs/font-awesome/css/font-awesome.css",
"/libs/toastr/toastr.min.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[] options
{ .ScriptBundles
"/libs/jquery/jquery.js", .Add("GlobalScripts")
"/libs/bootstrap/js/bootstrap.bundle.js", .AddFiles(
"/libs/jquery-validation/jquery.validate.js", "/libs/jquery/jquery.js",
"/libs/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js", "/libs/bootstrap/js/bootstrap.bundle.js",
"/libs/jquery-form/jquery.form.min.js", "/libs/jquery-validation/jquery.validate.js",
"/libs/datatables.net/js/jquery.dataTables.js", "/libs/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js",
"/libs/datatables.net-bs4/js/dataTables.bootstrap4.js", "/libs/jquery-form/jquery.form.min.js",
"/libs/sweetalert/sweetalert.min.js", "/libs/datatables.net/js/jquery.dataTables.js",
"/libs/toastr/toastr.min.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/core/abp.js",
"/libs/abp/jquery/abp.dom.js", "/libs/abp/jquery/abp.dom.js",
"/libs/abp/jquery/abp.ajax.js", "/libs/abp/jquery/abp.ajax.js",
"/libs/abp/jquery/abp.resource-loader.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/jquery-extensions.js",
"/libs/abp/aspnetcore.mvc.ui.theme.shared/jquery-form/jquery-form-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/dom-event-handlers.js",
"/libs/abp/aspnetcore.mvc.ui.theme.shared/bootstrap/modal-manager.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/datatables/datatables-extensions.js",
"/libs/abp/aspnetcore.mvc.ui.theme.shared/sweetalert/abp-sweetalert.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/aspnetcore.mvc.ui.theme.shared/toastr/abp-toastr.js"
}); );
}); });
services.AddAssemblyOf<AbpAspNetCoreMvcUiThemeSharedModule>(); services.AddAssemblyOf<AbpAspNetCoreMvcUiThemeSharedModule>();

43
src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs

@ -4,27 +4,56 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{ {
public class BundleCollection public class BundleCollection
{ {
private readonly Dictionary<string, List<string>> _bundles; private readonly Dictionary<string, BundleConfiguration> _bundleContributors;
public BundleCollection() public BundleCollection()
{ {
_bundles = new Dictionary<string, List<string>>(); _bundleContributors = new Dictionary<string, BundleConfiguration>();
} }
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<string>()); if (_bundleContributors.ContainsKey(bundleName))
list.AddRange(files); {
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<string> GetFiles(string bundleName) public List<string> GetFiles(string bundleName)
{ {
var files = _bundles.GetOrDefault(bundleName); var bundleConfiguration = _bundleContributors.GetOrDefault(bundleName);
if (files == null) if (bundleConfiguration == null)
{ {
throw new AbpException("Undefined bundle: " + bundleName); throw new AbpException("Undefined bundle: " + bundleName);
} }
var files = new List<string>();
foreach (var contributor in bundleConfiguration.Contributors)
{
contributor.Contribute(files);
}
return files; return files;
} }
} }

12
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));
}
}
}

15
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();
}
}
}

9
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<IBundleContributor>
{
}
}

20
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<string>();
}
public void Contribute(List<string> files)
{
files.AddRange(Files);
}
}
}

9
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<string> files);
}
}

23
src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs

@ -57,6 +57,29 @@ namespace System.Collections.Generic
source.Insert(index, item); source.Insert(index, item);
} }
public static void ReplaceWhile<T>(this IList<T> source, Predicate<T> selector, T item)
{
for (int i = 0; i < source.Count; i++)
{
if (selector(source[i]))
{
source[i] = item;
}
}
}
public static void ReplaceWhile<T>(this IList<T> source, Predicate<T> selector, Func<T, T> itemFactory)
{
for (int i = 0; i < source.Count; i++)
{
var item = source[i];
if (selector(item))
{
source[i] = itemFactory(item);
}
}
}
public static void MoveItem<T>(this List<T> source, Predicate<T> selector, int targetIndex) public static void MoveItem<T>(this List<T> source, Predicate<T> selector, int targetIndex)
{ {
if (!targetIndex.IsBetween(0, source.Count - 1)) if (!targetIndex.IsBetween(0, source.Count - 1))

34
test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs

@ -10,7 +10,7 @@ namespace System.Collections.Generic
public void InsertAfter() public void InsertAfter()
{ {
var list = Enumerable.Range(1, 3).ToList(); var list = Enumerable.Range(1, 3).ToList();
list.InsertAfter(i => i == 2, 42); list.InsertAfter(i => i == 2, 42);
list.Count.ShouldBe(4); list.Count.ShouldBe(4);
@ -65,5 +65,37 @@ namespace System.Collections.Generic
list[3].ShouldBe(2); list[3].ShouldBe(2);
list[4].ShouldBe(3); 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);
}
} }
} }

Loading…
Cancel
Save