Skip to content

Commit

Permalink
move preprocessor configuration into ISquishItOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCuse committed Sep 19, 2017
1 parent e8503d6 commit 2bf0535
Show file tree
Hide file tree
Showing 30 changed files with 269 additions and 169 deletions.
17 changes: 10 additions & 7 deletions SquishIt.AspNet/ConfigurationLoader.cs
Expand Up @@ -11,16 +11,19 @@ public static class ConfigurationLoader
{
public static void Initialize()
{
Configuration.Apply(Load);
Configuration.Apply(RegisterPlatform);
}

public static void Load(ISquishItOptions options)
public static void RegisterPlatform(ISquishItOptions options)
{
options.CacheImplementation = new CacheImplementation();
options.PathTranslator = new PathTranslator();
options.DebugStatusReader = new DebugStatusReader();
options.TrustLevel = new TrustLevel();
options.QueryStringUtility = new QueryStringUtility();
options.Platform = new PlatformConfiguration
{
CacheImplementation = new CacheImplementation(),
PathTranslator = new PathTranslator(),
DebugStatusReader = new DebugStatusReader(),
TrustLevel = new TrustLevel(),
QueryStringUtility = new QueryStringUtility()
};
}
}
}
2 changes: 1 addition & 1 deletion SquishIt.AspNet/Utilities/DebugStatusReader.cs
Expand Up @@ -36,7 +36,7 @@ public bool IsDebuggingEnabled(Func<bool> debugPredicate = null)

if(HttpContext.Current != null && HttpContext.Current.IsDebuggingEnabled)
{
return !Configuration.Instance.TrustLevel.IsHighOrUnrestrictedTrust || machineConfigReader.IsNotRetailDeployment;
return !Configuration.Instance.Platform.TrustLevel.IsHighOrUnrestrictedTrust || machineConfigReader.IsNotRetailDeployment;
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion SquishIt.CoffeeScript/Bootstrap.cs
Expand Up @@ -6,7 +6,7 @@ public class Bootstrap
{
public static void Initialize()
{
Bundle.RegisterScriptPreprocessor(new CoffeeScriptPreprocessor());
Configuration.Instance.RegisterScriptPreprocessor(new CoffeeScriptPreprocessor());
}
}
}
2 changes: 1 addition & 1 deletion SquishIt.Framework/Base/BundleBase.Rendering.Internals.cs
Expand Up @@ -76,7 +76,7 @@ IPreprocessor FindPreprocessor(string extension)
var instanceTypes = bundleState.Preprocessors.Select(ipp => ipp.GetType()).ToArray();

var preprocessor =
bundleState.Preprocessors.Union(Bundle.Preprocessors.Where(pp => !instanceTypes.Contains(pp.GetType())))
bundleState.Preprocessors.Union(Configuration.Instance.Preprocessors.Where(pp => !instanceTypes.Contains(pp.GetType())))
.FirstOrDefault(p => p.ValidFor(extension));

return preprocessor;
Expand Down
2 changes: 1 addition & 1 deletion SquishIt.Framework/Base/BundleBase.cs
Expand Up @@ -41,7 +41,7 @@ public abstract partial class BundleBase<T> : IRenderable where T : BundleBase<T
protected IDebugStatusReader debugStatusReader;
protected IDirectoryWrapper directoryWrapper;
protected IHasher hasher;
protected IPathTranslator pathTranslator = Configuration.Instance.PathTranslator;
protected IPathTranslator pathTranslator = Configuration.Instance.Platform.PathTranslator;

IMinifier<T> minifier;

Expand Down
108 changes: 0 additions & 108 deletions SquishIt.Framework/Bundle.cs
Expand Up @@ -12,114 +12,6 @@ namespace SquishIt.Framework
/// </summary>
public class Bundle
{
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" };
internal static readonly HashSet<String> AllowedStyleExtensions = new HashSet<string> { ".CSS" };

/// <summary>
/// Register a preprocessor instance to be used for all bundle types.
/// </summary>
/// <typeparam name="T"><see cref="IPreprocessor">IPreprocessor</see> implementation type.</typeparam>
/// <param name="instance"><see cref="IPreprocessor">IPreprocessor</see> instance.</param>
public static void RegisterGlobalPreprocessor<T>(T instance) where T : IPreprocessor
{
ValidatePreprocessor<T>(instance);
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>
/// Register a preprocessor instance to be used for script bundles.
/// </summary>
/// <typeparam name="T"><see cref="IPreprocessor">IPreprocessor</see> implementation type.</typeparam>
/// <param name="instance"><see cref="IPreprocessor">IPreprocessor</see> instance.</param>
public static void RegisterScriptPreprocessor<T>(T instance) where T : IPreprocessor
{
ValidatePreprocessor<T>(instance);
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>
/// Register a preprocessor instance to be used for all style bundles.
/// </summary>
/// <typeparam name="T"><see cref="IPreprocessor">IPreprocessor</see> implementation type.</typeparam>
/// <param name="instance"><see cref="IPreprocessor">IPreprocessor</see> instance.</param>
public static void RegisterStylePreprocessor<T>(T instance) where T : IPreprocessor
{
ValidatePreprocessor<T>(instance);
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)
{
if(Preprocessors.Any(p => p.GetType() == typeof(T)))
{
throw new InvalidOperationException(string.Format("Can't add multiple preprocessors of type: {0}", typeof(T).FullName));
}

foreach(var extension in instance.Extensions)
{
if (AllExtensions.Contains(extension))
{
throw new InvalidOperationException(string.Format("Can't add multiple preprocessors for extension: {0}", extension));
}
}
}

static IEnumerable<string> AllExtensions
{
get { return AllowedGlobalExtensions.Union(AllowedScriptExtensions).Union(AllowedStyleExtensions).Select(x => x.ToUpper()); }
}

public static void ClearPreprocessors()
{
Preprocessors.Clear();

AllowedGlobalExtensions.Clear();
AllowedScriptExtensions.Clear();
AllowedStyleExtensions.Clear();

AllowedScriptExtensions.Add(".JS");
AllowedStyleExtensions.Add(".CSS");
}

/// <summary>
/// Create a javascript bundle.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions SquishIt.Framework/CSS/CSSAssetsFileHasher.cs
Expand Up @@ -110,11 +110,11 @@ static string AppendQueryStringPairValue(string url, string key, string value)
}

//TODO: look at DI for this - currently CSSAssetsFileHasher is instantiated from CSSBundle, dont really have a need to keep a dependency there just to pass in
var querystring = Configuration.Instance.QueryStringUtility.Parse(queryString);//HttpUtility.ParseQueryString(queryString);
var querystring = Configuration.Instance.Platform.QueryStringUtility.Parse(queryString);//HttpUtility.ParseQueryString(queryString);

querystring.Add(key, value);

var querystringwithAppendedValue = Configuration.Instance.QueryStringUtility.Flatten(querystring);
var querystringwithAppendedValue = Configuration.Instance.Platform.QueryStringUtility.Flatten(querystring);

if (!string.IsNullOrEmpty(querystringwithAppendedValue))
{
Expand Down
6 changes: 3 additions & 3 deletions SquishIt.Framework/CSS/CSSBundle.cs
Expand Up @@ -43,12 +43,12 @@ protected override IMinifier<CSSBundle> DefaultMinifier

protected override IEnumerable<string> allowedExtensions
{
get { return bundleState.AllowedExtensions.Union(Bundle.AllowedGlobalExtensions.Union(Bundle.AllowedStyleExtensions)); }
get { return bundleState.AllowedExtensions.Union(Configuration.Instance.AllowedGlobalExtensions.Union(Configuration.Instance.AllowedStyleExtensions)); }
}

protected override IEnumerable<string> disallowedExtensions
{
get { return Bundle.AllowedScriptExtensions; }
get { return Configuration.Instance.AllowedScriptExtensions; }
}

protected override string defaultExtension
Expand All @@ -62,7 +62,7 @@ protected override string tagFormat
}

public CSSBundle()
: this(Configuration.Instance.DebugStatusReader)
: this(Configuration.Instance.Platform.DebugStatusReader)
{
}

Expand Down
2 changes: 1 addition & 1 deletion SquishIt.Framework/Caches/ContentCache.cs
Expand Up @@ -11,7 +11,7 @@ public abstract class ContentCache : IContentCache

public ContentCache()
{
_cacheImplementation = Configuration.Instance.CacheImplementation;
_cacheImplementation = Configuration.Instance.Platform.CacheImplementation;
}

private readonly List<string> CacheKeys = new List<string>();
Expand Down

0 comments on commit 2bf0535

Please sign in to comment.