Class: RailsRoutesAnalyzer::RouteFileAnnotator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_routes_analyzer/route_file_annotator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(try_to_fix: false, allow_deleting: false, force_overwrite: false, analysis: nil, **kwargs) ⇒ RouteFileAnnotator

Returns a new instance of RouteFileAnnotator.

Parameters:

  • try_to_fix (Boolean) (defaults to: false)

    should automatic fixes be attempted

  • allow_deleting (Boolean) (defaults to: false)

    should route lines be deleted when they match no actions

  • force_overwrite (Boolean) (defaults to: false)

    allow overwriting routes file even if it has uncommited changes or is outside Rails.root



7
8
9
10
11
12
# File 'lib/rails_routes_analyzer/route_file_annotator.rb', line 7

def initialize(try_to_fix: false, allow_deleting: false, force_overwrite: false, analysis: nil, **kwargs)
  @analysis = analysis || RailsRoutesAnalyzer::RouteAnalysis.new(**kwargs)
  @try_to_fix = try_to_fix
  @allow_deleting = allow_deleting
  @force_overwrite = force_overwrite
end

Class Method Details

.check_file_git_status(filename, report: false, skip_git: false, repo_root: Rails.root.to_s) ⇒ Object



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
# File 'lib/rails_routes_analyzer/route_file_annotator.rb', line 95

def check_file_git_status(filename, report: false, skip_git: false, repo_root: Rails.root.to_s)
  return skip_git if skip_git

  git = nil
  begin
    require 'git'
    git = Git.open(repo_root)
  rescue => e
    log_notice "Couldn't access git repository at Rails root #{repo_root}. #{e.message}" if report
    return false
  end

  repo_relative_filename = filename.to_s.sub("#{repo_root}/", '')

  # This seems to be required to force some kind of git status
  # refresh because without it tests would randomly detect a file
  # as modified by git-status when the file in fact has no changes.
  #
  # Currently randomly causes a NoMethodError exception but can still help.
  begin
    git.diff.each { |file| }
  rescue NoMethodError # rubocop:disable Lint/HandleExceptions
  end

  if git.status.changed.key?(repo_relative_filename)
    log_notice "Refusing to modify '#{repo_relative_filename}' as it has uncommited changes" if report
    return false
  end

  true
end

.check_file_is_modifiable(filename, report: false, **kwargs) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/rails_routes_analyzer/route_file_annotator.rb', line 86

def check_file_is_modifiable(filename, report: false, **kwargs)
  unless filename.to_s.starts_with?(Rails.root.to_s)
    log_notice "Refusing to modify files outside Rails root: #{Rails.root}" if report
    return false
  end

  check_file_git_status(filename, report: report, **kwargs)
end

.log_notice(message = nil) ⇒ Object



80
81
82
83
84
# File 'lib/rails_routes_analyzer/route_file_annotator.rb', line 80

def log_notice(message = nil)
  return if ENV['RAILS_ENV'] == 'test'
  message ||= yield if block_given?
  $stderr.puts "# #{message}" if message.present?
end

Instance Method Details

#annotate_routes_file(route_filename, inplace: false, do_exit: true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails_routes_analyzer/route_file_annotator.rb', line 41

def annotate_routes_file(route_filename, inplace: false, do_exit: true)
  filenames = files_to_work_on(route_filename, inplace: inplace)
  if filenames.is_a?(Integer)
    exit filenames if do_exit
    return filenames
  end

  filenames.map! { |file| RailsRoutesAnalyzer.get_full_filename(file) }

  filenames.each do |filename|
    unless File.exist?(filename)
      $stderr.puts "Can't find routes file: #{filename}"
      exit 1
    end
  end

  if filenames.size != 1 && !inplace
    raise ArgumentError, "got #{filenames.size} files but can annotate only one at a time to stdout"
  end

  filenames.each do |filename|
    if inplace
      if @force_overwrite || self.class.check_file_is_modifiable(filename, report: true)
        content = annotated_file_content(filename)
        File.open(filename, 'w') { |f| f.write content }
      end
    else
      puts annotated_file_content(filename)
    end
  end
end

#annotated_file_content(route_filename) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rails_routes_analyzer/route_file_annotator.rb', line 14

def annotated_file_content(route_filename)
  route_lines = @analysis.route_lines_for_file(route_filename)

  if route_lines.none?(&:issues?)
    log_notice { "Didn't find any route issues for file: #{route_filename}, only found issues in files: #{@analysis.all_unique_issues_file_names.join(', ')}" }
  end

  log_notice { "Annotating #{route_filename}" }

  route_lines_map = route_lines.index_by(&:line_number)

  "".tap do |output|
    File.readlines(route_filename).each_with_index do |line, index|
      route_line = route_lines_map[index + 1]

      output <<
        if route_line
          route_line.annotate(line,
                              try_to_fix:     @try_to_fix,
                              allow_deleting: @allow_deleting)
        else
          line
        end
    end
  end
end