forked from CoreWCF/CoreWCF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.targets
65 lines (60 loc) · 2.84 KB
/
resources.targets
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
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="GenerateResourcesSource"
Inputs="$(StringResourcesPath)"
Outputs="$(IntermediateResOutputFileFullPath)"
BeforeTargets="BeforeCompile">
<GenerateStringResources ResxFilePath="$(StringResourcesPath)" GeneratedSourceOutputPath="$(IntermediateResOutputFileFullPath)" AssemblyName="$(AssemblyName)" />
<ItemGroup>
<Compile Include="$(IntermediateResOutputFileFullPath)" />
</ItemGroup>
</Target>
<UsingTask
TaskName="GenerateStringResources"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<ResxFilePath Required = "true" />
<GeneratedSourceOutputPath Required = "true" />
<AssemblyName Required = "true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Xml.Linq"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
Log.LogMessage(MessageImportance.High, $"Reading resource file {ResxFilePath}");
XElement resourceFile = XElement.Load(ResxFilePath);
var resStrings = from res in resourceFile.Elements("data") select new { Key = res.Attribute("name").Value, Value = res.Element("value").Value.Replace("\"","\"\"") };
Log.LogMessage(MessageImportance.High, $"Writing resources source file {GeneratedSourceOutputPath}");
using(var writer = File.CreateText(GeneratedSourceOutputPath))
{
string indent = " ";
writer.WriteLine("// Do not edit this file manually it is auto-generated during the build based on the .resx file for this project.");
writer.WriteLine("namespace CoreWCF");
writer.WriteLine("{");
writer.WriteLine(indent + "internal static partial class SR");
writer.WriteLine(indent + "{");
writer.WriteLine("#pragma warning disable 0414");
writer.WriteLine(indent + $"private const string s_resourceName = \"FxResources.{AssemblyName}.SR\";");
writer.WriteLine("#pragma warning restore 0414");
writer.WriteLine();
foreach(var item in resStrings)
{
writer.WriteLine(indent + indent + $"internal static string {item.Key} => SR.GetResourceString(\"{item.Key}\", @\"{item.Value}\");");
}
writer.WriteLine(indent + indent + $"internal static System.Type ResourceType => typeof(FxResources.{AssemblyName}.SR);");
writer.WriteLine(indent + "}");
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine($"namespace FxResources.{AssemblyName}");
writer.WriteLine("{");
writer.WriteLine(indent + "internal static class SR { }");
writer.WriteLine("}");
}
]]>
</Code>
</Task>
</UsingTask>
</Project>