-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathruby_version.rb
159 lines (135 loc) · 4.61 KB
/
ruby_version.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require "language_pack/shell_helpers"
module LanguagePack
class RubyVersion
class BadVersionError < BuildpackError
def initialize(output = "")
msg = ""
msg << output
msg << "Can not parse Ruby Version:\n"
msg << "Valid versions listed on: https://devcenter.heroku.com/articles/ruby-support\n"
super msg
end
end
BOOTSTRAP_VERSION_NUMBER = "3.1.6".freeze
DEFAULT_VERSION_NUMBER = "3.1.6".freeze
DEFAULT_VERSION = "ruby-#{DEFAULT_VERSION_NUMBER}".freeze
LEGACY_VERSION_NUMBER = "1.9.2".freeze
LEGACY_VERSION = "ruby-#{LEGACY_VERSION_NUMBER}".freeze
RUBY_VERSION_REGEX = %r{
(?<ruby_version>\d+\.\d+\.\d+){0}
(?<patchlevel>p-?\d+){0}
(?<engine>\w+){0}
(?<engine_version>.+){0}
ruby-\g<ruby_version>(-\g<patchlevel>)?(-\g<engine>-\g<engine_version>)?
}x
attr_reader :set, :version, :version_without_patchlevel, :patchlevel, :engine, :ruby_version, :engine_version
include LanguagePack::ShellHelpers
def initialize(bundler_output, app = {})
@set = nil
@bundler_output = bundler_output
@app = app
set_version
parse_version
@version_without_patchlevel = @version.sub(/-p-?\d+/, '')
end
def warn_ruby_26_bundler?
return false if Gem::Version.new(self.ruby_version) >= Gem::Version.new("2.6.3")
return false if Gem::Version.new(self.ruby_version) < Gem::Version.new("2.6.0")
return true
end
def ruby_192_or_lower?
Gem::Version.new(self.ruby_version) <= Gem::Version.new("1.9.2")
end
# https://github.com/bundler/bundler/issues/4621
def version_for_download
if patchlevel_is_significant? && @patchlevel && @patchlevel.sub(/p/, '').to_i >= 0
@version
else
version_without_patchlevel
end
end
def file_name
"#{version_for_download}.tgz"
end
# Before Ruby 2.1 patch releases were done via patchlevel i.e. 1.9.3-p426 versus 1.9.3-p448
# With 2.1 and above patches are released in the "minor" version instead i.e. 2.1.0 versus 2.1.1
def patchlevel_is_significant?
!jruby? && Gem::Version.new(self.ruby_version) <= Gem::Version.new("2.1")
end
def rake_is_vendored?
Gem::Version.new(self.ruby_version) >= Gem::Version.new("1.9")
end
def default?
@version == none
end
# determine if we're using jruby
# @return [Boolean] true if we are and false if we aren't
def jruby?
engine == :jruby
end
# convert to a Gemfile ruby DSL incantation
# @return [String] the string representation of the Gemfile ruby DSL
def to_gemfile
if @engine == :ruby
"ruby '#{ruby_version}'"
else
"ruby '#{ruby_version}', :engine => '#{engine}', :engine_version => '#{engine_version}'"
end
end
# does this vendor bundler
def vendored_bundler?
false
end
# Returns the next logical version in the minor series
# for example if the current ruby version is
# `ruby-2.3.1` then then `next_logical_version(1)`
# will produce `ruby-2.3.2`.
def next_logical_version(increment = 1)
return false if patchlevel_is_significant?
split_version = @version_without_patchlevel.split(".")
teeny = split_version.pop
split_version << teeny.to_i + increment
split_version.join(".")
end
def next_minor_version(increment = 1)
split_version = @version_without_patchlevel.split(".")
split_version[1] = split_version[1].to_i + increment
split_version[2] = 0
split_version.join(".")
end
def next_major_version(increment = 1)
split_version = @version_without_patchlevel.split("-").last.split(".")
split_version[0] = Integer(split_version[0]) + increment
split_version[1] = 0
split_version[2] = 0
return "ruby-#{split_version.join(".")}"
end
private
def none
if @app[:is_new]
DEFAULT_VERSION
elsif @app[:last_version]
@app[:last_version]
else
LEGACY_VERSION
end
end
def set_version
if @bundler_output.empty?
@set = false
@version = none
else
@set = :gemfile
@version = @bundler_output
end
end
def parse_version
md = RUBY_VERSION_REGEX.match(version)
raise BadVersionError.new("'#{version}' is not valid") unless md
@ruby_version = md[:ruby_version]
@patchlevel = md[:patchlevel]
@engine_version = md[:engine_version] || @ruby_version
@engine = (md[:engine] || :ruby).to_sym
end
end
end