Class: Herb::ActionView::RenderAnalyzer
- Inherits:
-
Object
- Object
- Herb::ActionView::RenderAnalyzer
- Includes:
- Colors
- Defined in:
- lib/herb/action_view/render_analyzer.rb
Defined Under Namespace
Classes: Result
Constant Summary
Constants included from Colors
Colors::CLEAR_SCREEN, Colors::HIDE_CURSOR, Colors::SHOW_CURSOR
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#project_path ⇒ Object
readonly
Returns the value of attribute project_path.
Instance Method Summary collapse
- #analyze(erb_files = nil, view_root = nil) ⇒ Object
- #analyze_from_collected(render_calls_by_file:, dynamic_prefixes_from_erb: [], layout_refs_from_erb: []) ⇒ Object
- #check! ⇒ Object
- #fully_resolvable?(file_path) ⇒ Boolean
- #graph! ⇒ Object
- #graph_file!(file_path) ⇒ Object
-
#initialize(project_path, configuration: nil) ⇒ RenderAnalyzer
constructor
A new instance of RenderAnalyzer.
- #print_file_lists(result) ⇒ Object
- #print_issue_summary(result) ⇒ Object
- #print_summary_line(result) ⇒ Object
Methods included from Colors
bold, bright_magenta, cyan, dimmed, enabled?, fg, fg_bg, green, magenta, red, white, yellow
Constructor Details
#initialize(project_path, configuration: nil) ⇒ RenderAnalyzer
Returns a new instance of RenderAnalyzer.
21 22 23 24 |
# File 'lib/herb/action_view/render_analyzer.rb', line 21 def initialize(project_path, configuration: nil) @project_path = Pathname.new(File.(project_path)) @configuration = configuration || Configuration.load(@project_path.to_s) end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
19 20 21 |
# File 'lib/herb/action_view/render_analyzer.rb', line 19 def configuration @configuration end |
#project_path ⇒ Object (readonly)
Returns the value of attribute project_path.
19 20 21 |
# File 'lib/herb/action_view/render_analyzer.rb', line 19 def project_path @project_path end |
Instance Method Details
#analyze(erb_files = nil, view_root = nil) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/herb/action_view/render_analyzer.rb', line 371 def analyze(erb_files = nil, view_root = nil) erb_files ||= find_erb_files view_root ||= find_view_root render_calls_by_file = collect_render_calls_by_file(erb_files) ruby_partial_references = collect_ruby_render_references partial_files = find_partial_files(view_root) all_render_calls = render_calls_by_file.values.flatten static_calls, dynamic_calls = partition_dynamic(all_render_calls) render_graph = build_render_graph(render_calls_by_file, partial_files, view_root) unresolved = find_unresolved(static_calls, partial_files, view_root) unused = find_unused_by_reachability( render_graph, partial_files, ruby_partial_references, collect_all_dynamic_prefixes(dynamic_calls, ruby_partial_references), view_root ) Result.new( render_calls: all_render_calls, dynamic_calls: dynamic_calls, partial_files: partial_files, unresolved: unresolved, unused: unused, view_root: view_root ) end |
#analyze_from_collected(render_calls_by_file:, dynamic_prefixes_from_erb: [], layout_refs_from_erb: []) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/herb/action_view/render_analyzer.rb', line 402 def analyze_from_collected(render_calls_by_file:, dynamic_prefixes_from_erb: [], layout_refs_from_erb: []) view_root = find_view_root @dynamic_prefixes_from_erb = dynamic_prefixes_from_erb @layout_refs_from_erb = layout_refs_from_erb ruby_partial_references = collect_ruby_render_references partial_files = find_partial_files(view_root) all_render_calls = render_calls_by_file.values.flatten static_calls, dynamic_calls = partition_dynamic(all_render_calls) render_graph = build_render_graph(render_calls_by_file, partial_files, view_root) unresolved = find_unresolved(static_calls, partial_files, view_root) unused = find_unused_by_reachability( render_graph, partial_files, ruby_partial_references, collect_all_dynamic_prefixes(dynamic_calls, ruby_partial_references), view_root ) Result.new( render_calls: all_render_calls, dynamic_calls: dynamic_calls, partial_files: partial_files, unresolved: unresolved, unused: unused, view_root: view_root ) end |
#check! ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/herb/action_view/render_analyzer.rb', line 26 def check! start_time = Time.now erb_files = find_erb_files view_root = find_view_root if erb_files.empty? puts "No ERB files found." return false end puts "" puts "#{bold("Herb")} \u{1f33f} #{dimmed("v#{Herb::VERSION}")}" puts "" if configuration.config_path puts "#{green("\u2713")} Using Herb config file at #{dimmed(configuration.config_path.to_s)}" else puts dimmed("No .herb.yml found, using defaults") end puts dimmed("Checking render calls in #{erb_files.count} #{pluralize(erb_files.count, "file")}...") result = analyze(erb_files, view_root) duration = Time.now - start_time print_results(result, duration) result.issues? end |
#fully_resolvable?(file_path) ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/herb/action_view/render_analyzer.rb', line 57 def fully_resolvable?(file_path) file_path = @project_path.join(file_path).to_s unless Pathname.new(file_path).absolute? erb_files = find_erb_files view_root = find_view_root render_calls_by_file = collect_render_calls_by_file(erb_files) partial_files = find_partial_files(view_root) visited = Set.new queue = [file_path] while (current = queue.shift) next if visited.include?(current) visited << current calls = render_calls_by_file[current] || [] calls.each do |call| return false if call[:dynamic] partial_ref = call[:partial] || call[:layout] next unless partial_ref return false if dynamic_partial?(partial_ref) resolved = resolve_partial(partial_ref, current, partial_files, view_root) return false unless resolved queue << resolved end end true end |
#graph! ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/herb/action_view/render_analyzer.rb', line 221 def graph! erb_files = find_erb_files view_root = find_view_root if erb_files.empty? puts "No ERB files found." return end puts "" puts "#{bold("Herb")} \u{1f33f} #{dimmed("v#{Herb::VERSION}")}" puts "" if configuration.config_path puts "#{green("\u2713")} Using Herb config file at #{dimmed(configuration.config_path.to_s)}" else puts dimmed("No .herb.yml found, using defaults") end puts dimmed("Building render graph for #{erb_files.count} #{pluralize(erb_files.count, "file")}...") render_calls_by_file = collect_render_calls_by_file(erb_files) ruby_partial_references = collect_ruby_render_references partial_files = find_partial_files(view_root) render_graph = build_render_graph(render_calls_by_file, partial_files, view_root) all_render_calls = render_calls_by_file.values.flatten _, dynamic_calls = partition_dynamic(all_render_calls) dynamic_prefixes = collect_all_dynamic_prefixes(dynamic_calls, ruby_partial_references) reverse_graph = Hash.new { |hash, key| hash[key] = [] } #: Hash[String, Array[String]] render_graph.each do |file, partial_names| next if file.start_with?("__") display_name = file_display_name(file, view_root) partial_names.each do |partial_name| reverse_graph[partial_name] << display_name end end ruby_partial_references.each do |reference| next unless reference.is_a?(String) reverse_graph[reference] << "#{dimmed("[Ruby]")} #{reference}" end entry_points = render_graph.keys.reject { |file| file.start_with?("__") || File.basename(file).start_with?("_") }.sort reachable = compute_reachable(render_graph, partial_files, ruby_partial_references, dynamic_prefixes) puts "" puts separator puts "" if entry_points.any? puts " #{bold("Entry points:")} #{dimmed("(#{entry_points.count} #{pluralize(entry_points.count, "template")})")}" entry_points.each do |file| display = file_display_name(file, view_root) partials = render_graph[file] || [] puts "" puts " #{cyan(display)}" if partials.empty? puts " #{dimmed("(no render calls)")}" else print_partial_tree(partials, render_graph, partial_files, view_root, reachable, indent: " ", visited: Set.new) end end end ruby_static_references = ruby_partial_references.select { |reference| reference.is_a?(String) } if ruby_static_references.any? puts "" puts " #{separator}" puts "" puts " #{bold("Ruby references:")} #{dimmed("(#{ruby_static_references.count} #{pluralize(ruby_static_references.count, "partial")})")}" ruby_static_references.sort.each do |reference| resolved = partial_files[reference] status = resolved ? green("\u2713") : red("\u2717") puts "" puts " #{status} #{bold(reference)}" if resolved children = render_graph[resolved] || [] print_partial_tree(children, render_graph, partial_files, view_root, reachable, indent: " ", visited: Set.new) end end end unreachable = partial_files.except(*reachable) puts "" puts " #{separator}" puts "" puts " #{bold("Partial usage:")} #{dimmed("(who renders each partial)")}" partial_files.keys.sort.each do |name| callers = reverse_graph[name] status = reachable.include?(name) ? green("\u2713") : yellow("~") puts "" puts " #{status} #{bold(name)}" if callers.any? callers.sort.each_with_index do |caller_name, index| connector = index == callers.size - 1 ? "\u2514\u2500\u2500" : "\u251c\u2500\u2500" puts " #{connector} #{dimmed("rendered by")} #{caller_name}" end else puts " #{dimmed("(not rendered by any file)")}" end end if unreachable.any? puts "" puts " #{separator}" puts "" puts " #{bold(yellow("Unreachable partials:"))} #{dimmed("(#{unreachable.count} #{pluralize(unreachable.count, "file")})")}" unreachable.each do |name, file| display = file_display_name(file, view_root) children = render_graph[file] || [] puts "" puts " #{yellow("~")} #{bold(name)} #{dimmed(display)}" if children.any? print_partial_tree(children, render_graph, partial_files, view_root, reachable, indent: " ", visited: Set.new) end end end puts "" puts " #{separator}" puts "" puts " #{bold("Summary:")}" puts " #{label("Entry points")} #{cyan(entry_points.count.to_s)}" puts " #{label("Partials")} #{cyan(partial_files.count.to_s)}" puts " #{label("Reachable")} #{bold(green(reachable.count.to_s))}" puts " #{label("Unreachable")} #{unreachable.any? ? bold(yellow(unreachable.count.to_s)) : bold(green("0"))}" puts "" end |
#graph_file!(file_path) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/herb/action_view/render_analyzer.rb', line 94 def graph_file!(file_path) is_partial = File.basename(file_path).start_with?("_") puts "" puts " #{bold("Herb")} \u{1f33f} #{dimmed("v#{Herb::VERSION}")}" puts "" puts " #{dimmed("Building render graph...")}" erb_files = find_erb_files view_root = find_view_root if erb_files.empty? puts "No ERB files found in project." return end render_calls_by_file = collect_render_calls_by_file(erb_files) ruby_partial_references = collect_ruby_render_references partial_files = find_partial_files(view_root) render_graph = build_render_graph(render_calls_by_file, partial_files, view_root) all_render_calls = render_calls_by_file.values.flatten _, dynamic_calls = partition_dynamic(all_render_calls) dynamic_prefixes = collect_all_dynamic_prefixes(dynamic_calls, ruby_partial_references) reachable = compute_reachable(render_graph, partial_files, ruby_partial_references, dynamic_prefixes) reverse_graph = Hash.new { |hash, key| hash[key] = [] } #: Hash[String, Array[String]] render_graph.each do |file, partial_names| next if file.start_with?("__") partial_names.each do |partial_name| reverse_graph[partial_name] << file end end ruby_partial_references.each do |reference| next unless reference.is_a?(String) reverse_graph[reference] << "__ruby__" end puts "" if is_partial partial_name = partial_name_for_file(file_path, view_root) unless partial_name puts "Could not determine partial name for: #{file_path}" return end display = file_display_name(file_path, view_root) status = reachable.include?(partial_name) ? green("\u2713") : yellow("~") puts " #{status} #{bold(partial_name)} #{dimmed(display)}" puts "" callers = reverse_graph[partial_name] if callers.any? puts " #{bold("Rendered by:")}" callers.each_with_index do |caller_file, index| connector = index == callers.size - 1 ? "\u2514\u2500\u2500" : "\u251c\u2500\u2500" if caller_file == "__ruby__" puts " #{connector} #{dimmed("[Ruby code]")}" else caller_display = file_display_name(caller_file, view_root) caller_basename = File.basename(caller_file) caller_status = caller_basename.start_with?("_") ? dimmed("(partial)") : dimmed("(entry point)") puts " #{connector} #{cyan(caller_display)} #{caller_status}" end end puts "" puts " #{bold("Reachable from:")}" entry_chains = trace_to_entry_points(partial_name, reverse_graph, partial_files, view_root) if entry_chains.any? entry_chains.each_with_index do |chain, index| connector = index == entry_chains.size - 1 ? "\u2514\u2500\u2500" : "\u251c\u2500\u2500" reversed = chain.reverse chain_display = reversed.each_with_index.map do |name, i| if i == 0 bold(green(name)) elsif i == reversed.size - 1 bold(name) else dimmed(name) end end.join(dimmed(" \u2192 ")) puts " #{connector} #{chain_display}" end else puts " #{yellow("(not reachable from any entry point)")}" end else puts " #{dimmed("Not rendered by any file.")}" end children = render_graph[file_path] || [] if children.any? puts "" puts " #{bold("Renders:")}" print_partial_tree(children, render_graph, partial_files, view_root, reachable, indent: " ", visited: Set.new) end else display = file_display_name(file_path, view_root) puts " #{cyan(display)} #{dimmed("(entry point)")}" puts "" children = render_graph[file_path] || [] if children.any? puts " #{bold("Renders:")}" print_partial_tree(children, render_graph, partial_files, view_root, reachable, indent: " ", visited: Set.new) else puts " #{dimmed("No render calls in this file.")}" end end puts "" end |
#print_file_lists(result) ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/herb/action_view/render_analyzer.rb', line 434 def print_file_lists(result) return unless result.issues? if result.unresolved.any? puts "\n" puts " #{bold("Unresolved render calls:")}" puts " #{dimmed("These render calls reference partials that could not be found on disk.")}" grouped = result.unresolved.group_by { |call| call[:file] } grouped.each do |file, calls| relative = relative_path(file) puts "" puts " #{cyan(relative)}:" calls.each do |call| location = call[:location] ? dimmed("at #{call[:location]}") : nil expected = expected_file_path(call[:partial], result.view_root) puts " #{red("\u2717")} #{bold(call[:partial])} #{location} #{dimmed("-")} #{dimmed(expected)}" end end end return unless result.unused.any? puts "\n #{separator}" if result.unresolved.any? puts "\n" puts " #{bold("Unused partials:")}" puts " #{dimmed("These partial files are not referenced by any reachable render call.")}" result.unused.each do |name, file| relative = relative_path(file) puts "" puts " #{cyan(relative)}:" puts " #{yellow("~")} #{bold(name)} #{dimmed("not referenced")}" end end |
#print_issue_summary(result) ⇒ Object
474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/herb/action_view/render_analyzer.rb', line 474 def print_issue_summary(result) return unless result.issues? if result.unresolved.any? files_count = result.unresolved.map { |call| call[:file] }.uniq.count puts " #{white("Unresolved partials")} #{dimmed("(#{result.unresolved.count} #{pluralize(result.unresolved.count, "reference")} in #{files_count} #{pluralize(files_count, "file")})")}" end return unless result.unused.any? puts " #{white("Unused partials")} #{dimmed("(#{result.unused.count} #{pluralize(result.unused.count, "file")})")}" end |
#print_summary_line(result) ⇒ Object
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/herb/action_view/render_analyzer.rb', line 488 def print_summary_line(result) render_parts = [] #: Array[String] partials_only = result.render_calls.count { |call| call[:partial] } render_parts << stat(result.render_calls.count, "total", :green) render_parts << stat(partials_only, "with partial", :green) render_parts << stat(result.dynamic_calls.count, "dynamic", :yellow) if result.dynamic_calls.any? other_count = result.render_calls.count - partials_only render_parts << stat(other_count, "other", :green) if other_count.positive? partial_parts = [] #: Array[String] partial_parts << stat(result.partial_files.count, "on disk", :green) partial_parts << stat(result.unresolved.count, "unresolved", :red) if result.unresolved.any? partial_parts << stat(result.unused.count, "unused", :yellow) if result.unused.any? puts " #{label("Renders")} #{render_parts.join(" | ")}" puts " #{label("Partials")} #{partial_parts.join(" | ")}" end |