-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
bootstrap.rb
176 lines (157 loc) · 4.29 KB
/
bootstrap.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
module Mutant
class Env
# Boostrap environment
class Bootstrap
include Adamantium::Flat, Concord::Public.new(:config, :cache), Procto.call(:env)
SEMANTICS_MESSAGE_FORMAT =
"%<message>s. Fix your lib to follow normal ruby semantics!\n" \
'{Module,Class}#name should return resolvable constant name as String or nil'.freeze
CLASS_NAME_RAISED_EXCEPTION =
'%<scope_class>s#name from: %<scope>s raised an error: %<exception>s'.freeze
CLASS_NAME_TYPE_MISMATCH_FORMAT =
'%<scope_class>s#name from: %<scope>s returned %<name>s'.freeze
private_constant(*constants(false))
# Scopes that are eligible for matching
#
# @return [Enumerable<Matcher::Scope>]
#
# @api private
attr_reader :matchable_scopes
# New bootstrap env
#
# @return [Env]
#
# @api private
def self.new(_config, _cache = Cache.new)
super
end
# Initialize object
#
# @return [Object]
#
# @api private
def initialize(*)
super
infect
initialize_matchable_scopes
end
# Print warning message
#
# @param [String]
#
# @return [self]
#
# @api private
def warn(message)
config.reporter.warn(message)
self
end
# Environment after bootstraping
#
# @return [Env]
#
# @api private
# rubocop:disable MethodLength
#
def env
subjects = matched_subjects
Env.new(
actor_env: Actor::Env.new(Thread),
config: config,
cache: cache,
subjects: subjects,
matchable_scopes: matchable_scopes,
integration: @integration,
selector: Selector::Expression.new(@integration),
mutations: subjects.flat_map(&:mutations)
)
end
private
# Scope name from scopeing object
#
# @param [Class, Module] scope
#
# @return [String]
# if scope has a name and does not raise exceptions obtaining it
#
# @return [nil]
# otherwise
#
# @api private
def scope_name(scope)
scope.name
rescue => exception
semantics_warning(
CLASS_NAME_RAISED_EXCEPTION,
scope_class: scope.class,
scope: scope,
exception: exception.inspect
)
nil
end
# Infect environment
#
# @return [undefined]
#
# @api private
def infect
config.includes.each(&$LOAD_PATH.method(:<<))
config.requires.each(&Kernel.method(:require))
@integration = config.integration.new(config).setup
end
# Matched subjects
#
# @return [Enumerable<Subject>]
#
# @api private
def matched_subjects
Matcher::Compiler.call(config.matcher).call(self)
end
# Initialize matchable scopes
#
# @return [undefined]
#
# @api private
def initialize_matchable_scopes
scopes = ObjectSpace.each_object(Module).each_with_object([]) do |scope, aggregate|
expression = expression(scope) || next
aggregate << Scope.new(scope, expression)
end
@matchable_scopes = scopes.sort_by { |scope| scope.expression.syntax }
end
# Try to turn scope into expression
#
# @param [Class, Module] scope
#
# @return [Expression]
# if scope can be represented in an expression
#
# @return [nil]
# otherwise
#
# @api private
def expression(scope)
name = scope_name(scope) or return
unless name.instance_of?(String)
semantics_warning(
CLASS_NAME_TYPE_MISMATCH_FORMAT,
scope_class: scope.class,
scope: scope,
name: name
)
return
end
config.expression_parser.try_parse(name)
end
# Write a semantics warning
#
# @return [undefined]
#
# @api private
def semantics_warning(format, options)
message = format % options
warn(SEMANTICS_MESSAGE_FORMAT % { message: message })
end
end # Boostrap
end # Env
end # Mutant