Class: Dependabot::Nuget::FileUpdater

Inherits:
FileUpdaters::Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/nuget/file_updater.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.differs_in_more_than_blank_lines?(original_content, updated_content) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dependabot/nuget/file_updater.rb', line 38

def self.differs_in_more_than_blank_lines?(original_content, updated_content)
  # Compare the line counts of the original and updated content, but ignore lines only containing white-space.
  # This prevents false positives when there are trailing empty lines in the original content, for example.
  original_lines = (original_content&.lines || []).map(&:strip).reject(&:empty?)
  updated_lines = updated_content.lines.map(&:strip).reject(&:empty?)

  # if the line count differs, then something changed
  return true unless original_lines.count == updated_lines.count

  # check each line pair, ignoring blanks (filtered above)
  original_lines.zip(updated_lines).any? { |pair| pair[0] != pair[1] }
end

.updated_files_regexObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dependabot/nuget/file_updater.rb', line 20

def self.updated_files_regex
  [
    /.*\.([a-z]{2})?proj$/, # Matches files with any extension like .csproj, .vbproj, etc., in any directory
    /packages\.config$/i,           # Matches packages.config in any directory
    /app\.config$/i,                # Matches app.config in any directory
    /web\.config$/i,                # Matches web.config in any directory
    /global\.json$/i,               # Matches global.json in any directory
    /dotnet-tools\.json$/i,         # Matches dotnet-tools.json in any directory
    /Directory\.Build\.props$/i,    # Matches Directory.Build.props in any directory
    /Directory\.Build\.targets$/i,  # Matches Directory.Build.targets in any directory
    /Directory\.targets$/i,         # Matches Directory.targets in any directory or root directory
    /Packages\.props$/i, # Matches Packages.props in any directory
    /.*\.nuspec$/, # Matches any .nuspec files in any directory
    %r{^\.config/dotnet-tools\.json$} # Matches .config/dotnet-tools.json in only root directory
  ]
end

Instance Method Details

#updated_dependency_filesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dependabot/nuget/file_updater.rb', line 52

def updated_dependency_files
  base_dir = "/"
  SharedHelpers.in_a_temporary_repo_directory(base_dir, repo_contents_path) do
    dependencies.each do |dependency|
      try_update_projects(dependency) || try_update_json(dependency)
    end
    updated_files = dependency_files.filter_map do |f|
      updated_content = File.read(dependency_file_path(f))
      next if updated_content == f.content

      normalized_content = normalize_content(f, updated_content)
      next if normalized_content == f.content

      next unless FileUpdater.differs_in_more_than_blank_lines?(f.content, normalized_content)

      puts "The contents of file [#{f.name}] were updated."

      updated_file(file: f, content: normalized_content)
    end
    updated_files
  end
end