ruby_checker 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING.LESSER ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
4
+ #
5
+ # This file is part of ruby_checker
6
+ #
7
+ # ruby_checker is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ruby_checker is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ source "https://rubygems.org"
21
+
22
+ gemspec
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # ruby_checker [![Build Status](https://travis-ci.org/mssola/ruby_checker.svg?branch=master)](https://travis-ci.org/mssola/ruby_checker) [![Gem Version](https://badge.fury.io/rb/ruby_checker.svg)](https://badge.fury.io/rb/ruby_checker)
2
+
3
+ `ruby_checker` provides a simple framework to enforce a specific ruby version and interpreter to be used. This is useful for projects that want to support only a sigle ruby version/interpreter.
4
+
5
+ ## Basic usage
6
+
7
+ Using it is as easy as follows:
8
+
9
+ ```ruby
10
+ require "ruby_checker"
11
+
12
+ RubyChecker::RubyChecker.new.check!(supported: "2.6.5")
13
+ ```
14
+
15
+ This will enforce the `2.6.5` version for your application. Note though that it
16
+ is not restricting the interpreter being used. In order to do this, just
17
+ instantiate the object as follows:
18
+
19
+ ```ruby
20
+ require "ruby_checker"
21
+
22
+ RubyChecker::RubyChecker.new.check!(interpreter: RubyChecker::MRI, supported: "2.6.5")
23
+ ```
24
+
25
+ This gem supports the major ruby vendors by using the following constants:
26
+ `RubyChecker::MRI`, `RubyChecker::JRUBY`, `RubyChecker::MRUBY` and
27
+ `RubyChecker::TRUFFLE`.
28
+
29
+ ## Ruby on Rails integration
30
+
31
+ If you are using this gem from within a Ruby on Rails project, you will notice the following:
32
+
33
+ - This gem will use the `Rails.logger` for logging purposes instead of printing
34
+ everything on the standard output.
35
+ - You don't have to provide the `support` argument for the `RubyChecker` class,
36
+ since it will be guessed from the `.ruby-version` file from the root of your
37
+ project.
38
+
39
+ ## Contributing
40
+
41
+ Read the [CONTRIBUTING.md](./CONTRIBUTING.md) file.
42
+
43
+ ## [Changelog](https://pbs.twimg.com/media/DJDYCcLXcAA_eIo?format=jpg&name=small)
44
+
45
+ Read the [CHANGELOG.md](./CHANGELOG.md) file.
46
+
47
+ ## License
48
+
49
+ This project is based on work I did for the
50
+ [Portus](https://github.com/SUSE/Portus) project. I've extracted my code into a
51
+ gem so it can be also used for other projects that might be interested in this.
52
+
53
+ ```
54
+ Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
55
+
56
+ ruby_checker is free software: you can redistribute it and/or modify
57
+ it under the terms of the GNU Lesser General Public License as published by
58
+ the Free Software Foundation, either version 3 of the License, or
59
+ (at your option) any later version.
60
+
61
+ ruby_checker is distributed in the hope that it will be useful,
62
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
63
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64
+ GNU General Public License for more details.
65
+
66
+ You should have received a copy of the GNU Lesser General Public License
67
+ along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
68
+ ```
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
4
+ #
5
+ # This file is part of ruby_checker
6
+ #
7
+ # ruby_checker is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ruby_checker is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require "git_validation/task"
21
+ require "rake/testtask"
22
+ require "rubocop/rake_task"
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << "test"
26
+ t.libs << "lib"
27
+ t.test_files = FileList["test/**/test_*.rb"]
28
+ end
29
+
30
+ RuboCop::RakeTask.new(:rubocop) do |t|
31
+ t.options = ["--display-cop-names"]
32
+ end
33
+
34
+ GitValidation::Task.new(:"git-validation") do |t|
35
+ t.from = "74a6c20fc4d3"
36
+ end
37
+
38
+ task default: :all
39
+ task all: %i[test rubocop git-validation]
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
4
+ #
5
+ # This file is part of ruby_checker.
6
+ #
7
+ # ruby_checker is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ruby_checker is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ module RubyChecker
21
+ # MissingSupportedVersionError is raised whenever the supported version could
22
+ # not be guessed and it was not given.
23
+ class MissingSupportedVersionError < StandardError
24
+ def initialize
25
+ super("Supported version could not be guessed and it was not provided")
26
+ end
27
+ end
28
+
29
+ # RubyEngineNotAvailableError is raised whenever RUBY_ENGINE is detected to
30
+ # not be available.
31
+ class RubyEngineNotAvailableError < StandardError
32
+ def initialize
33
+ super("Apparently your Ruby interpreter does not define the 'RUBY_ENGINE' constant." \
34
+ " This is not supported.")
35
+ end
36
+ end
37
+
38
+ # NotSupportedError is raised when the current interpreter is detected to not
39
+ # be supported.
40
+ class NotSupportedError < StandardError
41
+ def initialize(interpreter)
42
+ super("Current interpreter is not supported. You should use '#{interpreter}' instead.")
43
+ end
44
+ end
45
+
46
+ # OutdatedRubyError is raised whenever the current Ruby is detected to be too
47
+ # old.
48
+ class OutdatedRubyError < StandardError
49
+ def initialize(supported)
50
+ super("Please, use Ruby #{supported}")
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
4
+ #
5
+ # This file is part of ruby_checker.
6
+ #
7
+ # ruby_checker is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ruby_checker is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require "ruby_checker/errors"
21
+
22
+ module RubyChecker
23
+ ANY = 0
24
+ MRI = 1
25
+ JRUBY = 2
26
+ MRUBY = 3
27
+ TRUFFLE = 4
28
+
29
+ # Interpreter adds checking capabilities for interpreters.
30
+ class Interpreter
31
+ def initialize(required)
32
+ @required = required
33
+ end
34
+
35
+ # check! performs checks for interpreter, ruby engines, etc. It might raise
36
+ # the following exceptions:
37
+ #
38
+ # - NotSupportedError: if the current interpreter does not match with the
39
+ # expectation.
40
+ # - RubyEngineNotAvailableError: if the RUBY_ENGINE constant has not been
41
+ # defined.
42
+ def check!
43
+ return true if @required == ANY
44
+
45
+ raise NotSupportedError, stringify(@required) if current_interpreter != @required
46
+
47
+ true
48
+ end
49
+
50
+ # Returns the constant representing the current ruby interpreter. It will
51
+ # return nil if the interpreter is not supported by this gem and it will
52
+ # raise a RubyEngineNotAvailableError if the RUBY_ENGINE constant has not
53
+ # been defined.
54
+ def current_interpreter
55
+ raise RubyEngineNotAvailableError if ruby_engine.nil?
56
+
57
+ case ruby_engine
58
+ when "ruby"
59
+ MRI
60
+ when "jruby"
61
+ JRUBY
62
+ when "mruby"
63
+ MRUBY
64
+ when "truffleruby"
65
+ TRUFFLE
66
+ end
67
+ end
68
+
69
+ # ruby_engine returns the value for RUBY_ENGINE, or nil if this constant is
70
+ # not defined.
71
+ #
72
+ # This method lives mainly so it can be stubbed in tests.
73
+ def ruby_engine
74
+ defined?(RUBY_ENGINE) ? RUBY_ENGINE : nil
75
+ end
76
+
77
+ # Returns a string representation for the given interpreter.
78
+ def stringify(interpreter)
79
+ return "unknown interpreter" unless interpreter.is_a?(Integer)
80
+
81
+ %w[MRI JRuby mruby truffleruby][interpreter - 1] || "unknown interpreter"
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
4
+ #
5
+ # This file is part of ruby_checker.
6
+ #
7
+ # ruby_checker is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ruby_checker is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ module RubyChecker
21
+ # Logger handles logging by delegating the task:
22
+ #
23
+ # - If we are in Rails, use the default Rails logger.
24
+ # - If we are not in Rails, simply use the `puts` method.
25
+ class Logger
26
+ # warn logs the given message as a warning.
27
+ def warn(msg)
28
+ if defined?(Rails)
29
+ Rails.logger.tagged("ruby_checker") { Rails.logger.warn(msg) }
30
+ else
31
+ puts "[ruby_checker] Warning: #{msg}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
4
+ #
5
+ # This file is part of ruby_checker.
6
+ #
7
+ # ruby_checker is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ruby_checker is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require "ruby_checker/errors"
21
+ require "ruby_checker/interpreter"
22
+ require "ruby_checker/versions"
23
+
24
+ require "rubygems/version"
25
+
26
+ module RubyChecker
27
+ # RubyChecker is the main class for this gem. See the README.md on how to use it.
28
+ class RubyChecker
29
+ def initialize(interpreter: ::RubyChecker::ANY, supported: nil)
30
+ @interpreter = interpreter
31
+ @supported = supported
32
+ @current = Gem::Version.new(RUBY_VERSION)
33
+ end
34
+
35
+ # check! performs all the checks that are required.
36
+ def check!
37
+ @supported = parsed_supported_version
38
+ raise MissingSupportedVersionError if @supported.nil?
39
+
40
+ perform_checks!
41
+ end
42
+
43
+ protected
44
+
45
+ # parsed_supported_version normalizes the @supported instance variable so it
46
+ # can be passed to the different checkers.
47
+ def parsed_supported_version
48
+ return Gem::Version.new(@supported) if @supported.is_a?(String) && @supported != ""
49
+
50
+ file = ruby_version_file
51
+ return nil if file.nil? || !File.file?(file)
52
+
53
+ Gem::Version.new(File.read(file).strip)
54
+ end
55
+
56
+ # perform_checks! does the actual calls to the diferent checkers.
57
+ def perform_checks!
58
+ Interpreter.new(@interpreter).check!
59
+ Versions.new(current: @current, supported: @supported).check!
60
+ end
61
+
62
+ # ruby_version_file returns the path to the .ruby-version file if it can be
63
+ # guessed. Otherwise it returns nil.
64
+ def ruby_version_file
65
+ return nil unless defined?(Rails)
66
+
67
+ Rails.root.join(".ruby-version")
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
4
+ #
5
+ # This file is part of ruby_checker
6
+ #
7
+ # ruby_checker is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ruby_checker is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ module RubyChecker
21
+ # The current version of RubyChecker.
22
+ VERSION = "1.0.0"
23
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
4
+ #
5
+ # This file is part of ruby_checker.
6
+ #
7
+ # ruby_checker is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ruby_checker is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require "ruby_checker/logger"
21
+ require "rubygems/version"
22
+
23
+ module RubyChecker
24
+ # Versions performs checks on the given versions (current and supported).
25
+ class Versions
26
+ def initialize(current:, supported:)
27
+ @current = current
28
+ @supported = supported
29
+ @logger = Logger.new
30
+ end
31
+
32
+ # check! performs tests for the current and the supported versions of
33
+ # ruby. It returns true if nothing bad is happening, false if there was a
34
+ # warning, and it will raise an OutdatedRubyError if the version mismatch
35
+ # is unbearable.
36
+ def check!
37
+ if @current > @supported
38
+ check_newer_versions
39
+ elsif @current < @supported
40
+ check_older_versions!
41
+ else
42
+ true
43
+ end
44
+ end
45
+
46
+ protected
47
+
48
+ # check_newer_versions performs checks by assuming that the supported
49
+ # version is older than the current one.
50
+ #
51
+ # Returns true if the difference is on the patch level, false otherwise.
52
+ def check_newer_versions
53
+ return true if same_major_minor?
54
+
55
+ @logger.warn "Using #{@current}, but #{@supported} is the one supported."
56
+ false
57
+ end
58
+
59
+ # check_older_versions! performs checks by assuming that the supported
60
+ # version is newer than the current one.
61
+ #
62
+ # It returns false if the mismatch is only on the patch level, otherwise it
63
+ # will raise an OutdatedRubyError.
64
+ def check_older_versions!
65
+ # Raise an error if this is not a patch-level release mistake, but a major
66
+ # or minor version difference.
67
+ raise OutdatedRubyError, @supported unless same_major_minor?
68
+
69
+ # Recommend an upgrade of a patch-level version.
70
+ @logger.warn "Using #{@current}, but #{@supported} is the one supported."
71
+ false
72
+ end
73
+
74
+ # same_major_minor returns true of the supported and the current versions
75
+ # match by the major and the minor numbers of the version.
76
+ def same_major_minor?
77
+ if RUBY_ENGINE == "truffleruby"
78
+ @current.to_s.split(".")[0..1] == @supported.to_s.split(".")[0..1]
79
+ else
80
+ @current.canonical_segments[0..1] == @supported.canonical_segments[0..1]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2020 Miquel Sabaté Solà <mikisabate@gmail.com>
4
+ #
5
+ # This file is part of ruby_checker
6
+ #
7
+ # ruby_checker is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ruby_checker is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ruby_checker. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ # RubyChecker contains all the classes and modules that implement this library.
21
+ module RubyChecker
22
+ end
23
+
24
+ require "ruby_checker/ruby_checker"