-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.cake
245 lines (200 loc) · 6.14 KB
/
utilities.cake
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
#r "Library/NuGet/YamlDotNet.3.6.0/lib/net35/YamlDotNet.dll"
using YamlDotNet.Serialization;
using System.Text.RegularExpressions;
public T GetBuildConfiguration<T>() where T : new()
{
var workingDirectorySegments = Context.Environment.WorkingDirectory.Segments;
var workingDirectoryName = workingDirectorySegments[workingDirectorySegments.Length - 1];
var configFile = (new [] { "build.yml", String.Format("../{0}.build.yml", workingDirectoryName) })
.FirstOrDefault(System.IO.File.Exists);
if (configFile == null)
{
return new T();
}
return new Deserializer(ignoreUnmatched: true).Deserialize<T>(new StreamReader(configFile));
}
public string GetSolution()
{
var solutions = System.IO.Directory.GetFiles(Context.Environment.WorkingDirectory.FullPath, "*.sln");
if (solutions.Length == 1)
{
return solutions[0];
}
else
{
if (solutions.Length == 0)
{
throw new Exception("No solution found.");
}
else
{
throw new Exception("Multiple solutions found.");
}
}
}
public string Which(string executable)
{
char[] seperators = { System.IO.Path.PathSeparator };
var envPath = Environment.GetEnvironmentVariable("PATH");
var envPathExt = Environment.GetEnvironmentVariable("PATHEXT");
var paths = envPath == null ?
new string[0] :
envPath.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
var pathExts = envPathExt == null ?
new string[0] :
envPathExt.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
foreach (var path in paths)
{
var testPath = System.IO.Path.Combine(path, executable);
/* We test the extensionful version first since it's not uncommon for multiplatform programs to ship with a
* Unix executable without an extension in the same directory as a Windows extension with an extension such as
* .cmd, .bat. In those cases trying to execute the extensionless version will fail on Windows.
*/
foreach (var pathExt in pathExts)
{
var testPathExt = System.IO.Path.Combine(path, executable) + pathExt;
if (FileExists(testPathExt))
{
return testPathExt;
}
}
if (FileExists(testPath))
{
return testPath;
}
}
return null;
}
public string GetGitRevision(bool useShort)
{
var git = Which("git");
if (git != null)
{
IEnumerable<string> output;
var shortOption = useShort ? "--short" : "";
StartProcess(git,
new ProcessSettings { RedirectStandardOutput = true, Arguments = $"rev-parse {shortOption} HEAD"},
out output
);
var outputList = output.ToList();
if (outputList.Count == 1)
{
return outputList[0];
}
else
{
throw new Exception("Could not read revision from git");
}
}
return null;
}
public SemVer GetVersion()
{
return GetChangeLog().LatestVersion;
}
public ChangeLog GetChangeLog()
{
return new ChangeLog($"CHANGES.md");
}
public sealed class ChangeLog
{
private static readonly Regex VersionPattern = new Regex(@"^## v(?<version>.+)$", RegexOptions.Compiled);
public SemVer LatestVersion { get; }
public string LatestChanges { get; }
public ChangeLog(string path)
{
var lines = System.IO.File.ReadAllLines(path);
var latestChanges = new List<string>();
if (lines.Any())
{
var versionMatch = VersionPattern.Match(lines[0]);
if (versionMatch.Success)
{
LatestVersion = new SemVer(versionMatch.Groups["version"].Value);
}
else
{
throw new Exception("ChangeLog is in incorrect format.");
}
foreach (var line in lines.Skip(1))
{
if (!VersionPattern.IsMatch(line))
{
latestChanges.Add(line);
}
else
{
break;
}
}
LatestChanges = string.Join("\n", latestChanges.ToArray());
}
else
{
throw new Exception("ChangeLog is empty.");
}
}
}
public sealed class SemVer
{
private static readonly Regex Pattern = new Regex(
@"^(?<major>[1-9]\d*?|0)\.(?<minor>[1-9]\d*?|0)\.(?<patch>[1-9]\d*?|0)(?:-(?<pre>[\dA-Z-]+))?(?:\+(?<build>[\dA-Z-]+))?$",
RegexOptions.IgnoreCase | RegexOptions.Compiled
);
private string _string;
public uint Major { get; }
public uint Minor { get; }
public uint Patch { get; }
public string Pre { get; }
public string Build { get; }
public SemVer(string s)
{
_string = s.Trim();
var match = Pattern.Match(_string);
if (match.Success)
{
Major = uint.Parse(match.Groups["major"].Value);
Minor = uint.Parse(match.Groups["minor"].Value);
Patch = uint.Parse(match.Groups["patch"].Value);
var preGroup = match.Groups["pre"];
if (preGroup.Success)
{
Pre = preGroup.Value;
}
var buildGroup = match.Groups["build"];
if (buildGroup.Success)
{
Build = buildGroup.Value;
}
}
else
{
throw new FormatException($"Unable to parse semantic version: {_string}");
}
}
public SemVer(uint major = 0, uint minor = 0, uint patch = 0, string pre = null, string build = null)
{
Major = major;
Minor = minor;
Patch = patch;
Pre = pre;
Build = build;
_string = $"{Major}.{Minor}.{Patch}";
if (pre != null)
{
_string += $"-{Pre}";
}
if (build != null)
{
_string += $"+{Build}";
}
}
public override string ToString()
{
return _string;
}
public static implicit operator string(SemVer version)
{
return version.ToString();
}
}