Class: Dependabot::Bundler::UpdateChecker::FilePreparer

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/bundler/update_checker/file_preparer.rb

Overview

This class takes a set of dependency files and sanitizes them for use in UpdateCheckers::Ruby::Bundler. In particular, it:

  • Removes any version requirement on the dependency being updated (in the Gemfile)

  • Sanitizes any provided gemspecs to remove file imports etc. (since Dependabot doesn’t pull down the entire repo). This process is imperfect - an alternative would be to clone the repo

  • Sets the ruby version in the Gemfile to be the lowest possible version allowed by the gemspec, if the gemspec has a required ruby version range

Constant Summary collapse

VERSION_REGEX =
/[0-9]+(?:\.[A-Za-z0-9\-_]+)*/

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:, dependency:, remove_git_source: false, unlock_requirement: true, replacement_git_pin: nil, latest_allowable_version: nil, lock_ruby_version: true) ⇒ FilePreparer

Returns a new instance of FilePreparer.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dependabot/bundler/update_checker/file_preparer.rb', line 56

def initialize(
  dependency_files:,
  dependency:,
  remove_git_source: false,
  unlock_requirement: true,
  replacement_git_pin: nil,
  latest_allowable_version: nil,
  lock_ruby_version: true
)
  @dependency_files = T.let(dependency_files, T::Array[Dependabot::DependencyFile])
  @dependency = T.let(dependency, Dependabot::Dependency)
  @remove_git_source = T.let(remove_git_source, T::Boolean)
  @unlock_requirement = T.let(unlock_requirement, T::Boolean)
  @replacement_git_pin = T.let(replacement_git_pin, T.nilable(String))
  @latest_allowable_version = T.let(
    latest_allowable_version&.to_s,
    T.nilable(String)
  )
  @lock_ruby_version = T.let(lock_ruby_version, T::Boolean)
end

Instance Method Details

#gemspec_sourcesObject



38
39
40
41
42
43
# File 'lib/dependabot/bundler/update_checker/file_preparer.rb', line 38

def gemspec_sources
  [
    ::Bundler::Source::Path,
    ::Bundler::Source::Gemspec
  ]
end

#prepared_dependency_filesObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
# File 'lib/dependabot/bundler/update_checker/file_preparer.rb', line 80

def prepared_dependency_files
  files = []

  gemfile_file = gemfile
  if gemfile_file
    files << DependencyFile.new(
      name: gemfile_file.name,
      content: gemfile_content_for_update_check(gemfile_file),
      directory: gemfile_file.directory
    )
  end

  top_level_gemspecs.each do |gemspec|
    files << DependencyFile.new(
      name: gemspec.name,
      content: gemspec_content_for_update_check(gemspec),
      directory: gemspec.directory
    )
  end

  path_gemspecs.each do |file|
    files << DependencyFile.new(
      name: file.name,
      content: sanitize_gemspec_content(T.must(file.content)),
      directory: file.directory,
      support_file: file.support_file?
    )
  end

  evaled_gemfiles.each do |file|
    files << DependencyFile.new(
      name: file.name,
      content: gemfile_content_for_update_check(file),
      directory: file.directory
    )
  end

  # No editing required for lockfile or Ruby version file
  files += [
    lockfile,
    ruby_version_file,
    tool_versions_file,
    *imported_ruby_files,
    *specification_files
  ].compact
end