# frozen_string_literal: true class RenuoVersion SEMVER_SCHEMA = /^\d+\.\d+\.\d+(\.[a-z]+\d+)?$/ SEGMENTS = %w[major minor patch].freeze def self.create(str) raise "Invalid segments (#{str} =~ #{SEMVER_SCHEMA})" unless SEMVER_SCHEMA.match?(str) new(*str.split('.')) end attr_reader :segments def initialize(major, minor, patch, prerelease = nil) @segments = [major, minor, patch].map(&:to_i) @segments.push(prerelease.to_s) if prerelease end def bump(segment) raise "Invalid segment #{segment} (must be one of #{SEGMENTS})" unless SEGMENTS.include?(segment) send(:"bump_#{segment}") self end # Returns true if it's a release candidate (e.g. 3.0.0.rc4) def prerelease? @segments.length > 3 end def to_s @segments.join('.') end private def bump_major @segments = [@segments[0] + 1, 0, 0] end def bump_minor @segments = [@segments[0], @segments[1] + 1, 0] end def bump_patch if prerelease? old_number = @segments[3][/\d+/] @segments[3] = @segments[3].gsub(old_number, (old_number.to_i + 1).to_s) else # default patch @segments = [*@segments[0..1], @segments[2] + 1] end end end