-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVersion.cs
More file actions
74 lines (62 loc) · 2.52 KB
/
Copy pathVersion.cs
File metadata and controls
74 lines (62 loc) · 2.52 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
using System;
using System.Globalization;
namespace H.Necessaire
{
public class Version
{
private static readonly string[] possibleVersionPartsSeparators = new string[] { "\r", "\n", "\t", "; ", ", ", "| " };
public static readonly Version Unknown = new Version
{
Number = VersionNumber.Unknown.Clone(),
Timestamp = DateTime.MinValue,
Branch = "N/A",
Commit = "N/A",
};
public Version()
{
}
public Version(VersionNumber number, DateTime timestamp, string branch, string commit) : this()
{
this.Number = number;
this.Timestamp = timestamp;
this.Branch = branch;
this.Commit = commit;
}
public VersionNumber Number { get; set; } = VersionNumber.Unknown;
public DateTime Timestamp { get; set; }
public string Branch { get; set; }
public string Commit { get; set; }
public Version Clone()
{
return
new Version
{
Branch = this.Branch,
Commit = this.Commit,
Timestamp = this.Timestamp,
Number = this.Number?.Clone(),
};
}
public override string ToString()
{
return ToString(Environment.NewLine);
}
public string ToString(string separator)
{
return $"{Number}{separator}{Timestamp.ToUniversalTime().ToString(DataPrintingExtensions.ParsableTimeStampFormat)}{separator}{Branch}{separator}{Commit}";
}
public static Version Parse(string versionString)
{
if (string.IsNullOrWhiteSpace(versionString) || !versionString.Contains("."))
{
throw new InvalidOperationException("The given version string does not have the expected format");
}
string[] parts = versionString.Split(possibleVersionPartsSeparators, StringSplitOptions.RemoveEmptyEntries);
VersionNumber number = VersionNumber.Parse(parts[0].Trim());
DateTime timestamp = parts.Length > 1 ? DateTime.ParseExact(parts[1].Trim(), DataPrintingExtensions.ParsableTimeStampFormat, CultureInfo.InvariantCulture).EnsureUtc() : DateTime.MinValue;
string branch = parts.Length > 2 ? parts[2].Trim() : null;
string commit = parts.Length > 3 ? parts[3].Trim() : null;
return new Version(number, timestamp, branch, commit);
}
}
}