Permalink
Browse files

Added simple collections to support programmatic configuration.

  • Loading branch information...
1 parent dabd220 commit fa5dc3bbf56fd40dfd399e71ef5d6ac2645bf7d3 @tdupont750 committed Apr 3, 2012
@@ -1,5 +1,4 @@
<?xml version="1.0"?>
-
<bundleConfig ignoreIfDebug="true" ignoreIfLocal="true">
<cssBundles>
<add bundlePath="~/css/shared">
@@ -5,9 +5,6 @@
namespace MvcApplication
{
- // Note: For instructions on enabling IIS6 or IIS7 classic mode,
- // visit http://go.microsoft.com/?LinkId=9394801
-
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
@@ -35,10 +32,8 @@ public static void RegisterRoutes(RouteCollection routes)
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
-
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
-
BundleTable.Bundles.RegisterTemplateBundles();
// Only code required for MvcBundleConfig wire up
@@ -183,7 +183,9 @@
<Content Include="Views\Home\Index.cshtml" />
</ItemGroup>
<ItemGroup>
- <Content Include="Bundles.config" />
+ <Content Include="Bundles.config">
+ <SubType>Designer</SubType>
+ </Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MvcBundleConfig\MvcBundleConfig.csproj">
@@ -0,0 +1,100 @@
+using System;
+using System.Collections.Generic;
+using System.Web;
+using System.Web.Configuration;
+using MvcBundleConfig.Configuration;
+using MvcBundleConfig.Models;
+
+namespace MvcBundleConfig
+{
+ public class BundleConfigurationManager
+ {
+ private static BundleConfigCollection _collection;
+
+ public static BundleConfigCollection GetBundleConfigCollection()
+ {
+ if (_collection == null)
+ throw new ApplicationException("BundleConfigCollection has not been initialized.");
+
+ return _collection;
+ }
+
+ public static void Init(BundleConfigCollection collection = null)
+ {
+ if (collection == null)
+ {
+ BundleConfigurationSection config;
+
+ try
+ {
+ config = (BundleConfigurationSection)WebConfigurationManager.GetSection("bundleConfig");
+ }
+ catch (Exception ex)
+ {
+ throw new ApplicationException("Can not find section <bundleConfig> in the configuration file", ex);
+ }
+
+ var cssBundles = CreateBundleConfigs(config.CssBundles);
+ var jsBundles = CreateBundleConfigs(config.JsBundles);
+
+ collection = new BundleConfigCollection
+ {
+ IgnoreIfDebug = config.IgnoreIfDebug,
+ IgnoreIfLocal = config.IgnoreIfLocal,
+ CssBundles = cssBundles,
+ JsBundles = jsBundles
+ };
+ }
+
+ _collection = collection;
+ }
+
+ private static IList<BundleConfig> CreateBundleConfigs(BundleConfigurationElementCollection bundles)
+ {
+ var result = new List<BundleConfig>();
+
+ foreach (BundleConfigurationElement bundle in bundles)
+ {
+ var bundleConfig = new BundleConfig
+ {
+ BundlePath = bundle.BundlePath,
+ Minify = bundle.Minify,
+ Directories = new List<BundleDirectory>(),
+ Files = new List<BundleFile>()
+ };
+
+ foreach (BundleDirectoryConfigurationElement directory in bundle.Directories)
+ {
+ bundleConfig.Directories.Add(new BundleDirectory
+ {
+ DirectoryPath = directory.DirectoryPath,
+ SearchPattern = directory.SearchPattern,
+ SearchSubdirectories = directory.SearchSubdirectories,
+ ThrowIfNotExist = directory.ThrowIfNotExist
+ });
+ }
+
+ foreach (BundleFileConfigurationElement file in bundle.Files)
+ {
+ bundleConfig.Files.Add(new BundleFile
+ {
+ FilePath = file.FilePath,
+ ThrowIfNotExist = file.ThrowIfNotExist
+ });
+ }
+
+ result.Add(bundleConfig);
+ }
+
+ return result;
+ }
+
+ public static bool Ignore(HttpContextBase httpContext)
+ {
+ var config = GetBundleConfigCollection();
+
+ return (config.IgnoreIfDebug && httpContext.IsDebuggingEnabled)
+ || (config.IgnoreIfLocal && httpContext.Request.IsLocal);
+ }
+ }
+}
@@ -1,36 +0,0 @@
-using System;
-using System.Web;
-using System.Web.Configuration;
-
-namespace MvcBundleConfig.Configuration
-{
- public class BundleConfigurationManager
- {
- private static BundleConfigurationSection _config;
-
- public static BundleConfigurationSection GetBundleConfiguration()
- {
- if (_config == null)
- {
- try
- {
- _config = (BundleConfigurationSection)WebConfigurationManager.GetSection("bundleConfig");
- }
- catch (Exception ex)
- {
- throw new ApplicationException("Can not find section <bundleConfig> in the configuration file", ex);
- }
- }
-
- return _config;
- }
-
- public static bool Ignore(HttpContextBase httpContext)
- {
- var config = GetBundleConfiguration();
-
- return (config.IgnoreIfDebug && httpContext.IsDebuggingEnabled)
- || (config.IgnoreIfLocal && httpContext.Request.IsLocal);
- }
- }
-}
@@ -1,35 +1,39 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Optimization;
+using MvcBundleConfig;
using MvcBundleConfig.Configuration;
+using MvcBundleConfig.Models;
namespace System.Web.Optimization
{
public static class BundleCollectionExtensions
{
- public static void RegisterConfigurationBundles(this BundleCollection bundles)
+ public static void RegisterConfigurationBundles(this BundleCollection bundles, BundleConfigCollection bundleConfigCollection = null)
{
- var config = BundleConfigurationManager.GetBundleConfiguration();
+ BundleConfigurationManager.Init(bundleConfigCollection);
- AddBuldeConfiguration<CssMinify>(bundles, config.CssBundles);
- AddBuldeConfiguration<JsMinify>(bundles, config.JsBundles);
+ var config = BundleConfigurationManager.GetBundleConfigCollection();
+
+ AddBundleConfiguration<CssMinify>(bundles, config.CssBundles);
+ AddBundleConfiguration<JsMinify>(bundles, config.JsBundles);
}
- private static void AddBuldeConfiguration<T>(BundleCollection bundles, BundleConfigurationElementCollection configCollection)
+ private static void AddBundleConfiguration<T>(BundleCollection bundles, IEnumerable<BundleConfig> bundleConfigs)
where T : IBundleTransform, new()
{
- foreach (BundleConfigurationElement bundleConfig in configCollection)
+ foreach (var bundleConfig in bundleConfigs)
{
var transform = bundleConfig.Minify
? (IBundleTransform)new T()
: new NoTransform();
var bundle = new Bundle(bundleConfig.BundlePath, transform);
- foreach (BundleFileConfigurationElement file in bundleConfig.Files)
+ foreach (var file in bundleConfig.Files)
bundle.AddFile(file.FilePath, file.ThrowIfNotExist);
- foreach (BundleDirectoryConfigurationElement directory in bundleConfig.Directories)
+ foreach (var directory in bundleConfig.Directories)
bundle.AddDirectory(directory.DirectoryPath, directory.SearchPattern, directory.SearchSubdirectories, directory.ThrowIfNotExist);
bundles.Add(bundle);
@@ -2,6 +2,7 @@
using System.Text;
using System.Web.Mvc;
using System.Web.Optimization;
+using MvcBundleConfig;
using MvcBundleConfig.Configuration;
namespace System.Web.Mvc
@@ -0,0 +1,13 @@
+using System.Collections.Generic;
+
+namespace MvcBundleConfig.Models
+{
+ public class BundleConfig
+ {
+ public string BundlePath { get; set; }
+ public bool Minify { get; set; }
+
+ public IList<BundleDirectory> Directories { get; set; }
+ public IList<BundleFile> Files { get; set; }
+ }
+}
@@ -0,0 +1,13 @@
+using System.Collections.Generic;
+
+namespace MvcBundleConfig.Models
+{
+ public class BundleConfigCollection
+ {
+ public bool IgnoreIfDebug { get; set; }
+ public bool IgnoreIfLocal { get; set; }
+
+ public IList<BundleConfig> CssBundles { get; set; }
+ public IList<BundleConfig> JsBundles { get; set; }
+ }
+}
@@ -0,0 +1,10 @@
+namespace MvcBundleConfig.Models
+{
+ public class BundleDirectory
+ {
+ public string DirectoryPath { get; set; }
+ public string SearchPattern { get; set; }
+ public bool SearchSubdirectories { get; set; }
+ public bool ThrowIfNotExist { get; set; }
+ }
+}
@@ -0,0 +1,12 @@
+using System;
+using System.Linq;
+using System.Text;
+
+namespace MvcBundleConfig.Models
+{
+ public class BundleFile
+ {
+ public string FilePath { get; set; }
+ public bool ThrowIfNotExist { get; set; }
+ }
+}
@@ -52,14 +52,18 @@
<ItemGroup>
<Compile Include="Configuration\BundleConfigurationElement.cs" />
<Compile Include="Configuration\BundleConfigurationElementCollection.cs" />
- <Compile Include="Configuration\BundleConfigurationManager.cs" />
+ <Compile Include="BundleConfigurationManager.cs" />
<Compile Include="Configuration\BundleConfigurationSection.cs" />
<Compile Include="Configuration\BundleDirectoryConfigurationElement.cs" />
<Compile Include="Configuration\BundleDirectoryConfigurationElementCollection.cs" />
<Compile Include="Configuration\BundleFileConfigurationElement.cs" />
<Compile Include="Configuration\BundleFileConfigurationElementCollection.cs" />
<Compile Include="Extensions\BundleCollectionExtensions.cs" />
<Compile Include="Extensions\HtmlHelperExtensions.cs" />
+ <Compile Include="Models\BundleConfig.cs" />
+ <Compile Include="Models\BundleConfigCollection.cs" />
+ <Compile Include="Models\BundleDirectory.cs" />
+ <Compile Include="Models\BundleFile.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

1 comment on commit fa5dc3b

Cool!

Please sign in to comment.