-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeLogVersioningTask.cs
More file actions
90 lines (75 loc) · 2.93 KB
/
ChangeLogVersioningTask.cs
File metadata and controls
90 lines (75 loc) · 2.93 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
using System;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;
namespace ChangeLogVersioningTask
{
// ReSharper disable once UnusedMember.Global -- Exposed as MSBuild task
public class ChangeLogVersioningTask : Microsoft.Build.Utilities.Task
{
public bool CrashOnFailure { get; set; }
public string ChangeLogDirectory { get; set; }
public string ChangeLogFileName { get; set; }
[Output]
public string VersionPrefix { get; set; }
protected Regex VersionMatchRegex = new Regex("(?:\\[)(?<Version>\\d+\\.\\d+\\.\\d+)(?:\\])",
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
public override bool Execute()
{
if (string.IsNullOrWhiteSpace(ChangeLogFileName))
{
LogDependingOnParameter("Parameter ChangeLogFileName is not set");
return !CrashOnFailure;
}
if (string.IsNullOrWhiteSpace(ChangeLogDirectory))
{
LogDependingOnParameter("Failed to get project directory");
return !CrashOnFailure;
}
var changeLogFullPath = Path.Combine(ChangeLogDirectory, ChangeLogFileName);
if (!File.Exists(changeLogFullPath))
{
LogDependingOnParameter($"Could not find changelog file at {changeLogFullPath}");
return !CrashOnFailure;
}
string versionString = null;
using (var changeLogFile = File.OpenRead(changeLogFullPath))
{
using (var changeLogStream = new StreamReader(changeLogFile))
{
while (!changeLogStream.EndOfStream)
{
var currentLine = changeLogStream.ReadLine();
if (currentLine is null)
continue;
var match = VersionMatchRegex.Match(currentLine);
if (match.Success)
{
versionString = match.Groups["Version"].Value;
break;
}
}
}
}
if (versionString is null)
{
LogDependingOnParameter($"Failed to find version string in {changeLogFullPath}");
return !CrashOnFailure;
}
VersionPrefix = versionString;
Log.LogMessage(MessageImportance.High, $"First version found in {changeLogFullPath} is {versionString}");
return true;
}
private void LogDependingOnParameter(string message)
{
if (CrashOnFailure)
{
Log.LogError(message);
}
else
{
Log.LogMessage(MessageImportance.High, message);
}
}
}
}