forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostContext.cs
More file actions
104 lines (89 loc) · 3.23 KB
/
HostContext.cs
File metadata and controls
104 lines (89 loc) · 3.23 KB
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
98
99
100
101
102
103
104
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.IO;
using System.Xml.Linq;
namespace ServiceStack
{
// Dummy class to satisfy linked files from SS.Razor project
public static class HostContext
{
public static HostConfig Config
{
get
{
return config;
}
}
public static string AppConfigPath
{
get
{
return appConfigPath ?? GetAppConfigPath();
}
set
{
appConfigPath = value;
}
}
private static string GetAppConfigPath()
{
var configPath = ProjectDir + "web.config";
if (File.Exists(configPath))
{
appConfigPath = configPath;
return appConfigPath;
}
configPath = ProjectDir + "Web.config"; //*nix FS FTW!
if (File.Exists(configPath))
{
appConfigPath = configPath;
return appConfigPath;
}
var appHostDll = new FileInfo(ProjectTargetPath).Name;
configPath = ProjectDir + "{0}.config".Fmt(appHostDll);
if (!File.Exists(configPath))
return null;
appConfigPath = configPath;
return appConfigPath;
}
public static string ProjectDir { get; set; }
public static string ProjectTargetPath { get; set; }
private static readonly HostConfig config = new HostConfig();
private static string appConfigPath;
}
// Dummy class to satisfy linked files from SS.Razor project
public class HostConfig
{
public HashSet<string> RazorNamespaces
{
get
{
if (razorNamespaces != null)
return razorNamespaces;
razorNamespaces = new HashSet<string>();
//Infer from <system.web.webPages.razor> - what VS.NET's intell-sense uses
var configPath = HostContext.AppConfigPath;
if (configPath != null)
{
var xml = File.ReadAllText(configPath);
var doc = XElement.Parse(xml);
doc.AnyElement("system.web.webPages.razor")
.AnyElement("pages")
.AnyElement("namespaces")
.AllElements("add").ToList()
.ForEach(x => razorNamespaces.Add(x.AnyAttribute("namespace").Value));
}
//E.g. <add key="servicestack.razor.namespaces" value="System,ServiceStack.Text" />
if (ConfigurationManager.AppSettings[NamespacesAppSettingsKey] != null)
{
var list = new List<string>(ConfigurationManager.AppSettings[NamespacesAppSettingsKey].Split(','));
list.ForEach(x => razorNamespaces.Add(x));
}
return razorNamespaces;
}
}
private HashSet<string> razorNamespaces;
private const string NamespacesAppSettingsKey = "servicestack.razor.namespaces";
}
}