-
Notifications
You must be signed in to change notification settings - Fork 802
/
build.cs
274 lines (243 loc) · 8.6 KB
/
build.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
using Cake.Common;
using Cake.Common.IO;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Pack;
using Cake.Core;
using Cake.Frosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Build;
public class BuildContext : FrostingContext
{
public string BuildConfiguration { get; }
public bool LocalPush { get; }
public string SerenityDir { get; }
public string SerenitySrc => Path.Combine(SerenityDir, "src");
public string SerenityNetSln => Path.Combine(SerenitySrc, "Serenity.Net.slnf");
public string SerenityPackages => Path.Combine(SerenityDir, "packages");
public string SerenityNupkg => Path.Combine(SerenityDir, "build", ".nupkg");
public string CorelibDir => Path.Combine(SerenityDir, "packages", "corelib");
public static string MyPackagesDir => Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile),
".nuget", "my-packages");
private static readonly HashSet<string> SkipDirsToRoot = new(StringComparer.OrdinalIgnoreCase)
{
".build",
"artifacts",
"bin",
"build",
"cake",
"Debug",
"obj",
"Release"
};
public BuildContext(ICakeContext context) : base(context)
{
BuildConfiguration = context.Argument("conguration", "Release");
LocalPush = context.Argument("localpush", false);
string workingDir = context.Environment.WorkingDirectory.ToString();
while (SkipDirsToRoot.Contains(Path.GetFileName(workingDir)))
Path.Combine(workingDir, "..");
string tryPath(string relative)
{
var path = Path.Combine(workingDir, relative);
if (File.Exists(Path.Combine(path, "Serenity.sln")))
return path;
return null;
}
SerenityDir = tryPath(".") ?? tryPath("Serenity") ?? tryPath("..") ??
throw new Exception("Couldn't locate Serenity.sln from current directory!");
context.Environment.WorkingDirectory = SerenityDir;
}
}
public static partial class Utilities
{
public static void WriteHeader(string header)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("*** " + header + " ***");
Console.ResetColor();
}
public static void PushToMyPackages(this BuildContext context, string nupkgDir)
{
var myPackagesDir = BuildContext.MyPackagesDir;
if (!Directory.Exists(myPackagesDir))
{
Directory.CreateDirectory(myPackagesDir);
context.DotNetNuGetAddSource("MyPackages", new()
{
Source = myPackagesDir
});
}
foreach (var package in Directory.GetFiles(nupkgDir, "*.nupkg"))
{
var filename = Path.ChangeExtension(Path.GetFileName(package), null);
var match = NuPkgIdVersionRegex().Match(filename);
if (match != null && match.Groups.Count >= 2)
{
var id = match.Groups[1].Value;
var version = match.Groups[2].Value[1..];
var dir = Path.Combine(myPackagesDir, id, version);
if (Directory.Exists(dir))
Directory.Delete(dir, true);
}
context.DotNetNuGetPush(package, new()
{
Source = myPackagesDir
});
}
}
public static void PushPackages(this ICakeContext context, string nupkgDir, params string[] toSources)
{
foreach (var package in Directory.GetFiles(nupkgDir, "*.nupkg"))
{
foreach (var toSource in toSources)
{
context.DotNetNuGetPush(package, new()
{
Source = toSource,
SkipDuplicate = true
});
}
}
}
[System.Text.RegularExpressions.GeneratedRegex(@"(.+?)((\.[0-9]+)+)")]
private static partial System.Text.RegularExpressions.Regex NuPkgIdVersionRegex();
}
public sealed class Clean : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.CleanDirectory(context.SerenityNupkg);
context.CreateDirectory(context.SerenityNupkg);
context.CleanDirectories(context.SerenitySrc + "/Serenity.*/**/bin/" + context.Configuration);
context.CleanDirectories(context.SerenityDir + "/tests/**/bin/");
context.CleanDirectories(context.SerenityDir + "/packages/*/out");
}
}
[IsDependentOn(typeof(Clean))]
public sealed class Compile : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
Utilities.WriteHeader($"Building {context.SerenityNetSln}");
context.DotNetBuild(context.SerenityNetSln, new()
{
Configuration = context.BuildConfiguration,
Verbosity = DotNetVerbosity.Minimal
});
}
}
[IsDependentOn(typeof(Compile))]
public sealed class DotNetTests : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var projects = context.GetFiles(context.SerenityDir + "tests/**/*.csproj");
foreach (var project in projects)
{
context.DotNetTest(project.FullPath, new()
{
Configuration = context.BuildConfiguration,
NoBuild = true
});
}
}
}
[IsDependentOn(typeof(Compile))]
public sealed class JsTests : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.StartProcess("powershell", new Cake.Core.IO.ProcessSettings
{
Arguments = "pnpm test",
WorkingDirectory = context.CorelibDir
});
}
}
internal class DotNetPacker(BuildContext context, string baseDir, string nupkgDir)
{
private readonly string baseDir = baseDir ?? throw new ArgumentNullException(nameof(baseDir));
private readonly string nupkgDir = nupkgDir ?? throw new ArgumentNullException(nameof(DotNetPacker.nupkgDir));
public void Pack(string project)
{
var projectFile = Path.Combine(baseDir, project);
if (!File.Exists(projectFile))
{
Console.Error.WriteLine(projectFile + " not found!");
Environment.Exit(1);
}
Utilities.WriteHeader("dotnet pack " + projectFile);
context.DotNetPack(projectFile, new DotNetPackSettings
{
Configuration = context.BuildConfiguration,
OutputDirectory = nupkgDir,
ArgumentCustomization = args => args.Append("-p:ContinuousIntegrationBuild=true")
});
}
}
[IsDependentOn(typeof(DotNetTests))]
[IsDependentOn(typeof(JsTests))]
[TaskDescription("Pack Serenity.Net projects")]
public sealed class Pack : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var packer = new DotNetPacker(context, context.SerenitySrc, context.SerenityNupkg);
packer.Pack("Serenity.Net.Core/Serenity.Net.Core.csproj");
packer.Pack("Serenity.Net.Data/Serenity.Net.Data.csproj");
packer.Pack("Serenity.Net.Entity/Serenity.Net.Entity.csproj");
packer.Pack("Serenity.Net.Services/Serenity.Net.Services.csproj");
packer.Pack("Serenity.Net.Web/Serenity.Net.Web.csproj");
packer.Pack("Serenity.Net.CodeGenerator/Serenity.Net.CodeGenerator.csproj");
packer.Pack("Serenity.Assets/Serenity.Assets.csproj");
packer.Pack("../packages/corelib/Serenity.CoreLib.csproj");
}
}
public sealed class LocalPushOnly : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.PushToMyPackages(context.SerenityNupkg);
}
}
[IsDependentOn(typeof(Pack))]
public sealed class LocalPush : FrostingTask<BuildContext>
{
public override bool ShouldRun(BuildContext context)
{
return context.LocalPush;
}
public override void Run(BuildContext context)
{
context.PushToMyPackages(context.SerenityNupkg);
}
}
[IsDependentOn(typeof(Pack))]
[IsDependentOn(typeof(LocalPush))]
public sealed class Push : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.PushPackages(context.SerenityNupkg,
"https://api.nuget.org/v3/index.json",
"serenity.is");
}
}
[IsDependentOn(typeof(Pack))]
public sealed class Default : FrostingTask<BuildContext>
{
}
public static class Program
{
public static int Main(string[] args)
{
if (args?.Length > 0 && args[0].All(char.IsLetter))
args[0] = "--target=" + args[0];
return new CakeHost()
.UseContext<BuildContext>()
.Run(args);
}
}