forked from CoreWCF/CoreWCF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.targets
94 lines (84 loc) · 5.86 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?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)"
DependsOnTargets="ReadAndSplitResourceFile;FilterAndCreateResourceSourceLines"
BeforeTargets="BeforeCompile">
<PropertyGroup>
<!-- MSBuild trims item group strings so need to use a property value to get the source code indented -->
<_Indent>%20%20%20%20</_Indent>
</PropertyGroup>
<ItemGroup>
<SRCodePreamble Include="// Do not edit this file manually it is auto-generated during the build based on the .resx file for this project." />
<SRCodePreamble Include="namespace CoreWCF" />
<SRCodePreamble Include="{" />
<SRCodePreamble Include="$(_Indent)internal static partial class SR" />
<SRCodePreamble Include="$(_Indent){" />
<SRCodePreamble Include="#pragma warning disable 0414" />
<SRCodePreamble Include="$(_Indent)private const string s_resourceName = "FxResources.$(AssemblyName).SR"%3b" />
<SRCodePreamble Include="#pragma warning restore 0414" />
<SRCodePreamble Include="$(_Indent)" />
</ItemGroup>
<ItemGroup>
<SRCodePostamble Include="$(_Indent)$(_Indent)internal static System.Type ResourceType => typeof(FxResources.$(AssemblyName).SR)%3b" />
<SRCodePostamble Include="$(_Indent)}" />
<SRCodePostamble Include="}" />
<SRCodePostamble Include="$(_Indent)" />
<SRCodePostamble Include="namespace FxResources.$(AssemblyName)" />
<SRCodePostamble Include="{" />
<SRCodePostamble Include="$(_Indent)internal static class SR { }" />
<SRCodePostamble Include="}" />
</ItemGroup>
<Message Text="Writing resources source file $(IntermediateResOutputFileFullPath)" Importance="high"/>
<WriteLinesToFile File="$(IntermediateResOutputFileFullPath)" Lines="@(SRCodePreamble)" Overwrite="true" />
<WriteLinesToFile File="$(IntermediateResOutputFileFullPath)" Lines="@(ResourceItemSourceLine)" Overwrite="false" />
<WriteLinesToFile File="$(IntermediateResOutputFileFullPath)" Lines="@(SRCodePostamble)" Overwrite="false" />
<ItemGroup>
<Compile Include="$(IntermediateResOutputFileFullPath)" />
</ItemGroup>
</Target>
<Target Name="FilterAndCreateResourceSourceLines" Inputs="@ResourceStringItems" Outputs="%(ResourceStringItems.Identity)" DependsOnTargets="ReadAndSplitResourceFile">
<!-- Check if line from resource file contains a string resource -->
<PropertyGroup>
<!-- MSBuild doesn't allow property functions to be used for ItemGroup conditions so need to evaluate whether the line
contains a resouce string using a property, then use the stored property value in ItemGroup conditions -->
<ResourceStringValueContainsResName>$([System.String]::new('%(ResourceStringItems.Identity)').Contains(`resName`))</ResourceStringValueContainsResName>
<!-- MSBuild trims item group strings so need to use a property value to get the source code indented -->
<_Indent>%20%20%20%20</_Indent>
</PropertyGroup>
<!-- Parse resource line to extract resource name and value -->
<PropertyGroup Condition="'$(ResourceStringValueContainsResName)' == 'True'">
<ResourceName>$([System.Text.RegularExpressions.Regex]::Match($([System.String]::new('%(ResourceStringItems.Identity)')),`(?<=\s*<resName>)(.*?)(?=</resName>)`))</ResourceName>
<ResourceValue>$([System.Text.RegularExpressions.Regex]::Match($([System.String]::new('%(ResourceStringItems.Identity)')),`(?<=\s*<resValue>)(.*?)(?=</resValue>.*)`))</ResourceValue>
<!-- Replace single quotes with double quotes as resources will be emitted to a source file contained inside a string literal: @"..." -->
<ResourceValue>$([System.Text.RegularExpressions.Regex]::Replace($(ResourceValue),`"`,`""`))</ResourceValue>
</PropertyGroup>
<!-- Save parsed resource name and value into ResourceFileContentsClean. This only contains data from actual resource items and excludes items such as resource file headers -->
<ItemGroup Condition="'$(ResourceStringValueContainsResName)' == 'True'">
<ResourceItemSourceLine Include="$(_Indent)$(_Indent)internal static string $(ResourceName) => SR.GetResourceString("$(ResourceName)", @"$(ResourceValue)")%3b" />
<ResourceItemSourceLine Include="$(_Indent)" />
</ItemGroup>
</Target>
<Target Name="ReadAndSplitResourceFile">
<Message Text="Resource File Name = $(StringResourcesPath)" Importance="high" />
<PropertyGroup>
<ResourceFileContents>$([System.IO.File]::ReadAllText($(StringResourcesPath)))</ResourceFileContents>
<!-- Remove comment block as it contains some example resource items which would otherwise be picked up by a later regex -->
<ResourceFileContents>$([System.Text.RegularExpressions.Regex]::Replace($(ResourceFileContents), `<!--.*-->`, ``, System.Text.RegularExpressions.RegexOptions.Singleline))</ResourceFileContents>
<!-- Rearrange resources to be on single lines. E.g.
<data name = "SampleResourceString" xml:space="preserve">
<value>This is a resource string</value>
</data>
Becomes
<resName>SampleResourceString</resName><resValue>This is a resource string</resValue>
-->
<ResourceRegexExpression><data\s+name\s*=\s*"(?<name>.*?)"\s*(?:xml:space\s*=\s*"\w+")?>\s*<value>(?<value>.*?)</value>\s*</data></ResourceRegexExpression>
<ResourceFileContents>$([System.Text.RegularExpressions.Regex]::Replace($(ResourceFileContents), `$(ResourceRegexExpression)`, `<resName>$1</resName><resValue>$2</resValue>`))</ResourceFileContents>
</PropertyGroup>
<!-- Split each resource item to be it's own item in the item group -->
<ItemGroup>
<ResourceStringItems Include="$(ResourceFileContents.Split('%0d'))"/>
</ItemGroup>
</Target>
</Project>