forked from microsoft/CsWinRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strings.props
204 lines (195 loc) · 7.95 KB
/
Strings.props
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
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PrepareForBuildDependsOn>
UpdateStringSources;UpdateStringsHeader;$(PrepareForBuildDependsOn)
</PrepareForBuildDependsOn>
<ComputeCompileInputsTargets>
AddStringSources;$(ComputeCompileInputsTargets)
</ComputeCompileInputsTargets>
</PropertyGroup>
<UsingTask TaskName="WriteStringSources"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Input ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<Namespace Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
foreach(ITaskItem item in Input)
{
var content = item.GetMetadata("Content");
var source = item.GetMetadata("Source");
var ns = item.GetMetadata("Namespace");
var file = item.GetMetadata("File");
var global = file.Replace('.', '_');
if (!string.IsNullOrEmpty(ns))
{
global = ns.Replace('.', '_') + "_" + global;
}
Log.LogMessage(MessageImportance.High, "Writing {0}", source);
Directory.CreateDirectory(new FileInfo(source).DirectoryName);
File.WriteAllLines(source, GetContentLines(content));
IEnumerable<string> GetContentLines(string file)
{
yield return "// This file was generated by the build.";
yield return "#include \"pch.h\"";
yield return "namespace " + Namespace + "::strings {";
yield return "extern char const " + global + "[] = R\"xyz(";
int count = 0;
foreach(string line in File.ReadLines(file))
{
count += line.Length + 2;
if(count > 16000)
{
yield return ")xyz\" R\"xyz(" + line;
count = line.Length;
}
else
{
yield return line;
}
}
yield return ")xyz\";";
yield return "}";
}
}
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="WriteStringsHeader"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Input ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<Header Required="true" />
<Namespace Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
var lines = new List<string>();
lines.Add("// " + Namespace + " static string declarations. This file was generated by the build.");
lines.Add("#pragma once");
lines.Add("namespace " + Namespace + "::strings");
lines.Add("{");
foreach(var index in Input.GroupBy(item => item.GetMetadata("Index")))
{
foreach(ITaskItem item in index)
{
var content = item.GetMetadata("Content");
var ns = item.GetMetadata("Namespace");
var file = item.GetMetadata("File");
var global = file.Replace('.', '_');
if (!string.IsNullOrEmpty(ns))
{
global = ns.Replace('.', '_') + "_" + global;
}
var fileSize = new FileInfo(content).Length;
lines.Add(" extern char const " + global + "[" + fileSize + "];");
}
lines.Add(" static const struct");
lines.Add(" {");
lines.Add(" char const* name;");
lines.Add(" char const* value;");
lines.Add(" } " + (index.Key == "" ? "base" : index.Key.Trim('\\')) +"[] = {");
var delimeter = "";
foreach(ITaskItem item in index)
{
var ns = item.GetMetadata("Namespace");
var file = item.GetMetadata("File");
var key = ns;
if (string.IsNullOrEmpty(key))
{
key = file;
}
var global = file.Replace('.', '_');
if (!string.IsNullOrEmpty(ns))
{
global = ns.Replace('.', '_') + "_" + global;
}
lines.Add(" " + delimeter + "{\"" + key + "\", " + global + "}");
delimeter = ", ";
}
lines.Add(" };");
}
lines.Add("}");
if (File.Exists(Header))
{
var existing = File.ReadAllLines(Header);
if(existing.SequenceEqual(lines))
{
Log.LogMessage(MessageImportance.Low, "{0} already up-to-date", Header);
return true;
}
}
Log.LogMessage(MessageImportance.High, "Writing {0}", Header);
Directory.CreateDirectory(new FileInfo(Header).DirectoryName);
File.WriteAllLines(Header, lines);
]]>
</Code>
</Task>
</UsingTask>
<ItemGroup>
<StaticStringSpec Include="$(SolutionDir)\strings\**\*.*;$(MSBuildProjectDirectory)\strings\**\*.*" />
<StaticStringDirs Include="@(StaticStringSpec->'%(RootDir)%(Directory)'->Distinct())" />
<StaticStrings Include="@(StaticStringSpec->'%(FullPath)'->Distinct())">
<Content>%(Identity)</Content>
<Source>$([System.String]::new('$(IntDir)strings\%(RecursiveDir)%(Filename).cpp'))</Source>
<File>$([System.String]::new('%(Filename)'))</File>
</StaticStrings>
</ItemGroup>
<Target Name="SetStaticStringIndex">
<ItemGroup>
<StaticStrings Condition="'%(RecursiveDir)' != ''">
<Path>%(RecursiveDir)</Path>
<Index>$([System.IO.Path]::GetDirectoryName($([System.IO.Path]::GetDirectoryName($([System.String]::new('%(RecursiveDir)'))))))</Index>
<Namespace>$([System.IO.Path]::GetFileName($([System.IO.Path]::GetDirectoryName($([System.String]::new('%(RecursiveDir)'))))))</Namespace>
</StaticStrings>
</ItemGroup>
</Target>
<Target Name="AddStringSources">
<ItemGroup>
<ClCompile Include="@(StaticStrings->'%(Source)')" />
</ItemGroup>
</Target>
<Target Name="UpdateStringSources" BeforeTargets="Build;PrepareForBuild;ClCompile"
DependsOnTargets="SetStaticStringIndex"
Condition="@(StaticStrings->Count()) >= 1"
Inputs="@(StaticStrings)"
Outputs="%(Source)">
<WriteLinesToFile Encoding="Unicode"
File="$(TLogLocation)strings.read.1.tlog"
Lines="@(StaticStrings->'^%(Source)');@(StaticStrings->'%(Identity)')"/>
<WriteLinesToFile Encoding="Unicode"
File="$(TLogLocation)strings.write.1.tlog"
Lines="@(StaticStrings->'^%(Identity)');@(StaticStrings->'%(Source)')"/>
<Message Text="Static strings:
@(StaticStrings->'%(File): %(Content), %(Source)','
')" Importance="High"/>
<WriteStringSources Input="@(StaticStrings)" Namespace="$(RootNamespace)"/>
</Target>
<Target Name="UpdateStringsHeader" BeforeTargets="Build;PrepareForBuild;ClCompile"
DependsOnTargets="SetStaticStringIndex"
Condition="@(StaticStrings->Count()) >= 1"
Inputs="@(StaticStringDirs)"
Outputs="$(IntDir)strings.h">
<PropertyGroup>
<StaticStringDirList>@(StaticStringDirs,'|')</StaticStringDirList>
</PropertyGroup>
<WriteLinesToFile Encoding="Unicode"
File="$(TLogLocation)strings.read.1.tlog"
Lines="^$(IntDir)strings.h;@(StaticStringDirs)"/>
<WriteLinesToFile Encoding="Unicode"
File="$(TLogLocation)strings.write.1.tlog"
Lines="^$(StaticStringDirList);$(IntDir)strings.h"/>
<WriteStringsHeader Input="@(StaticStrings)" Header="$(IntDir)strings.h" Namespace="$(RootNamespace)"/>
<ItemGroup>
<ClCompile>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(IntDir)</AdditionalIncludeDirectories>
</ClCompile>
</ItemGroup>
</Target>
</Project>