-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapper.cs
66 lines (52 loc) · 1.87 KB
/
Mapper.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
using System.IO;
using System.Web.Bem.Configuration;
using System.Web.Hosting;
using System.Web.Mvc;
namespace System.Web.Bem.BundleMappers
{
public abstract class Mapper
{
#region static members
public static Mapper Create(BemConfiguration config)
{
string name = config.Get(cfg => cfg.Mapper, BemConfiguration.Defaults.MAPPER);
Mapper mapper;
switch (name)
{
case "Single":
mapper = new Single();
break;
case "ByController":
mapper = new ByController();
break;
default:
var type = Type.GetType(name, true);
mapper = (Mapper)Activator.CreateInstance(type);
break;
}
mapper.Init(config);
return mapper;
}
#endregion
#region instance members
protected string DefaultBundle { get; set; }
protected string RootDir { get; set; }
public virtual void Init(BemConfiguration config)
{
var rootDir = config.Get(cfg => cfg.RootDir, BemConfiguration.Defaults.ROOT_DIR);
RootDir = Path.IsPathRooted(rootDir) ? rootDir : HostingEnvironment.MapPath(rootDir);
DefaultBundle = config.Get(cfg => cfg.DefaultBundle, BemConfiguration.Defaults.DEFAULT_BUNDLE);
}
public virtual string Map(ControllerContext context)
{
var bundleName = GetBundleName(context);
return GetBundlePath(bundleName);
}
protected virtual string GetBundlePath(string bundleName)
{
return Path.Combine(RootDir, bundleName, bundleName + ".bemhtml.js");
}
protected abstract string GetBundleName(ControllerContext context);
#endregion
}
}