forked from OData/RESTier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRESTier.tasks.targets
221 lines (199 loc) · 9.47 KB
/
RESTier.tasks.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<Project ToolsVersion="4.0" DefaultTargets="RunTests" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- This task does not support concurrent execution from multiple project files. Its use is only appropriate when
NuGet.exe is not in use. -->
<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<OutputFileName ParameterType="System.String" Required="true" />
<MinimumVersion ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.Diagnostics" />
<Using Namespace="System.IO" />
<Using Namespace="System.Net" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Version minimumRequiredVersion;
if (!Version.TryParse(MinimumVersion, out minimumRequiredVersion))
{
Log.LogError("MinimumVersion '{0}' is not a valid Version.", MinimumVersion);
}
try
{
OutputFileName = Path.GetFullPath(OutputFileName);
if (File.Exists(OutputFileName))
{
// If NuGet.exe exists but is less than the minimum required version, delete it so that the
// latest version will be downloaded.
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(OutputFileName);
string toParse;
if (versionInfo != null && versionInfo.ProductVersion != null)
{
toParse = versionInfo.ProductVersion;
}
else
{
toParse = null;
}
Version current;
Version parsed;
if (toParse != null && Version.TryParse(toParse, out parsed))
{
current = parsed;
}
else
{
// Treat a missing or invalid version like V0.0 (which will trigger a delete and download).
current = new Version(0, 0);
}
if (current < minimumRequiredVersion)
{
File.Delete(OutputFileName);
}
}
if (!File.Exists(OutputFileName))
{
Log.LogMessage("Downloading latest version of NuGet.exe...");
WebClient webClient = new WebClient();
webClient.DownloadFile("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", OutputFileName);
}
return true;
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="RegexReplace" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<Find ParameterType="System.String" Required="true" />
<Replace ParameterType="System.String" Required="true" />
<WarnOnNoMatch ParameterType="System.Boolean" Required="false" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try
{
Regex regex = new Regex(Find, RegexOptions.Multiline | RegexOptions.Compiled);
foreach (ITaskItem file in Files)
{
string fullPath = Path.GetFullPath(file.ItemSpec);
string originalText = File.ReadAllText(fullPath);
bool matched = regex.IsMatch(originalText);
if (!matched)
{
if (WarnOnNoMatch)
{
Log.LogWarning("No matches for '{0}' in '{1}'.", Find, fullPath);
}
}
else
{
File.SetAttributes(fullPath, File.GetAttributes(fullPath) & ~FileAttributes.ReadOnly);
File.WriteAllText(fullPath, regex.Replace(originalText, Replace), Encoding.UTF8);
}
}
return true;
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="PrintTestRunSummary" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<TestResultsDirectory ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Using Namespace="System" />
<Using Namespace="System.Collections.Generic" />
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Using Namespace="System.Xml.Linq" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try
{
string[] testResultFiles = Directory.GetFiles(TestResultsDirectory);
int testsPassed = 0;
int testsFailed = 0;
int testsSkipped = 0;
decimal timeSpent = 0;
List<string> testFailures = new List<string>();
foreach (string testResultFile in testResultFiles)
{
XElement xml;
using (FileStream fileStream = File.OpenRead(testResultFile))
{
xml = XElement.Load(fileStream);
}
var assemblies = xml.Elements(XName.Get("assembly"));
foreach (XElement assembly in assemblies)
{
int failures = Int32.Parse(assembly.Attribute(XName.Get("failed")).Value);
testsPassed += Int32.Parse(assembly.Attribute(XName.Get("passed")).Value);
testsFailed += failures;
testsSkipped += Int32.Parse(assembly.Attribute(XName.Get("skipped")).Value);
timeSpent += Decimal.Parse(assembly.Attribute(XName.Get("time")).Value);
if (failures > 0)
{
foreach (XElement classWithFailure in assembly.Elements(XName.Get("class"))
.Where(c => Int32.Parse(c.Attribute(XName.Get("failed")).Value) > 0))
{
foreach (XElement failure in classWithFailure.Elements(XName.Get("test"))
.Where(test => test.Attribute(XName.Get("result")).Value == "Fail"))
{
testFailures.Add(failure.Attribute("name").Value);
}
}
}
}
}
if (testFailures.Count > 0)
{
Console.WriteLine();
Console.WriteLine(" Test Failures:");
ConsoleColor originalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
foreach (string testFailure in testFailures)
{
Console.WriteLine(" " + testFailure);
}
Console.ForegroundColor = originalColor;
}
Console.WriteLine();
Console.WriteLine(" Tests passed: {0}, Tests failed: {1}, Tests skipped: {2}", testsPassed, testsFailed, testsSkipped);
Console.WriteLine(" Time spent running tests: {0} seconds", timeSpent);
return true;
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>