forked from killnine/MetroChart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XAMLReplace.targets
163 lines (145 loc) · 6.82 KB
/
XAMLReplace.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
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
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- For WPF projects -->
<MarkupCompilePass1DependsOn>
$(MarkupCompilePass1DependsOn)
PreprocessXaml;
</MarkupCompilePass1DependsOn>
<!-- For Silverlight projects -->
<CompileXamlDependsOn>
$(CompileXamlDependsOn)
PreprocessXaml;
</CompileXamlDependsOn>
</PropertyGroup>
<Target Name="PreprocessXaml">
<CreateItem Include="@(Page)" Condition="'%(Page.Link)'!=''">
<Output TaskParameter="Include" ItemName="LinkedPages" />
</CreateItem>
<CreateItem Include="@(Page)" Condition="'%(Page.Link)'==''">
<Output TaskParameter="Include" ItemName="UnlinkedPages" />
</CreateItem>
<Copy SourceFiles="@(LinkedPages->'%(FullPath)')" DestinationFiles="@(LinkedPages->'%(Link)')" />
<ItemGroup>
<FinalLinkedPages Include="@(LinkedPages->'%(Link)')" />
</ItemGroup>
<ItemGroup>
<Page Remove="@(Page)" />
<Page Include="@(FinalLinkedPages);@(UnlinkedPages)" />
</ItemGroup>
<AdjustXAML DefineConstants="$(DefineConstants)" Folder="$(ProjectDir)" />
</Target>
<UsingTask TaskName="ReplaceGenericXAML" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<SolutionFolder ParameterType="System.String" Required="true" />
<ProjectFolder ParameterType="System.String" Required="true" />
<DefineConstants ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
Log.LogMessage("ProjectFolder " + ProjectFolder, MessageImportance.High);
string usingString = "=\"using:";
string clrString = "=\"clr-namespace:";
//replace generic.xaml with the major file
string mainGenericXAML = SolutionFolder + @"\SPCAF.Client.Core\Themes\Generic.xaml";
if(ProjectFolder.Contains("De.TorstenMandelkow.MetroChart"))
{
mainGenericXAML = SolutionFolder + @"\De.TorstenMandelkow.MetroChart\Themes\Generic.xaml";
}
else if(ProjectFolder.Contains("TestApplication"))
{
mainGenericXAML = SolutionFolder + @"\TestApplication.Shared\Themes\Generic.xaml";
}
Log.LogMessage("mainGenericXAML " + mainGenericXAML, MessageImportance.High);
string mainGenericContent = System.IO.File.ReadAllText(mainGenericXAML);
Log.LogMessage("Loaded content of main generic.xaml file " + mainGenericXAML, MessageImportance.High);
if(DefineConstants.Contains("NETFX_CORE"))
{
StringBuilder replacedLines = new StringBuilder();
string[] allLines = System.IO.File.ReadAllLines(mainGenericXAML);
foreach (string line in allLines)
{
string replacedLine = line;
replacedLine = replacedLine.Replace(clrString, usingString);
replacedLine = replacedLine.Replace("{DynamicResource ", "{StaticResource ");
if(replacedLine.Contains(";assembly="))
{ //remove 'assembly=..." from the line
int startOfAssembly = replacedLine.IndexOf(";assembly=");
int endOfAssembly = replacedLine.IndexOf("\"", startOfAssembly);
replacedLine = replacedLine.Remove(startOfAssembly, endOfAssembly - startOfAssembly);
}
replacedLine = replacedLine.Replace("xmlns:cal=\"http://www.caliburnproject.org\"", "xmlns:cal=\"clr-namespace:Caliburn.Micro\"");
replacedLines.AppendLine(replacedLine);
}
mainGenericContent = replacedLines.ToString();
}
if(!DefineConstants.Contains("SILVERLIGHT"))
{
mainGenericContent = mainGenericContent.Replace("/TestApplicationSL;component","");
}
string genericXAMLForReplace = ProjectFolder + @"\Themes\Generic.xaml";
Log.LogMessage("Replaced content of " + genericXAMLForReplace, MessageImportance.High);
System.IO.File.WriteAllText(genericXAMLForReplace, mainGenericContent);
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="AdjustXAML" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<Folder ParameterType="System.String" Required="true" />
<DefineConstants ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
string usingString = "=\"using:";
string clrString = "=\"clr-namespace:";
foreach(FileInfo xamlFile in new DirectoryInfo(Folder).GetFiles("*.xaml", SearchOption.AllDirectories))
{
Log.LogMessage("Replacing in File: " + xamlFile.FullName, MessageImportance.High);
string content = System.IO.File.ReadAllText(xamlFile.FullName);
if(DefineConstants.Contains("NETFX_CORE"))
{
StringBuilder replacedLines = new StringBuilder();
string[] allLines = System.IO.File.ReadAllLines(xamlFile.FullName);
foreach (string line in allLines)
{
string replacedLine = line;
replacedLine = replacedLine.Replace(clrString, usingString);
replacedLine = replacedLine.Replace("{DynamicResource ", "{StaticResource ");
if(replacedLine.Contains(";assembly="))
{ //remove 'assembly=..." from the line
int startOfAssembly = replacedLine.IndexOf(";assembly=");
int endOfAssembly = replacedLine.IndexOf("\"", startOfAssembly);
replacedLine = replacedLine.Remove(startOfAssembly, endOfAssembly - startOfAssembly);
}
replacedLine = replacedLine.Replace("xmlns:cal=\"http://www.caliburnproject.org\"", "xmlns:cal=\"clr-namespace:Caliburn.Micro\"");
replacedLines.AppendLine(replacedLine);
}
//content = content.Replace("xmlns:cal=\"http://www.caliburnproject.org\"", "xmlns:cal=\"clr-namespace:Caliburn.Micro\"");
//content = content.Replace(clrString, usingString);
System.IO.File.WriteAllText(xamlFile.FullName, replacedLines.ToString());
Log.LogMessage("Writing to " + xamlFile.FullName, MessageImportance.High);
}
else
{
if(content.Contains(usingString))
{
content = content.Replace(usingString, clrString);
System.IO.File.WriteAllText(xamlFile.FullName, content);
Log.LogMessage("Writing to " + xamlFile.FullName, MessageImportance.High);
}
}
}
</Code>
</Task>
</UsingTask>
<Target Name="BeforeBuild">
<Message Text="BeforeBuild" Importance="high" />
<ReplaceGenericXAML SolutionFolder="$(SolutionDir)" ProjectFolder="$(ProjectDir)" DefineConstants="$(DefineConstants)" />
</Target>
<!--
<Target Name="BeforeCompile">
<AdjustXAML DefineConstants="$(DefineConstants)" Folder="$(IntermediateOutputPath)" />
</Target>
-->
</Project>