Class: BulldozeRenamer::RenamingOrchestrator
- Inherits:
-
Object
- Object
- BulldozeRenamer::RenamingOrchestrator
- Defined in:
- lib/bulldoze_renamer/renaming_orchestrator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #count_in_content(target, content) ⇒ Object
- #file_occurences ⇒ Object
- #files ⇒ Object
- #files_that_have_occurences ⇒ Object
- #find_occurences(file, mappings) ⇒ Object
- #format_number(number) ⇒ Object
- #inflections_mapping ⇒ Object
- #inflections_that_are_present ⇒ Object
-
#initialize(path:, current_name:, target_name:, perform: nil) ⇒ RenamingOrchestrator
constructor
A new instance of RenamingOrchestrator.
- #new_filename_for(filename) ⇒ Object
- #perform!(out:) ⇒ Object
- #perform_directory!(dirname, out) ⇒ Object
- #perform_file!(filename, occurences, out) ⇒ Object
- #report_file_occurences ⇒ Object
- #report_file_occurences_line(file) ⇒ Object
- #report_header_file_occurences ⇒ Object
- #report_inflections_mapping ⇒ Object
- #reports_for_files ⇒ Object
- #was_found(occ) ⇒ Object
Constructor Details
#initialize(path:, current_name:, target_name:, perform: nil) ⇒ RenamingOrchestrator
5 6 7 8 9 10 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 5 def initialize(path:, current_name:, target_name:, perform: nil) @path = path @current_name = current_name @target_name = target_name @perform = perform end |
Class Method Details
.rename_with_options(options, out:, err:) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 173 def self.(, out:, err:) orch = RenamingOrchestrator.new() out.puts orch.report_inflections_mapping if orch.files_that_have_occurences.empty? out.puts "'#{[:current_name]}' can not be found in any of the files in '#{[:path]}'" else out.puts orch.reports_for_files if [:perform] puts orch.perform!(out: out) else out.puts "\nThis is an overview of changes that would be made\nRun same command with -p option to perform" end end end |
Instance Method Details
#count_in_content(target, content) ⇒ Object
12 13 14 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 12 def count_in_content(target, content) ('~' + content + '~').split(target).size - 1 end |
#file_occurences ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 52 def file_occurences return @file_occurences if @file_occurences result = {} files.each do |file| result[file] = find_occurences(file, inflections_mapping) end @file_occurences = result end |
#files ⇒ Object
48 49 50 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 48 def files @files ||= FileFinder.find(@path) end |
#files_that_have_occurences ⇒ Object
87 88 89 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 87 def files_that_have_occurences files.select { |f| was_found(file_occurences[f]) } end |
#find_occurences(file, mappings) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 16 def find_occurences(file, mappings) result = {} Dir.chdir @path do occurs_in_filename = mappings[:inflections][:underscore] && File.basename(file).include?(mappings[:inflections][:underscore][:current]) occurs_in_filename ||= mappings[:inflections][:dasherize] && File.basename(file).include?(mappings[:inflections][:dasherize][:current]) result[:filename] = occurs_in_filename ? 1 : 0 unless FileTest.directory?(file) mappings[:inflections].each do |inflection, values| result[inflection] = count_in_content(values[:current], File.read(file)) end end end result end |
#format_number(number) ⇒ Object
40 41 42 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 40 def format_number(number) number && number > 0 ? "%4d" % number : ' _' end |
#inflections_mapping ⇒ Object
44 45 46 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 44 def inflections_mapping @inflections_mapping ||= StringInflector.new(@current_name, @target_name).mappings end |
#inflections_that_are_present ⇒ Object
64 65 66 67 68 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 64 def inflections_that_are_present @inflections_that_are_present ||= StringInflector.inflections + [:filename] & file_occurences.values.inject({}) { |a,h| h.each { |k,v| v > 0 && (a[k] ||= 0 ; a[k] += v) };a }.keys end |
#new_filename_for(filename) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 114 def new_filename_for(filename) dirname = File.dirname(filename) basename = File.basename(filename) inflections_mapping[:inflections].each do |m, v| if basename.include?(v[:current]) return File.join(dirname, basename.gsub(v[:current], v[:target])) end end filename end |
#perform!(out:) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 155 def perform!(out:) out.puts "Performing:" directories = [] files_that_have_occurences.each do |filename| occurences = file_occurences[filename] if FileTest.directory?(File.join(@path, filename)) directories << filename else perform_file!(filename, occurences, out) end end directories.reverse.each do |dir| perform_directory!(dir, out) end end |
#perform_directory!(dirname, out) ⇒ Object
125 126 127 128 129 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 125 def perform_directory!(dirname, out) new_dirname = new_filename_for(dirname) out.puts "d #{dirname} -> #{new_dirname}" FileContentSubstitutor.new(@path).move_directory(dirname, new_dirname) end |
#perform_file!(filename, occurences, out) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 131 def perform_file!(filename, occurences, out) new_filename = if occurences[:filename] > 0 x = new_filename_for(filename) mode = 'r' unless occurences.select { |k,v| k != :filename && v > 0 }.empty? mode = 'R' end out.puts "#{mode} #{filename} -> #{x}" x else out.puts "f #{filename}" filename end present_mappings = occurences.select { |k,v| k != :filename && v > 0 } mappings = present_mappings.map do |m,c| inflections_mapping[:inflections][m].values end FileContentSubstitutor.new(@path). substitute_in(filename, new_filename, mappings) end |
#report_file_occurences ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 91 def report_file_occurences result = "" files_that_have_occurences.each do |f| result += report_file_occurences_line(f)[1..-1] end result end |
#report_file_occurences_line(file) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 79 def report_file_occurences_line(file) result = "" inflections_that_are_present.each_with_index do |inflection, index| result += format_number(file_occurences[file][inflection]) end result + ' ' + file + "\n" end |
#report_header_file_occurences ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 70 def report_header_file_occurences result = "" inflections_that_are_present.each_with_index do |inflection, index| result += (" |" * index + ' ' + inflection.to_s + "\n")[1..-1] end result += (" |" * inflections_that_are_present.size + "\n")[1..-1] result end |
#report_inflections_mapping ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 99 def report_inflections_mapping longest_current = inflections_mapping[:inflections].values.map { |v| v[:current].size }.max result = "" inflections_mapping[:inflections].each do |i, v| result += "%-11s: %-#{longest_current}s -> %s\n" % [i, v[:current], v[:target]] end result + "\n" end |
#reports_for_files ⇒ Object
109 110 111 112 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 109 def reports_for_files report_header_file_occurences + report_file_occurences end |
#was_found(occ) ⇒ Object
36 37 38 |
# File 'lib/bulldoze_renamer/renaming_orchestrator.rb', line 36 def was_found(occ) occ.values.sum > 0 end |