Skip to content

Commit

Permalink
Merge pull request #361 from CocoaPods/dani_semver_metadata
Browse files Browse the repository at this point in the history
[Pod::Version] Add support for build metadata
  • Loading branch information
benasher44 authored Dec 13, 2016
2 parents 98ee4f1 + 609761b commit 77dfd4a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
[Dimitris Koutsogiorgas](https://github.com/dnkoutso)
[#5180](https://github.com/CocoaPods/CocoaPods/issues/5180)

* Add support for version metadata as specified by http://semver.org/#spec-item-10.
[Danielle Tomlinson](https://github.com/dantoml)
[#6224](https://github.com/CocoaPods/CocoaPods/issues/6224)

##### Bug Fixes

* Raise an error if `inherit!` is used on an abstract target.
Expand Down
23 changes: 19 additions & 4 deletions lib/cocoapods-core/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ module Pod
# 4. 0.9
#
class Version < Pod::Vendor::Gem::Version
# Override the constants defined by the superclass to add Semantic
# Versioning prerelease support (with a dash). E.g.: 1.0.0-alpha1
# Override the constants defined by the superclass to add:
# - Semantic Versioning prerelease support (with a dash). E.g.: 1.0.0-alpha1
# - Semantic Versioning metadata support (with a +) E.g: 1.0.0+96ef7ed
#
# For more info, see: http://semver.org
#
VERSION_PATTERN = '[0-9]+(\.[0-9a-zA-Z\-]+)*'
METADATA_PATTERN = '(\+[0-9a-zA-Z\-\.]+)'
VERSION_PATTERN = "[0-9]+(\\.[0-9a-zA-Z\\-]+)*#{METADATA_PATTERN}?"
ANCHORED_VERSION_PATTERN = /\A\s*(#{VERSION_PATTERN})*\s*\z/

# @param [String,Version] version
Expand Down Expand Up @@ -86,7 +88,7 @@ def self.correct?(version)

# @!group Semantic Versioning

SEMVER_PATTERN = '[0-9]+(\.[0-9]+(\.[0-9]+(-[0-9A-Za-z\-\.]+)?)?)?'
SEMVER_PATTERN = "[0-9]+(\\.[0-9]+(\\.[0-9]+(-[0-9A-Za-z\\-\\.]+)?#{METADATA_PATTERN}?)?)?"
ANCHORED_SEMANTIC_VERSION_PATTERN = /\A\s*(#{SEMVER_PATTERN})*\s*\z/

# @return [Bool] Whether the version conforms to the Semantic Versioning
Expand Down Expand Up @@ -182,6 +184,19 @@ def <=(other)

protected

# This overrides the Gem::Version implementation of `_segments` to drop the
# metadata from comparisons as per http://semver.org/#spec-item-10
#
def _segments
# segments is lazy so it can pick up version values that come from
# old marshaled versions, which don't go through marshal_load.
# since this version object is cached in @@all, its @segments should be frozen

@segments ||= @version.sub(/#{METADATA_PATTERN}$/, '').scan(/[0-9]+|[a-z]+/i).map do |s|
/^\d+$/ =~ s ? s.to_i : s
end.freeze
end

def numeric_segments
segments.take_while { |s| s.is_a?(Numeric) }.reverse_each.drop_while { |s| s == 0 }.reverse
end
Expand Down
16 changes: 16 additions & 0 deletions spec/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ module Pod
Version.new('1.0.0-x.7.z.92').should.be.semantic
end

it 'reports a version with metadata as semantic' do
# Examples from http://semver.org/#spec-item-10
Version.new('1.0.0+5').should.be.semantic
Version.new('1.0.0+5114f85').should.be.semantic
Version.new('1.0.0-alpha+exp.sha.5114f85').should.be.semantic
end

it 'reports version with more than 3 segments not separated by a dash as non semantic' do
Version.new('1.0.2.3').should.not.be.semantic
end
Expand Down Expand Up @@ -102,6 +109,13 @@ module Pod
Version.new('1.1.0').should.be < Version.new('1.1.1')
end

it 'correctly ignores metadata in comparisons' do
Version.new('1.0.0+fff').should == Version.new('1.0.0')
Version.new('1.0.0+fff').should == Version.new('1.0.0+000')
Version.new('1.0.0-beta.1+fff').should == Version.new('1.0.0-beta.1+000')
Version.new('1.1.0+fff').should.be < Version.new('1.1.1+fff')
end

it 'ignores missing numeric identifiers while comparing' do
Version.new('1.9.0-alpha').should.be < Version.new('1.9-beta')
Version.new('2.0.0-beta').should.be < Version.new('2.0-rc')
Expand All @@ -125,6 +139,8 @@ module Pod
Version.new('1.0.0-beta.11').should.be < Version.new('1.0.0-rc.1')
Version.new('1.0.0-rc.1').should.be < Version.new('1.0.0')

Version.new('1.0.0-beta+fff').should == Version.new('1.0.0-beta+000')

# Example from CocoaPods/CocoaPods#5718
Version.new('1.0-beta.8').should.be < Version.new('1.0-beta.8a')
end
Expand Down

0 comments on commit 77dfd4a

Please sign in to comment.