private string ToString(string format) { if (string.IsNullOrEmpty(format)) { format = "b"; } switch (format.ToLower()) { case "b": return(CommitsSinceTag?.ToString()); case "s": return($"{CommitsSinceTag}" + $"{(string.IsNullOrEmpty(Hash) ? null : ".Sha." + Hash)}"); case "f": return ($"{CommitsSinceTag}" + $"{(string.IsNullOrEmpty(Branch) ? null : ".Branch." + Branch)}" + $"{(string.IsNullOrEmpty(Hash) ? null : ".Sha." + Hash)}" + $"{(string.IsNullOrEmpty(OtherMetadata) ? null : "." + OtherMetadata)}"); default: throw new ArgumentException("Unrecognised format", nameof(format)); } }
/// <summary> /// <para>b - Formats just the build number</para> /// <para>s - Formats the build number and the Commit Sha</para> /// <para>f - Formats the full build metadata</para> /// <para>p - Formats the padded build number. Can specify an integer for padding, default is 4. (i.e., p5)</para> /// </summary> public string ToString(string format, IFormatProvider formatProvider = null) { if (formatProvider != null) { if (formatProvider.GetFormat(GetType()) is ICustomFormatter formatter) { return(formatter.Format(format, this, formatProvider)); } } if (string.IsNullOrEmpty(format)) { format = "b"; } format = format.ToLower(); if (format.StartsWith("p", StringComparison.Ordinal)) { // Handle format var padding = 4; if (format.Length > 1) { // try to parse int p; if (int.TryParse(format.Substring(1), out p)) { padding = p; } } return(CommitsSinceTag?.ToString("D" + padding) ?? string.Empty); } return(ToString(format) .TrimStart('.')); }