Sha256: 1dc5e6b6cc5c1608216b9e490ff9906cb38c9b0de42219393248c01dfe138ee1

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module PmdTester
  # Utility to deal with semantic versions
  class Semver
    def self.compare(version_a, version_b)
      result = internal_compare(version_a, version_b)
      PmdTester.logger.debug "  result: #{result}"
      result
    end

    private_class_method def self.internal_compare(version_a, version_b)
      PmdTester.logger.debug "Comparing #{version_a} <=> #{version_b}"
      m = /(\d+)\.(\d+)\.(\d+)(.*)/.match(version_a)
      a_major = m[1].to_i
      a_minor = m[2].to_i
      a_patch = m[3].to_i
      a_snapshot = m[4]
      PmdTester.logger.debug "  a_major: #{a_major} a_minor: #{a_minor} a_patch: #{a_patch} a_snapshot: #{a_snapshot}"

      m = /(\d+)\.(\d+)\.(\d+)(.*)/.match(version_b)
      b_major = m[1].to_i
      b_minor = m[2].to_i
      b_patch = m[3].to_i
      b_snapshot = m[4]
      PmdTester.logger.debug "  b_major: #{b_major} b_minor: #{b_minor} b_patch: #{b_patch} b_snapshot: #{b_snapshot}"

      return a_major <=> b_major if a_major != b_major
      return a_minor <=> b_minor if a_minor != b_minor
      return a_patch <=> b_patch if a_patch != b_patch

      compare_snapshots(a_snapshot, b_snapshot)
    end

    private_class_method def self.compare_snapshots(a_snapshot, b_snapshot)
      return -1 if a_snapshot == '-SNAPSHOT' && b_snapshot == ''
      return 1 if a_snapshot == '' && b_snapshot == '-SNAPSHOT'

      a_snapshot <=> b_snapshot
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pmdtester-1.5.5 lib/pmdtester/semver.rb
pmdtester-1.5.4 lib/pmdtester/semver.rb
pmdtester-1.5.3 lib/pmdtester/semver.rb
pmdtester-1.5.2 lib/pmdtester/semver.rb
pmdtester-1.5.1 lib/pmdtester/semver.rb
pmdtester-1.5.0 lib/pmdtester/semver.rb