/
tasks.cake
207 lines (159 loc) · 6.86 KB
/
tasks.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
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
#l "generic-tasks.cake"
#l "apps-uwp-tasks.cake"
#l "apps-wpf-tasks.cake"
#l "components-tasks.cake"
#addin "nuget:?package=System.Net.Http&version=4.3.3"
#addin "nuget:?package=Newtonsoft.Json&version=11.0.2"
#addin "nuget:?package=Cake.Sonar&version=1.1.0"
#tool "nuget:?package=MSBuild.SonarQube.Runner.Tool&version=4.3.0"
var Target = GetBuildServerVariable("Target", "Default");
Information("Running target '{0}'", Target);
Information("Using output directory '{0}'", OutputRootDirectory);
//-------------------------------------------------------------
private void BuildTestProjects()
{
foreach (var testProject in TestProjects)
{
Information("Building test project '{0}'", testProject);
var projectFileName = GetProjectFileName(testProject);
var msBuildSettings = new MSBuildSettings
{
Verbosity = Verbosity.Quiet, // Verbosity.Diagnostic
ToolVersion = MSBuildToolVersion.VS2017,
Configuration = ConfigurationName,
MSBuildPlatform = MSBuildPlatform.x86, // Always require x86, see platform for actual target platform
PlatformTarget = PlatformTarget.MSIL
};
// Force disable SonarQube
msBuildSettings.WithProperty("SonarQubeExclude", "true");
// Note: we need to set OverridableOutputPath because we need to be able to respect
// AppendTargetFrameworkToOutputPath which isn't possible for global properties (which
// are properties passed in using the command line)
var outputDirectory = string.Format("{0}/{1}/", OutputRootDirectory, testProject);
Information("Output directory: '{0}'", outputDirectory);
msBuildSettings.WithProperty("OverridableOutputPath", outputDirectory);
msBuildSettings.WithProperty("PackageOutputPath", OutputRootDirectory);
MSBuild(projectFileName, msBuildSettings);
}
}
//-------------------------------------------------------------
Task("UpdateInfo")
.Does(() =>
{
UpdateSolutionAssemblyInfo();
UpdateInfoForComponents();
UpdateInfoForUwpApps();
UpdateInfoForWpfApps();
});
//-------------------------------------------------------------
Task("Build")
.IsDependentOn("Clean")
.IsDependentOn("UpdateInfo")
.Does(async () =>
{
var enableSonar = !string.IsNullOrWhiteSpace(SonarUrl);
if (enableSonar)
{
SonarBegin(new SonarBeginSettings
{
// SonarQube info
Url = SonarUrl,
Login = SonarUsername,
Password = SonarPassword,
// Project info
Key = SonarProject,
// Branch only works with the branch plugin
//Branch = RepositoryBranchName,
Version = VersionFullSemVer,
// Minimize extreme logging
Verbose = false,
Silent = true,
});
}
else
{
Information("Skipping Sonar integration since url is not specified");
}
BuildComponents();
BuildUwpApps();
BuildWpfApps();
if (!string.IsNullOrWhiteSpace(SonarUrl))
{
SonarEnd(new SonarEndSettings
{
Login = SonarUsername,
Password = SonarPassword,
});
Information("Checking whether the project passed the SonarQube gateway...");
var status = "none";
// We need to use /api/qualitygates/project_status
var client = new System.Net.Http.HttpClient();
using (client)
{
var queryUri = string.Format("{0}/api/qualitygates/project_status?projectKey={1}", SonarUrl, SonarProject);
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
var byteArray = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", SonarUsername, SonarPassword));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
Debug("Invoking GET request: '{0}'", queryUri);
var response = await client.GetAsync(new Uri(queryUri));
Debug("Parsing request contents");
var content = response.Content;
var jsonContent = await content.ReadAsStringAsync();
Debug(jsonContent);
dynamic result = Newtonsoft.Json.Linq.JObject.Parse(jsonContent);
status = result.projectStatus.status;
}
Information("SonarQube gateway status returned from request: '{0}'", status);
if (string.IsNullOrWhiteSpace(status))
{
status = "none";
}
status = status.ToLower();
switch (status)
{
case "error":
Error("The SonarQube gateway for '{0}' returned ERROR, please check the error(s) at {1}/dashboard?id={0}", SonarProject, SonarUrl);
break;
case "warn":
Warning("The SonarQube gateway for '{0}' returned WARNING, please check the warning(s) at {1}/dashboard?id={0}", SonarProject, SonarUrl);
break;
case "none":
Warning("The SonarQube gateway for '{0}' returned NONE, please check why no gateway status is available at {1}/dashboard?id={0}", SonarProject, SonarUrl);
break;
case "ok":
Information("The SonarQube gateway for '{0}' returned OK, well done! If you want to show off the results, check out {1}/dashboard?id={0}", SonarProject, SonarUrl);
break;
default:
Error("Unexpected SonarQube gateway status '{0}' for project '{1}'", status, SonarProject);
break;
}
}
BuildTestProjects();
});
//-------------------------------------------------------------
Task("Package")
// Make sure we have the temporary "project.assets.json" in case we need to package with Visual Studio
.IsDependentOn("RestorePackages")
// Make sure to update if we are running on a new agent so we can sign nuget packages
.IsDependentOn("UpdateNuGet")
.IsDependentOn("CodeSign")
.Does(() =>
{
PackageComponents();
PackageUwpApps();
PackageWpfApps();
});
//-------------------------------------------------------------
// Wrapper tasks since we don't want to add "Build" as a
// dependency to "Package" because we want to run in multiple
// stages
//-------------------------------------------------------------
Task("BuildAndPackage")
.IsDependentOn("Build")
.IsDependentOn("Package");
//-------------------------------------------------------------
Task("Default")
.IsDependentOn("BuildAndPackage");
//-------------------------------------------------------------
RunTarget(Target);