public static BumpyVersion Increment(BumpyVersion version, int position, bool cascade) { var numbers = version.Numbers; var digits = version.Digits; position.ThrowIfOutOfRange(n => n <1 || n> numbers.Count, nameof(position), $"Position must be between 1 and {numbers.Count}"); var zeroBasedIndex = checked (position - 1); numbers[zeroBasedIndex]++; var digitCount = numbers[zeroBasedIndex].ToString().Length; digits[zeroBasedIndex] = Math.Max(digits[zeroBasedIndex], digitCount); if (cascade) { for (int i = position; i < numbers.Count; i++) { numbers[i] = 0; digits[i] = 1; } } return(new BumpyVersion(numbers, digits, version.Label, version.NumberDelimiter)); }
public static string EnsureExpectedVersion(string text, string regexPattern, BumpyVersion expectedVersion) { if (TryParseVersionInText(text, regexPattern, out var version, out _)) { if (version.Equals(expectedVersion)) { return(text); } } throw new InvalidOperationException($"The provided version '{expectedVersion}' cannot be captured in the text '{text.Trim()}' using the regex '{regexPattern}'. Please correct either the version or the regex"); }
public static bool TryParseVersionInText(string text, string regexPattern, out BumpyVersion version, out string marker) { version = null; var regex = new Regex(regexPattern, RegexOptions.Singleline); var match = regex.Match(text); var versionGroup = match.Groups[VersionGroupName]; var markerGroup = match.Groups[MarkerGroupName]; var success = versionGroup.Success; if (success) { version = ParseVersion(versionGroup.Value); } marker = markerGroup.Success ? markerGroup.Value : string.Empty; return(success); }
public static BumpyVersion Assign(BumpyVersion version, int position, string formattedNumber) { if (!int.TryParse(formattedNumber, out int number)) { throw new ArgumentException($"Expected '{formattedNumber}' to be a number"); } var numbers = version.Numbers; var digits = version.Digits; position.ThrowIfOutOfRange(n => n <1 || n> numbers.Count, nameof(position), $"Position must be between 1 and {numbers.Count}"); number.ThrowIfOutOfRange(n => n < 0, nameof(formattedNumber), "Number cannot be negative"); var zeroBasedIndex = checked (position - 1); numbers[zeroBasedIndex] = number; digits[zeroBasedIndex] = formattedNumber.Length; return(new BumpyVersion(numbers, digits, version.Label, version.NumberDelimiter)); }
public static BumpyVersion Label(BumpyVersion version, string versionLabel) { return(new BumpyVersion(version.Numbers, version.Digits, versionLabel, version.NumberDelimiter)); }