Created
November 19, 2012 13:47
-
-
Save jasondentler/4110739 to your computer and use it in GitHub Desktop.
Cassette, bundle per directory, recursive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.IO; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Hosting; | |
using Cassette; | |
using Cassette.HtmlTemplates; | |
using Cassette.IO; | |
using Cassette.Scripts; | |
using Cassette.Stylesheets; | |
using NLog; | |
namespace MyCo.MyApp | |
{ | |
public static class BundleCollectionExtensions | |
{ | |
public static readonly Logger Logger = LogManager.GetLogger("BundleCollectionExtensions", typeof(Logger)); | |
public static void AddScriptsRecursively( | |
this BundleCollection bundles, | |
string applicationRelativePath, | |
Action<ScriptBundle> customize) | |
{ | |
bundles.AddBundle<ScriptBundle>(applicationRelativePath, | |
bundle => | |
{ | |
customize(bundle); | |
bundle.Assets.ToList() | |
.ForEach(a => Logger.Debug("Bundle {0} contains {1}", bundle.Path, a.Path)); | |
}); | |
} | |
public static void AddScriptsRecursively( | |
this BundleCollection bundles, | |
string applicationRelativePath) | |
{ | |
bundles.AddScriptsRecursively(applicationRelativePath, | |
bundle => { }); | |
} | |
public static void AddStylesheetsRecursively( | |
this BundleCollection bundles, | |
string applicationRelativePath, | |
Action<StylesheetBundle> customize) | |
{ | |
bundles.AddBundle(applicationRelativePath, | |
customize); | |
} | |
public static void AddStylesheetsRecursively( | |
this BundleCollection bundles, | |
string applicationRelativePath) | |
{ | |
bundles.AddStylesheetsRecursively(applicationRelativePath, | |
bundle => { }); | |
} | |
public static void AddHtmlTemplatesRecursively( | |
this BundleCollection bundles, | |
string applicationRelativePath, | |
Action<HtmlTemplateBundle> customize) | |
{ | |
bundles.AddBundle(applicationRelativePath, | |
customize); | |
} | |
public static void AddHtmlTemplatesRecursively( | |
this BundleCollection bundles, | |
string applicationRelativePath) | |
{ | |
bundles.AddHtmlTemplatesRecursively(applicationRelativePath, | |
bundle => { }); | |
} | |
private static void AddBundle<TBundle>( | |
this BundleCollection bundles, | |
string applicationRelativePath, | |
Action<TBundle> customize) | |
where TBundle : Bundle | |
{ | |
var searcher = new FileSearch() | |
{ | |
SearchOption = SearchOption.TopDirectoryOnly | |
}; | |
var directory = GetFileSystemPath(applicationRelativePath); | |
directory | |
.GetDirectories() | |
.Select(subdirectory => AppendToRelativePath(applicationRelativePath, subdirectory)) | |
.ToList() | |
.ForEach(path => bundles.AddBundle(path, customize)); | |
bundles.Add(applicationRelativePath, searcher, customize); | |
} | |
private static IDirectory GetFileSystemPath(string applicationRelativePath) | |
{ | |
var path = HostingEnvironment.MapPath("~/" + applicationRelativePath); | |
return new FileSystemDirectory(path); | |
} | |
private static string AppendToRelativePath(string applicationRelativePath, IDirectory directory) | |
{ | |
var directoryPath = directory.FullPath; | |
var pathSeparator = Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture); | |
if (directoryPath.EndsWith(pathSeparator)) | |
directoryPath = Path.GetDirectoryName(directoryPath); | |
var directoryName = Path.GetFileName(directoryPath); | |
if (!applicationRelativePath.EndsWith("/")) | |
applicationRelativePath += "/"; | |
return applicationRelativePath + directoryName; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
using Cassette; | |
using Cassette.Scripts; | |
namespace MyCo.MyApp | |
{ | |
/// <summary> | |
/// Configures the Cassette asset modules for the web application. | |
/// </summary> | |
public class CassetteConfiguration : IConfiguration<BundleCollection> | |
{ | |
public void Configure(BundleCollection bundles) | |
{ | |
bundles.AddStylesheetsRecursively("_css"); | |
bundles.AddScriptsRecursively("_scripts", | |
bundle => | |
{ | |
switch (bundle.Path) | |
{ | |
case "~/_scripts/lib/html5bundle/IE/Top": | |
bundle.PageLocation = "IEFixesTop"; | |
break; | |
case "~/_scripts/lib/html5bundle/IE/Bottom": | |
bundle.PageLocation = "IEFixesBottom"; | |
break; | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment