-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResources.tt
241 lines (214 loc) · 7.38 KB
/
Resources.tt
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Windows.Forms" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Resources" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ output extension=".cs" #>
<#
var parameterMatcher = new Regex(@"\{(\d)\}");
var lines = new List<Tuple<string, string, string, bool, string, string>>();
using (var resxReader = new ResXResourceReader(Path.ChangeExtension(Host.TemplateFile, "resx")))
{
resxReader.UseResXDataNodes = true;
foreach (DictionaryEntry entry in resxReader)
{
var node = (ResXDataNode)entry.Value;
var value = (string)node.GetValue((System.ComponentModel.Design.ITypeResolutionService)null);
var matchedArgs
= parameterMatcher.Matches(value)
.Cast<Match>()
.Select(m => Convert.ToInt32(m.Groups[1].Value))
.ToArray();
var argGenerator
= new object[matchedArgs.Any() ? matchedArgs.Max() + 1 : 0];
lines.Add(Tuple.Create<string, string, string, bool, string, string>(
node.Name,
value,
node.Comment.StartsWith("## ExceptionType=") ? node.Comment.Substring(17) : null,
argGenerator.Any(),
string.Join(", ", argGenerator.Select((_, i) => "p" + i)),
"(" + string.Join(", ", argGenerator.Select((_, i) => "object p" + i)) + ")"
));
}
}
string outputNamespace = Host.ResolveParameterValue("directiveId", "namespaceDirectiveProcessor", "namespaceHint") ?? string.Empty;
#>
// <auto-generated />
namespace <#= outputNamespace #>.Resources
{
using System.CodeDom.Compiler;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.Threading;
// <summary>
// Strongly-typed and parameterized string resources.
// </summary>
[GeneratedCode("<#= Path.GetFileName(Host.TemplateFile) #>", "1.0.0.0")]
internal static class Strings
{<#
foreach (var line in lines)
{
#>
// <summary>
// A string like "<#= line.Item2 #>"
// </summary>
internal static string <#= line.Item1 #><#= line.Item4 ? line.Item6 : string.Empty #>
{
<#
if (!line.Item4)
{
#>get { return EntityRes.GetString(EntityRes.<#= line.Item1 #>); }
<#
}
else
{
#>return EntityRes.GetString(EntityRes.<#= line.Item1 #>, <#= line.Item5 #>);
<#
}#>
}
<#
}#>
}
// <summary>
// Strongly-typed and parameterized exception factory.
// </summary>
[GeneratedCode("<#= Path.GetFileName(Host.TemplateFile) #>", "1.0.0.0")]
internal static class Error
{<#
foreach (var line in lines.Where(l => l.Item3 != null))
{
#>
// <summary>
// <#= line.Item3 #> with message like "<#= line.Item2 #>"
// </summary>
internal static Exception <#= line.Item1 #><#= line.Item4 ? line.Item6 : "()" #>
{
return new <#= line.Item3 #>(Strings.<#= line.Item1 #><#= line.Item4 ? "(" + line.Item5 + ")" : string.Empty #>);
}
<#
}#>
// <summary>
// The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.
// </summary>
internal static Exception ArgumentOutOfRange(string paramName)
{
return new ArgumentOutOfRangeException(paramName);
}
// <summary>
// The exception that is thrown when the author has yet to implement the logic at this point in the program. This can act as an exception based TODO tag.
// </summary>
internal static Exception NotImplemented()
{
return new NotImplementedException();
}
// <summary>
// The exception that is thrown when an invoked method is not supported, or when there is an attempt to
// read, seek, or write to a stream that does not support the invoked functionality.
// </summary>
internal static Exception NotSupported()
{
return new NotSupportedException();
}
}
// <summary>
// AutoGenerated resource class. Usage:
// string s = EntityRes.GetString(EntityRes.MyIdenfitier);
// </summary>
[GeneratedCode("<#= Path.GetFileName(Host.TemplateFile) #>", "1.0.0.0")]
internal sealed class EntityRes
{
<#
foreach (var line in lines)
{
#> internal const string <#= line.Item1 #> = "<#= line.Item1 #>";
<#
}
#>
private static EntityRes loader;
private readonly ResourceManager resources;
private EntityRes()
{
resources = new ResourceManager(
"System.Data.Entity.Properties.<#= Path.GetFileNameWithoutExtension(Host.TemplateFile) #>",
#if NET40
typeof(System.Data.Entity.DbContext).Assembly);
#else
typeof(System.Data.Entity.DbContext).GetTypeInfo().Assembly);
#endif
}
private static EntityRes GetLoader()
{
if (loader == null)
{
var sr = new EntityRes();
Interlocked.CompareExchange(ref loader, sr, null);
}
return loader;
}
private static CultureInfo Culture
{
get { return null /*use ResourceManager default, CultureInfo.CurrentUICulture*/; }
}
public static ResourceManager Resources
{
get { return GetLoader().resources; }
}
public static string GetString(string name, params object[] args)
{
var sys = GetLoader();
if (sys == null)
{
return null;
}
var res = sys.resources.GetString(name, Culture);
if (args != null
&& args.Length > 0)
{
for (var i = 0; i < args.Length; i ++)
{
var value = args[i] as String;
if (value != null
&& value.Length > 1024)
{
args[i] = value.Substring(0, 1024 - 3) + "...";
}
}
return String.Format(CultureInfo.CurrentCulture, res, args);
}
else
{
return res;
}
}
public static string GetString(string name)
{
var sys = GetLoader();
if (sys == null)
{
return null;
}
return sys.resources.GetString(name, Culture);
}
public static string GetString(string name, out bool usedFallback)
{
// always false for this version of gensr
usedFallback = false;
return GetString(name);
}
public static object GetObject(string name)
{
var sys = GetLoader();
if (sys == null)
{
return null;
}
return sys.resources.GetObject(name, Culture);
}
}
}