forked from JasperFx/wolverine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cs
317 lines (254 loc) · 11 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using System.Threading;
using IntegrationTests;
using Npgsql;
using static System.Globalization.CultureInfo;
using static Bullseye.Targets;
using static SimpleExec.Command;
namespace build
{
internal class Build
{
private static void Main(string[] args)
{
Target("default", DependsOn("test-core", "test-policy", "test-extensions", "test-http", "commands"));
Target("restore", () =>
{
Run("dotnet", "restore wolverine.sln");
});
Target("compile", DependsOn("restore"),() =>
{
Run("dotnet",
$"build wolverine.sln --no-restore --framework net7.0");
});
Target("clean",() =>
{
Run("dotnet", "clean wolverine.sln --framework net7.0");
});
TestTarget("test-core", "CoreTests");
TestTarget("test-policy", "PolicyTests");
Target("test-extensions", DependsOn("compile"),() =>
{
RunTests("Extensions", "Wolverine.FluentValidation.Te" +
"sts");
RunTests("Extensions", "Wolverine.MemoryPack.Tests");
RunTests("Extensions", "Wolverine.MessagePack.Tests");
});
IntegrationTestTarget("test-http", "Http", "Wolverine.Http.Tests");
IntegrationTestTarget("test-persistence", "Persistence", "PersistenceTests");
IntegrationTestTarget("test-rabbit", "Transports", "RabbitMQ", "Wolverine.RabbitMQ.Tests");
IntegrationTestTarget("test-pulsar", "Transports", "Wolverine.Pulsar.Tests");
Target("test-samples", DependsOn("compile", "docker-up"), () =>
{
RunTests("samples", "TodoWebService", "TodoWebServiceTests");
RunTests("samples", "TestHarness", "BankingService.Tests");
RunTests("samples", "Middleware", "AppWithMiddleware.Tests");
RunTests("samples", "EFCoreSample", "ItemService.Tests");
});
Target("full", DependsOn("default", "test-persistence", "test-rabbit", "test-pulsar"), () =>
{
Console.WriteLine("Look Ma, I'm running full!");
});
Target("commands", DependsOn("compile"),() =>
{
var original = Directory.GetCurrentDirectory();
/*
Dir.chdir("src/Subscriber") do
sh "dotnet run -- ?"
sh "dotnet run -- export-json-schema obj/schema"
end
*/
Directory.SetCurrentDirectory(Path.Combine(original, "src", "Testing", "ConsoleApp"));
RunCurrentProject("?");
RunCurrentProject("describe");
Directory.SetCurrentDirectory(Path.Combine(original, "src", "Http", "WolverineWebApi"));
RunCurrentProject("codegen preview");
Directory.SetCurrentDirectory(original);
});
Target("ci", DependsOn("compile"));
Target("install", () =>
RunNpm("install"));
Target("install-mdsnippets", IgnoreIfFailed(() =>
Run("dotnet", $"tool install -g MarkdownSnippets.Tool")
));
Target("docs", DependsOn("install", "install-mdsnippets"), () => {
// Run docs site
RunNpm("run docs");
});
Target("docs-build", DependsOn("install", "install-mdsnippets"), () => {
// Run docs site
RunNpm("run docs:build");
});
Target("publish-docs", DependsOn("docs-build"), () =>
Run("npm", "run docs:publish"));
Target("clear-inline-samples", () => {
var files = Directory.GetFiles("./docs", "*.md", SearchOption.AllDirectories);
var pattern = @"<!-- snippet:(.+)-->[\s\S]*?<!-- endSnippet -->";
var replacePattern = $"<!-- snippet:$1-->{Environment.NewLine}<!-- endSnippet -->";
foreach (var file in files)
{
// Console.WriteLine(file);
var content = File.ReadAllText(file);
if (!content.Contains("<!-- snippet:")) {
continue;
}
var updatedContent = Regex.Replace(content, pattern, replacePattern);
File.WriteAllText(file, updatedContent);
}
});
Target("docker-up", () =>
{
Run("docker", "compose up -d");
WaitForDatabaseToBeReady();
});
var nugetProjects = new string[]{
"./src/Wolverine",
"./src/Transports/RabbitMQ/Wolverine.RabbitMQ",
"./src/Transports/Azure/Wolverine.AzureServiceBus",
"./src/Transports/AWS/Wolverine.AmazonSqs",
"./src/Persistence/Wolverine.RDBMS",
"./src/Persistence/Wolverine.Postgresql",
"./src/Persistence/Wolverine.EntityFrameworkCore",
"./src/Persistence/Wolverine.Marten",
"./src/Persistence/Wolverine.SqlServer",
"./src/Extensions/Wolverine.FluentValidation",
"./src/Extensions/Wolverine.MemoryPack",
"./src/Extensions/Wolverine.MessagePack",
"./src/Http/Wolverine.Http",
"./src/Http/Wolverine.Http.FluentValidation",
};
Target("pack", ForEach(nugetProjects), project =>
Run("dotnet", $"pack {project} -o ./artifacts --configuration Release"));
RunTargetsAndExit(args);
}
private static void WaitForDatabaseToBeReady()
{
var attempt = 0;
while (attempt < 10)
try
{
using var conn = new NpgsqlConnection(Servers.PostgresConnectionString);
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "select 1";
cmd.ExecuteReader();
Console.WriteLine("Postgresql is up and ready!");
break;
}
catch (Exception)
{
Thread.Sleep(250);
attempt++;
}
}
public static void TestTarget(string taskName, params string[] testTarget)
{
Target(taskName, DependsOn("compile"), () =>
{
RunTests(testTarget);
});
}
public static void IntegrationTestTarget(string taskName, params string[] testTarget)
{
Target(taskName, DependsOn("compile", "docker-up"), () =>
{
RunTests(testTarget);
});
}
private static void RunCurrentProject(string args)
{
Run("dotnet", $"run --framework net7.0 --no-build --no-restore -- {args}");
}
private static void CopyFilesRecursively(string sourcePath, string targetPath)
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
}
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*",SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
}
}
private static string InitializeDirectory(string path)
{
EnsureDirectoriesDeleted(path);
Directory.CreateDirectory(path);
return path;
}
private static void EnsureDirectoriesDeleted(params string[] paths)
{
foreach (var path in paths)
{
if (Directory.Exists(path))
{
var dir = new DirectoryInfo(path);
DeleteDirectory(dir);
}
}
}
private static void DeleteDirectory(DirectoryInfo baseDir)
{
baseDir.Attributes = FileAttributes.Normal;
foreach (var childDir in baseDir.GetDirectories())
DeleteDirectory(childDir);
foreach (var file in baseDir.GetFiles())
file.IsReadOnly = false;
baseDir.Delete(true);
}
private static void RunNpm(string args) =>
Run("npm", args, windowsName: "cmd.exe", windowsArgs: $"/c npm {args}");
private static void RunTests(params string[] paths)
{
var projectName = paths.Last();
var path = paths.Length == 1
? $"src/Testing/{projectName}/{projectName}.csproj"
: $"src/{string.Join('/', paths.SkipLast(1))}/{projectName}/{projectName}.csproj";
if (!String.IsNullOrEmpty(GetEnvironmentVariable("GITHUB_ACTIONS")))
{
path += " --logger \"GitHubActions;verbosity=detailed\"";
}
Run("dotnet", "test --no-build --no-restore --logger GitHubActions --framework net7.0 " + path);
}
private static string GetEnvironmentVariable(string variableName)
{
var val = Environment.GetEnvironmentVariable(variableName);
// Azure devops converts environment variable to upper case and dot to underscore
// https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch
// Attempt to fetch variable by updating it
if (string.IsNullOrEmpty(val))
{
val = Environment.GetEnvironmentVariable(variableName.ToUpper().Replace(".", "_"));
}
Console.WriteLine(val);
return val;
}
private static string GetFramework()
{
var frameworkName = Assembly.GetEntryAssembly().GetCustomAttribute<TargetFrameworkAttribute>().FrameworkName;
var version = float.Parse(frameworkName.Split('=')[1].Replace("v",""), InvariantCulture.NumberFormat);
return version < 5.0 ? $"netcoreapp{version.ToString("N1", InvariantCulture.NumberFormat)}" : $"net{version.ToString("N1", InvariantCulture.NumberFormat)}";
}
private static Action IgnoreIfFailed(Action action)
{
return () =>
{
try
{
action();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
};
}
}
}