forked from jetheredge/SquishIt
/
Bundle.cs
97 lines (81 loc) · 3.32 KB
/
Bundle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SquishIt.Framework.Css;
using SquishIt.Framework.JavaScript;
namespace SquishIt.Framework
{
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" };
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);
}
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);
}
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);
}
private 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));
}
}
}
private 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");
}
public static JavaScriptBundle JavaScript()
{
return new JavaScriptBundle();
}
public static JavaScriptBundle JavaScript(Utilities.IDebugStatusReader debugStatusReader)
{
return new JavaScriptBundle(debugStatusReader);
}
public static CSSBundle Css()
{
return new CSSBundle();
}
public static CSSBundle Css(Utilities.IDebugStatusReader debugStatusReader)
{
return new CSSBundle(debugStatusReader);
}
public static Configuration ConfigureDefaults()
{
return new Configuration();
}
}
}