semver_check 0.1.0

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: 4217ffcec4eca63ed8280bb221b7cf0fa8ab1dc545b85f4d51799aa3f4bdf997
4
+ data.tar.gz: ccfc6d8d567321e919f4c629cfaddd7935b8dda165020046bf643bf2ddf274de
5
+ SHA512:
6
+ metadata.gz: a0f7f029ea74293af76697b6b5e37a7897636de5059a792657f6e1963817cf7829f1d4ab2d306aa5d29f9e43aaca144e968d6407f2e407c46ef20fff010a98ba
7
+ data.tar.gz: 0c67e78f29bca2d694b0e662ad262178730d013af78c65e6bce4e4f1f7e52ae37a4b3c1cd1b332ab064f460d099e20b85b766dd8abb9d6f381cd4eb011083d80
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-25
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in semver_check.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 PuLLi
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,31 @@
1
+ # SemverCheck
2
+
3
+ TODO: Delete this and the text above, and describe your gem
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add semver_check
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install semver_check
14
+
15
+ ## Usage
16
+
17
+ TODO: Write usage instructions here
18
+
19
+ ## Development
20
+
21
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
22
+
23
+ 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).
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/semver_check.
28
+
29
+ ## License
30
+
31
+ 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 "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SemverCheck
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "semver_check/version"
4
+
5
+ # SemverCheck module
6
+ module SemverCheck
7
+ # Class to compare SemVer strings
8
+ class Compare
9
+ include Comparable
10
+
11
+ SEMVER_PATTERN = /\A
12
+ (?<semver>
13
+ (?<major>0|[1-9]\d*)
14
+ \.
15
+ (?<minor>0|[1-9]\d*)
16
+ \.
17
+ (?<patch>0|[1-9]\d*)
18
+ (?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)
19
+ (?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)
20
+ )?
21
+ (?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?
22
+ )
23
+ \Z/x
24
+
25
+ INT_RESULT = {
26
+ n: 1,
27
+ o: -1,
28
+ s: 0
29
+ }.freeze
30
+
31
+ MAPPING_TABLE = [
32
+ %w[n[nso]{2} n],
33
+ %w[sn[nso]{1} n],
34
+ %w[ssn n],
35
+ %w[o[nso]{2} o],
36
+ %w[so[nso]{1} o],
37
+ %w[sso o],
38
+ %w[sss s]
39
+ ].freeze
40
+
41
+ ORDER = %i[major minor patch prerelease].freeze
42
+
43
+ attr_reader :version
44
+
45
+ def initialize(version)
46
+ @version = prepare_version(version)
47
+ end
48
+
49
+ def <=>(other)
50
+ return nil unless other.is_a? Compare
51
+
52
+ comparison(other)
53
+ end
54
+
55
+ def !=(other)
56
+ [-1, 1].include?(self.<=>(other))
57
+ end
58
+
59
+ def to_s
60
+ @version[:semver]
61
+ end
62
+
63
+ private
64
+
65
+ def comparison(other)
66
+ comparison = ORDER.take(3).map do |part|
67
+ if @version[part] == other.version[part]
68
+ "s"
69
+ elsif @version[part] > other.version[part]
70
+ "n"
71
+ else
72
+ "o"
73
+ end
74
+ end.join
75
+ prerelease_sort(other, comparison)
76
+ end
77
+
78
+ def prerelease_sort(other, comparison)
79
+ v_pre = @version[:prerelease].nil? ? "" : @version[:prerelease]
80
+ o_pre = other.version[:prerelease].nil? ? "" : other.version[:prerelease]
81
+ version_compare = match(comparison)
82
+ if version_compare.zero?
83
+ sorted = [v_pre, o_pre].sort.reverse
84
+ if (v_pre == sorted.last && v_pre != o_pre && v_pre != "") || (v_pre != "" && o_pre == "")
85
+ -1
86
+ elsif (v_pre == sorted.first && v_pre != o_pre) || (v_pre == "" && o_pre != "")
87
+ 1
88
+ else
89
+ 0
90
+ end
91
+ else
92
+ version_compare
93
+ end
94
+ end
95
+
96
+ def match(comparison)
97
+ result = MAPPING_TABLE.map do |m|
98
+ m[1] if comparison.match?(/\A#{m[0]}\Z/)
99
+ end.compact
100
+
101
+ raise "Too many results" if result.length > 1
102
+
103
+ INT_RESULT[result.first.to_sym]
104
+ end
105
+
106
+ def prepare_version(version)
107
+ version = version.match(SEMVER_PATTERN)
108
+ raise ArgumentError, "No proper Semantic Versioning given" if version.nil?
109
+
110
+ version = version.named_captures.transform_keys(&:to_sym)
111
+ # convert major, minor and patch to int
112
+ ORDER.take(3).each { |part| version[part] = version[part].to_i }
113
+ version
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,4 @@
1
+ module SemverCheck
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: semver_check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - PuLLi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Fun project to compare two SemVer strings with a bit of regex magic.
14
+ email:
15
+ - the@pulli.dev
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/semver_check.rb
26
+ - lib/semver_check/version.rb
27
+ - sig/semver_check.rbs
28
+ homepage: https://github.com/the-pulli/semver_check
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ allowed_push_host: https://rubygems.org
33
+ homepage_uri: https://github.com/the-pulli/semver_check
34
+ source_code_uri: https://github.com/the-pulli/semver_check
35
+ changelog_uri: https://github.com/the-pulli/semver_check/CHANGELOG.md
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.3.26
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Little gem to compare two SemVer strings.
55
+ test_files: []