-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathutils.cake
More file actions
330 lines (276 loc) · 9.98 KB
/
utils.cake
File metadata and controls
330 lines (276 loc) · 9.98 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
using System.Text.RegularExpressions;
private void WriteImportOfVersionInfoToMsBuildFile()
{
WriteToMsBuildXml("./src/directory.build.props", root => {
root.AddFirst(
new XElement("Import",
new XAttribute("Project", "$(MSBuildThisFileDirectory)versioninfo.props"),
new XAttribute("Condition", "exists('$(MSBuildThisFileDirectory)versioninfo.props')")));
});
}
private void WriteToMsBuildXml(FilePath file, Action<XElement> add)
{
XElement root;
if (!FileExists(file))
{
Information("File not found, creating new");
root = new XElement(ns + "Project");
}
else
{
Information("Using existing file");
root = XElement.Load(file.FullPath);
}
add(root);
root.Save(file.FullPath, SaveOptions.None);
}
private FilePath GetVersionInfoFile()
{
return File("./src/versioninfo.props");
}
private void WriteVersionInformationToMsBuildFile(GitVersion version, FilePath changelogFile)
{
var log = ChangeLog.Parse(changelogFile.FullPath);
var releaseNotes = log?.GetVersion(version.MajorMinorPatch)?.Body;
// Ignore if version is prerelease
if (string.IsNullOrEmpty(releaseNotes))
{
if (string.IsNullOrEmpty(version.PreReleaseTag))
{
throw new Exception($"Release notes for version {version.MajorMinorPatch} is missing");
}
Warning($"Missing release notes for version {version.MajorMinorPatch} but ignoring it because this is a prelease version");
}
var file = GetVersionInfoFile();
WriteToMsBuildXml(file, root => {
Information($"Writing version information [{version.NuGetVersionV2}] to {file}");
root.Descendants(ns+"PropertyGroup").Remove();
var pg = new XElement(ns + "PropertyGroup");
pg.Add(new XElement(ns + "Version", version.AssemblySemFileVer));
pg.Add(new XElement(ns + "FileVersion", version.AssemblySemFileVer));
pg.Add(new XElement(ns + "AssemblyVersion", version.AssemblySemVer));
pg.Add(new XElement(ns + "InformationalVersion", version.InformationalVersion));
pg.Add(new XElement(ns + "PackageReleaseNotes", releaseNotes));
root.Add(pg);
});
}
private class ChangeLog
{
// patterns
private static Regex semver = new Regex(@"## ?\[?v?([\w\d.-]+\.[\w\d.-]+[a-zA-Z0-9])?");
private static Regex date = new Regex(@".*[ ](\d\d?\d?\d[-/.]\d\d?[-/.]\d\d?\d?\d?).*");
private static string EOL = Environment.NewLine;
public string Title {get;set;}
public string Description {get;set;}
public List<VersionInfo> Versions {get;} = new List<VersionInfo>();
public VersionInfo GetVersion(string version)
{
return Versions.FirstOrDefault(ver => version.Equals(ver.Version));
}
public override string ToString()
{
var str = $"Title: {Title}\nDescription: {Description}\nVersions:\n";
foreach (var ver in Versions)
{
str += ver.ToString() + "\n";
}
return str;
}
public static ChangeLog Parse(string filename)
{
var input = System.IO.File.ReadAllLines(filename);
return Parse(input);
}
public static ChangeLog Parse(IEnumerable<string> lines)
{
var log = new ChangeLog();
VersionInfo current = null;
string activeSubhead = null;
foreach (var line in lines)
{
log.HandleLine(line, ref current, ref activeSubhead);
}
// push last version into log
log.AddVersion(current);
// clean up description
log.Description = Clean(log.Description);
if (log.Description == string.Empty)
{
log.Description = null;
}
return log;
}
private void HandleLine(string line, ref VersionInfo current, ref string activeSubhead)
{
// skip line if it's a link label
if (Regex.IsMatch(line, @"^\[[^[\]]*\] *?:"))
{
return;
}
// set title if it's there
if (Title == null && Regex.IsMatch(line, "^# ?[^#]"))
{
Title = line.Substring(1).Trim();
return;
}
// new version found!
if (Regex.IsMatch(line, "^## ?[^#]"))
{
if (current?.Title != null)
{
AddVersion(current);
}
current = new VersionInfo();
activeSubhead = null;
var match = semver.Match(line);
if (match.Success)
{
current.Version = match.Groups[1].Value;
}
current.Title = line.Substring(2).Trim();
match = date.Match(current.Title);
if (current.Title != null && match.Success)
{
current.Date = match.Groups[1].Value;
}
return;
}
// deal with body or description content
if (current != null)
{
current.HandleLine(line, ref activeSubhead);
}
else
{
Description = (Description ?? string.Empty) + line + EOL;
}
}
private void AddVersion(VersionInfo current)
{
if (current != null)
{
current.Body = Clean(current.Body);
Versions.Add(current);
}
}
private static string Clean(string str)
{
if (str == null)
{
return string.Empty;
}
str = str.Trim();
str = Regex.Replace(str, "^[" + EOL + "]*", string.Empty);
str = Regex.Replace(str, "[" + EOL + "]*$", string.Empty);
return str;
}
}
private class VersionInfo
{
// patterns
private static Regex subhead = new Regex("^###");
private static Regex listitem = new Regex("^[*-]");
public string Version {get;set;}
public string Title {get;set;}
public string Date {get;set;}
public string Body {get;set;} = string.Empty;
public Dictionary<string, List<string>> Parsed {get;} = new Dictionary<string, List<string>> {
{ "_", new List<string>() }
};
public override string ToString()
{
var str = $" Version: {Version}\n Title: {Title}\n Date: {Date}\n Body: {Body}\n";
foreach (var key in Parsed.Keys)
{
str += $" {key}:\n";
foreach (var line in Parsed[key])
{
str += $" {line}\n";
}
}
return str;
}
public void HandleLine(string line, ref string activeSubhead)
{
Body += line + "\n";
// handle case where current line is a 'subhead':
// - 'handleize' subhead.
// - add subhead to 'parsed' data if not already present.
var match = subhead.Match(line);
if (match.Success)
{
var key = line.Replace("###", string.Empty).Trim();
if (!Parsed.ContainsKey(key))
{
Parsed[key] = new List<string>();
activeSubhead = key;
}
}
// handle case where current line is a 'list item':
match = listitem.Match(line);
if (match.Success)
{
line = RemoveMarkdown(line);
// add line to 'catch all' array
Parsed["_"].Add(line);
// add line to 'active subhead' if applicable (eg. 'Added', 'Changed', etc.)
if (activeSubhead != null)
{
Parsed[activeSubhead].Add(line);
}
}
}
}
private static string RemoveMarkdown(string str, string listUnicodeChar = null, bool stripListLeaders = true, bool gfm = true, bool useImgAltText = true)
{
var output = str ?? string.Empty;
// Remove horizontal rules (stripListLeaders conflict with this rule, which is why it has been moved to the top)
output = Regex.Replace(output, @"(?m)^(-\s*?|\*\s*?|_\s*?){3,}\s*$", string.Empty);
if (stripListLeaders)
{
var replacement = "$1";
if (listUnicodeChar != null && listUnicodeChar != string.Empty)
{
replacement = $"{listUnicodeChar} $1";
}
output = Regex.Replace(output, @"(?m)^([\s\t]*)([\*\-\+]|\d+\.)\s+", replacement);
}
if (gfm)
{
// Header
output = Regex.Replace(output, @"\n={2,}", "\n");
// Fenced codeblocks
output = Regex.Replace(output, @"~{3}.*\n", string.Empty);
// Strikethrough
output = Regex.Replace(output, "~~", string.Empty);
// Fenced codeblocks
output = Regex.Replace(output, @"`{3}.*\n", string.Empty);
}
// Remove HTML tags
output = Regex.Replace(output, @"<[^>]*>", string.Empty);
// Remove setext-style headers
output = Regex.Replace(output, @"^[=\-]{2,}\s*$", string.Empty);
// Remove footnotes?
output = Regex.Replace(output, @"\[\^.+?\](\: .*?$)?", string.Empty);
output = Regex.Replace(output, @"\s{0,2}\[.*?\]: .*?$", string.Empty);
// Remove images
output = Regex.Replace(output, @"\!\[(.*?)\][\[\(].*?[\]\)]", useImgAltText ? "$1" : string.Empty);
// Remove inline links
output = Regex.Replace(output, @"\[(.*?)\][\[\(].*?[\]\)]", "$1");
// Remove blockquotes
output = Regex.Replace(output, @"^\s{0,3}>\s?", string.Empty);
// Remove reference-style links?
output = Regex.Replace(output, @"^\s{1,2}\[(.*?)\]: (\S+)( "".*?"")?\s*$", string.Empty);
// Remove atx-style headers
output = Regex.Replace(output, @"(?m)^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$", "$1$2$3");
// Remove emphasis (repeat the line to remove double emphasis)
output = Regex.Replace(output, @"([\*_]{1,3})(\S.*?\S{0,1})\1", "$2");
output = Regex.Replace(output, @"([\*_]{1,3})(\S.*?\S{0,1})\1", "$2");
// Remove code blocks
output = Regex.Replace(output, @"(?m)(`{3,})(.*?)\1", "$2");
// Remove inline code
output = Regex.Replace(output, @"`(.+?)`", "$1");
// Replace two or more newlines with exactly two? Not entirely sure this belongs here...
output = Regex.Replace(output, @"\n{2,}", "\n\n");
return output;
}