-
Notifications
You must be signed in to change notification settings - Fork 46
/
lib.cake
162 lines (140 loc) · 4.67 KB
/
lib.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
public void DefaultClean()
{
var binDirectories = GetFiles("**/*.*proj")
.Select(csproj => csproj.GetDirectory().Combine("bin"))
.Where(binDirectory => DirectoryExists(binDirectory))
.ToList();
if (binDirectories.Any())
{
Information("Deleting bin directories:");
foreach (var binDirectory in binDirectories)
{
for (var attempt = 1;; attempt++)
{
Information(binDirectory);
try
{
DeleteDirectory(binDirectory, new DeleteDirectorySettings { Recursive = true });
break;
}
catch (IOException ex) when (attempt < 3 && (WinErrorCode)ex.HResult == WinErrorCode.DirNotEmpty)
{
Information("Another process added files to the directory while its contents were being deleted. Retrying...");
}
}
}
}
else
{
Information("No bin directories to delete.");
}
}
private enum WinErrorCode : ushort
{
DirNotEmpty = 145
}
public static string GetCISuffix(CISettings settings)
{
if (settings.PullRequestNumber != null)
{
return $"{settings.BuildNumber}.pr.{settings.PullRequestNumber.Value}";
}
if ("master".Equals(settings.Branch, StringComparison.OrdinalIgnoreCase))
{
return $"ci.{settings.BuildNumber}";
}
if (settings.Branch == null) return settings.BuildNumber.ToString();
var builder = (StringBuilder)null;
var separate = true;
if (settings.Branch != null)
{
foreach (var c in settings.Branch)
{
if (IsValidSemVerLabelChar(c))
{
if (separate)
{
if (builder == null)
builder = new StringBuilder().Append(settings.BuildNumber).Append('.');
else
builder.Append('-');
separate = false;
}
builder.Append(c);
if (builder.Length == 20) break; // NuGet prerelease version length limit
}
else
{
separate = true;
}
}
}
return builder?.ToString() ?? settings.BuildNumber.ToString();
bool IsValidSemVerLabelChar(char value)
{
// https://semver.org/#spec-item-9
return value == '-'
|| ('0' <= value && value <= '9')
|| ('A' <= value && value <= 'Z')
|| ('a' <= value && value <= 'z');
}
}
public static string ChangeVersionSuffix(string version, string prereleaseLabel)
{
if (string.IsNullOrEmpty(version) || !char.IsDigit(version[0]) || !char.IsLetterOrDigit(version[version.Length - 1]))
{
throw new ArgumentException("Version must begin with a digit and end with a digit or letter.", nameof(version));
}
var suffixIndex = version.IndexOf('-');
if (suffixIndex == -1) suffixIndex = version.IndexOf('+');
if (string.IsNullOrEmpty(prereleaseLabel))
{
return suffixIndex == -1
? version
: version.Substring(0, suffixIndex);
}
if (!char.IsLetterOrDigit(prereleaseLabel[0]) || !char.IsLetterOrDigit(prereleaseLabel[prereleaseLabel.Length - 1]))
{
throw new ArgumentException("Prerelease label must be null or empty or must begin and end with a digit or letter.", nameof(prereleaseLabel));
}
return suffixIndex == -1
? version + '-' + prereleaseLabel
: version.Substring(0, suffixIndex + 1) + prereleaseLabel;
}
public bool TryReadFromCsproj(FilePath filePath, out string version)
{
version = XmlPeek(filePath, "/Project/PropertyGroup/Version")?.Trim();
if (!string.IsNullOrEmpty(version)) return true;
version = null;
return false;
}
public CISettings? TryDetectCISettings()
{
if (AppVeyor.IsRunningOnAppVeyor)
{
var env = AppVeyor.Environment;
return env.PullRequest.IsPullRequest
? new CISettings(env.Build.Number, env.PullRequest.Number)
: new CISettings(env.Build.Number, env.Repository.Branch);
}
return null;
}
public readonly struct CISettings
{
public int BuildNumber { get; }
public string Branch { get; }
public int? PullRequestNumber { get; }
public bool IsPullRequest => PullRequestNumber != null;
public CISettings(int buildNumber, string branch)
{
BuildNumber = buildNumber;
Branch = branch;
PullRequestNumber = null;
}
public CISettings(int buildNumber, int pullRequestNumber)
{
BuildNumber = buildNumber;
Branch = null;
PullRequestNumber = pullRequestNumber;
}
}