forked from CoreWCF/CoreWCF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.targets
78 lines (75 loc) · 4.37 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
66
67
68
69
70
71
72
73
74
75
76
77
78
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="GenerateResourcesSource"
Inputs="$(StringResourcesPath);$(CommonStringResourcesPath)"
Outputs="$(IntermediateResOutputFileFullPath);$(IntermediateCommonResOutputFileFullPath)"
BeforeTargets="BeforeCompile">
<GenerateStringResources ResxFilePath="$(StringResourcesPath)" GeneratedSourceOutputPath="$(IntermediateResOutputFileFullPath)" AssemblyName="$(AssemblyName)" ClassName="SR"/>
<GenerateStringResources Condition="'$(CommonStringResourcesPath)' != ''" ResxFilePath="$(CommonStringResourcesPath)" GeneratedSourceOutputPath="$(IntermediateCommonResOutputFileFullPath)" AssemblyName="$(AssemblyName)" ClassName="SRCommon"/>
<ItemGroup>
<Compile Include="$(IntermediateResOutputFileFullPath)" />
<Compile Condition="'$(CommonStringResourcesPath)' != ''" Include="$(IntermediateCommonResOutputFileFullPath)" />
</ItemGroup>
</Target>
<UsingTask
TaskName="GenerateStringResources"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<ResxFilePath Required = "true" />
<GeneratedSourceOutputPath Required = "true" />
<AssemblyName Required = "true" />
<ClassName 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("using System.Resources;");
writer.WriteLine("using System.Runtime.CompilerServices;");
writer.WriteLine();
writer.WriteLine("namespace CoreWCF");
writer.WriteLine("{");
writer.WriteLine(indent + $"internal static partial class {ClassName}");
writer.WriteLine(indent + "{");
writer.WriteLine(@"
private static ResourceManager s_resourceManager;
internal static ResourceManager ResourceManager => s_resourceManager ??= new ResourceManager(ResourceType);
internal static string GetResourceString(string resourceKey) => ResourceManager.GetString(resourceKey);
internal static string Format(string resourceFormat, params object[] args) => args == null ? resourceFormat : string.Format(resourceFormat, args);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static string Format(string resourceFormat) => resourceFormat;
internal static string Format(string resourceFormat, object p1) => string.Format(resourceFormat, p1);
internal static string Format(string resourceFormat, object p1, object p2) => string.Format(resourceFormat, p1, p2);
internal static string Format(string resourceFormat, object p1, object p2, object p3) => string.Format(resourceFormat, p1, p2, p3);");
writer.WriteLine();
Log.LogMessage(MessageImportance.High, $"Writing {resStrings.Count()} resource strings");
foreach(var item in resStrings)
{
writer.WriteLine(indent + indent + $"/// <summary>{item.Value}</summary>");
writer.WriteLine(indent + indent + $"internal static string {item.Key} => GetResourceString(\"{item.Key}\");");
}
writer.WriteLine(indent + indent + $"internal static System.Type ResourceType => typeof(FxResources.{AssemblyName}.{ClassName});");
writer.WriteLine(indent + "}");
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine($"namespace FxResources.{AssemblyName}");
writer.WriteLine("{");
writer.WriteLine(indent + $"internal static class {ClassName} {{ }}");
writer.WriteLine("}");
}
]]>
</Code>
</Task>
</UsingTask>
</Project>