-
Notifications
You must be signed in to change notification settings - Fork 33
/
Program.cs
298 lines (234 loc) · 11.5 KB
/
Program.cs
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
294
295
296
297
298
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using osu.Desktop.Deploy.Builders;
using osu.Desktop.Deploy.Uploaders;
using osu.Framework;
using osu.Framework.IO.Network;
namespace osu.Desktop.Deploy
{
internal static class Program
{
public const string STAGING_FOLDER = "staging";
public const string TEMPLATES_FOLDER = "templates";
public const string RELEASES_FOLDER = "releases";
public static string StagingPath => Path.Combine(Environment.CurrentDirectory, STAGING_FOLDER);
public static string TemplatesPath => Path.Combine(Environment.CurrentDirectory, TEMPLATES_FOLDER);
public static string ReleasesPath => Path.Combine(Environment.CurrentDirectory, RELEASES_FOLDER);
public static string SolutionName => GetConfiguration("SolutionName");
public static string ProjectName => GetConfiguration("ProjectName");
public static string PackageName => GetConfiguration("PackageName");
public static string IconName => GetConfiguration("IconName");
public static bool GitHubUpload => bool.Parse(ConfigurationManager.AppSettings["GitHubUpload"] ?? "false");
public static string? GitHubUsername => ConfigurationManager.AppSettings["GitHubUsername"];
public static string? GitHubRepoName => ConfigurationManager.AppSettings["GitHubRepoName"];
public static string? GitHubAccessToken => ConfigurationManager.AppSettings["GitHubAccessToken"];
public static string GitHubApiEndpoint => $"https://api.github.com/repos/{GitHubUsername}/{GitHubRepoName}/releases";
public static string GitHubRepoUrl => $"https://github.com/{GitHubUsername}/{GitHubRepoName}";
public static bool CanGitHub => !string.IsNullOrEmpty(GitHubAccessToken);
public static string? WindowsCodeSigningMetadataPath => ConfigurationManager.AppSettings["WindowsCodeSigningMetadataPath"];
public static string? AndroidCodeSigningCertPath => ConfigurationManager.AppSettings["AndroidCodeSigningCertPath"];
public static string? AppleCodeSignCertName => ConfigurationManager.AppSettings["AppleCodeSignCertName"];
public static string? AppleInstallSignCertName => ConfigurationManager.AppSettings["AppleInstallSignCertName"];
public static string? AppleNotaryProfileName => ConfigurationManager.AppSettings["AppleNotaryProfileName"];
public static string? AppleKeyChainPath => ConfigurationManager.AppSettings["AppleKeyChainPath"];
public static bool IncrementVersion => bool.Parse(ConfigurationManager.AppSettings["IncrementVersion"] ?? "true");
public static string SolutionPath { get; private set; } = null!;
private static bool interactive;
/// <summary>
/// args[0]: code signing passphrase
/// args[1]: version
/// args[2]: platform
/// args[3]: arch
/// </summary>
public static void Main(string[] args)
{
interactive = args.Length == 0;
displayHeader();
findSolutionPath();
if (!Directory.Exists(RELEASES_FOLDER))
{
Logger.Write("WARNING: No release directory found. Make sure you want this!", ConsoleColor.Yellow);
Directory.CreateDirectory(RELEASES_FOLDER);
}
GitHubRelease? lastRelease = null;
if (CanGitHub)
{
Logger.Write("Checking GitHub releases...");
lastRelease = GetLastGithubRelease();
Logger.Write(lastRelease == null
? "This is the first GitHub release"
: $"Last GitHub release was {lastRelease.Name}.");
}
//increment build number until we have a unique one.
string verBase = DateTime.Now.ToString("yyyy.Mdd.");
int increment = 0;
if (lastRelease?.TagName.StartsWith(verBase, StringComparison.InvariantCulture) ?? false)
increment = int.Parse(lastRelease.TagName.Split('.')[2]) + (IncrementVersion ? 1 : 0);
string version = $"{verBase}{increment}";
var targetPlatform = RuntimeInfo.OS;
if (args.Length > 1 && !string.IsNullOrEmpty(args[1]))
version = args[1];
if (args.Length > 2 && !string.IsNullOrEmpty(args[2]))
Enum.TryParse(args[2], true, out targetPlatform);
Console.ResetColor();
Console.WriteLine($"Increment Version: {IncrementVersion}");
Console.WriteLine($"Signing Certificate: {WindowsCodeSigningMetadataPath}");
Console.WriteLine($"Upload to GitHub: {GitHubUpload}");
Console.WriteLine();
Console.Write($"Ready to deploy version {version} on platform {targetPlatform}!");
PauseIfInteractive();
RunCommand("dotnet", "tool restore", useSolutionPath: false);
Builder builder;
switch (targetPlatform)
{
case RuntimeInfo.Platform.Windows:
builder = new WindowsBuilder(version);
break;
case RuntimeInfo.Platform.Linux:
builder = new LinuxBuilder(version);
break;
case RuntimeInfo.Platform.macOS:
builder = new MacOSBuilder(version, getArg(3));
break;
case RuntimeInfo.Platform.iOS:
builder = new IOSBuilder(version);
break;
case RuntimeInfo.Platform.Android:
builder = new AndroidBuilder(version, getArg(0));
break;
default:
throw new PlatformNotSupportedException(targetPlatform.ToString());
}
Uploader uploader = builder.CreateUploader();
Logger.Write("Restoring previous build...");
uploader.RestoreBuild();
Logger.Write("Running build...");
builder.Build();
Logger.Write("Creating release...");
uploader.PublishBuild(version);
if (CanGitHub && GitHubUpload)
openGitHubReleasePage();
Logger.Write("Done!");
PauseIfInteractive();
}
private static void displayHeader()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine(" Please note that OSU! and PPY are registered trademarks and as such covered by trademark law.");
Console.WriteLine(" Do not distribute builds of this project publicly that make use of these.");
Console.ResetColor();
Console.WriteLine();
}
private static void openGitHubReleasePage()
{
Process.Start(new ProcessStartInfo
{
FileName = GitHubApiEndpoint,
UseShellExecute = true //see https://github.com/dotnet/corefx/issues/10361
});
}
public static GitHubRelease? GetLastGithubRelease(bool includeDrafts = false)
{
var req = new JsonWebRequest<List<GitHubRelease>>($"{GitHubApiEndpoint}");
req.AuthenticatedBlockingPerform();
return req.ResponseObject.FirstOrDefault(r => includeDrafts || !r.Draft);
}
/// <summary>
/// Find the base path of the active solution (git checkout location)
/// </summary>
private static void findSolutionPath()
{
string? path = Path.GetDirectoryName(Environment.CommandLine.Replace("\"", "").Trim());
if (string.IsNullOrEmpty(path))
path = Environment.CurrentDirectory;
while (true)
{
if (File.Exists(Path.Combine(path, $"{SolutionName}.sln")))
break;
if (Directory.Exists(Path.Combine(path, "osu")) && File.Exists(Path.Combine(path, "osu", $"{SolutionName}.sln")))
{
path = Path.Combine(path, "osu");
break;
}
path = path.Remove(path.LastIndexOf(Path.DirectorySeparatorChar));
}
SolutionPath = path + Path.DirectorySeparatorChar;
}
public static bool RunCommand(string command, string args, bool useSolutionPath = true, Dictionary<string, string>? environmentVariables = null, bool throwIfNonZero = true,
bool exitOnFail = true)
{
string workingDirectory = useSolutionPath ? SolutionPath : Environment.CurrentDirectory;
Logger.Write($"Using working directory {workingDirectory}...");
Logger.Write($"Running {command} {args}...");
var psi = new ProcessStartInfo(command, args)
{
WorkingDirectory = workingDirectory,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
if (environmentVariables != null)
{
foreach (var pair in environmentVariables)
psi.EnvironmentVariables.Add(pair.Key, pair.Value);
}
try
{
Process? p = Process.Start(psi);
if (p == null) return false;
string output = p.StandardOutput.ReadToEnd();
output += p.StandardError.ReadToEnd();
p.WaitForExit();
if (p.ExitCode == 0) return true;
Logger.Write(output);
}
catch (Exception e)
{
Logger.Write(e.Message);
}
if (!throwIfNonZero) return false;
if (exitOnFail)
Logger.Error($"Command {command} {args} failed!");
else
Logger.Write($"Command {command} {args} failed!");
return false;
}
public static string? ReadLineMasked(string prompt)
{
Console.WriteLine(prompt);
var fg = Console.ForegroundColor;
Console.ForegroundColor = Console.BackgroundColor;
var ret = Console.ReadLine();
Console.ForegroundColor = fg;
return ret;
}
public static void PauseIfInteractive()
{
if (interactive)
Console.ReadLine();
else
Console.WriteLine();
}
public static string GetConfiguration(string key)
=> ConfigurationManager.AppSettings[key] ?? throw new Exception($"Configuration key '{key}' not found.");
public static void AuthenticatedBlockingPerform(this WebRequest r)
{
r.AddHeader("Authorization", $"token {GitHubAccessToken}");
r.Perform();
}
private static string? getArg(int index)
{
string[] args = Environment.GetCommandLineArgs();
return args.Length > ++index ? args[index] : null;
}
}
}