/
rewriter.rb
280 lines (245 loc) · 9.06 KB
/
rewriter.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# frozen_string_literal: true
module Synvert::Core
# Rewriter is the top level namespace in a snippet.
#
# One Rewriter can contain one or many [Synvert::Core::Rewriter::Instance],
# which define the behavior what files and what codes to detect and rewrite to what code.
#
# Synvert::Rewriter.new 'factory_girl_short_syntax', 'use FactoryGirl short syntax' do
# if_gem 'factory_girl', {gte: '2.0.0'}
#
# within_files 'spec/**/*.rb' do
# with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
# replace_with "create({{arguments}})"
# end
# end
# end
class Rewriter
autoload :Action, 'synvert/core/rewriter/action'
autoload :AppendAction, 'synvert/core/rewriter/action/append_action'
autoload :InsertAction, 'synvert/core/rewriter/action/insert_action'
autoload :InsertAfterAction, 'synvert/core/rewriter/action/insert_after_action'
autoload :ReplaceWithAction, 'synvert/core/rewriter/action/replace_with_action'
autoload :ReplaceErbStmtWithExprAction, 'synvert/core/rewriter/action/replace_erb_stmt_with_expr_action'
autoload :RemoveAction, 'synvert/core/rewriter/action/remove_action'
autoload :Warning, 'synvert/core/rewriter/warning'
autoload :Instance, 'synvert/core/rewriter/instance'
autoload :Scope, 'synvert/core/rewriter/scope'
autoload :WithinScope, 'synvert/core/rewriter/scope/within_scope'
autoload :GotoScope, 'synvert/core/rewriter/scope/goto_scope'
autoload :Condition, 'synvert/core/rewriter/condition'
autoload :IfExistCondition, 'synvert/core/rewriter/condition/if_exist_condition'
autoload :UnlessExistCondition, 'synvert/core/rewriter/condition/unless_exist_condition'
autoload :IfOnlyExistCondition, 'synvert/core/rewriter/condition/if_only_exist_condition'
autoload :Helper, 'synvert/core/rewriter/helper'
autoload :RubyVersion, 'synvert/core/rewriter/ruby_version'
autoload :GemSpec, 'synvert/core/rewriter/gem_spec'
class << self
# Register a rewriter with its group and name.
#
# @param group [String] the rewriter group.
# @param name [String] the unique rewriter name.
# @param rewriter [Synvert::Core::Rewriter] the rewriter to register.
def register(group, name, rewriter)
group, name = group.to_s, name.to_s
rewriters[group] ||= {}
rewriters[group][name] = rewriter
end
# Fetch a rewriter by group and name.
#
# @param group [String] rewrtier group.
# @param name [String] rewrtier name.
# @return [Synvert::Core::Rewriter] the matching rewriter.
# @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
def fetch(group, name)
group, name = group.to_s, name.to_s
if exist? group, name
rewriters[group][name]
else
raise RewriterNotFound.new "Rewriter #{group} #{name} not found"
end
end
# Get a registered rewriter by group and name, then process that rewriter.
#
# @param group [String] the rewriter group.
# @param name [String] the rewriter name.
# @return [Synvert::Core::Rewriter] the registered rewriter.
# @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
def call(group, name)
group, name = group.to_s, name.to_s
if exist? group, name
rewriter = rewriters[group][name]
rewriter.process
rewriter
else
raise RewriterNotFound.new "Rewriter #{group}/#{name} not found"
end
end
# Check if one rewriter exist.
#
# @param group [String] the rewriter group.
# @param name [String] the rewriter name.
# @return [Boolean] true if the rewriter exist.
def exist?(group, name)
group, name = group.to_s, name.to_s
if rewriters[group] && rewriters[group][name]
true
else
false
end
end
# Get all available rewriters
#
# @return [Hash<String, Hash<String, Rewriter>>]
def availables
rewriters
end
# Clear all registered rewriters.
def clear
rewriters.clear
end
private
def rewriters
@rewriters ||= {}
end
end
# @!attribute [r] group
# @return [String] the group of rewriter
# @!attribute [r] name
# @return [String] the unique name of rewriter
# @!attribute [r] sub_snippets
# @return [Array<Synvert::Core::Rewriter>] all rewriters this rewiter calls.
# @!attribute [r] helper
# @return [Array] helper methods.
# @!attribute [r] warnings
# @return [Array<Synvert::Core::Rewriter::Warning>] warning messages.
attr_reader :group, :name, :sub_snippets, :helpers, :warnings
# Initialize a rewriter.
# When a rewriter is initialized, it is also registered.
#
# @param group [String] group of the rewriter.
# @param name [String] name of the rewriter.
# @param block [Block] a block defines the behaviors of the rewriter, block code won't be called when initialization.
# @return [Synvert::Core::Rewriter]
def initialize(group, name, &block)
@group = group
@name = name
@block = block
@helpers = []
@sub_snippets = []
@warnings = []
self.class.register(@group, @name, self)
end
# Process the rewriter.
# It will call the block.
def process
self.instance_eval &@block
end
# Process rewriter with sandbox mode.
# It will call the block but doesn't change any file.
def process_with_sandbox
@sandbox = true
self.process
@sandbox = false
end
# Add a warning.
#
# @param warning [Synvert::Core::Rewriter::Warning]
def add_warning(warning)
@warnings << warning
end
#######
# DSL #
#######
# Parse description dsl, it sets description of the rewrite.
# Or get description.
#
# @param description [String] rewriter description.
# @return rewriter description.
def description(description = nil)
if description
@description = description
else
@description
end
end
# Parse if_ruby dal, it checks if ruby version if greater than or equal to the specified ruby version.
#
# @param version, [String] specified ruby version.
def if_ruby(version)
@ruby_version = Rewriter::RubyVersion.new(version)
end
# Parse if_gem dsl, it compares version of the specified gem.
#
# @param name [String] gem name.
# @param comparator [Hash] equal, less than or greater than specified version, e.g. {gte: '2.0.0'},
# key can be eq, lt, gt, lte, gte or ne.
def if_gem(name, comparator)
@gem_spec = Rewriter::GemSpec.new(name, comparator)
end
# Parse within_files dsl, it finds specified files.
# It creates a [Synvert::Core::Rewriter::Instance] to rewrite code.
#
# @param file_pattern [String] pattern to find files, e.g. spec/**/*_spec.rb
# @param options [Hash] instance options.
# @param block [Block] the block to rewrite code in the matching files.
def within_files(file_pattern, options = {}, &block)
return if @sandbox
if (!@ruby_version || @ruby_version.match?) && (!@gem_spec || @gem_spec.match?)
Rewriter::Instance.new(self, file_pattern, options, &block).process
end
end
# Parse within_file dsl, it finds a specifiled file.
alias_method :within_file, :within_files
# Parses add_file dsl, it adds a new file.
#
# @param filename [String] file name of newly created file.
# @param content [String] file body of newly created file.
def add_file(filename, content)
return if @sandbox
filepath = File.join(Configuration.instance.get(:path), filename)
if File.exist?(filepath)
puts "File #{filepath} already exists."
return
end
FileUtils.mkdir_p File.dirname(filepath)
File.open filepath, 'w' do |file|
file.write content
end
end
# Parses remove_file dsl, it removes a file.
#
# @param filename [String] file name.
def remove_file(filename)
return if @sandbox
file_path = File.join(Configuration.instance.get(:path), filename)
File.delete(file_path) if File.exist?(file_path)
end
# Parse add_snippet dsl, it calls anther rewriter.
#
# @param group [String] group of another rewriter.
# @param name [String] name of another rewriter.
def add_snippet(group, name)
@sub_snippets << self.class.call(group.to_s, name.to_s)
end
# Parse helper_method dsl, it defines helper method for [Synvert::Core::Rewriter::Instance].
#
# @param name [String] helper method name.
# @param block [Block] helper method block.
def helper_method(name, &block)
@helpers << { name: name, block: block }
end
# Parse todo dsl, it sets todo of the rewriter.
# Or get todo.
#
# @param todo_list [String] rewriter todo.
# @return [String] rewriter todo.
def todo(todo = nil)
if todo
@todo = todo
else
@todo
end
end
end
end