-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cake
293 lines (260 loc) · 9.82 KB
/
build.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Install Add-ins.
#addin nuget:?package=Cake.Git
#addin nuget:?package=Newtonsoft.Json
// Install Tools.
// Load Other Scripts.
#load "build/Constants.cake";
#load "build/SegmentBump.cake";
#load "build/Parameters.cake";
#load "build/SolutionData.cake";
#load "build/Project.cake";
#load "build/BuildVersion.cake";
#load "build/ProjectFile.cake";
#load "build/ProjectReference.cake";
//////////////////////////////////////////////////////////////////////
// PARAMETERS
//////////////////////////////////////////////////////////////////////
var parameters = Parameters.CreateNew(Context);
var solutionData = SolutionData.CreateNew(Context, parameters);
var setupComplete = false;
var publishingError = false;
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context => {
Information("Building Solution Information...");
solutionData.PopulateSolutionProjects(context, parameters);
Information("Adding Project Version Data...");
solutionData.AddProjectVersions();
Information("Adding Project File Data...");
solutionData.AddProjectFiles();
Information("Adding Solution Project Reference Data...");
solutionData.AddProjectReferences();
Information("Calculating Build Order...");
solutionData.AddProjectBuildOrder();
// Load Previous Build Data (if any)
if(FileExists(Constants.JsonFile))
{
Information("`projectData.json` Found{0}Loading previous build data...", Parameters.NewLine(1));
var previousBuildDetails = Parameters.ReadJsonFile(Constants.JsonFile);
solutionData.CompareProjectsForChanges(previousBuildDetails.Projects);
}
else
{
Warning("`projectData.json` not Found{0}All Projects will be require new build.", Parameters.NewLine(1));
solutionData.SetAllProjectsToBuild();
}
// DISPLAY SETUP DETAILS
// Parameter Information
Information("Target: {0}", parameters.Target);
Information("Configuration: {0}", parameters.Configuration);
Information("Segment to Bump: {0}", parameters.SegmentBump);
Information("Next Segment Threshold: {0}%", parameters.SegmentThreshold);
Information("Verbosity: {0}", parameters.Verbosity);
// Solution Information
Information("Solution Directory: {0}", solutionData.SolutionDirectory);
Information("Artifact clsDirectory: {0}", solutionData.ArtifactDirectory);
// Project(s) Information
Information("{0}{1}", Parameters.NewLine(1), Parameters.Line(50, Constants.Star));
Information("Solution Projects:");
Information("{0}", Parameters.Line(50, Constants.Star));
foreach(var project in solutionData.Projects)
{
Information("{0}Project: {1}", Parameters.Indent(1), project.Name);
Information("{0}File Name: {1}", Parameters.Indent(1), project.CSProjFile.GetFilename());
Information("{0}Project Path: {1}", Parameters.Indent(1), project.ProjectPath.FullPath);
Information("{0}Build Folder: {1}", Parameters.Indent(1), project.BuildFolder.FullPath);
Information("{0}Is Test Project: {1}", Parameters.Indent(1), project.IsTestProject);
if(!project.IsTestProject)
{
if(parameters.Verbosity == Constants.Verbosity.Verbose)
{
Verbose("{0}Project Files:", Parameters.Indent(1));
foreach(var projectFile in project.ProjectFiles)
{
Verbose("{0}File: {1} ({2})", Parameters.Indent(2), projectFile.Name, projectFile.Hash);
}
}
Information("{0}Current Version: {1}", Parameters.Indent(1), project.Version.SemanticVersion);
Information("{0}Requires New Version: {1}", Parameters.Indent(1), project.RequiresNewVersion);
Information("{0}Percentage of Change: {1}%", Parameters.Indent(1), project.ChangePercentage * 100);
Information("{0}Requires New Build: {1}", Parameters.Indent(1), project.RequiresNewBuild);
Information("{0}Build Order: {1}", Parameters.Indent(1), project.BuildOrder);
Information("{0}Requires Reference Package Updates: {1}", Parameters.Indent(1), project.RequiresReferencePackageUpdates);
Information("{0}Solution Project References:", Parameters.Indent(1));
if(project.ProjectReferences != null && project.ProjectReferences.Any())
{
foreach(var reference in project.ProjectReferences)
{
Information("{0}{1} (v{2})", Parameters.Indent(2), reference.Name, reference.Version);
}
}
else
{
Information("{0}Project has no Package References", Parameters.Indent(2));
}
}
Information("{0}", Parameters.Line(50, Constants.SingleLine));
}
setupComplete = true;
});
Teardown(context => {
Information("Finished running tasks");
if(setupComplete)
{
Information("Saving {0} File", Constants.JsonFile);
Parameters.WriteJsonFile(Constants.JsonFile, solutionData);
}
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean-All-Build-Folders")
.Does(() =>
{
foreach(var project in solutionData.Projects)
{
CleanDirectory(project.BuildFolder.FullPath);
}
var allFiles = GetFiles(solutionData.ArtifactDirectory.FullPath + "/*.nupkg");
Information("Package Files Found: {0}", allFiles.Count());
var filesToKeep = solutionData.GetLatestNuGetPackageNames;
Information("Package Files To Keep: {0}", filesToKeep.Count());
foreach(var file in allFiles)
{
if(filesToKeep.FirstOrDefault(ftk => ftk.FullPath == file.FullPath) == null)
{
DeleteFile(file);
}
}
});
Task("Update-Project-Versions")
.IsDependentOn("Clean-All-Build-Folders")
.Does(() =>
{
var projectsWithReferences = solutionData.Projects.Where(p => p.HasChildReferences);
while(solutionData.Projects.Any(p => p.RequiresNewVersion))
{
var project = solutionData.Projects.FirstOrDefault(p => p.RequiresNewVersion);
var oldVersion = project.Version.SemanticVersion;
project.BumpBuildVersion(parameters);
project.SaveProjectVersion();
Information("Project: `{0}`{1}{2}Updating Version v{3} => v{4}",
project.Name,
Parameters.NewLine(1),
Parameters.Indent(1),
oldVersion,
project.Version.SemanticVersion);
Information("{0}--Reference Projects--", Parameters.NewLine(1));
//TODO: Fix this
foreach(var childProject in projectsWithReferences)
{
if(childProject.ProjectReferences.Any(pr => pr.Name == project.Name))
{
childProject.RequiresReferencePackageUpdates = true;
Information("{0}{1}", Parameters.Indent(1), childProject.Name);
}
else
{
Information("{0}Project has no Package References", Parameters.Indent(1));
break;
}
}
Information("{0}", Parameters.Line(50, Constants.SingleLine));
}
});
Task("Update-Project-Reference-Versions")
.IsDependentOn("Update-Project-Versions")
.Does(context =>
{
while(solutionData.Projects.Any(p => p.RequiresReferencePackageUpdates))
{
var project = solutionData.Projects.FirstOrDefault(p => p.RequiresReferencePackageUpdates);
Information("Updating References in {0}", project.Name);
//project.UpdateProjectReference(context, parameters.Projects);
}
});
Task("Restore-Build-Package-Projects")
.IsDependentOn("Update-Project-Reference-Versions")
.Does(() =>
{
foreach(var project in solutionData.Projects.OrderBy(p => p.BuildOrder))
{
// Restore NuGet Packages for Current Project
Information("Starting NuGet Restore for {0}", project.Name);
DotNetCoreRestore(project.CSProjFile.FullPath,
new DotNetCoreRestoreSettings
{
ConfigFile = "./nuget.config"
});
Information("Finished NuGet Restore for {0}", project.Name);
// Build Current Project
Information("Starting Build for {0}", project.Name);
DotNetCoreBuild(project.CSProjFile.FullPath, new DotNetCoreBuildSettings()
{
Configuration = parameters.Configuration
});
project.RequiresNewBuild = false;
Information("Finished Build for {0}", project.Name);
// Package Current Project
Information("Starting NuGet Packaging for {0}", project.Name);
DotNetCorePack(project.CSProjFile.FullPath,
new DotNetCorePackSettings {
Configuration = parameters.Configuration,
OutputDirectory = solutionData.ArtifactDirectory.FullPath,
NoBuild = true
});
Information("Finished NuGet Packaging for {0}", project.Name);
// Update Project File Hashes
Information("Starting Hash Update for {0}", project.Name);
project.UpdateProjectFileHash();
Information("Finished Hash Update for {0}", project.Name);
Information("{0}", Parameters.Line(50, Constants.SingleLine));
}
});
Task("Restore-Build-Tests")
.IsDependentOn("Restore-Build-Package-Projects")
.Does(() =>
{
foreach(var testProject in solutionData.Tests)
{
Information("Starting NuGet Restore for {0}", testProject.Name);
DotNetCoreRestore(testProject.CSProjFile.FullPath,
new DotNetCoreRestoreSettings
{
ConfigFile = "./nuget.config"
});
Information("Finished NuGet Restore for {0}", testProject.Name);
Information("Starting Build for {0}", testProject.Name);
DotNetCoreBuild(testProject.CSProjFile.FullPath, new DotNetCoreBuildSettings()
{
Configuration = parameters.Configuration
});
Information("Finished Build for {0}", testProject.Name);
}
});
Task("Run-Tests")
.IsDependentOn("Restore-Build-Tests")
.Does(() =>
{
foreach(var testProject in solutionData.Tests)
{
Information("Starting Tests for {0}", testProject.Name);
DotNetCoreTest(testProject.CSProjFile.FullPath,
new DotNetCoreTestSettings()
{
Configuration = parameters.Configuration,
NoBuild = true
});
Information("Finished Tests for {0}", testProject.Name);
}
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Run-Tests");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(parameters.Target);