Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/preprocessor-extensibility' into…
Browse files Browse the repository at this point in the history
… preprocessor-pipeline

Conflicts:
	SquishIt.Framework/Files/Input.cs
	SquishIt.Framework/Resolvers/EmbeddedResourceResolver.cs
	SquishIt.Framework/Resolvers/FileSystemResolver.cs
	SquishIt.Framework/Resolvers/HttpResolver.cs
	SquishIt.Framework/Resolvers/IResolver.cs
	SquishIt.Tests/FileSystemResolverTests.cs
	SquishIt.Tests/Stubs/StubResolver.cs
  • Loading branch information
AlexCuse committed Mar 28, 2012
2 parents f51e179 + 83c9517 commit e2b368e
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 26 deletions.
4 changes: 3 additions & 1 deletion SquishIt.Framework/Base/Asset.cs
Expand Up @@ -9,6 +9,7 @@ internal class Asset
internal int Order { get; set; }
internal bool IsEmbeddedResource { get; set; }
internal bool DownloadRemote { get; set; }
internal bool IsRecursive { get; set; }

internal bool IsLocal
{
Expand All @@ -34,12 +35,13 @@ internal Asset()
{
}

internal Asset(string localPath, string remotePath = null, int order = 0, bool isEmbeddedResource = false)
internal Asset(string localPath, string remotePath = null, int order = 0, bool isEmbeddedResource = false, bool isRecursive = true)
{
LocalPath = localPath;
RemotePath = remotePath;
Order = order;
IsEmbeddedResource = isEmbeddedResource;
IsRecursive = isRecursive;
}
}
}
21 changes: 14 additions & 7 deletions SquishIt.Framework/Base/BundleBase.cs
Expand Up @@ -86,7 +86,7 @@ private Input GetInputFile(Asset asset)
{
if (debugStatusReader.IsDebuggingEnabled())
{
return GetFileSystemPath(asset.LocalPath);
return GetFileSystemPath(asset.LocalPath, asset.IsRecursive);
}

if (asset.IsRemoteDownload)
Expand All @@ -95,7 +95,7 @@ private Input GetInputFile(Asset asset)
}
else
{
return GetFileSystemPath(asset.LocalPath);
return GetFileSystemPath(asset.LocalPath, asset.IsRecursive);
}
}
else
Expand All @@ -114,20 +114,20 @@ private List<Input> GetInputFiles(List<Asset> assets)
return inputFiles;
}

private Input GetFileSystemPath(string localPath)
private Input GetFileSystemPath(string localPath, bool isRecursive = true)
{
string mappedPath = FileSystem.ResolveAppRelativePathToFileSystem(localPath);
return new Input(mappedPath, ResolverFactory.Get<FileSystemResolver>());
return new Input(mappedPath, isRecursive, ResolverFactory.Get<FileSystemResolver>());
}

private Input GetHttpPath(string remotePath)
{
return new Input(remotePath, ResolverFactory.Get<HttpResolver>());
return new Input(remotePath, false, ResolverFactory.Get<HttpResolver>());
}

private Input GetEmbeddedResourcePath(string resourcePath)
{
return new Input(resourcePath, ResolverFactory.Get<EmbeddedResourceResolver>());
return new Input(resourcePath, false, ResolverFactory.Get<EmbeddedResourceResolver>());
}

protected IEnumerable<IPreprocessor> FindPreprocessors(string file)
Expand Down Expand Up @@ -237,9 +237,16 @@ public T Add(string fileOrFolderPath)
return (T)this;
}

public T AddDirectory(string folderPath, bool recursive = true)
{
AddAsset(new Asset(folderPath, isRecursive: recursive));
return (T)this;
}

public T AddString(string content)
{
arbitrary.Add(content);
if(!arbitrary.Contains(content))
arbitrary.Add(content);
return (T)this;
}

Expand Down
5 changes: 5 additions & 0 deletions SquishIt.Framework/Bundle.cs
Expand Up @@ -88,5 +88,10 @@ public static CSSBundle Css(Utilities.IDebugStatusReader debugStatusReader)
{
return new CSSBundle(debugStatusReader);
}

public static Configuration ConfigureDefaults()
{
return new Configuration();
}
}
}
121 changes: 121 additions & 0 deletions SquishIt.Framework/Configuration.cs
@@ -0,0 +1,121 @@
using System;
using SquishIt.Framework.Css;
using SquishIt.Framework.JavaScript;
using SquishIt.Framework.Minifiers;
using SquishIt.Framework.Minifiers.CSS;
using SquishIt.Framework.Minifiers.JavaScript;

namespace SquishIt.Framework
{
public class Configuration
{
public Configuration UseMinifierForCss<TMinifier>()
where TMinifier : IMinifier<CSSBundle>
{
return UseMinifierForCss(typeof (TMinifier));
}

public Configuration UseMinifierForCss(Type minifierType)
{
if (!typeof(IMinifier<CSSBundle>).IsAssignableFrom(minifierType))
throw new InvalidCastException(
String.Format("Type '{0}' must implement '{1}' to be used for Css minification.",
minifierType, typeof (IMinifier<CSSBundle>)));
_defaultCssMinifier = minifierType;
return this;
}

public Configuration UseMinifierForJs<TMinifier>()
where TMinifier : IMinifier<JavaScriptBundle>
{
return UseMinifierForJs(typeof (TMinifier));
}

public Configuration UseMinifierForJs(Type minifierType)
{
if (!typeof(IMinifier<JavaScriptBundle>).IsAssignableFrom(minifierType))
throw new InvalidCastException(
String.Format("Type '{0}' must implement '{1}' to be used for Javascript minification.",
minifierType, typeof (IMinifier<JavaScriptBundle>)));
_defaultJsMinifier = minifierType;
return this;
}

/// <summary>
/// Use Yahoo YUI Compressor for CSS minification by default.
/// </summary>
public Configuration UseYuiForCssMinification()
{
return UseMinifierForCss<YuiCompressor>();
}

/// <summary>
/// Use Microsoft Ajax Minifier for CSS minification by default.
/// </summary>
public Configuration UseMsAjaxForCssMinification()
{
return UseMinifierForCss<MsCompressor>();
}

/// <summary>
/// By default, perform no minification of CSS.
/// </summary>
public Configuration UseNoCssMinification()
{
return UseMinifierForCss<NullCompressor>();
}

/// <summary>
/// Use Microsoft Ajax Minifier for Javascript minification by default.
/// </summary>
public Configuration UseMsAjaxForJsMinification()
{
return UseMinifierForJs<MsMinifier>();
}

/// <summary>
/// Use Yahoo YUI Compressor for Javascript minification by default.
/// </summary>
public Configuration UseYuiForJsMinification()
{
return UseMinifierForJs<YuiMinifier>();
}

/// <summary>
/// Use Google Closure for Javascript minification by default.
/// </summary>
public Configuration UseClosureForMinification()
{
return UseMinifierForJs<ClosureMinifier>();
}

/// <summary>
/// By default, perform no minification of Javascript.
/// </summary>
public Configuration UseNoJsMinification()
{
return UseMinifierForJs<NullMinifier>();
}

/// <summary>
/// Use Douglas Crockford's JsMin for Javascript minification by default.
/// </summary>
public Configuration UseJsMinForJsMinification()
{
return UseMinifierForJs<JsMinMinifier>();
}

static Type _defaultCssMinifier = typeof (MsCompressor);
static Type _defaultJsMinifier = typeof (MsMinifier);

internal static IMinifier<CSSBundle> DefaultCssMinifier()
{
return (IMinifier<CSSBundle>)Activator.CreateInstance(_defaultCssMinifier);
}

public static IMinifier<JavaScriptBundle> DefaultJsMinifier()
{
return (IMinifier<JavaScriptBundle>)Activator.CreateInstance(_defaultJsMinifier);
}
}
}
2 changes: 1 addition & 1 deletion SquishIt.Framework/Css/CssBundle.cs
Expand Up @@ -36,7 +36,7 @@ protected override string CachePrefix

protected override IMinifier<CSSBundle> DefaultMinifier
{
get { return new MsCompressor(); }
get { return Configuration.DefaultCssMinifier(); }
}

protected override IEnumerable<string> allowedExtensions
Expand Down
6 changes: 4 additions & 2 deletions SquishIt.Framework/Files/Input.cs
Expand Up @@ -6,10 +6,12 @@ public class Input
{
public string Path { get; private set; }
public Resolvers.IResolver Resolver { get; private set; }
public bool IsRecursive { get; private set; }

public Input(string filePath, Resolvers.IResolver resolver)
public Input(string filePath, bool recursive, Resolvers.IResolver resolver)
{
Path = filePath;
IsRecursive = recursive;
Resolver = resolver;
}

Expand All @@ -21,7 +23,7 @@ public IEnumerable<string> TryResolve(IEnumerable<string> allowedExtensions, IEn
{
if (IsDirectory)
{
return Resolver.TryResolveFolder(Path, allowedExtensions, disallowedExtensions);
return Resolver.TryResolveFolder(Path, IsRecursive, allowedExtensions, disallowedExtensions);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion SquishIt.Framework/JavaScript/JavaScriptBundle.cs
Expand Up @@ -20,7 +20,7 @@ public class JavaScriptBundle : BundleBase<JavaScriptBundle>

protected override IMinifier<JavaScriptBundle> DefaultMinifier
{
get { return new MsMinifier(); }
get { return Configuration.DefaultJsMinifier(); }
}

protected override IEnumerable<string> allowedExtensions
Expand Down
2 changes: 1 addition & 1 deletion SquishIt.Framework/Resolvers/EmbeddedResourceResolver.cs
Expand Up @@ -32,7 +32,7 @@ public string TryResolve(string file)
}
}

public IEnumerable<string> TryResolveFolder(string path, IEnumerable<string> allowedExtensions, IEnumerable<string> disallowedFileExtensions)
public IEnumerable<string> TryResolveFolder(string path, bool recursive, IEnumerable<string> allowedExtensions, IEnumerable<string> disallowedExtensions)
{
throw new NotImplementedException("Adding entire directories only supported by FileSystemResolver.");
}
Expand Down
4 changes: 2 additions & 2 deletions SquishIt.Framework/Resolvers/FileSystemResolver.cs
Expand Up @@ -17,11 +17,11 @@ public bool IsDirectory(string path)
return Directory.Exists(path);
}

public IEnumerable<string> TryResolveFolder(string path, IEnumerable<string> allowedFileExtensions, IEnumerable<string> disallowedFileExtensions)
public IEnumerable<string> TryResolveFolder(string path, bool recursive, IEnumerable<string> allowedFileExtensions, IEnumerable<string> disallowedFileExtensions)
{
if (IsDirectory(path))
{
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
var files = Directory.GetFiles(path, "*.*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Where(
f => (allowedFileExtensions == null
|| allowedFileExtensions.Select(s => s.ToUpper()).Any(x => Extensions(f).Contains(x))
Expand Down
3 changes: 1 addition & 2 deletions SquishIt.Framework/Resolvers/HttpResolver.cs
Expand Up @@ -32,8 +32,7 @@ public string TryResolve(string file)
}
}

public IEnumerable<string> TryResolveFolder(string path, IEnumerable<string> allowedExtensions, IEnumerable<string> disallowedFileExtensions)
{
public IEnumerable<string> TryResolveFolder(string path, bool recursive, IEnumerable<string> allowedExtensions, IEnumerable<string> disallowedExtensions) {
throw new NotImplementedException("Adding entire directories only supported by FileSystemResolver.");
}

Expand Down
2 changes: 1 addition & 1 deletion SquishIt.Framework/Resolvers/IResolver.cs
Expand Up @@ -6,6 +6,6 @@ public interface IResolver
{
bool IsDirectory(string path);
string TryResolve(string path);
IEnumerable<string> TryResolveFolder(string path, IEnumerable<string> allowedFileExtensions, IEnumerable<string> disallowedFileExtensions);
IEnumerable<string> TryResolveFolder(string path, bool recursive, IEnumerable<string> allowedExtensions, IEnumerable<string> disallowedExtensions);
}
}
1 change: 1 addition & 0 deletions SquishIt.Framework/SquishIt.Framework.csproj
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Base\Asset.cs" />
<Compile Include="Base\BundleBase.cs" />
<Compile Include="Base\BundleState.cs" />
<Compile Include="Configuration.cs" />
<Compile Include="Utilities\CriticalRenderingSection.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="IBundleCache.cs" />
Expand Down
2 changes: 1 addition & 1 deletion SquishIt.Framework/Utilities/DebugStatusReader.cs
Expand Up @@ -26,7 +26,7 @@ public bool IsDebuggingEnabled()
{
//check retail setting in machine.config
//Thanks Dave Ward! http://www.encosia.com
Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();
System.Configuration.Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();
var group = machineConfig.GetSectionGroup("system.web");
if (group != null)
{
Expand Down
3 changes: 2 additions & 1 deletion SquishIt.Tests/CssBundleTests.cs
Expand Up @@ -70,11 +70,12 @@ public void CanAddMultiplePathFiles()
.WithDebuggingEnabled(true)
.Create();

// Obsolete Test
cssBundle1.Add("/css/first.css", "/css/second.css");
cssBundle2.Add("/css/first.css").Add("/css/second.css");

var cssBundle1Assets = cssBundle1.bundleState.Assets;
var cssBundle2Assets = cssBundle1.bundleState.Assets;
var cssBundle2Assets = cssBundle2.bundleState.Assets;

Assert.AreEqual(cssBundle1Assets.Count, cssBundle2Assets.Count);
for (var i = 0; i < cssBundle1Assets.Count; i++)
Expand Down
5 changes: 2 additions & 3 deletions SquishIt.Tests/FileSystemResolverTests.cs
Expand Up @@ -70,7 +70,7 @@ public void CanResolveDirectory()
File.Create(Path.Combine(directory.FullName, "file1")).Close();
File.Create(Path.Combine(directory.FullName, "file2")).Close();

var result = new FileSystemResolver().TryResolveFolder(path, null, null).ToList();
var result = new FileSystemResolver().TryResolveFolder(path, true, null, null).ToList();
Assert.AreEqual(2, result.Count);
Assert.Contains(path + Path.DirectorySeparatorChar + "file1", result);
Assert.Contains(path + Path.DirectorySeparatorChar + "file2", result);
Expand All @@ -94,8 +94,7 @@ public void CanResolveDirectory_Filters_Files_By_Extension()
File.Create(Path.Combine(directory.FullName, "file21.JS")).Close();
File.Create(Path.Combine(directory.FullName, "asdf.css.js")).Close();

var result = new FileSystemResolver().TryResolveFolder(path, new[] { ".js" }, new [] { ".css" }).ToList();
Assert.AreEqual(2, result.Count);
var result = new FileSystemResolver().TryResolveFolder(path, true, new[] { ".js" }, new [] { ".css" }).ToList();
Assert.Contains(path + Path.DirectorySeparatorChar + "file1.js", result);
Assert.Contains(path + Path.DirectorySeparatorChar + "file21.JS", result);
}
Expand Down
3 changes: 1 addition & 2 deletions SquishIt.Tests/Stubs/StubResolver.cs
Expand Up @@ -20,8 +20,7 @@ public string TryResolve(string file)
return _pathToResolveTo;
}

public IEnumerable<string> TryResolveFolder(string path, IEnumerable<string> allowedFileExtensions, IEnumerable<string> disallowedFileExtensions)
{
public IEnumerable<string> TryResolveFolder(string path, bool recursive, IEnumerable<string> allowedFileExtensions, IEnumerable<string> disallowedFileExtensions) {
return _directoryContents
.Where(
f => (allowedFileExtensions == null
Expand Down
8 changes: 7 additions & 1 deletion SquishItAspNetTest/Default.aspx
Expand Up @@ -54,7 +54,13 @@
.Add("~/css/import.css")
.WithAttribute("media", "screen")
.ForceRelease()
.Render("~/combinedimport_#.css") %>
.Render("~/combinedimport_#.css") %>
<!-- NonRecursive Test -->
<%= Bundle.Css()
.AddDirectory("~/css/", true)
.WithAttribute("media", "screen")
.ForceDebug()
.Render("~/nonrecursivedirectory_#.css") %>
<form id="form1" runat="server">
<div>

Expand Down

0 comments on commit e2b368e

Please sign in to comment.