Permalink
Browse files

include IgnoreExtensions on IPreprocessor interface (#279)

  • Loading branch information...
1 parent e4494a5 commit bd155a94d97e4c380872cd140e8dde8b1b93792d @AlexCuse AlexCuse committed Mar 10, 2014
@@ -117,9 +117,11 @@ IPreprocessor FindPreprocessor(string extension)
{
var instanceTypes = bundleState.Preprocessors.Select(ipp => ipp.GetType()).ToArray();
- return Bundle.Preprocessors.Where(pp => !instanceTypes.Contains(pp.GetType()))
+ var preprocessor = Bundle.Preprocessors.Where(pp => !instanceTypes.Contains(pp.GetType()))
.Union(bundleState.Preprocessors)
.FirstOrDefault(p => p.ValidFor(extension));
+
+ return preprocessor;
}
string ExpandAppRelativePath(string file)
@@ -48,6 +48,14 @@ internal void AddPreprocessor(IPreprocessor instance)
}
Preprocessors.Add(instance);
}
+ if (instance.IgnoreExtensions.NullSafeAny())
+ {
+ foreach (var extension in instance.IgnoreExtensions)
+ {
+ AllowedExtensions.Add(extension);
+ }
+ Preprocessors.Add(new NullPreprocessor(instance.IgnoreExtensions));
+ }
}
}
}
@@ -6,7 +6,7 @@ namespace SquishIt.Framework.Base
/// <summary>
/// Base class for implementing custom preprocessors.
/// </summary>
- public abstract class Preprocessor: IPreprocessor
+ public abstract class Preprocessor : IPreprocessor
{
/// <summary>
/// Report to caller whether preprocessor is active for provided file extension.
@@ -24,11 +24,16 @@ public bool ValidFor(string extension)
/// <param name="filePath">Disk location of content.</param>
/// <param name="content">Content.</param>
/// <returns><see cref="IProcessResult">IProcessResult</see></returns>
- public abstract IProcessResult Process(string filePath, string content);
+ public abstract IProcessResult Process(string filePath, string content);
/// <summary>
/// File extensions this preprocessor instance can be used for.
/// </summary>
public abstract string[] Extensions { get; }
+
+ /// <summary>
+ /// Extensions that should be "ignored" when calculating preprocessors to use from file path.
+ /// </summary>
+ public virtual string[] IgnoreExtensions { get { return new string[0]; } }
}
}
@@ -12,8 +12,7 @@ namespace SquishIt.Framework
/// </summary>
public class Bundle
{
- private static readonly IPreprocessor defaultNullPreprocessor = new NullPreprocessor(".JS", ".CSS", ".HTML");
- internal static readonly List<IPreprocessor> Preprocessors = new List<IPreprocessor> { defaultNullPreprocessor };
+ internal static readonly List<IPreprocessor> Preprocessors = new List<IPreprocessor>();
internal static readonly HashSet<String> AllowedGlobalExtensions = new HashSet<string>();
internal static readonly HashSet<String> AllowedScriptExtensions = new HashSet<string> { ".JS" };
@@ -27,8 +26,19 @@ public class Bundle
public static void RegisterGlobalPreprocessor<T>(T instance) where T : IPreprocessor
{
ValidatePreprocessor<T>(instance);
- foreach (var ext in instance.Extensions) AllowedGlobalExtensions.Add(ext.ToUpper());
+ foreach (var ext in instance.Extensions)
+ {
+ AllowedGlobalExtensions.Add(ext.ToUpper());
+ }
Preprocessors.Add(instance);
+ if (instance.IgnoreExtensions.NullSafeAny())
+ {
+ foreach(var ext in instance.IgnoreExtensions)
+ {
+ AllowedGlobalExtensions.Add(ext.ToUpper());
+ }
+ Preprocessors.Add(new NullPreprocessor(instance.IgnoreExtensions));
+ }
}
/// <summary>
@@ -39,8 +49,19 @@ public class Bundle
public static void RegisterScriptPreprocessor<T>(T instance) where T : IPreprocessor
{
ValidatePreprocessor<T>(instance);
- foreach (var ext in instance.Extensions) AllowedScriptExtensions.Add(ext.ToUpper());
+ foreach(var ext in instance.Extensions)
+ {
+ AllowedScriptExtensions.Add(ext.ToUpper());
+ }
Preprocessors.Add(instance);
+ if(instance.IgnoreExtensions.NullSafeAny())
+ {
+ foreach(var ext in instance.IgnoreExtensions)
+ {
+ AllowedScriptExtensions.Add(ext.ToUpper());
+ }
+ Preprocessors.Add(new NullPreprocessor(instance.IgnoreExtensions));
+ }
}
/// <summary>
@@ -51,8 +72,19 @@ public class Bundle
public static void RegisterStylePreprocessor<T>(T instance) where T : IPreprocessor
{
ValidatePreprocessor<T>(instance);
- foreach (var ext in instance.Extensions) AllowedStyleExtensions.Add(ext.ToUpper());
+ foreach (var ext in instance.Extensions)
+ {
+ AllowedStyleExtensions.Add(ext.ToUpper());
+ }
Preprocessors.Add(instance);
+ if(instance.IgnoreExtensions.NullSafeAny())
+ {
+ foreach(var ext in instance.IgnoreExtensions)
+ {
+ AllowedStyleExtensions.Add(ext.ToUpper());
+ }
+ Preprocessors.Add(new NullPreprocessor(instance.IgnoreExtensions));
+ }
}
static void ValidatePreprocessor<T>(IPreprocessor instance)
@@ -86,7 +118,6 @@ public static void ClearPreprocessors()
AllowedScriptExtensions.Add(".JS");
AllowedStyleExtensions.Add(".CSS");
- Preprocessors.Add(defaultNullPreprocessor);
}
/// <summary>
@@ -24,5 +24,10 @@ public interface IPreprocessor
/// File extensions this preprocessor instance can be used for.
/// </summary>
string[] Extensions { get; }
+
+ /// <summary>
+ /// Extensions that should be "ignored" when calculating preprocessors to use from file path.
+ /// </summary>
+ string[] IgnoreExtensions { get; }
}
}
@@ -18,7 +18,12 @@ public override string[] Extensions
get { return new[] { ".hogan" }; }
}
- public override IProcessResult Process(string filePath, string content)
+ public override string[] IgnoreExtensions
+ {
+ get { return new [] { ".html" }; }
+ }
+
+ public override IProcessResult Process(string filePath, string content)
{
var compiler = new HoganCompiler();
string renderFunc = compiler.Compile(content);
@@ -20,13 +20,21 @@ public override string[] Extensions
get { return new[] { ".less" }; }
}
+ public override string[] IgnoreExtensions
+ {
+ get
+ {
+ return new[] { ".css" };
+ }
+ }
+
/// <summary>
/// Can be overridden to build engine with specific configuration.
/// </summary>
public static Func<ILessEngine> EngineBuilder = () => new EngineFactory().GetEngine();
- public LessPreprocessor() : this(EngineBuilder, Configuration.Instance.DefaultPathTranslator()){ }
-
+ public LessPreprocessor() : this(EngineBuilder, Configuration.Instance.DefaultPathTranslator()) { }
+
/// <summary>
/// Construct LESS preprocessor with custom engine construction.
/// </summary>
@@ -45,16 +53,15 @@ public override IProcessResult Process(string filePath, string content)
string dir = Path.GetDirectoryName(filePath);
string appPath = string.Empty;
- if (!string.IsNullOrEmpty(dir))
+ if(!string.IsNullOrEmpty(dir))
{
appPath = pathTranslator.ResolveFileSystemPathToAppRelative(dir);
}
- var dependencies = engine.GetImports().Select(importPath =>
- {
+ var dependencies = engine.GetImports().Select(importPath => {
return pathTranslator.ResolveAppRelativePathToFileSystem(Path.Combine(appPath, importPath));
});
-
+
return new ProcessResult(css, dependencies);
}
}
@@ -18,7 +18,12 @@ public override string[] Extensions
get { return new[] { ".hogan" }; }
}
- public override IProcessResult Process(string filePath, string content)
+ public override string[] IgnoreExtensions
+ {
+ get { return new [] { ".html" }; }
+ }
+
+ public override IProcessResult Process(string filePath, string content)
{
var compiler = new HoganCompiler();
string renderFunc = compiler.Compile(content);
@@ -1074,12 +1074,12 @@ public void CanBundleDirectoryContentsInDebug_Writes_And_Ignores_Preprocessed_De
{
var path = Guid.NewGuid().ToString();
var file1 = TestUtilities.PrepareRelativePath(path + "\\file1.style.css");
- var file2 = TestUtilities.PrepareRelativePath(path + "\\file1.style.css.squishit.debug.css");
+ var file2 = TestUtilities.PrepareRelativePath(path + "\\file1.style.squishit.debug.css");
var content = "some stuffs";
var preprocessor = new StubStylePreprocessor();
- using(new ScriptPreprocessorScope<StubStylePreprocessor>(preprocessor))
+ using(new StylePreprocessorScope<StubStylePreprocessor>(preprocessor))
using(new ResolverFactoryScope(typeof(FileSystemResolver).FullName, StubResolver.ForDirectory(new[] { file1, file2 })))
{
var frf = new StubFileReaderFactory();
@@ -23,7 +23,7 @@ public class StylePreprocessorScope<T> : IDisposable where T : IPreprocessor
{
public StylePreprocessorScope(T instance)
{
- Bundle.RegisterScriptPreprocessor(instance);
+ Bundle.RegisterStylePreprocessor(instance);
}
public StylePreprocessorScope()
@@ -40,7 +40,7 @@ public class GlobalPreprocessorScope<T> : IDisposable where T:IPreprocessor
{
public GlobalPreprocessorScope(T instance)
{
- Bundle.RegisterScriptPreprocessor(instance);
+ Bundle.RegisterGlobalPreprocessor(instance);
}
public GlobalPreprocessorScope()
@@ -2,30 +2,26 @@
using SquishIt.Framework;
using SquishIt.Framework.Base;
-namespace SquishIt.Tests.Stubs {
- public interface IStubPreprocessor : IPreprocessor
+namespace SquishIt.Tests.Stubs
+{
+ public interface IStubPreprocessor : IPreprocessor
{
string CalledWith { get; }
}
- public class StubGlobalPreprocessor : IStubPreprocessor
+ public class StubGlobalPreprocessor : Preprocessor, IStubPreprocessor
{
string calledWith;
- public bool ValidFor (string extension)
- {
- return Extensions.Contains (extension);
- }
-
- public IProcessResult Process(string filePath, string content)
+ public override IProcessResult Process(string filePath, string content)
{
calledWith = content;
- return new ProcessResult("globey");
+ return new ProcessResult("globey");
}
- public string[] Extensions
+ public override string[] Extensions
{
- get { return new[] { "global" }; }
+ get { return new[] { ".global" }; }
}
public string CalledWith
@@ -34,52 +30,46 @@ public string CalledWith
}
}
- public class StubScriptPreprocessor : IStubPreprocessor
+ public class StubScriptPreprocessor : Preprocessor, IStubPreprocessor
{
string calledWith;
- public bool ValidFor (string extension)
- {
- return Extensions.Contains (extension);
- }
-
- public IProcessResult Process(string filePath, string content)
+ public override IProcessResult Process(string filePath, string content)
{
calledWith = content;
- return new ProcessResult("scripty");
+ return new ProcessResult("scripty");
}
- public string[] Extensions
+ public override string[] Extensions
{
- get { return new[] { "script" }; }
+ get { return new[] { ".script" }; }
}
+ public override string[] IgnoreExtensions { get { return new[] { ".js" }; } }
+
public string CalledWith
{
get { return calledWith; }
}
}
- public class StubStylePreprocessor : IStubPreprocessor
+ public class StubStylePreprocessor : Preprocessor, IStubPreprocessor
{
string calledWith;
- public bool ValidFor (string extension)
- {
- return Extensions.Contains (extension);
- }
-
- public IProcessResult Process(string filePath, string content)
+ public override IProcessResult Process(string filePath, string content)
{
calledWith = content;
- return new ProcessResult("styley");
+ return new ProcessResult("styley");
}
- public string[] Extensions
+ public override string[] Extensions
{
- get { return new[] { "style" }; }
+ get { return new[] { ".style" }; }
}
+ public override string[] IgnoreExtensions { get { return new[] { ".css" }; } }
+
public string CalledWith
{
get { return calledWith; }

0 comments on commit bd155a9

Please sign in to comment.