forked from JasperFx/marten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmartenbuild.cs
292 lines (229 loc) · 11.5 KB
/
martenbuild.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
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using System.Threading;
using Npgsql;
using static Bullseye.Targets;
using static SimpleExec.Command;
using static Westwind.Utilities.FileUtils;
namespace martenbuild
{
internal class MartenBuild
{
private const string BUILD_VERSION = "4.0.0-alpha.2";
private const string DockerConnectionString =
"Host=localhost;Port=5432;Database=marten_testing;Username=postgres;password=postgres";
private static void Main(string[] args)
{
var framework = GetFramework();
var configuration = GetEnvironmentVariable("config");
configuration = string.IsNullOrEmpty(configuration) ? "debug" : configuration;
var disableTestParallelization = GetEnvironmentVariable("disable_test_parallelization");
Target("ci", DependsOn("connection", "default"));
Target("default", DependsOn("mocha", "test", "storyteller"));
Target("clean", () =>
EnsureDirectoriesDeleted("results", "artifacts"));
Target("connection", () =>
File.WriteAllText("src/Marten.Testing/connection.txt", GetEnvironmentVariable("connection")));
Target("install", () =>
RunNpm("install"));
Target("mocha", DependsOn("install"), () =>
RunNpm("run test"));
Target("compile", DependsOn("clean"), () =>
{
Run("dotnet",
$"build src/Marten.Testing/Marten.Testing.csproj --framework {framework} --configuration {configuration}");
Run("dotnet",
$"build src/Marten.Schema.Testing/Marten.Schema.Testing.csproj --framework {framework} --configuration {configuration}");
});
Target("compile-noda-time", DependsOn("clean"), () =>
Run("dotnet", $"build src/Marten.NodaTime.Testing/Marten.NodaTime.Testing.csproj --framework {framework} --configuration {configuration}"));
Target("test-noda-time", DependsOn("compile-noda-time"), () =>
Run("dotnet", $"test src/Marten.NodaTime.Testing/Marten.NodaTime.Testing.csproj --framework {framework} --configuration {configuration} --no-build"));
Target("test-schema", () =>
Run("dotnet", $"test src/Marten.Schema.Testing/Marten.Schema.Testing.csproj --framework {framework} --configuration {configuration} --no-build"));
Target("test-commands", () =>
Run("dotnet", $"test src/Marten.CommandLine.Testing/Marten.CommandLine.Testing.csproj --framework {framework} --configuration {configuration} --no-build"));
Target("test-codegen", () =>
{
var projectPath = "src/CommandLineRunner";
Run("dotnet", $"run -- codegen delete", projectPath);
Run("dotnet", $"run -- codegen write", projectPath);
Run("dotnet", $"run -- test", projectPath);
});
Target("test-marten", DependsOn("compile", "test-noda-time"), () =>
Run("dotnet", $"test src/Marten.Testing/Marten.Testing.csproj --framework {framework} --configuration {configuration} --no-build"));
Target("test-plv8", DependsOn("compile"), () =>
Run("dotnet", $"test src/Marten.PLv8.Testing/Marten.PLv8.Testing.csproj --framework {framework} --configuration {configuration} --no-build"));
Target("test", DependsOn("setup-test-parallelization", "test-marten", "test-noda-time", "test-commands", "test-schema", "test-plv8", "test-codegen"));
Target("storyteller", DependsOn("compile"), () =>
Run("dotnet", $"run --framework {framework} --culture en-US", "src/Marten.Storyteller"));
Target("open_st", DependsOn("compile"), () =>
Run("dotnet", $"storyteller open --framework {framework} --culture en-US", "src/Marten.Storyteller"));
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("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("publish-docs-preview", DependsOn("docs-build"), () =>
RunNpm("run deploy"));
Target("publish-docs", DependsOn("docs-build"), () =>
RunNpm("run deploy:prod"));
Target("benchmarks", () =>
Run("dotnet", "run --project src/MartenBenchmarks --configuration Release"));
Target("recordbenchmarks", () =>
{
var profile = GetEnvironmentVariable("profile");
if (!string.IsNullOrEmpty(profile))
{
CopyDirectory("BenchmarkDotNet.Artifacts/results", InitializeDirectory($"benchmarks/{profile}"));
}
});
Target("pack", DependsOn("compile"), ForEach("./src/Marten", "./src/Marten.CommandLine", "./src/Marten.NodaTime", "./src/Marten.PLv8"), project =>
Run("dotnet", $"pack {project} -o ./artifacts --configuration Release"));
Target("init-db", () =>
{
Run("docker-compose", "up -d");
WaitForDatabaseToBeReady();
});
Target("setup-test-parallelization", () => {
if (string.IsNullOrEmpty(disableTestParallelization))
{
Console.WriteLine("disable_test_parallelization env var not set, this step is ignored.");
return;
}
var test_projects = new string[] {
"src/Marten.Testing",
"src/Marten.Schema.Testing",
"src/Marten.NodaTime.Testing"
};
foreach (var item in test_projects)
{
var assemblyInfoFile = Path.Join(item, "AssemblyInfo.cs");
File.WriteAllText(assemblyInfoFile, $"using Xunit;{Environment.NewLine}[assembly: CollectionBehavior(DisableTestParallelization = {disableTestParallelization})]");
}
});
RunTargetsAndExit(args);
}
private static void WaitForDatabaseToBeReady()
{
var attempt = 0;
while (attempt < 10)
try
{
using (var conn = new NpgsqlConnection(DockerConnectionString))
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "create extension if not exists plv8";
cmd.ExecuteNonQuery();
Console.WriteLine("Postgresql is up and ready!");
break;
}
}
catch (Exception)
{
Thread.Sleep(250);
attempt++;
}
}
private static void PublishDocs(string branchName, bool exportWithGithubProjectPrefix, string docTargetDir = "doc-target")
{
Run("git", $"clone -b {branchName} https://github.com/jasperfx/marten.git {InitializeDirectory(docTargetDir)}");
// if you are not using git --global config, un-comment the block below, update and use it
// Run("git", "config user.email user_email", docTargetDir);
// Run("git", "config user.name user_name", docTargetDir);
if (exportWithGithubProjectPrefix)
Run("dotnet", $"stdocs export {docTargetDir} ProjectWebsite -d documentation -c src -v {BUILD_VERSION} --project marten");
else
Run("dotnet", $"stdocs export {docTargetDir} Website -d documentation -c src -v {BUILD_VERSION}");
Run("git", "add --all", docTargetDir);
Run("git", $"commit -a -m \"Documentation Update for {BUILD_VERSION}\" --allow-empty", docTargetDir);
Run("git", $"push origin {branchName}", docTargetDir);
}
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 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",""), System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
return version < 5.0 ? $"netcoreapp{version.ToString("N1")}" : $"net{version.ToString("N1")}";
}
private static Action IgnoreIfFailed(Action action)
{
return () =>
{
try
{
action();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
};
}
}
}