Class: Synvert::Core::Rewriter
- Inherits:
-
Object
- Object
- Synvert::Core::Rewriter
- Defined in:
- lib/synvert/core/rewriter.rb
Overview
Rewriter is the top level namespace in a snippet.
One Rewriter checks if the depndency version matches, and it can contain one or many Instance, which define the behavior what files and what codes to detect and rewrite to what code.
Defined Under Namespace
Modules: Helper Classes: AnyValue, Condition, GemSpec, GotoScope, IfExistCondition, IfOnlyExistCondition, Instance, QueryScope, ReplaceErbStmtWithExprAction, RubyVersion, Scope, UnlessExistCondition, Warning, WithinScope
Instance Attribute Summary collapse
-
#affected_files ⇒ Set
readonly
Affected fileds.
-
#gem_spec ⇒ Object
readonly
Returns the value of attribute gem_spec.
-
#group ⇒ String
readonly
The group of rewriter.
-
#helper ⇒ Array
readonly
Helper methods.
-
#helpers ⇒ Object
readonly
Returns the value of attribute helpers.
-
#name ⇒ String
readonly
The unique name of rewriter.
-
#ruby_version ⇒ Rewriter::RubyVersion
readonly
The ruby version.
-
#sub_snippets ⇒ Array<Synvert::Core::Rewriter>
readonly
All rewriters this rewiter calls.
-
#warnings ⇒ Array<Synvert::Core::Rewriter::Warning>
readonly
Warning messages.
Class Method Summary collapse
-
.availables ⇒ Hash<String, Hash<String, Rewriter>>
Get all available rewriters.
-
.call(group, name, options = { run_instance: true }) ⇒ Synvert::Core::Rewriter
Get a registered rewriter by group and name, then process that rewriter.
-
.clear ⇒ Object
Clear all registered rewriters.
-
.execute { ... } ⇒ Object
Execute the temporary rewriter without group and name.
-
.fetch(group, name) ⇒ Synvert::Core::Rewriter
Fetch a rewriter by group and name.
-
.register(group, name, rewriter) ⇒ Object
Register a rewriter with its group and name.
Instance Method Summary collapse
-
#add_affected_file(file_path) ⇒ Object
Add an affected file.
-
#add_file(filename, content) ⇒ Object
Parses
add_filedsl, it adds a new file. -
#add_snippet(group, name) ⇒ Object
Parse
add_snippetdsl, it calls anther rewriter. -
#add_warning(warning) ⇒ Object
Add a warning.
-
#description(description = nil) ⇒ Object
Parse
descriptiondsl, it sets description of the rewrite. -
#helper_method(name) { ... } ⇒ Object
Parse
helper_methoddsl, it defines helper method for Instance. -
#if_gem(name, version) ⇒ Object
Parse
if_gemdsl, it compares version of the specified gem. -
#if_ruby(version) ⇒ Object
Parse
if_rubydsl, it checks if ruby version is greater than or equal to the specified ruby version. -
#initialize(group, name) { ... } ⇒ Rewriter
constructor
Initialize a Rewriter.
-
#process ⇒ Object
Process the rewriter.
-
#process_with_sandbox ⇒ Object
Process rewriter with sandbox mode.
-
#redo_until_no_change ⇒ Object
Rerun the snippet until no change.
-
#remove_file(filename) ⇒ Object
Parses
remove_filedsl, it removes a file. - #test ⇒ Object
-
#todo(todo = nil) ⇒ String
Parse
tododsl, it sets todo of the rewriter. -
#within_files(file_patterns, &block) ⇒ Object
(also: #within_file)
Parse
within_filesdsl, it finds specified files.
Constructor Details
#initialize(group, name) { ... } ⇒ Rewriter
Initialize a Rewriter. When a rewriter is initialized, it is already registered.
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/synvert/core/rewriter.rb', line 132 def initialize(group, name, &block) @group = group @name = name @block = block @helpers = [] @sub_snippets = [] @warnings = [] @affected_files = Set.new @redo_until_no_change = false @options = { run_instance: true, write_to_file: true } @test_results = [] self.class.register(@group, @name, self) end |
Instance Attribute Details
#affected_files ⇒ Set (readonly)
Returns affected fileds.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec |
#gem_spec ⇒ Object (readonly)
Returns the value of attribute gem_spec.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec |
#group ⇒ String (readonly)
Returns the group of rewriter.
124 125 126 |
# File 'lib/synvert/core/rewriter.rb', line 124 def group @group end |
#helper ⇒ Array (readonly)
Returns helper methods.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec |
#helpers ⇒ Object (readonly)
Returns the value of attribute helpers.
124 125 126 |
# File 'lib/synvert/core/rewriter.rb', line 124 def helpers @helpers end |
#name ⇒ String (readonly)
Returns the unique name of rewriter.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec |
#ruby_version ⇒ Rewriter::RubyVersion (readonly)
Returns the ruby version.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec |
#sub_snippets ⇒ Array<Synvert::Core::Rewriter> (readonly)
Returns all rewriters this rewiter calls.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec |
#warnings ⇒ Array<Synvert::Core::Rewriter::Warning> (readonly)
Returns warning messages.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec |
Class Method Details
.availables ⇒ Hash<String, Hash<String, Rewriter>>
Get all available rewriters
92 93 94 |
# File 'lib/synvert/core/rewriter.rb', line 92 def availables rewriters end |
.call(group, name, options = { run_instance: true }) ⇒ Synvert::Core::Rewriter
Get a registered rewriter by group and name, then process that rewriter.
79 80 81 82 83 84 85 86 87 |
# File 'lib/synvert/core/rewriter.rb', line 79 def call(group, name, = { run_instance: true }) rewriter = fetch(group, name) if [:run_instance] rewriter.process else rewriter.process_with_sandbox end rewriter end |
.clear ⇒ Object
Clear all registered rewriters.
97 98 99 |
# File 'lib/synvert/core/rewriter.rb', line 97 def clear rewriters.clear end |
.execute { ... } ⇒ Object
Execute the temporary rewriter without group and name.
38 39 40 41 42 |
# File 'lib/synvert/core/rewriter.rb', line 38 def execute(&block) rewriter = Rewriter.new('', '', &block) rewriter.process rewriter end |
.fetch(group, name) ⇒ Synvert::Core::Rewriter
Fetch a rewriter by group and name.
62 63 64 65 66 67 68 69 |
# File 'lib/synvert/core/rewriter.rb', line 62 def fetch(group, name) group = group.to_s name = name.to_s rewriter = rewriters.dig(group, name) raise RewriterNotFound, "Rewriter #{group} #{name} not found" unless rewriter rewriter end |
.register(group, name, rewriter) ⇒ Object
Register a rewriter with its group and name.
49 50 51 52 53 54 |
# File 'lib/synvert/core/rewriter.rb', line 49 def register(group, name, rewriter) group = group.to_s name = name.to_s rewriters[group] ||= {} rewriters[group][name] = rewriter end |
Instance Method Details
#add_affected_file(file_path) ⇒ Object
Add an affected file.
191 192 193 |
# File 'lib/synvert/core/rewriter.rb', line 191 def add_affected_file(file_path) @affected_files.add(file_path) end |
#add_file(filename, content) ⇒ Object
Parses add_file dsl, it adds a new file.
274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/synvert/core/rewriter.rb', line 274 def add_file(filename, content) return unless @options[:run_instance] filepath = File.join(Configuration.root_path, filename) if File.exist?(filepath) puts "File #{filepath} already exists." return end FileUtils.mkdir_p File.dirname(filepath) File.write(filepath, content) end |
#add_snippet(group, name) ⇒ Object
Parse add_snippet dsl, it calls anther rewriter.
319 320 321 |
# File 'lib/synvert/core/rewriter.rb', line 319 def add_snippet(group, name) @sub_snippets << self.class.call(group.to_s, name.to_s, @options) end |
#add_warning(warning) ⇒ Object
Add a warning.
184 185 186 |
# File 'lib/synvert/core/rewriter.rb', line 184 def add_warning(warning) @warnings << warning end |
#description(description = nil) ⇒ Object
Parse description dsl, it sets description of the rewrite. Or get description.
207 208 209 210 211 212 213 |
# File 'lib/synvert/core/rewriter.rb', line 207 def description(description = nil) if description @description = description else @description end end |
#helper_method(name) { ... } ⇒ Object
Parse helper_method dsl, it defines helper method for Instance.
336 337 338 |
# File 'lib/synvert/core/rewriter.rb', line 336 def helper_method(name, &block) @helpers << { name: name, block: block } end |
#if_gem(name, version) ⇒ Object
Parse if_gem dsl, it compares version of the specified gem.
232 233 234 |
# File 'lib/synvert/core/rewriter.rb', line 232 def if_gem(name, version) @gem_spec = Rewriter::GemSpec.new(name, version) end |
#if_ruby(version) ⇒ Object
Parse if_ruby dsl, it checks if ruby version is greater than or equal to the specified ruby version.
221 222 223 |
# File 'lib/synvert/core/rewriter.rb', line 221 def if_ruby(version) @ruby_version = Rewriter::RubyVersion.new(version) end |
#process ⇒ Object
Process the rewriter. It will call the block.
148 149 150 151 152 153 |
# File 'lib/synvert/core/rewriter.rb', line 148 def process @affected_files = Set.new instance_eval(&@block) process if !@affected_files.empty? && @redo_until_no_change # redo end |
#process_with_sandbox ⇒ Object
Process rewriter with sandbox mode. It will call the block but doesn’t change any file.
157 158 159 160 161 162 163 164 |
# File 'lib/synvert/core/rewriter.rb', line 157 def process_with_sandbox @options[:run_instance] = false begin process ensure @options[:run_instance] = true end end |
#redo_until_no_change ⇒ Object
Rerun the snippet until no change.
367 368 369 |
# File 'lib/synvert/core/rewriter.rb', line 367 def redo_until_no_change @redo_until_no_change = true end |
#remove_file(filename) ⇒ Object
Parses remove_file dsl, it removes a file.
293 294 295 296 297 298 |
# File 'lib/synvert/core/rewriter.rb', line 293 def remove_file(filename) return unless @options[:run_instance] file_path = File.join(Configuration.root_path, filename) File.delete(file_path) if File.exist?(file_path) end |
#test ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/synvert/core/rewriter.rb', line 166 def test @options[:write_to_file] = false begin @affected_files = Set.new instance_eval(&@block) if !@affected_files.empty? && @redo_until_no_change # redo test end ensure @options[:write_to_file] = true end @test_results end |
#todo(todo = nil) ⇒ String
Parse todo dsl, it sets todo of the rewriter. Or get todo.
354 355 356 357 358 359 360 |
# File 'lib/synvert/core/rewriter.rb', line 354 def todo(todo = nil) if todo @todo = todo else @todo end end |
#within_files(file_patterns, &block) ⇒ Object Also known as: within_file
Parse within_files dsl, it finds specified files. It creates a Instance to rewrite code.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/synvert/core/rewriter.rb', line 245 def within_files(file_patterns, &block) return unless @options[:run_instance] return if @ruby_version && !@ruby_version.match? return if @gem_spec && !@gem_spec.match? instance = Rewriter::Instance.new(self, Array(file_patterns), &block) if @options[:write_to_file] instance.process else results = instance.test @test_results += results.select { |result| result.affected? } end end |