forked from daveaglick/Scripty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cake
293 lines (252 loc) · 9.31 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
// The following environment variables need to be set for Publish target:
// NUGET_API_KEY
// SCRIPTY_GITHUB_TOKEN
// Publishing workflow:
// - Update ReleaseNotes.md
// - Update the version in Scripty.CustomTool/source.extension.vsixmanifest
// - Run a normal build with Cake to set SolutionInfo.cs in the repo ("build.cmd")
// - Commit the changes to develop, switch to master, and ff merge from develop
// - Run a Publish build with Cake ("build -target Publish")
// - No need to add a version tag to the repo - added by GitHub on publish
// - Manually upload the .vsix in src\artifacts to the Visual Studio Gallery
// - Switch back to develop branch
#addin "Cake.FileHelpers"
#addin "Octokit"
using Octokit;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
var isLocal = BuildSystem.IsLocalBuild;
var isRunningOnAppVeyor = AppVeyor.IsRunningOnAppVeyor;
var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest;
var buildNumber = AppVeyor.Environment.Build.Number;
var releaseNotes = ParseReleaseNotes("./ReleaseNotes.md");
var version = releaseNotes.Version.ToString();
var semVersion = version + (isLocal ? string.Empty : string.Concat("-build-", buildNumber));
var buildDir = Directory("./src/Scripty/bin") + Directory(configuration);
var buildResultDir = Directory("./build") + Directory(semVersion);
var nugetRoot = buildResultDir + Directory("nuget");
var binDir = buildResultDir + Directory("bin");
var zipFile = "Scripty-v" + semVersion + ".zip";
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information("Building version {0} of Scripty.", semVersion);
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
CleanDirectories(new DirectoryPath[] { buildDir, buildResultDir, binDir, nugetRoot });
});
Task("Restore-Packages")
.IsDependentOn("Clean")
.Does(() =>
{
NuGetRestore("./src/Scripty.sln");
});
Task("Patch-Assembly-Info")
.IsDependentOn("Restore-Packages")
.Does(() =>
{
var file = "./src/SolutionInfo.cs";
CreateAssemblyInfo(file, new AssemblyInfoSettings {
Product = "Scripty",
Copyright = "Copyright \xa9 Scripty Contributors",
Version = version,
FileVersion = version,
InformationalVersion = semVersion
});
});
Task("Build")
.IsDependentOn("Patch-Assembly-Info")
.Does(() =>
{
MSBuild("./src/Scripty.sln", new MSBuildSettings()
.SetConfiguration(configuration)
.SetVerbosity(Verbosity.Minimal)
//.SetVerbosity(Verbosity.Verbose)
.SetMSBuildPlatform(MSBuildPlatform.x86)
);
});
Task("Run-Unit-Tests")
.IsDependentOn("Build")
.Does(() =>
{
var settings = new NUnit3Settings
{
Work = buildResultDir.Path.FullPath
};
if (isRunningOnAppVeyor)
{
settings.Where = "cat != ExcludeFromAppVeyor";
}
NUnit3("./src/**/bin/" + configuration + "/*.Tests.dll", settings);
});
Task("Copy-Files")
.IsDependentOn("Build")
.Does(() =>
{
CopyDirectory(buildDir, binDir);
CopyFiles(new FilePath[] { "LICENSE", "README.md", "ReleaseNotes.md" }, binDir);
});
Task("Zip-Files")
.IsDependentOn("Copy-Files")
.Does(() =>
{
var zipPath = buildResultDir + File(zipFile);
var files = GetFiles(binDir.Path.FullPath + "/**/*");
Zip(binDir, zipPath, files);
});
Task("Create-Library-Packages")
.IsDependentOn("Build")
.Does(() =>
{
// Get the set of nuspecs to package
List<FilePath> nuspecs = new List<FilePath>(GetFiles("./src/Scripty.*/*.nuspec") + GetFiles("./src/*.Scripty/*.nuspec"));
// Package all nuspecs
foreach (var nuspec in nuspecs)
{
// Common settings
var nuGetPackSettings = new NuGetPackSettings
{
Version = semVersion,
BasePath = nuspec.GetDirectory(),
OutputDirectory = nugetRoot,
Symbols = false,
NoPackageAnalysis = true,
Properties = new Dictionary<string, string>
{
{ "Configuration", configuration }
}
};
// Add the tools property to the MSBuild package
if(nuspec.GetFilenameWithoutExtension().FullPath == "Scripty.MsBuild")
{
nuGetPackSettings.ArgumentCustomization = args => args.Append("-Tool");
}
// Do the packing
NuGetPack(nuspec.ChangeExtension(".csproj"), nuGetPackSettings);
}
});
Task("Create-Tools-Package")
.IsDependentOn("Build")
.Does(() =>
{
var nuspec = GetFiles("./src/Scripty/*.nuspec").FirstOrDefault();
if (nuspec == null)
{
throw new InvalidOperationException("Could not find tools nuspec.");
}
var pattern = string.Format("bin\\{0}\\**\\*", configuration); // This is needed to get around a Mono scripting issue (see #246, #248, #249)
NuGetPack(nuspec, new NuGetPackSettings
{
Version = semVersion,
BasePath = nuspec.GetDirectory(),
OutputDirectory = nugetRoot,
Symbols = false,
Files = new []
{
new NuSpecContent
{
Source = pattern,
Target = "tools"
}
}
});
});
Task("Publish-Packages")
.IsDependentOn("Create-Packages")
.WithCriteria(() => isLocal)
// TODO: Add criteria that makes sure this is the master branch
.Does(() =>
{
var apiKey = EnvironmentVariable("NUGET_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
throw new InvalidOperationException("Could not resolve NuGet API key.");
}
foreach (var nupkg in GetFiles(nugetRoot.Path.FullPath + "/*.nupkg"))
{
NuGetPush(nupkg, new NuGetPushSettings
{
ApiKey = apiKey,
Source = "https://www.nuget.org/api/v2/package" // This can be removed with a new version of Cake, see #970
});
}
});
Task("Publish-Release")
.IsDependentOn("Zip-Files")
.WithCriteria(() => isLocal)
// TODO: Add criteria that makes sure this is the master branch
.Does(() =>
{
var githubToken = EnvironmentVariable("SCRIPTY_GITHUB_TOKEN");
if (string.IsNullOrEmpty(githubToken))
{
throw new InvalidOperationException("Could not resolve GitHub token.");
}
var github = new GitHubClient(new ProductHeaderValue("ScriptyCakeBuild"))
{
Credentials = new Credentials(githubToken)
};
var release = github.Repository.Release.Create("daveaglick", "Scripty", new NewRelease("v" + semVersion)
{
Name = semVersion,
Body = string.Join(Environment.NewLine, releaseNotes.Notes),
Prerelease = true,
TargetCommitish = "master"
}).Result;
var zipPath = buildResultDir + File(zipFile);
using (var zipStream = System.IO.File.OpenRead(zipPath.Path.FullPath))
{
var releaseAsset = github.Repository.Release.UploadAsset(release, new ReleaseAssetUpload(zipFile, "application/zip", zipStream, null)).Result;
}
});
Task("Update-AppVeyor-Build-Number")
.WithCriteria(() => isRunningOnAppVeyor)
.Does(() =>
{
AppVeyor.UpdateBuildVersion(semVersion);
});
Task("Upload-AppVeyor-Artifacts")
.IsDependentOn("Zip-Files")
.WithCriteria(() => isRunningOnAppVeyor)
.Does(() =>
{
var artifact = buildResultDir + File(zipFile);
AppVeyor.UploadArtifact(artifact);
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Create-Packages")
.IsDependentOn("Create-Library-Packages")
.IsDependentOn("Create-Tools-Package");
Task("Package")
.IsDependentOn("Zip-Files")
.IsDependentOn("Create-Packages")
.IsDependentOn("Test");
Task("Test")
.IsDependentOn("Run-Unit-Tests");
Task("Default")
.IsDependentOn("Package");
Task("Publish")
.IsDependentOn("Publish-Packages")
.IsDependentOn("Publish-Release");
Task("AppVeyor")
.IsDependentOn("Update-AppVeyor-Build-Number")
.IsDependentOn("Upload-AppVeyor-Artifacts");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);