Class: AvoUpgrade::UpgradeTool

Inherits:
Object
  • Object
show all
Defined in:
lib/avo_upgrade/upgrade_tool.rb

Direct Known Subclasses

Upgrade29to30

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/avo_upgrade/upgrade_tool.rb', line 7

def run
  upgrade_tool = new
  puts "DISCLAIMER: Please be aware that this is an automated upgrade and may make unintended changes to your files. We recommend that you carefully review all changes before committing them. While we have taken steps to ensure that the upgrade process is safe and reliable, there is always a possibility of unintended consequences. It is your responsibility to ensure that the modifications made by the upgrade are acceptable for your use case."
  upgrade_tool.enter_to_continue
  print upgrade_tool.summary
  puts "Please make sure you commited all your changes before running this upgrade."
  print "Do you want to run this upgrade? [y/n]: "
  input = gets.chomp
  return unless input == "y" || input == "Y"
  upgrade_tool.run
end

Instance Method Details

#avo_global_filesObject



20
21
22
23
24
# File 'lib/avo_upgrade/upgrade_tool.rb', line 20

def avo_global_files
  Dir.glob(File.join(Rails.root.join("app", "avo"), '**/*')).select { |f| File.file?(f) } +
  Dir.glob(File.join(Rails.root.join("app", "controllers", "avo"), '**/*')).select { |f| File.file?(f) } +
  Dir.glob(File.join(Rails.root.join("app", "views", "avo"), '**/*')).select { |f| File.file?(f) }
end

#class_names_for(component) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/avo_upgrade/upgrade_tool.rb', line 26

def class_names_for(component)
  names = []

  Dir.glob(File.join(send("#{component}_path"), '**/*.rb')).each do |file|
    # Match class definitions in the file
    File.read(file).scan(/class\s+(\w+)/).each do |match|
      # Add the matched class names to the list
      names << match.first
    end
  end

  names
end

#enter_to_continueObject



86
87
88
89
# File 'lib/avo_upgrade/upgrade_tool.rb', line 86

def enter_to_continue
  print "\nPress ENTER to continue."
  gets.chomp
end

#files_from(path) ⇒ Object



48
49
50
# File 'lib/avo_upgrade/upgrade_tool.rb', line 48

def files_from(path)
  Dir.glob("#{path}/*.rb").select { |file| File.file?(file) }
end

#remove_block_arg_on(files, text_array) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/avo_upgrade/upgrade_tool.rb', line 69

def remove_block_arg_on(files, text_array)
  match_array = text_array.map do |text|
    if proc_arg?(text)
      proc_arg_match_params(text)
    else
      lambda_arg_match_params(text)
    end
  end
  replace_text_on(files, match_array.to_h, exact_match: false)
end

#remove_config(config) ⇒ Object

Receives the code at the beginning of a line and deletes the line.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/avo_upgrade/upgrade_tool.rb', line 92

def remove_config(config)
  avo_config_path = File.join Rails.root.join("config", "initializers", "avo.rb")

  # Read the content of the file into an array of lines
  lines = File.readlines(avo_config_path)

  # Open the file for writing
  File.open(avo_config_path, "w") do |file|
    lines.each do |line|
      # Check if the line starts with the given config code
      if line.strip.start_with?(config)
        # Do nothing, skip the line
      else
        # Write the line to the file
        file.puts(line)
      end
    end
  end
end

#remove_text_on(files, text_array) ⇒ Object



65
66
67
# File 'lib/avo_upgrade/upgrade_tool.rb', line 65

def remove_text_on(files, text_array)
  replace_text_on(files, text_array.map { |text| [text, ""] }.to_h, exact_match: false)
end

#replace_in_filename(old_text, new_text, path:) ⇒ Object



80
81
82
83
84
# File 'lib/avo_upgrade/upgrade_tool.rb', line 80

def replace_in_filename(old_text, new_text, path:)
  Dir.glob("#{path}/*.rb").each do |file_path|
    `#{@mv_cmd == "1" ? "git mv" : "mv"} #{file_path} #{file_path.gsub(/#{old_text}/, new_text)}`
  end
end

#replace_text_on(files, hash, exact_match: true) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/avo_upgrade/upgrade_tool.rb', line 52

def replace_text_on(files, hash, exact_match: true)
  files.each do |file|
    text = File.read(file)

    hash.each do |old_text, new_text|
      old_text = /\b#{Regexp.escape(old_text)}\b/ if exact_match
      text.gsub!(old_text, new_text)
    end

    File.open(file, 'w') { |f| f.write(text) }
  end
end