semvruler 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e551a21ff24c378974f06f05b6a356beda50c5b5671ea5b0bd69ccbf31a86c96
4
+ data.tar.gz: 90a0d2bbcdc832cb3ace11b30fef608e324b54a537ce988089231bf39ff17a26
5
+ SHA512:
6
+ metadata.gz: b542d08f2d4bd2483ad9eedef974fb11409309ebdb2adf0a41877232ebbeb81a8228f2a2d551179b457e027e9379a9755fb510d1d7497082f56548c1f8943c64
7
+ data.tar.gz: 429d14e8e8729e35f88579aa71918096bdb55c838ab823728e077f3aab4c6cf79b6490063b95efd8939e56de4608e8cf6ce1e308b2e39047c7e0519ca9c27ecc
@@ -0,0 +1,13 @@
1
+ version: 2.1
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: ruby:2.7.0
6
+ steps:
7
+ - checkout
8
+ - run:
9
+ name: Run the default task
10
+ command: |
11
+ gem install bundler -v 2.2.19
12
+ bundle install
13
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: single_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Metrics/BlockLength:
13
+ Enabled: false
14
+
15
+ Style/Documentation:
16
+ Enabled: false
17
+
18
+ Lint/MixedRegexpCaptureTypes:
19
+ Enabled: false
20
+
21
+ Layout/LineLength:
22
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.1] - 2021-07-04
4
+ ### Added
5
+ - Version parsing and destructuring
6
+ - Rules parsing & modification
7
+ - Version comparison and matching
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in semvruler.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Semvruler
2
+
3
+ Provides some utility classes to read, compare and match [semantic versions](https://semver.org/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'semvruler'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install semvruler
20
+
21
+ ## Usage
22
+
23
+ ### Creating versions
24
+
25
+ ```ruby
26
+
27
+ # Creates and destructure version string
28
+ version = Semvruler.version('2.2.5-pr.0')
29
+
30
+ version.major # => 2
31
+ version.minor # => 2
32
+ version.patch # => 5
33
+ version.build # => nil
34
+ version.prerelease # => ['pr','0']
35
+
36
+ # You can put back together the original string
37
+ version.to_s # => '2.2.5-pr.0'
38
+
39
+ # Returns an array when multiple versions passed
40
+ versions = Semvruler.versions(['3.4.5', '2.2.5', '1.1.1', '1.1.1-pr.0'])
41
+ ```
42
+
43
+ ### Comparable versions
44
+
45
+ Versions implement ruby's comparable interface and '~>' operator as well.
46
+
47
+ ```ruby
48
+ versions = Semvruler.versions(['3.4.5', '2.2.5', '1.1.1', '1.1.1-pr.0'])
49
+ versions.sort # => ["1.1.1-pr.0", "1.1.1", "2.2.5", "3.4.5"]
50
+
51
+ ver1 = version[0]
52
+ ver2 = version[1]
53
+
54
+ ver1 != ver2 # => true
55
+ ver1 == ver2 # => false
56
+ ver1 > ver2 # => true
57
+ ver1 >= ver2 # => true
58
+ ver1 < ver2 # => false
59
+ ver1 <= ver2 # => false
60
+ ```
61
+
62
+ ### Rules instantiation
63
+
64
+ ```ruby
65
+ rule = Semvruler.rule('~> 2.0.2')
66
+ rule.to_s # => '~> 2.0.2'
67
+
68
+ # Rules can be adjusted
69
+ rule.add('< 10.0.1')
70
+ rule.remove('< 10.0.1')
71
+ rule.merge(rule2)
72
+ ```
73
+
74
+ ### Rules Matching
75
+
76
+ ```ruby
77
+ rule = Semvruler.rule('!= 2.0.2')
78
+ rule.match?('2.0.2') # => false
79
+ rule.match?('2.3.2') # => true
80
+
81
+ # Rules respond to to_proc
82
+ rule = Semvruler.rule('~> 2.0.2')
83
+ ['3.2.1', '1.2.3', '2.1.1'].find(&rule) # => '2.1.1'
84
+ ['3.2.1', '1.2.3', '2.1.1'].select(&rule) # => ['2.1.1']
85
+ ['3.2.1', '1.2.3', '2.1.1'].reject(&rule) # => ['3.2.1', '1.2.3']
86
+ ```
87
+
88
+ ## Development
89
+
90
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
91
+
92
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nika-kirosh/semvruler
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'semvruler'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require 'pry'
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/semvruler.rb ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+ require_relative 'semvruler/version'
5
+ require_relative 'semvruler/order'
6
+ require_relative 'semvruler/semversion'
7
+ require_relative 'semvruler/constraint'
8
+ require_relative 'semvruler/rule'
9
+
10
+ module Semvruler
11
+ class Error < StandardError; end
12
+
13
+ class << self
14
+ %i[versions version].each do |method_name|
15
+ define_method(method_name) do |value|
16
+ Semversion.parse(value)
17
+ end
18
+ end
19
+
20
+ def rule(*value)
21
+ Rule.parse(value)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Semvruler
4
+ class Constraint
5
+ FORMAT = /^((?<type>=|!=|~>|>=|>|<=|<)\s*)?(?<version>.*)$/.freeze
6
+
7
+ class FormatError < StandardError
8
+ def initialize(msg = 'Invalid format for version')
9
+ super
10
+ end
11
+ end
12
+
13
+ class << self
14
+ def parse(value, safe: true)
15
+ if value.respond_to?(:map)
16
+ value.map { |item| cast(item, safe) }
17
+ else
18
+ cast(value, safe)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def cast(value, safe)
25
+ capture = FORMAT.match(value)
26
+ assert_format(capture, safe)
27
+ end
28
+
29
+ def assert_format(capture, safe)
30
+ version = Semversion.parse(capture[:version], safe: safe) if capture
31
+ if capture && version
32
+ new(capture[:type], version)
33
+ else
34
+ raise FormatError unless safe
35
+
36
+ nil
37
+ end
38
+ end
39
+ end
40
+
41
+ attr_reader :version
42
+
43
+ def to_s
44
+ "#{type_as_string}#{version}"
45
+ end
46
+
47
+ def initialize(type, version)
48
+ @type = type
49
+ @version = version
50
+ end
51
+
52
+ def type
53
+ @type || '='
54
+ end
55
+
56
+ def match?(other)
57
+ other = Semversion.parse(other, safe: false)
58
+ other.send(operation, version)
59
+ end
60
+
61
+ protected
62
+
63
+ def operation
64
+ type == '=' ? '==' : type
65
+ end
66
+
67
+ private
68
+
69
+ def type_as_string
70
+ "#{@type} " if @type
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Semvruler
4
+ class Order
5
+ def initialize(*chain)
6
+ @chain = chain
7
+ end
8
+
9
+ def <=>(other)
10
+ index = (0..2).find { |idx| compare_at(other, idx) != 0 }
11
+ return compare_at(other, index) if index
12
+ return compare_missed_lenght(other) if compare_missed_lenght(other) != 0
13
+
14
+ index = (3..[size, other.size].max).find { |idx| compare_at(other, idx) != 0 }
15
+ return compare_at(other, index) if index
16
+
17
+ no_difference_found
18
+ end
19
+
20
+ protected
21
+
22
+ attr_reader :chain
23
+
24
+ def no_difference_found
25
+ 0
26
+ end
27
+
28
+ def compare_missed_lenght(other)
29
+ if size == 3 || other.size == 3
30
+ (size <=> other.size) * -1
31
+ else
32
+ no_difference_found
33
+ end
34
+ end
35
+
36
+ def compare_at(other, idx)
37
+ is_numeric = int_at?(idx) || other.int_at?(idx)
38
+
39
+ current = is_numeric ? read_int_at(idx) : self[idx].to_s
40
+ nxt = is_numeric ? other.read_int_at(idx) : other[idx].to_s
41
+
42
+ current <=> nxt
43
+ end
44
+
45
+ def [](idx)
46
+ chain[idx]
47
+ end
48
+
49
+ def read_int_at(idx)
50
+ if int_at?(idx)
51
+ self[idx].to_i
52
+ else
53
+ self[idx].nil? ? -1 : Float::INFINITY
54
+ end
55
+ end
56
+
57
+ def int_at?(idx)
58
+ /^\d+/.match?(self[idx].to_s)
59
+ end
60
+
61
+ def size
62
+ chain.size
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Semvruler
4
+ class Rule
5
+ class << self
6
+ def parse(value)
7
+ constraints = Array(Constraint.parse(value, safe: false))
8
+ new(constraints)
9
+ end
10
+ end
11
+
12
+ def initialize(constraints)
13
+ @constraints = Hash[constraints.map { |c| [c.to_s, c] }]
14
+ end
15
+
16
+ def [](idx)
17
+ constraints[idx]
18
+ end
19
+
20
+ def size
21
+ constraints.size
22
+ end
23
+
24
+ def add(value)
25
+ constraint = Constraint.parse(value)
26
+ constraints[constraint.to_s] = constraint
27
+ end
28
+
29
+ def remove(value)
30
+ constraint = Constraint.parse(value)
31
+ constraints.delete(constraint.to_s)
32
+ end
33
+
34
+ def merge(other)
35
+ new_constraints = [*constraints.values, *other.constraints.values]
36
+ self.class.new(new_constraints)
37
+ end
38
+
39
+ def match?(version)
40
+ constraints.values.all? { |c| c.match?(version) }
41
+ end
42
+
43
+ def to_proc
44
+ ->(version) { match?(version) }
45
+ end
46
+
47
+ protected
48
+
49
+ attr_reader :constraints
50
+ end
51
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Semvruler
4
+ class Semversion
5
+ include Comparable
6
+
7
+ FORMAT = /
8
+ ^
9
+ (?<major>0|[1-9]\d*)\.
10
+ (?<minor>0|[1-9]\d*)
11
+ (\.(?<patch>0|[1-9]\d*))?
12
+ (-(?<prerelease>(0|[1-9]\d*|\d*[a-z-][0-9a-z-]*)(\.(0|[1-9]\d*|\d*[a-z-][0-9a-z-]*))*))?
13
+ (\+(?<build>[0-9a-z-]+(\.[0-9a-z-]+)*))?
14
+ $
15
+ /xi.freeze
16
+
17
+ class FormatError < StandardError
18
+ def initialize(msg = 'Invalid format for version')
19
+ super
20
+ end
21
+ end
22
+
23
+ class << self
24
+ def parse(value, safe: true)
25
+ if value.respond_to?(:map)
26
+ value.map { |item| cast(item, safe) }
27
+ else
28
+ cast(value, safe)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def cast(value, safe)
35
+ captures = FORMAT.match(value)
36
+
37
+ if captures
38
+ new(captures)
39
+ else
40
+ raise FormatError unless safe
41
+
42
+ nil
43
+ end
44
+ end
45
+ end
46
+
47
+ attr_reader :major, :minor, :patch, :prerelease, :build
48
+
49
+ def to_s
50
+ "#{core_as_string}#{prerelease_as_string}#{build_as_string}"
51
+ end
52
+
53
+ def initialize(format)
54
+ @major = format[:major].to_i
55
+ @minor = format[:minor].to_i
56
+ @patch = format[:patch].to_i
57
+ @build = format[:build]
58
+ @prerelease = format[:prerelease]&.split('.') || []
59
+ @order = Order.new(@major, @minor, @patch, *@prerelease)
60
+ end
61
+
62
+ def <=>(other)
63
+ order <=> other.order
64
+ end
65
+
66
+ define_method('~>') do |other|
67
+ ceil_major = other.patch.positive? ? other.major : other.major + 1
68
+ ceil_minor = other.patch.positive? ? other.minor + 1 : 0
69
+ self < self.class.new(major: ceil_major, minor: ceil_minor, patch: 0) && self >= other
70
+ end
71
+
72
+ protected
73
+
74
+ attr_reader :order
75
+
76
+ private
77
+
78
+ def core_as_string
79
+ "#{major}.#{minor}.#{patch}"
80
+ end
81
+
82
+ def prerelease_as_string
83
+ return if prerelease.empty?
84
+
85
+ str = prerelease.join('.')
86
+ "-#{str}"
87
+ end
88
+
89
+ def build_as_string
90
+ return unless build
91
+
92
+ "+#{build}"
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Semvruler
4
+ VERSION = '0.1.1'
5
+ end
data/semvruler.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/semvruler/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'semvruler'
7
+ spec.version = Semvruler::VERSION
8
+ spec.authors = ['Monica L. Quiros']
9
+ spec.email = ['nika.kirosh@gmail.com']
10
+
11
+ spec.summary = 'Utility to match and compare semantic versions in ruby'
12
+ spec.homepage = 'https://github.com/nika-kirosh/semvruler'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.5.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = 'https://github.com/nika-kirosh/semvruler/blob/main/CHANGELOG.md'
19
+
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ # Dependencies
28
+
29
+ spec.add_development_dependency 'pry', '~> 0.14.0'
30
+ spec.add_development_dependency 'rake', '~> 13.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'rubocop', '~> 1.7'
33
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: semvruler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Monica L. Quiros
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.14.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.14.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ description:
70
+ email:
71
+ - nika.kirosh@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".circleci/config.yml"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/semvruler.rb
88
+ - lib/semvruler/constraint.rb
89
+ - lib/semvruler/order.rb
90
+ - lib/semvruler/rule.rb
91
+ - lib/semvruler/semversion.rb
92
+ - lib/semvruler/version.rb
93
+ - semvruler.gemspec
94
+ homepage: https://github.com/nika-kirosh/semvruler
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/nika-kirosh/semvruler
99
+ source_code_uri: https://github.com/nika-kirosh/semvruler
100
+ changelog_uri: https://github.com/nika-kirosh/semvruler/blob/main/CHANGELOG.md
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 2.5.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.1.2
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Utility to match and compare semantic versions in ruby
120
+ test_files: []