-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuildserver-continuaci.cake
182 lines (138 loc) · 5.75 KB
/
buildserver-continuaci.cake
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
public class ContinuaCIBuildServer : BuildServerBase
{
public ContinuaCIBuildServer(ICakeContext cakeContext)
: base(cakeContext)
{
}
//-------------------------------------------------------------
public override async Task OnTestFailedAsync()
{
await ImportUnitTestsAsync();
}
//-------------------------------------------------------------
public override async Task AfterTestAsync()
{
await ImportUnitTestsAsync();
}
//-------------------------------------------------------------
private async Task ImportUnitTestsAsync()
{
foreach (var project in BuildContext.Tests.Items)
{
await ImportTestFilesAsync(project);
}
}
//-------------------------------------------------------------
private async Task ImportTestFilesAsync(string projectName)
{
var continuaCIContext = GetContinuaCIContext();
if (!continuaCIContext.IsRunningOnContinuaCI)
{
return;
}
CakeContext.Warning($"Importing test results for '{projectName}'");
var testResultsDirectory = System.IO.Path.Combine(BuildContext.General.OutputRootDirectory, "testresults");
if (!CakeContext.DirectoryExists(testResultsDirectory))
{
CakeContext.Warning("No test results directory");
return;
}
var type = string.Empty;
var importType = string.Empty;
if (IsNUnitTestProject(BuildContext, projectName))
{
type = "nunit";
importType = "nunit";
}
if (IsXUnitTestProject(BuildContext, projectName))
{
type = "xunit";
importType = "mstest"; // Xml type is different
}
if (string.IsNullOrWhiteSpace(type))
{
CakeContext.Warning("Could not find test project type");
return;
}
CakeContext.Warning($"Determined project type '{type}'");
var cakeFilePattern = System.IO.Path.Combine(testResultsDirectory, projectName, "*.xml");
CakeContext.Warning($"Using pattern '{cakeFilePattern}'");
var testResultsFiles = CakeContext.GetFiles(cakeFilePattern);
if (!testResultsFiles.Any())
{
CakeContext.Warning($"No test result file found using '{cakeFilePattern}'");
return;
}
var continuaCiFilePattern = System.IO.Path.Combine(testResultsDirectory, "**.xml");
CakeContext.Information($"Importing test results from using '{continuaCiFilePattern}' using import type '{importType}'");
var message = $"@@continua[importUnitTestResults type='{importType}' filePatterns='{cakeFilePattern}']";
WriteIntegration(message);
}
//-------------------------------------------------------------
public override async Task PinBuildAsync(string comment)
{
var continuaCIContext = GetContinuaCIContext();
if (!continuaCIContext.IsRunningOnContinuaCI)
{
return;
}
CakeContext.Information("Pinning build in Continua CI");
var message = string.Format("@@continua[pinBuild comment='{0}' appendComment='{1}']",
comment, !string.IsNullOrWhiteSpace(comment));
WriteIntegration(message);
}
//-------------------------------------------------------------
public override async Task SetVersionAsync(string version)
{
var continuaCIContext = GetContinuaCIContext();
if (!continuaCIContext.IsRunningOnContinuaCI)
{
return;
}
CakeContext.Information("Setting version '{0}' in Continua CI", version);
var message = string.Format("@@continua[setBuildVersion value='{0}']", version);
WriteIntegration(message);
}
//-------------------------------------------------------------
public override async Task SetVariableAsync(string variableName, string value)
{
var continuaCIContext = GetContinuaCIContext();
if (!continuaCIContext.IsRunningOnContinuaCI)
{
return;
}
CakeContext.Information("Setting variable '{0}' to '{1}' in Continua CI", variableName, value);
var message = string.Format("@@continua[setVariable name='{0}' value='{1}' skipIfNotDefined='true']", variableName, value);
WriteIntegration(message);
}
//-------------------------------------------------------------
public override Tuple<bool, string> GetVariable(string variableName, string defaultValue)
{
var continuaCIContext = GetContinuaCIContext();
if (!continuaCIContext.IsRunningOnContinuaCI)
{
return new Tuple<bool, string>(false, string.Empty);
}
var exists = false;
var value = string.Empty;
var buildServerVariables = continuaCIContext.Environment.Variable;
if (buildServerVariables.ContainsKey(variableName))
{
CakeContext.Information("Variable '{0}' is specified via Continua CI", variableName);
exists = true;
value = buildServerVariables[variableName];
}
return new Tuple<bool, string>(exists, value);
}
//-------------------------------------------------------------
private IContinuaCIProvider GetContinuaCIContext()
{
return CakeContext.ContinuaCI();
}
//-------------------------------------------------------------
private void WriteIntegration(string message)
{
// Must be Console.WriteLine
CakeContext.Information(message);
}
}