reweave 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +202 -0
- data/bin/reweave +200 -0
- data/lib/reweave/config.rb +119 -0
- data/lib/reweave/discovery/file_discoverer.rb +23 -0
- data/lib/reweave/editing/edit.rb +52 -0
- data/lib/reweave/editing/edit_set.rb +96 -0
- data/lib/reweave/editing/editor.rb +53 -0
- data/lib/reweave/editing.rb +23 -0
- data/lib/reweave/execution/engine.rb +317 -0
- data/lib/reweave/execution/execution_context.rb +87 -0
- data/lib/reweave/execution/project.rb +45 -0
- data/lib/reweave/execution.rb +10 -0
- data/lib/reweave/parser.rb +71 -0
- data/lib/reweave/recipe/composite_recipe.rb +26 -0
- data/lib/reweave/recipe/recipe.rb +118 -0
- data/lib/reweave/recipe/recipe_descriptor.rb +35 -0
- data/lib/reweave/recipe/recipe_option.rb +46 -0
- data/lib/reweave/recipe/recipe_registry.rb +44 -0
- data/lib/reweave/recipe/scanning_recipe.rb +29 -0
- data/lib/reweave/recipe/validation_error.rb +5 -0
- data/lib/reweave/recipe.rb +11 -0
- data/lib/reweave/recipes/rails/active_job_continuations.rb +62 -0
- data/lib/reweave/recipes/rails/controller_markdown_rendering.rb +48 -0
- data/lib/reweave/recipes/rails/remove_deprecated_gems.rb +71 -0
- data/lib/reweave/recipes/rails/replace_deprecated_find.rb +63 -0
- data/lib/reweave/recipes/rails/solid_cache_migration.rb +60 -0
- data/lib/reweave/recipes/rails/solid_queue_migration.rb +52 -0
- data/lib/reweave/recipes/rails/update_active_record.rb +70 -0
- data/lib/reweave/recipes/rails/update_configuration.rb +59 -0
- data/lib/reweave/recipes/rails/update_rails_routes.rb +51 -0
- data/lib/reweave/recipes/rails/upgrade_rails.rb +39 -0
- data/lib/reweave/recipes/ruby/find_method_calls.rb +72 -0
- data/lib/reweave/recipes/ruby/modernize_syntax.rb +81 -0
- data/lib/reweave/recipes/ruby/remove_unused_methods.rb +93 -0
- data/lib/reweave/recipes/ruby/rename_method.rb +55 -0
- data/lib/reweave/results/changeset.rb +96 -0
- data/lib/reweave/results/execution_result.rb +104 -0
- data/lib/reweave/results.rb +9 -0
- data/lib/reweave/source.rb +92 -0
- data/lib/reweave/version.rb +5 -0
- data/lib/reweave/visitor/cursor.rb +105 -0
- data/lib/reweave/visitor/ruby_visitor.rb +397 -0
- data/lib/reweave/visitor/visitor.rb +127 -0
- data/lib/reweave/visitor.rb +10 -0
- data/lib/reweave.rb +27 -0
- metadata +164 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "edit"
|
|
4
|
+
|
|
5
|
+
module Reweave
|
|
6
|
+
module Editing
|
|
7
|
+
class EditSet
|
|
8
|
+
include Enumerable
|
|
9
|
+
|
|
10
|
+
def initialize(edits = [])
|
|
11
|
+
@edits = edits
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def add(edit)
|
|
15
|
+
@edits << edit
|
|
16
|
+
self
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def <<(edit)
|
|
20
|
+
add(edit)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def each(&block)
|
|
24
|
+
@edits.each(&block)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def empty?
|
|
28
|
+
@edits.empty?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def size
|
|
32
|
+
@edits.size
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def for_source(source_id)
|
|
36
|
+
EditSet.new(@edits.select { |e| e.source_id == source_id })
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def validate!
|
|
40
|
+
validate_offsets!
|
|
41
|
+
validate_overlaps!
|
|
42
|
+
validate_encoding!
|
|
43
|
+
self
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def apply(source)
|
|
47
|
+
Editor.apply(source, self)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def to_a
|
|
51
|
+
@edits.dup
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def +(other)
|
|
55
|
+
EditSet.new(@edits + other.to_a)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def validate_offsets!
|
|
61
|
+
@edits.each do |edit|
|
|
62
|
+
if edit.start_offset < 0 || edit.end_offset < 0
|
|
63
|
+
raise EditError, "Negative offset in edit: #{edit}"
|
|
64
|
+
end
|
|
65
|
+
if edit.start_offset > edit.end_offset
|
|
66
|
+
raise EditError, "Start offset after end offset: #{edit}"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def validate_overlaps!
|
|
72
|
+
by_source = @edits.group_by(&:source_id)
|
|
73
|
+
by_source.each do |source_id, edits|
|
|
74
|
+
sorted = edits.sort_by { |e| [e.start_offset, e.end_offset] }
|
|
75
|
+
sorted.each_cons(2) do |a, b|
|
|
76
|
+
if a.overlaps?(b)
|
|
77
|
+
if a.insertion? && b.insertion? && a.end_offset == b.start_offset
|
|
78
|
+
# Adjacent insertions at same position - allowed with deterministic ordering
|
|
79
|
+
next
|
|
80
|
+
end
|
|
81
|
+
raise EditError, "Overlapping edits on source #{source_id}: #{a} overlaps #{b}"
|
|
82
|
+
end
|
|
83
|
+
if a.end_offset == b.start_offset && a.replacement? && b.replacement?
|
|
84
|
+
raise EditError, "Adjacent replacements on source #{source_id}: #{a} then #{b}"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def validate_encoding!
|
|
91
|
+
# UTF-8 boundary validation would go here
|
|
92
|
+
# For now, we trust the byte offsets from Prism
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "edit_set"
|
|
4
|
+
|
|
5
|
+
module Reweave
|
|
6
|
+
module Editing
|
|
7
|
+
class Editor
|
|
8
|
+
class << self
|
|
9
|
+
def apply(source, edit_set)
|
|
10
|
+
edit_set.validate!
|
|
11
|
+
source = source.dup # Ensure mutable
|
|
12
|
+
edits_by_source = edit_set.group_by(&:source_id)
|
|
13
|
+
|
|
14
|
+
edits_by_source.each_value do |edits|
|
|
15
|
+
sorted = edits.sort_by { |e| -e.start_offset }
|
|
16
|
+
sorted.each do |edit|
|
|
17
|
+
apply_edit(source, edit)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
source
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def apply_edit(source, edit)
|
|
24
|
+
if edit.start_offset > source.bytesize || edit.end_offset > source.bytesize
|
|
25
|
+
raise EditError, "Edit offsets exceed source length: #{edit} (source: #{source.bytesize} bytes)"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Validate UTF-8 boundaries
|
|
29
|
+
unless valid_utf8_boundary?(source, edit.start_offset)
|
|
30
|
+
raise EditError, "Edit start offset splits UTF-8 sequence: #{edit}"
|
|
31
|
+
end
|
|
32
|
+
unless valid_utf8_boundary?(source, edit.end_offset)
|
|
33
|
+
raise EditError, "Edit end offset splits UTF-8 sequence: #{edit}"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
source[edit.start_offset...edit.end_offset] = edit.replacement
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def valid_utf8_boundary?(source, offset)
|
|
40
|
+
return true if offset == 0 || offset == source.bytesize
|
|
41
|
+
|
|
42
|
+
# Check if offset is in the middle of a UTF-8 sequence
|
|
43
|
+
# UTF-8 continuation bytes start with 10xxxxxx
|
|
44
|
+
byte = source.getbyte(offset)
|
|
45
|
+
return true unless byte
|
|
46
|
+
|
|
47
|
+
# If it's a continuation byte, we're in the middle of a sequence
|
|
48
|
+
(byte & 0xC0) != 0x80
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "editing/edit"
|
|
4
|
+
require_relative "editing/edit_set"
|
|
5
|
+
require_relative "editing/editor"
|
|
6
|
+
|
|
7
|
+
module Reweave
|
|
8
|
+
module Editing
|
|
9
|
+
class EditError < StandardError; end
|
|
10
|
+
|
|
11
|
+
def self.replace(source_id, start_offset, end_offset, replacement, recipe_name)
|
|
12
|
+
Edit.new(source_id:, start_offset:, end_offset:, replacement:, recipe_name:)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.insert(source_id, offset, text, recipe_name)
|
|
16
|
+
Edit.new(source_id:, start_offset: offset, end_offset: offset, replacement: text, recipe_name:)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.delete(source_id, start_offset, end_offset, recipe_name)
|
|
20
|
+
Edit.new(source_id:, start_offset:, end_offset:, replacement: "", recipe_name:)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require_relative "execution_context"
|
|
5
|
+
require_relative "project"
|
|
6
|
+
require_relative "../../reweave/results/execution_result"
|
|
7
|
+
require_relative "../../reweave/results/changeset"
|
|
8
|
+
require_relative "../../reweave/recipe/scanning_recipe"
|
|
9
|
+
|
|
10
|
+
module Reweave
|
|
11
|
+
module Execution
|
|
12
|
+
class Engine
|
|
13
|
+
attr_reader :context
|
|
14
|
+
|
|
15
|
+
def initialize(dry_run: true, max_cycles: 10)
|
|
16
|
+
@dry_run = dry_run
|
|
17
|
+
@max_cycles = max_cycles
|
|
18
|
+
@processed_file_ops = Set.new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def run(recipe, path: ".", source: nil, patterns: nil, exclude: nil)
|
|
22
|
+
@context = ExecutionContext.new(
|
|
23
|
+
project_root: path,
|
|
24
|
+
dry_run: @dry_run,
|
|
25
|
+
max_cycles: @max_cycles
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
begin
|
|
29
|
+
if source
|
|
30
|
+
execute_on_source(recipe, source)
|
|
31
|
+
else
|
|
32
|
+
execute_on_project(recipe, path, patterns: patterns, exclude: exclude)
|
|
33
|
+
end
|
|
34
|
+
rescue => e
|
|
35
|
+
@context.error("Execution failed: #{e.message}")
|
|
36
|
+
return Results::ExecutionResult.new(
|
|
37
|
+
change_set: Results::ChangeSet.new,
|
|
38
|
+
diagnostics: @context.diagnostics,
|
|
39
|
+
stats: @context.stats,
|
|
40
|
+
context: @context,
|
|
41
|
+
success: false,
|
|
42
|
+
error: e.message
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
Results::ExecutionResult.new(
|
|
47
|
+
change_set: @change_set,
|
|
48
|
+
diagnostics: @context.diagnostics,
|
|
49
|
+
stats: @context.stats,
|
|
50
|
+
context: @context,
|
|
51
|
+
success: true
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def execute_on_source(recipe, source)
|
|
58
|
+
source_file = SourceFile.new(path: "(string)", source: source)
|
|
59
|
+
@change_set = Results::ChangeSet.new
|
|
60
|
+
|
|
61
|
+
run_recipes_on_file(recipe, source_file)
|
|
62
|
+
|
|
63
|
+
if source_file.source != source
|
|
64
|
+
@change_set.add(Results::FileChange.new(
|
|
65
|
+
path: source_file.path,
|
|
66
|
+
original_source: source,
|
|
67
|
+
updated_source: source_file.source,
|
|
68
|
+
recipes: [recipe.display_name],
|
|
69
|
+
change_type: :modified
|
|
70
|
+
))
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def execute_on_project(recipe, path, patterns: nil, exclude: nil)
|
|
75
|
+
project = Project.from_path(path, patterns: patterns || ["**/*.rb"], exclude: exclude || [])
|
|
76
|
+
@change_set = Results::ChangeSet.new
|
|
77
|
+
|
|
78
|
+
# Expand composite recipes
|
|
79
|
+
expanded_recipes = expand_recipes(recipe)
|
|
80
|
+
|
|
81
|
+
# Handle scanning recipes first
|
|
82
|
+
scanning_recipes, transformation_recipes = expanded_recipes.partition(&:scanning_recipe?)
|
|
83
|
+
|
|
84
|
+
accumulators = {}
|
|
85
|
+
scanning_recipes.each do |scanning_recipe|
|
|
86
|
+
accumulator = scanning_recipe.initial_accumulator
|
|
87
|
+
project.valid_source_files.each do |source_file|
|
|
88
|
+
scanning_recipe.scan(source_file, accumulator, @context)
|
|
89
|
+
end
|
|
90
|
+
accumulators[scanning_recipe] = accumulator
|
|
91
|
+
@context.stats[:recipes_executed] += 1
|
|
92
|
+
|
|
93
|
+
# Generate output files from scanning results
|
|
94
|
+
generated_changes = scanning_recipe.generate(accumulator, project)
|
|
95
|
+
generated_changes.each do |change|
|
|
96
|
+
@change_set.add(change)
|
|
97
|
+
if change.created?
|
|
98
|
+
@context.stats[:files_created] += 1
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# In dry-run mode, just run once
|
|
104
|
+
if @dry_run
|
|
105
|
+
transformation_recipes.each do |transformation_recipe|
|
|
106
|
+
break if @context.cancelled?
|
|
107
|
+
run_recipe_on_project(transformation_recipe, project, accumulators)
|
|
108
|
+
end
|
|
109
|
+
@context.stats[:cycles] = 1
|
|
110
|
+
return
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Run transformation cycles
|
|
114
|
+
cycle = 0
|
|
115
|
+
changed = true
|
|
116
|
+
|
|
117
|
+
while changed && cycle < @max_cycles && !@context.cancelled?
|
|
118
|
+
cycle += 1
|
|
119
|
+
@context.stats[:cycles] = cycle
|
|
120
|
+
changed = false
|
|
121
|
+
|
|
122
|
+
transformation_recipes.each do |transformation_recipe|
|
|
123
|
+
break if @context.cancelled?
|
|
124
|
+
|
|
125
|
+
recipe_changed = run_recipe_on_project(transformation_recipe, project, accumulators)
|
|
126
|
+
changed = true if recipe_changed
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
if cycle >= @max_cycles && changed
|
|
131
|
+
@context.warn("Max cycles reached, transformations may not have converged")
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def expand_recipes(recipe)
|
|
136
|
+
expanded = []
|
|
137
|
+
expand_recipe(recipe, expanded, Set.new)
|
|
138
|
+
expanded
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def expand_recipe(recipe, expanded, visited)
|
|
142
|
+
return if visited.include?(recipe.object_id)
|
|
143
|
+
visited.add(recipe.object_id)
|
|
144
|
+
|
|
145
|
+
if recipe.is_a?(CompositeRecipe)
|
|
146
|
+
recipe.recipes.each { |r| expand_recipe(r, expanded, visited) }
|
|
147
|
+
else
|
|
148
|
+
expanded << recipe
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def run_recipe_on_project(recipe, project, accumulators)
|
|
153
|
+
any_changed = false
|
|
154
|
+
|
|
155
|
+
visitor = create_visitor(recipe, accumulators)
|
|
156
|
+
return false unless visitor
|
|
157
|
+
|
|
158
|
+
@context.stats[:recipes_executed] += 1
|
|
159
|
+
|
|
160
|
+
project.valid_source_files.each do |source_file|
|
|
161
|
+
break if @context.cancelled?
|
|
162
|
+
|
|
163
|
+
@context.stats[:files_scanned] += 1
|
|
164
|
+
|
|
165
|
+
edits = run_visitor_on_file(visitor, source_file)
|
|
166
|
+
|
|
167
|
+
# Handle file operations after each file visit
|
|
168
|
+
file_ops = visitor.file_operations
|
|
169
|
+
visitor.instance_variable_set(:@file_operations, [])
|
|
170
|
+
file_ops.each do |op|
|
|
171
|
+
# Create a unique key for this file operation to avoid duplicates across cycles
|
|
172
|
+
op_key = case op[:type]
|
|
173
|
+
when :create then [:create, op[:path]]
|
|
174
|
+
when :delete then [:delete, op[:path]]
|
|
175
|
+
when :rename then [:rename, op[:old_path], op[:new_path]]
|
|
176
|
+
end
|
|
177
|
+
next if @processed_file_ops.include?(op_key)
|
|
178
|
+
@processed_file_ops.add(op_key)
|
|
179
|
+
|
|
180
|
+
case op[:type]
|
|
181
|
+
when :create
|
|
182
|
+
full_path = File.join(project.root, op[:path])
|
|
183
|
+
if !@dry_run
|
|
184
|
+
FileUtils.mkdir_p(File.dirname(full_path))
|
|
185
|
+
File.write(full_path, op[:contents])
|
|
186
|
+
project.add_source_file(op[:path])
|
|
187
|
+
end
|
|
188
|
+
@change_set.add(Results::FileChange.new(
|
|
189
|
+
path: full_path,
|
|
190
|
+
original_source: "",
|
|
191
|
+
updated_source: op[:contents],
|
|
192
|
+
recipes: [recipe.display_name],
|
|
193
|
+
change_type: :created
|
|
194
|
+
))
|
|
195
|
+
@context.stats[:files_created] += 1
|
|
196
|
+
any_changed = true
|
|
197
|
+
when :delete
|
|
198
|
+
full_path = File.join(project.root, op[:path])
|
|
199
|
+
if !@dry_run && File.exist?(full_path)
|
|
200
|
+
File.delete(full_path)
|
|
201
|
+
project.remove_source_file(op[:path])
|
|
202
|
+
end
|
|
203
|
+
source_file_for_delete = project.find_source_file(op[:path])
|
|
204
|
+
original = source_file_for_delete&.source || ""
|
|
205
|
+
@change_set.add(Results::FileChange.new(
|
|
206
|
+
path: full_path,
|
|
207
|
+
original_source: original,
|
|
208
|
+
updated_source: "",
|
|
209
|
+
recipes: [recipe.display_name],
|
|
210
|
+
change_type: :deleted
|
|
211
|
+
))
|
|
212
|
+
@context.stats[:files_deleted] += 1
|
|
213
|
+
any_changed = true
|
|
214
|
+
when :rename
|
|
215
|
+
old_full = File.join(project.root, op[:old_path])
|
|
216
|
+
new_full = File.join(project.root, op[:new_path])
|
|
217
|
+
if !@dry_run && File.exist?(old_full)
|
|
218
|
+
FileUtils.mkdir_p(File.dirname(new_full))
|
|
219
|
+
File.rename(old_full, new_full)
|
|
220
|
+
project.remove_source_file(op[:old_path])
|
|
221
|
+
project.add_source_file(op[:new_path])
|
|
222
|
+
end
|
|
223
|
+
source_file_for_rename = project.find_source_file(op[:old_path])
|
|
224
|
+
original = source_file_for_rename&.source || ""
|
|
225
|
+
@change_set.add(Results::FileChange.new(
|
|
226
|
+
path: old_full,
|
|
227
|
+
original_source: original,
|
|
228
|
+
updated_source: original,
|
|
229
|
+
recipes: [recipe.display_name],
|
|
230
|
+
change_type: :renamed
|
|
231
|
+
))
|
|
232
|
+
@context.stats[:files_deleted] += 1
|
|
233
|
+
@context.stats[:files_created] += 1
|
|
234
|
+
any_changed = true
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
next if edits.empty?
|
|
239
|
+
|
|
240
|
+
@context.stats[:files_matched] += 1
|
|
241
|
+
@context.stats[:total_edits] += edits.size
|
|
242
|
+
|
|
243
|
+
if !@dry_run
|
|
244
|
+
source_file.apply_edits(edits)
|
|
245
|
+
@change_set.add(Results::FileChange.new(
|
|
246
|
+
path: source_file.path,
|
|
247
|
+
original_source: source_file.source,
|
|
248
|
+
updated_source: source_file.source,
|
|
249
|
+
recipes: [recipe.display_name],
|
|
250
|
+
change_type: :modified
|
|
251
|
+
))
|
|
252
|
+
@context.stats[:files_changed] += 1
|
|
253
|
+
any_changed = true
|
|
254
|
+
else
|
|
255
|
+
new_source = Editing::Editor.apply(source_file.source.dup, edits)
|
|
256
|
+
if new_source != source_file.source
|
|
257
|
+
@change_set.add(Results::FileChange.new(
|
|
258
|
+
path: source_file.path,
|
|
259
|
+
original_source: source_file.source,
|
|
260
|
+
updated_source: new_source,
|
|
261
|
+
recipes: [recipe.display_name],
|
|
262
|
+
change_type: :modified
|
|
263
|
+
))
|
|
264
|
+
any_changed = true
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
any_changed
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def run_recipes_on_file(recipe, source_file)
|
|
273
|
+
expanded = expand_recipes(recipe)
|
|
274
|
+
expanded.each do |r|
|
|
275
|
+
visitor = create_visitor(r, {})
|
|
276
|
+
next unless visitor
|
|
277
|
+
|
|
278
|
+
edits = run_visitor_on_file(visitor, source_file)
|
|
279
|
+
next if edits.empty?
|
|
280
|
+
|
|
281
|
+
source_file.apply_edits(edits)
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def create_visitor(recipe, accumulators)
|
|
286
|
+
if recipe.is_a?(ScanningRecipe)
|
|
287
|
+
accumulator = accumulators[recipe]
|
|
288
|
+
recipe.get_visitor(accumulator, @context)
|
|
289
|
+
else
|
|
290
|
+
recipe.get_visitor(@context)
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def run_visitor_on_file(visitor, source_file)
|
|
295
|
+
return Editing::EditSet.new unless visitor
|
|
296
|
+
|
|
297
|
+
# Re-parse to get fresh AST
|
|
298
|
+
source_file.reparse!
|
|
299
|
+
|
|
300
|
+
# Reset visitor's edits for this file
|
|
301
|
+
visitor.instance_variable_set(:@edits, Editing::EditSet.new)
|
|
302
|
+
|
|
303
|
+
# Update visitor's source_file
|
|
304
|
+
visitor.instance_variable_set(:@source_file, source_file)
|
|
305
|
+
|
|
306
|
+
# Traverse the AST - use run if available (for visitors that manage their own source_file)
|
|
307
|
+
if visitor.respond_to?(:run)
|
|
308
|
+
visitor.run(source_file)
|
|
309
|
+
else
|
|
310
|
+
visitor.visit(source_file.root)
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
visitor.edits
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Reweave
|
|
4
|
+
module Execution
|
|
5
|
+
class ExecutionContext
|
|
6
|
+
attr_reader :project_root, :dry_run, :max_cycles, :logger, :diagnostics, :messages, :metadata, :stats
|
|
7
|
+
|
|
8
|
+
def initialize(project_root: ".", dry_run: true, max_cycles: 10, logger: nil)
|
|
9
|
+
@project_root = File.expand_path(project_root)
|
|
10
|
+
@dry_run = dry_run
|
|
11
|
+
@max_cycles = max_cycles
|
|
12
|
+
@logger = logger || default_logger
|
|
13
|
+
@diagnostics = []
|
|
14
|
+
@messages = []
|
|
15
|
+
@metadata = {}
|
|
16
|
+
@stats = {
|
|
17
|
+
files_scanned: 0,
|
|
18
|
+
files_matched: 0,
|
|
19
|
+
files_changed: 0,
|
|
20
|
+
files_created: 0,
|
|
21
|
+
files_deleted: 0,
|
|
22
|
+
cycles: 0,
|
|
23
|
+
recipes_executed: 0,
|
|
24
|
+
total_edits: 0
|
|
25
|
+
}
|
|
26
|
+
@cancelled = false
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def log(level, message)
|
|
30
|
+
@logger.call(level, message)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def info(message)
|
|
34
|
+
log(:info, message)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def warn(message)
|
|
38
|
+
log(:warn, message)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def error(message)
|
|
42
|
+
log(:error, message)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def debug(message)
|
|
46
|
+
log(:debug, message)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def add_diagnostic(diagnostic)
|
|
50
|
+
@diagnostics << diagnostic
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def add_message(message)
|
|
54
|
+
@messages << message
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def cancel!
|
|
58
|
+
@cancelled = true
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def cancelled?
|
|
62
|
+
@cancelled
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def increment_stat(key, amount = 1)
|
|
66
|
+
@stats[key] += amount
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def to_h
|
|
70
|
+
{
|
|
71
|
+
project_root: @project_root,
|
|
72
|
+
dry_run: @dry_run,
|
|
73
|
+
max_cycles: @max_cycles,
|
|
74
|
+
stats: @stats,
|
|
75
|
+
diagnostics: @diagnostics,
|
|
76
|
+
messages: @messages
|
|
77
|
+
}
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def default_logger
|
|
83
|
+
->(level, message) { puts "[#{level.upcase}] #{message}" }
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../reweave/source"
|
|
4
|
+
|
|
5
|
+
module Reweave
|
|
6
|
+
module Execution
|
|
7
|
+
class Project
|
|
8
|
+
attr_reader :root, :source_files, :config
|
|
9
|
+
|
|
10
|
+
def initialize(root, source_files: nil, config: nil)
|
|
11
|
+
@root = File.expand_path(root)
|
|
12
|
+
@source_files = source_files || SourceSet.new
|
|
13
|
+
@config = config || {}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.from_path(root, patterns: ["**/*.rb"], exclude: [])
|
|
17
|
+
files = Reweave::Discovery::FileDiscoverer.discover(root, patterns: patterns, exclude: exclude)
|
|
18
|
+
source_files = SourceSet.from_paths(files)
|
|
19
|
+
new(root, source_files: source_files)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def add_source_file(path)
|
|
23
|
+
file = SourceFile.new(path: File.join(@root, path))
|
|
24
|
+
@source_files.add(file)
|
|
25
|
+
file
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def remove_source_file(path)
|
|
29
|
+
@source_files.remove(File.join(@root, path))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def find_source_file(path)
|
|
33
|
+
@source_files[File.join(@root, path)]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def valid_source_files
|
|
37
|
+
@source_files.valid_files
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def paths
|
|
41
|
+
@source_files.paths
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
module Reweave
|
|
6
|
+
module Parser
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def parse(source, path: nil)
|
|
10
|
+
result = Prism.parse(source)
|
|
11
|
+
ParseResult.new(source, path, result)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class ParseResult
|
|
16
|
+
attr_reader :source, :path, :prism_result, :root, :diagnostics, :encoding
|
|
17
|
+
|
|
18
|
+
def initialize(source, path, prism_result)
|
|
19
|
+
@source = source
|
|
20
|
+
@path = path
|
|
21
|
+
@prism_result = prism_result
|
|
22
|
+
@root = prism_result.value
|
|
23
|
+
@diagnostics = build_diagnostics(prism_result)
|
|
24
|
+
@encoding = prism_result.encoding
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def success?
|
|
28
|
+
@diagnostics.none? { |d| d[:severity] == :error }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def errors
|
|
32
|
+
@diagnostics.select { |d| d[:severity] == :error }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def warnings
|
|
36
|
+
@diagnostics.select { |d| d[:severity] == :warning }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def build_diagnostics(prism_result)
|
|
42
|
+
prism_result.errors.map do |error|
|
|
43
|
+
location = error.location
|
|
44
|
+
{
|
|
45
|
+
severity: :error,
|
|
46
|
+
message: error.message,
|
|
47
|
+
path: @path,
|
|
48
|
+
start_offset: location.start_offset,
|
|
49
|
+
end_offset: location.end_offset,
|
|
50
|
+
start_line: location.start_line,
|
|
51
|
+
start_column: location.start_column,
|
|
52
|
+
end_line: location.end_line,
|
|
53
|
+
end_column: location.end_column
|
|
54
|
+
}
|
|
55
|
+
end + prism_result.warnings.map do |warning|
|
|
56
|
+
location = warning.location
|
|
57
|
+
{
|
|
58
|
+
severity: :warning,
|
|
59
|
+
message: warning.message,
|
|
60
|
+
path: @path,
|
|
61
|
+
start_offset: location.start_offset,
|
|
62
|
+
end_offset: location.end_offset,
|
|
63
|
+
start_line: location.start_line,
|
|
64
|
+
start_column: location.start_column,
|
|
65
|
+
end_line: location.end_line,
|
|
66
|
+
end_column: location.end_column
|
|
67
|
+
}
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|