-
Notifications
You must be signed in to change notification settings - Fork 916
/
file_updater.rb
219 lines (183 loc) 路 6.21 KB
/
file_updater.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
40
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
72
73
74
75
76
77
78
79
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
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
# frozen_string_literal: true
require "dependabot/file_updaters"
require "dependabot/file_updaters/base"
module Dependabot
module Bundler
class FileUpdater < Dependabot::FileUpdaters::Base
require_relative "file_updater/gemfile_updater"
require_relative "file_updater/gemspec_updater"
require_relative "file_updater/lockfile_updater"
def self.updated_files_regex
[
/^Gemfile$/,
/^Gemfile\.lock$/,
/^gems\.rb$/,
/^gems\.locked$/,
/^*\.gemspec$/
]
end
# rubocop:disable Metrics/PerceivedComplexity
def updated_dependency_files
updated_files = []
if gemfile && file_changed?(gemfile)
updated_files <<
updated_file(
file: gemfile,
content: updated_gemfile_content(gemfile)
)
end
if lockfile && dependencies.any?(&:appears_in_lockfile?)
updated_files <<
updated_file(file: lockfile, content: updated_lockfile_content)
end
top_level_gemspecs.each do |file|
next unless file_changed?(file)
updated_files <<
updated_file(file: file, content: updated_gemspec_content(file))
end
evaled_gemfiles.each do |file|
next unless file_changed?(file)
updated_files <<
updated_file(file: file, content: updated_gemfile_content(file))
end
check_updated_files(updated_files)
base_dir = updated_files.first.directory
updated_vendor_cache_files(base_directory: base_dir).each do |file|
updated_files << file
end
updated_files
end
# rubocop:enable Metrics/PerceivedComplexity
private
# Dynamically fetch the vendor cache folder from bundler
def vendor_cache_dir
return @vendor_cache_dir if defined?(@vendor_cache_dir)
@vendor_cache_dir =
SharedHelpers.in_a_forked_process do
# Set the path for path gemspec correctly
::Bundler.instance_variable_set(:@root, repo_contents_path)
::Bundler.app_cache
end
end
# Returns changed files in the vendor/cache folder
#
# @param base_directory [String] Update config base directory
# @return [Array<Dependabot::DependencyFile>]
def updated_vendor_cache_files(base_directory:)
return [] unless repo_contents_path && vendor_cache_dir
Dir.chdir(repo_contents_path) do
relative_dir = vendor_cache_dir.sub("#{repo_contents_path}/", "")
status = SharedHelpers.run_shell_command(
"git status --untracked-files=all --porcelain=v1 #{relative_dir}"
)
changed_paths = status.split("\n").map { |l| l.split(" ") }
changed_paths.map do |type, path|
deleted = type == "D"
encoding = ""
encoded_content = File.read(path) unless deleted
if binary_file?(path)
encoding = Dependabot::DependencyFile::ContentEncoding::BASE64
encoded_content = Base64.encode64(encoded_content) unless deleted
end
Dependabot::DependencyFile.new(
name: path,
content: encoded_content,
directory: base_directory,
deleted: deleted,
content_encoding: encoding
)
end
end
end
# notable filenames without a reliable extension:
TEXT_FILE_NAMES = [
"Gemfile",
"Gemfile.lock",
".bundlecache",
".gitignore"
].freeze
TEXT_FILE_EXTS = [
# code
".rb",
".erb",
".gemspec",
".js",
".html",
# config
".json",
".xml",
".toml",
".yaml",
".yml",
# docs
".md",
".txt"
].freeze
def binary_file?(path)
return false if TEXT_FILE_NAMES.include?(File.basename(path))
return false if TEXT_FILE_EXTS.include?(File.extname(path))
true
end
def check_required_files
file_names = dependency_files.map(&:name)
if lockfile && !gemfile
raise "A Gemfile must be provided if a lockfile is!"
end
return if file_names.any? { |name| name.match?(%r{^[^/]*\.gemspec$}) }
return if gemfile
raise "A gemspec or Gemfile must be provided!"
end
def check_updated_files(updated_files)
return if updated_files.reject { |f| dependency_files.include?(f) }.any?
raise "No files have changed!"
end
def gemfile
@gemfile ||= get_original_file("Gemfile") ||
get_original_file("gems.rb")
end
def lockfile
@lockfile ||= get_original_file("Gemfile.lock") ||
get_original_file("gems.locked")
end
def evaled_gemfiles
@evaled_gemfiles ||=
dependency_files.
reject { |f| f.name.end_with?(".gemspec") }.
reject { |f| f.name.end_with?(".specification") }.
reject { |f| f.name.end_with?(".lock") }.
reject { |f| f.name.end_with?(".ruby-version") }.
reject { |f| f.name == "Gemfile" }.
reject { |f| f.name == "gems.rb" }.
reject { |f| f.name == "gems.locked" }
end
def updated_gemfile_content(file)
GemfileUpdater.new(
dependencies: dependencies,
gemfile: file
).updated_gemfile_content
end
def updated_gemspec_content(gemspec)
GemspecUpdater.new(
dependencies: dependencies,
gemspec: gemspec
).updated_gemspec_content
end
def updated_lockfile_content
@updated_lockfile_content ||=
LockfileUpdater.new(
dependencies: dependencies,
dependency_files: dependency_files,
repo_contents_path: repo_contents_path,
credentials: credentials
).updated_lockfile_content
end
def top_level_gemspecs
dependency_files.
select { |file| file.name.end_with?(".gemspec") }.
reject(&:support_file?)
end
end
end
end
Dependabot::FileUpdaters.
register("bundler", Dependabot::Bundler::FileUpdater)