-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparameters.cake
219 lines (178 loc) · 9.31 KB
/
parameters.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
// ***********************************************************************
// Copyright (c) Charlie Poole and contributors.
// Licensed under the MIT License. See LICENSE.txt in root directory.
// ***********************************************************************
#load "./versioning.cake"
#load "./packaging.cake"
#load "./package-checks.cake"
#load "./package-tests.cake"
#load "./test-runner.cake"
#load "./test-results.cake"
#load "./test-reports.cake"
#load "./tests.cake"
#load "./utilities.cake"
using System;
// URLs for uploading packages
private const string MYGET_PUSH_URL = "https://www.myget.org/F/nunit/api/v2";
private const string NUGET_PUSH_URL = "https://api.nuget.org/v3/index.json";
private const string CHOCO_PUSH_URL = "https://push.chocolatey.org/";
// Environment Variable names holding API keys
private const string MYGET_API_KEY = "MYGET_API_KEY";
private const string NUGET_API_KEY = "NUGET_API_KEY";
private const string CHOCO_API_KEY = "CHOCO_API_KEY";
private const string GITHUB_ACCESS_TOKEN = "GITHUB_ACCESS_TOKEN";
// Pre-release labels that we publish
private static readonly string[] LABELS_WE_PUBLISH_ON_MYGET = { "dev", "pre" };
private static readonly string[] LABELS_WE_PUBLISH_ON_NUGET = { "alpha", "beta", "rc" };
private static readonly string[] LABELS_WE_PUBLISH_ON_CHOCOLATEY = { "alpha", "beta", "rc" };
private static readonly string[] LABELS_WE_RELEASE_ON_GITHUB = { "alpha", "beta", "rc" };
public class BuildParameters
{
private ISetupContext _context;
private BuildSystem _buildSystem;
public static BuildParameters Create(ISetupContext context)
{
var parameters = new BuildParameters(context);
parameters.Validate();
return parameters;
}
private BuildParameters(ISetupContext context)
{
_context = context;
_buildSystem = context.BuildSystem();
Target = _context.TargetTask.Name;
TasksToExecute = _context.TasksToExecute.Select(t => t.Name);
ProjectDirectory = _context.Environment.WorkingDirectory.FullPath + "/";
Configuration = _context.Argument("configuration", DEFAULT_CONFIGURATION);
MyGetApiKey = _context.EnvironmentVariable(MYGET_API_KEY);
NuGetApiKey = _context.EnvironmentVariable(NUGET_API_KEY);
ChocolateyApiKey = _context.EnvironmentVariable(CHOCO_API_KEY);
GitHubAccessToken = _context.EnvironmentVariable(GITHUB_ACCESS_TOKEN);
BuildVersion = new BuildVersion(context, this);
}
public string Target { get; }
public IEnumerable<string> TasksToExecute { get; }
public ICakeContext Context => _context;
public string Configuration { get; }
public BuildVersion BuildVersion { get; }
public string PackageVersion => BuildVersion.PackageVersion;
public string AssemblyVersion => BuildVersion.AssemblyVersion;
public string AssemblyFileVersion => BuildVersion.AssemblyFileVersion;
public string AssemblyInformationalVersion => BuildVersion.AssemblyInformationalVersion;
public bool IsLocalBuild => _buildSystem.IsLocalBuild;
public bool IsRunningOnUnix => _context.IsRunningOnUnix();
public bool IsRunningOnWindows => _context.IsRunningOnWindows();
public bool IsRunningOnAppVeyor => _buildSystem.AppVeyor.IsRunningOnAppVeyor;
// Directories
public string ProjectDirectory { get; }
public string OutputDirectory => ProjectDirectory + "bin/" + Configuration + "/";
public string SourceDirectory => ProjectDirectory + "src/";
public string PackageDirectory => ProjectDirectory + "output/";
public string ToolsDirectory => ProjectDirectory + "tools/";
public string NuGetInstallDirectory => ToolsDirectory + NUGET_ID + "/";
public string ChocolateyInstallDirectory => ToolsDirectory + CHOCO_ID + "/";
public string NuGetPackageName => NUGET_ID + "." + PackageVersion + ".nupkg";
public string ChocolateyPackageName => CHOCO_ID + "." + PackageVersion + ".nupkg";
public string NuGetPackage => PackageDirectory + NuGetPackageName;
public string ChocolateyPackage => PackageDirectory + ChocolateyPackageName;
public string MyGetPushUrl => MYGET_PUSH_URL;
public string NuGetPushUrl => NUGET_PUSH_URL;
public string ChocolateyPushUrl => CHOCO_PUSH_URL;
public string MyGetApiKey { get; }
public string NuGetApiKey { get; }
public string ChocolateyApiKey { get; }
public string GitHubAccessToken { get; }
public string BranchName => BuildVersion.BranchName;
public bool IsReleaseBranch => BuildVersion.IsReleaseBranch;
public bool IsPreRelease => BuildVersion.IsPreRelease;
public bool ShouldPublishToMyGet =>
!IsPreRelease || LABELS_WE_PUBLISH_ON_MYGET.Contains(BuildVersion.PreReleaseLabel);
public bool ShouldPublishToNuGet =>
!IsPreRelease || LABELS_WE_PUBLISH_ON_NUGET.Contains(BuildVersion.PreReleaseLabel);
public bool ShouldPublishToChocolatey =>
!IsPreRelease || LABELS_WE_PUBLISH_ON_CHOCOLATEY.Contains(BuildVersion.PreReleaseLabel);
public bool IsProductionRelease =>
!IsPreRelease || LABELS_WE_RELEASE_ON_GITHUB.Contains(BuildVersion.PreReleaseLabel);
public string GetPathToConsoleRunner(string version)
{
return ToolsDirectory + "NUnit.ConsoleRunner." + version + "/tools/nunit3-console.exe";
}
private void Validate()
{
var validationErrors = new List<string>();
if (TasksToExecute.Contains("PublishPackages"))
{
if (ShouldPublishToMyGet && string.IsNullOrEmpty(MyGetApiKey))
validationErrors.Add("MyGet ApiKey was not set.");
if (ShouldPublishToNuGet && string.IsNullOrEmpty(NuGetApiKey))
validationErrors.Add("NuGet ApiKey was not set.");
if (ShouldPublishToChocolatey && string.IsNullOrEmpty(ChocolateyApiKey))
validationErrors.Add("Chocolatey ApiKey was not set.");
}
if (TasksToExecute.Contains("CreateDraftRelease") && (IsReleaseBranch || IsProductionRelease))
{
if (string.IsNullOrEmpty(GitHubAccessToken))
validationErrors.Add("GitHub Access Token was not set.");
}
if (validationErrors.Count > 0)
{
DumpSettings();
var msg = new StringBuilder("Parameter validation failed! See settings above.\n\nErrors found:\n");
foreach (var error in validationErrors)
msg.AppendLine(" " + error);
throw new InvalidOperationException(msg.ToString());
}
}
public void DumpSettings()
{
Console.WriteLine("\nTASKS");
Console.WriteLine("Target: " + Target);
Console.WriteLine("TasksToExecute: " + string.Join(", ", TasksToExecute));
Console.WriteLine("\nENVIRONMENT");
Console.WriteLine("IsLocalBuild: " + IsLocalBuild);
Console.WriteLine("IsRunningOnWindows: " + IsRunningOnWindows);
Console.WriteLine("IsRunningOnUnix: " + IsRunningOnUnix);
Console.WriteLine("IsRunningOnAppVeyor: " + IsRunningOnAppVeyor);
Console.WriteLine("\nVERSIONING");
Console.WriteLine("PackageVersion: " + PackageVersion);
Console.WriteLine("AssemblyVersion: " + AssemblyVersion);
Console.WriteLine("AssemblyFileVersion: " + AssemblyFileVersion);
Console.WriteLine("AssemblyInformationalVersion: " + AssemblyInformationalVersion);
Console.WriteLine("SemVer: " + BuildVersion.SemVer);
Console.WriteLine("IsPreRelease: " + BuildVersion.IsPreRelease);
Console.WriteLine("PreReleaseLabel: " + BuildVersion.PreReleaseLabel);
Console.WriteLine("PreReleaseSuffix: " + BuildVersion.PreReleaseSuffix);
Console.WriteLine("\nDIRECTORIES");
Console.WriteLine("Project: " + ProjectDirectory);
Console.WriteLine("Output: " + OutputDirectory);
//Console.WriteLine("Source: " + SourceDirectory);
//Console.WriteLine("NuGet: " + NuGetDirectory);
//Console.WriteLine("Choco: " + ChocoDirectory);
Console.WriteLine("Package: " + PackageDirectory);
//Console.WriteLine("ZipImage: " + ZipImageDirectory);
//Console.WriteLine("ZipTest: " + ZipTestDirectory);
//Console.WriteLine("NuGetTest: " + NuGetTestDirectory);
//Console.WriteLine("ChocoTest: " + ChocolateyTestDirectory);
Console.WriteLine("\nBUILD");
//Console.WriteLine("Build With: " + (UsingXBuild ? "XBuild" : "MSBuild"));
Console.WriteLine("Configuration: " + Configuration);
//Console.WriteLine("Engine Runtimes: " + string.Join(", ", SupportedEngineRuntimes));
//Console.WriteLine("Core Runtimes: " + string.Join(", ", SupportedCoreRuntimes));
//Console.WriteLine("Agent Runtimes: " + string.Join(", ", SupportedAgentRuntimes));
Console.WriteLine("\nPACKAGING");
Console.WriteLine("MyGetPushUrl: " + MyGetPushUrl);
Console.WriteLine("NuGetPushUrl: " + NuGetPushUrl);
Console.WriteLine("ChocolateyPushUrl: " + ChocolateyPushUrl);
Console.WriteLine("MyGetApiKey: " + (!string.IsNullOrEmpty(MyGetApiKey) ? "AVAILABLE" : "NOT AVAILABLE"));
Console.WriteLine("NuGetApiKey: " + (!string.IsNullOrEmpty(NuGetApiKey) ? "AVAILABLE" : "NOT AVAILABLE"));
Console.WriteLine("ChocolateyApiKey: " + (!string.IsNullOrEmpty(ChocolateyApiKey) ? "AVAILABLE" : "NOT AVAILABLE"));
Console.WriteLine("\nPUBLISHING");
Console.WriteLine("ShouldPublishToMyGet: " + ShouldPublishToMyGet);
Console.WriteLine("ShouldPublishToNuGet: " + ShouldPublishToNuGet);
Console.WriteLine("ShouldPublishToChocolatey: " + ShouldPublishToChocolatey);
Console.WriteLine("\nRELEASING");
Console.WriteLine("BranchName: " + BranchName);
Console.WriteLine("IsReleaseBranch: " + IsReleaseBranch);
Console.WriteLine("IsProductionRelease: " + IsProductionRelease);
}
}