-
Notifications
You must be signed in to change notification settings - Fork 6
/
warning.rb
97 lines (77 loc) · 2.36 KB
/
warning.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
require 'ruby_dep/logger'
require 'ruby_dep/ruby_version'
module RubyDep
PROJECT_URL = 'http://github.com/e2/ruby_dep'.freeze
class Warning
DISABLING_ENVIRONMENT_VAR = 'RUBY_DEP_GEM_SILENCE_WARNINGS'.freeze
PREFIX = 'RubyDep: WARNING: '.freeze
WARNING = {
insecure: 'Your Ruby has security vulnerabilities!'.freeze,
buggy: 'Your Ruby is outdated/buggy.'.freeze,
untracked: 'Your Ruby may not be supported.'.freeze
}.freeze
NOTICE_RECOMMENDATION = 'Your Ruby is: %s (%s).'\
' Recommendation: upgrade to %s.'.freeze
NOTICE_BUGGY_ALTERNATIVE = '(Or, at least to %s)'.freeze
NOTICE_HOW_TO_DISABLE = '(To disable warnings, see:'\
"#{PROJECT_URL}/wiki/Disabling-warnings )".freeze
NOTICE_OPEN_ISSUE = 'If you need this version supported,'\
" please open an issue at #{PROJECT_URL}".freeze
def initialize
@version = RubyVersion.new(RUBY_VERSION, RUBY_ENGINE)
@logger = Logger.new(STDERR, PREFIX)
end
def show_warnings
return if silenced?
return warn_ruby(WARNING[status]) if WARNING.key?(status)
return if status == :unknown
raise "Unknown problem type: #{problem.inspect}"
end
def silence!
ENV[DISABLING_ENVIRONMENT_VAR] = '1'
end
private
def silenced?
value = ENV[DISABLING_ENVIRONMENT_VAR]
(value || '0') !~ /^0|false|no|n$/
end
def status
@version.status
end
def warn_ruby(msg)
@logger.warning(msg)
@logger.notice(recommendation)
@logger.notice(NOTICE_HOW_TO_DISABLE)
end
def recommendation
return unrecognized_msg unless @version.recognized?
return recommendation_msg unless status == :insecure
[recommendation_msg, safer_alternatives_msg].join(' ')
end
def unrecognized_msg
format(
"Your Ruby is: %s '%s' (unrecognized). %s",
@version.version,
@version.engine,
NOTICE_OPEN_ISSUE
)
end
def recommended_versions
@version.recommended(:unknown)
end
def buggy_alternatives
@version.recommended(:buggy)
end
def recommendation_msg
format(
NOTICE_RECOMMENDATION,
@version.version,
status,
recommended_versions.join(' or ')
)
end
def safer_alternatives_msg
format(NOTICE_BUGGY_ALTERNATIVE, buggy_alternatives.join(' or '))
end
end
end