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.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +202 -0
  4. data/bin/reweave +200 -0
  5. data/lib/reweave/config.rb +119 -0
  6. data/lib/reweave/discovery/file_discoverer.rb +23 -0
  7. data/lib/reweave/editing/edit.rb +52 -0
  8. data/lib/reweave/editing/edit_set.rb +96 -0
  9. data/lib/reweave/editing/editor.rb +53 -0
  10. data/lib/reweave/editing.rb +23 -0
  11. data/lib/reweave/execution/engine.rb +317 -0
  12. data/lib/reweave/execution/execution_context.rb +87 -0
  13. data/lib/reweave/execution/project.rb +45 -0
  14. data/lib/reweave/execution.rb +10 -0
  15. data/lib/reweave/parser.rb +71 -0
  16. data/lib/reweave/recipe/composite_recipe.rb +26 -0
  17. data/lib/reweave/recipe/recipe.rb +118 -0
  18. data/lib/reweave/recipe/recipe_descriptor.rb +35 -0
  19. data/lib/reweave/recipe/recipe_option.rb +46 -0
  20. data/lib/reweave/recipe/recipe_registry.rb +44 -0
  21. data/lib/reweave/recipe/scanning_recipe.rb +29 -0
  22. data/lib/reweave/recipe/validation_error.rb +5 -0
  23. data/lib/reweave/recipe.rb +11 -0
  24. data/lib/reweave/recipes/rails/active_job_continuations.rb +62 -0
  25. data/lib/reweave/recipes/rails/controller_markdown_rendering.rb +48 -0
  26. data/lib/reweave/recipes/rails/remove_deprecated_gems.rb +71 -0
  27. data/lib/reweave/recipes/rails/replace_deprecated_find.rb +63 -0
  28. data/lib/reweave/recipes/rails/solid_cache_migration.rb +60 -0
  29. data/lib/reweave/recipes/rails/solid_queue_migration.rb +52 -0
  30. data/lib/reweave/recipes/rails/update_active_record.rb +70 -0
  31. data/lib/reweave/recipes/rails/update_configuration.rb +59 -0
  32. data/lib/reweave/recipes/rails/update_rails_routes.rb +51 -0
  33. data/lib/reweave/recipes/rails/upgrade_rails.rb +39 -0
  34. data/lib/reweave/recipes/ruby/find_method_calls.rb +72 -0
  35. data/lib/reweave/recipes/ruby/modernize_syntax.rb +81 -0
  36. data/lib/reweave/recipes/ruby/remove_unused_methods.rb +93 -0
  37. data/lib/reweave/recipes/ruby/rename_method.rb +55 -0
  38. data/lib/reweave/results/changeset.rb +96 -0
  39. data/lib/reweave/results/execution_result.rb +104 -0
  40. data/lib/reweave/results.rb +9 -0
  41. data/lib/reweave/source.rb +92 -0
  42. data/lib/reweave/version.rb +5 -0
  43. data/lib/reweave/visitor/cursor.rb +105 -0
  44. data/lib/reweave/visitor/ruby_visitor.rb +397 -0
  45. data/lib/reweave/visitor/visitor.rb +127 -0
  46. data/lib/reweave/visitor.rb +10 -0
  47. data/lib/reweave.rb +27 -0
  48. metadata +164 -0
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../reweave/recipe"
4
+ require_relative "../../../reweave/visitor/ruby_visitor"
5
+
6
+ module Reweave
7
+ module Recipes
8
+ module Rails
9
+ # Update deprecated ActiveRecord methods
10
+ class UpdateActiveRecord < Recipe
11
+ display_name "Update ActiveRecord"
12
+ description "Updates deprecated ActiveRecord methods to modern equivalents"
13
+
14
+ def get_visitor(context)
15
+ UpdateActiveRecordVisitor.new(context)
16
+ end
17
+ end
18
+
19
+ class UpdateActiveRecordVisitor < Reweave::Visitor::RubyVisitor
20
+ def initialize(context, **)
21
+ super(context, nil)
22
+ end
23
+
24
+ def run(source_file)
25
+ @source_file = source_file
26
+ traverse(source_file.root)
27
+ edits
28
+ end
29
+
30
+ def visit_call_node(node)
31
+ # update_attributes -> update
32
+ if node.name == :update_attributes
33
+ replace_location(node.message_loc, "update")
34
+ end
35
+
36
+ # update_attributes! -> update!
37
+ if node.name == :update_attributes!
38
+ replace_location(node.message_loc, "update!")
39
+ end
40
+
41
+ # destroy_all with conditions -> where(...).destroy_all
42
+ if node.name == :destroy_all
43
+ args = extract_arguments(node)
44
+ unless args.empty?
45
+ # This is a complex transformation, skip for safety
46
+ end
47
+ end
48
+
49
+ # delete_all with conditions -> where(...).delete_all
50
+ if node.name == :delete_all
51
+ args = extract_arguments(node)
52
+ unless args.empty?
53
+ # This is a complex transformation, skip for safety
54
+ end
55
+ end
56
+
57
+ # first/last with order -> order(...).first/last
58
+ if node.name == :first || node.name == :last
59
+ args = extract_arguments(node)
60
+ if args.any? { |a| a.is_a?(Prism::HashNode) }
61
+ # Could have order: { created_at: :desc }
62
+ end
63
+ end
64
+
65
+ super
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../reweave/recipe"
4
+ require_relative "../../../reweave/visitor/ruby_visitor"
5
+
6
+ module Reweave
7
+ module Recipes
8
+ module Rails
9
+ # Update Rails configuration files
10
+ class UpdateConfiguration < Recipe
11
+ display_name "Update configuration"
12
+ description "Updates deprecated configuration options in config/environments/*.rb, config/application.rb"
13
+
14
+ def get_visitor(context)
15
+ UpdateConfigurationVisitor.new(context)
16
+ end
17
+ end
18
+
19
+ class UpdateConfigurationVisitor < Reweave::Visitor::RubyVisitor
20
+ def initialize(context, **)
21
+ super(context, nil)
22
+ end
23
+
24
+ def run(source_file)
25
+ @source_file = source_file
26
+ traverse(source_file.root)
27
+ edits
28
+ end
29
+
30
+ def visit_call_node(node)
31
+ # config.active_record.belongs_to_required_by_default -> config.active_record.belongs_to_required_by_default = true
32
+ # This is a setter, handled by matching the method name
33
+
34
+ # config.assets.enabled -> config.assets.enabled (deprecated in Rails 7)
35
+ if match_call?(node, receiver: "config.assets", method: :enabled)
36
+ # Just flag it
37
+ end
38
+
39
+ # config.active_support.halt_callback_chains_on_return_false -> config.active_support.halt_callback_chains_on_return_false = true
40
+ if match_call?(node, receiver: "config.active_support", method: :halt_callback_chains_on_return_false)
41
+ # Just flag it
42
+ end
43
+
44
+ # config.active_record.suppress_multiple_database_errors -> config.active_record.suppress_multiple_database_errors = true
45
+ if match_call?(node, receiver: "config.active_record", method: :suppress_multiple_database_errors)
46
+ # Just flag it
47
+ end
48
+
49
+ super
50
+ end
51
+
52
+ def visit_block_node(node)
53
+ # Match config.xxx = value patterns in block context
54
+ super
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../reweave/recipe"
4
+ require_relative "../../../reweave/visitor/ruby_visitor"
5
+
6
+ module Reweave
7
+ module Recipes
8
+ module Rails
9
+ # Update Rails routes syntax
10
+ class UpdateRailsRoutes < Recipe
11
+ display_name "Update Rails routes"
12
+ description "Updates deprecated routes syntax (match, constraints, etc.)"
13
+
14
+ def get_visitor(context)
15
+ UpdateRailsRoutesVisitor.new(context)
16
+ end
17
+ end
18
+
19
+ class UpdateRailsRoutesVisitor < Reweave::Visitor::RubyVisitor
20
+ def initialize(context, **)
21
+ super(context, nil)
22
+ end
23
+
24
+ def run(source_file)
25
+ @source_file = source_file
26
+ traverse(source_file.root)
27
+ edits
28
+ end
29
+
30
+ def visit_call_node(node)
31
+ # match ':controller(/:action(/:id))' -> get ':controller(/:action(/:id))'
32
+ if node.name == :match
33
+ args = extract_arguments(node)
34
+ if args.first&.is_a?(Prism::StringNode)
35
+ # Check if it's a simple match without :via
36
+ kwargs = extract_keyword_arguments(node)
37
+ unless kwargs[:via]
38
+ replace_location(node.message_loc, "get")
39
+ end
40
+ end
41
+ end
42
+
43
+ # constraints(id: /regex/) -> constraints(id: /regex/) (already correct)
44
+ # namespace :admin do ... end -> namespace :admin do ... end (already correct)
45
+
46
+ super
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../reweave/recipe/composite_recipe"
4
+ require_relative "replace_deprecated_find"
5
+ require_relative "update_active_record"
6
+ require_relative "update_rails_routes"
7
+ require_relative "update_configuration"
8
+ require_relative "solid_queue_migration"
9
+ require_relative "solid_cache_migration"
10
+ require_relative "active_job_continuations"
11
+ require_relative "remove_deprecated_gems"
12
+ require_relative "controller_markdown_rendering"
13
+
14
+ module Reweave
15
+ module Recipes
16
+ module Rails
17
+ # Composite recipe for upgrading Rails applications
18
+ class UpgradeRails < CompositeRecipe
19
+ display_name "Upgrade Rails"
20
+ description "Comprehensive Rails upgrade recipe combining all individual migration recipes"
21
+
22
+ def initialize(**options)
23
+ recipes = [
24
+ ReplaceDeprecatedFind.new(**options),
25
+ UpdateActiveRecord.new(**options),
26
+ UpdateRailsRoutes.new(**options),
27
+ UpdateConfiguration.new(**options),
28
+ SolidQueueMigration.new(**options),
29
+ SolidCacheMigration.new(**options),
30
+ ActiveJobContinuations.new(**options),
31
+ RemoveDeprecatedGems.new(**options),
32
+ ControllerMarkdownRendering.new(**options)
33
+ ]
34
+ super(recipes, **options)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../reweave/recipe/scanning_recipe"
4
+ require_relative "../../../reweave/visitor/ruby_visitor"
5
+
6
+ module Reweave
7
+ module Recipes
8
+ module Ruby
9
+ # Scanning recipe that finds all method calls across the project
10
+ class FindMethodCalls < ScanningRecipe
11
+ display_name "Find method calls"
12
+ description "Scans the project and collects all method calls with their receivers"
13
+
14
+ def initial_accumulator
15
+ {calls: []}
16
+ end
17
+
18
+ def scan(source_file, accumulator, context)
19
+ visitor = FindMethodCallsVisitor.new(context, source_file, accumulator)
20
+ visitor.traverse(source_file.root)
21
+ end
22
+
23
+ def generate(accumulator, project)
24
+ # Generate a summary report file
25
+ calls = accumulator[:calls]
26
+ return [] if calls.empty?
27
+
28
+ content = "# Method Call Analysis Report\n\n"
29
+ content += "Total calls found: #{calls.size}\n\n"
30
+
31
+ # Group by method name
32
+ by_method = calls.group_by { |c| c[:method] }
33
+ by_method.each do |method, method_calls|
34
+ content += "## #{method} (#{method_calls.size} calls)\n"
35
+ method_calls.each do |call|
36
+ receiver = call[:receiver] ? "#{call[:receiver]}." : ""
37
+ content += " - #{receiver}#{method} at #{call[:file]}:#{call[:line]}\n"
38
+ end
39
+ content += "\n"
40
+ end
41
+
42
+ [Reweave::Results::FileChange.new(
43
+ path: "method_calls_report.md",
44
+ original_source: "",
45
+ updated_source: content,
46
+ recipes: [display_name],
47
+ change_type: :created
48
+ )]
49
+ end
50
+
51
+ class FindMethodCallsVisitor < Reweave::Visitor::RubyVisitor
52
+ def initialize(context, source_file, accumulator)
53
+ super(context, source_file)
54
+ @accumulator = accumulator
55
+ @source_file = source_file
56
+ end
57
+
58
+ def visit_call_node(node)
59
+ receiver_name = extract_receiver_name(node)
60
+ @accumulator[:calls] << {
61
+ method: node.name,
62
+ receiver: receiver_name,
63
+ file: @source_file.path,
64
+ line: node.location.start_line
65
+ }
66
+ super
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../reweave/recipe"
4
+ require_relative "../../../reweave/visitor/ruby_visitor"
5
+
6
+ module Reweave
7
+ module Recipes
8
+ module Ruby
9
+ # Modernize Ruby syntax (e.g., old-style hash to new-style)
10
+ class ModernizeSyntax < Recipe
11
+ display_name "Modernize syntax"
12
+ description "Converts old Ruby syntax to modern equivalents"
13
+
14
+ option :convert_hash_arrows, type: [TrueClass, FalseClass], default: true, description: "Convert => to : for symbol keys"
15
+ option :convert_lambda_arrows, type: [TrueClass, FalseClass], default: true, description: "Convert ->() to lambda literal"
16
+ option :convert_block_args, type: [TrueClass, FalseClass], default: false, description: "Convert block args to it/_1 syntax"
17
+
18
+ def get_visitor(context)
19
+ ModernizeSyntaxVisitor.new(context, config)
20
+ end
21
+ end
22
+
23
+ class ModernizeSyntaxVisitor < Reweave::Visitor::RubyVisitor
24
+ def initialize(context, config)
25
+ super(context, nil)
26
+ @config = config
27
+ end
28
+
29
+ def run(source_file)
30
+ @source_file = source_file
31
+ traverse(source_file.root)
32
+ edits
33
+ end
34
+
35
+ def visit_hash_node(node)
36
+ return super unless @config[:convert_hash_arrows]
37
+
38
+ node.elements.each do |elem|
39
+ next unless elem.is_a?(Prism::AssocNode)
40
+ next unless elem.key.is_a?(Prism::SymbolNode)
41
+
42
+ # Convert :key => value to key: value
43
+ key_loc = elem.key.location
44
+ operator_loc = elem.operator_loc
45
+
46
+ if operator_loc && operator_loc.start_offset == key_loc.end_offset
47
+ # This is a hash rocket, convert it
48
+ replace_location(operator_loc, ":")
49
+ # Remove the leading colon from the symbol
50
+ replace_location(key_loc, key_loc.slice.sub(/^:/, ""))
51
+ end
52
+ end
53
+ super
54
+ end
55
+
56
+ def visit_lambda_node(node)
57
+ return super unless @config[:convert_lambda_arrows]
58
+
59
+ # Convert -> { } to lambda { }
60
+ if node.opening_loc && node.opening_loc.slice == "->"
61
+ replace_location(node.opening_loc, "lambda")
62
+ end
63
+ super
64
+ end
65
+
66
+ def visit_block_parameters_node(node)
67
+ return super unless @config[:convert_block_args]
68
+
69
+ # Convert |x| to |_1| or |it| for single argument
70
+ if node.parameters.size == 1 && node.parameters.first.is_a?(Prism::RequiredParameterNode)
71
+ param = node.parameters.first
72
+ if param.name.start_with?("_") || param.name == "it" || param.name == "x"
73
+ replace_location(param.location, "it")
74
+ end
75
+ end
76
+ super
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../reweave/recipe/scanning_recipe"
4
+ require_relative "../../../reweave/visitor/ruby_visitor"
5
+
6
+ module Reweave
7
+ module Recipes
8
+ module Ruby
9
+ # Composite recipe that scans for method definitions and calls, then removes unused methods
10
+ class RemoveUnusedMethods < CompositeRecipe
11
+ display_name "Remove unused methods"
12
+ description "Finds and removes method definitions that are never called"
13
+
14
+ def initialize(**options)
15
+ scanner = FindMethodDefinitions.new(**options)
16
+ remover = RemoveUnusedMethodVisitor.new(scanner)
17
+ super([scanner, remover], **options)
18
+ end
19
+
20
+ # Scanning phase: find all method definitions and calls
21
+ class FindMethodDefinitions < ScanningRecipe
22
+ display_name "Find method definitions"
23
+ description "Scans for all method definitions and calls"
24
+
25
+ def initial_accumulator
26
+ {definitions: {}, calls: Set.new}
27
+ end
28
+
29
+ def scan(source_file, accumulator, context)
30
+ visitor = DefinitionScanner.new(context, source_file, accumulator)
31
+ visitor.traverse(source_file.root)
32
+ end
33
+
34
+ def generate(accumulator, project)
35
+ # No file generation for this phase
36
+ []
37
+ end
38
+
39
+ class DefinitionScanner < Reweave::Visitor::RubyVisitor
40
+ def initialize(context, source_file, accumulator)
41
+ super(context, source_file)
42
+ @accumulator = accumulator
43
+ @source_file = source_file
44
+ end
45
+
46
+ def visit_def_node(node)
47
+ method_name = node.name.to_s
48
+ @accumulator[:definitions][method_name] = {
49
+ node: node,
50
+ file: @source_file.path,
51
+ line: node.location.start_line
52
+ }
53
+ super
54
+ end
55
+
56
+ def visit_call_node(node)
57
+ @accumulator[:calls].add(node.name.to_s)
58
+ super
59
+ end
60
+ end
61
+ end
62
+
63
+ # Transformation phase: remove unused methods
64
+ class RemoveUnusedMethodVisitor < Reweave::Visitor::RubyVisitor
65
+ def initialize(scanner)
66
+ super(nil, nil)
67
+ @scanner = scanner
68
+ end
69
+
70
+ def run(source_file, context, accumulator)
71
+ @context = context
72
+ @accumulator = accumulator
73
+ @source_file = source_file
74
+ traverse(source_file.root)
75
+ edits
76
+ end
77
+
78
+ def visit_def_node(node)
79
+ method_name = node.name.to_s
80
+ # Check if this method is called anywhere
81
+ calls = @accumulator[:calls]
82
+ return super if calls.include?(method_name)
83
+
84
+ # Method is unused, remove it
85
+ node.location
86
+ delete(node)
87
+ super
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../reweave/recipe"
4
+
5
+ module Reweave
6
+ module Recipes
7
+ module Ruby
8
+ class RenameMethod < Recipe
9
+ display_name "Rename method"
10
+
11
+ description "Renames a method call across the codebase."
12
+
13
+ option :old_name, type: String, required: true, description: "Old method name"
14
+ option :new_name, type: String, required: true, description: "New method name"
15
+ option :receiver, type: String, required: false, description: "Optional receiver to match (e.g., 'User')"
16
+
17
+ def get_visitor(context)
18
+ RenameMethodVisitor.new(context, old_name: config[:old_name], new_name: config[:new_name], receiver: config[:receiver])
19
+ end
20
+ end
21
+
22
+ class RenameMethodVisitor < Reweave::Visitor::RubyVisitor
23
+ def initialize(context, old_name:, new_name:, receiver: nil)
24
+ super(context, nil)
25
+ @old_name = old_name.to_sym
26
+ @new_name = new_name
27
+ @receiver = receiver
28
+ end
29
+
30
+ def run(source_file)
31
+ @source_file = source_file
32
+ visit(source_file.root)
33
+ edits
34
+ end
35
+
36
+ def visit_call_node(node)
37
+ return super unless node.name == @old_name
38
+
39
+ if @receiver
40
+ receiver_name = extract_receiver_name(node)
41
+ return super unless receiver_name == @receiver
42
+ end
43
+
44
+ # Replace just the method name
45
+ loc = node.message_loc
46
+ if loc
47
+ replace_location(loc, @new_name)
48
+ end
49
+
50
+ super
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "diffy"
4
+
5
+ module Reweave
6
+ module Results
7
+ class FileChange
8
+ attr_reader :path, :original_source, :updated_source, :recipes, :change_type, :diff
9
+
10
+ def initialize(path:, original_source:, updated_source:, recipes:, change_type: :modified)
11
+ @path = path
12
+ @original_source = original_source
13
+ @updated_source = updated_source
14
+ @recipes = recipes
15
+ @change_type = change_type
16
+ @diff = generate_diff
17
+ end
18
+
19
+ def changed?
20
+ return true if @change_type == :created || @change_type == :deleted || @change_type == :renamed
21
+ @original_source != @updated_source
22
+ end
23
+
24
+ def modified?
25
+ @change_type == :modified
26
+ end
27
+
28
+ def created?
29
+ @change_type == :created
30
+ end
31
+
32
+ def deleted?
33
+ @change_type == :deleted
34
+ end
35
+
36
+ def renamed?
37
+ @change_type == :renamed
38
+ end
39
+
40
+ private
41
+
42
+ def generate_diff
43
+ return "" unless changed?
44
+ Diffy::Diff.new(@original_source, @updated_source, context: 3).to_s(:text)
45
+ end
46
+ end
47
+
48
+ class ChangeSet
49
+ include Enumerable
50
+
51
+ def initialize(changes = [])
52
+ @changes = changes
53
+ end
54
+
55
+ def add(change)
56
+ @changes << change
57
+ end
58
+
59
+ def each(&block)
60
+ @changes.each(&block)
61
+ end
62
+
63
+ def changed_files
64
+ select(&:changed?)
65
+ end
66
+
67
+ def modified_files
68
+ select(&:modified?)
69
+ end
70
+
71
+ def created_files
72
+ select(&:created?)
73
+ end
74
+
75
+ def deleted_files
76
+ select(&:deleted?)
77
+ end
78
+
79
+ def recipes
80
+ @changes.flat_map(&:recipes).uniq
81
+ end
82
+
83
+ def empty?
84
+ @changes.empty?
85
+ end
86
+
87
+ def size
88
+ @changes.size
89
+ end
90
+
91
+ def to_a
92
+ @changes.dup
93
+ end
94
+ end
95
+ end
96
+ end