-
Notifications
You must be signed in to change notification settings - Fork 915
/
file_preparer.rb
291 lines (236 loc) 路 9.22 KB
/
file_preparer.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
220
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
# frozen_string_literal: true
require "toml-rb"
require "dependabot/dependency_file"
require "dependabot/cargo/file_parser"
require "dependabot/cargo/update_checker"
module Dependabot
module Cargo
class UpdateChecker
# This class takes a set of dependency files and sanitizes them for use
# in UpdateCheckers::Rust::Cargo.
class FilePreparer
def initialize(dependency_files:, dependency:,
unlock_requirement: true,
replacement_git_pin: nil,
latest_allowable_version: nil)
@dependency_files = dependency_files
@dependency = dependency
@unlock_requirement = unlock_requirement
@replacement_git_pin = replacement_git_pin
@latest_allowable_version = latest_allowable_version
end
def prepared_dependency_files
files = []
files += manifest_files.map do |file|
DependencyFile.new(
name: file.name,
content: manifest_content_for_update_check(file),
directory: file.directory
)
end
files << lockfile if lockfile
files << toolchain if toolchain
files
end
private
attr_reader :dependency_files, :dependency, :replacement_git_pin,
:latest_allowable_version
def unlock_requirement?
@unlock_requirement
end
def replace_git_pin?
!replacement_git_pin.nil?
end
def manifest_content_for_update_check(file)
content = file.content
unless file.support_file?
content = replace_version_constraint(content, file.name)
content = replace_git_pin(content) if replace_git_pin?
end
content = replace_ssh_urls(content)
content
end
# Note: We don't need to care about formatting in this method, since
# we're only using the manifest to find the latest resolvable version
def replace_version_constraint(content, filename)
parsed_manifest = TomlRB.parse(content)
Cargo::FileParser::DEPENDENCY_TYPES.each do |type|
dependency_names_for_type(parsed_manifest, type).each do |name|
req = parsed_manifest.dig(type, name)
updated_req = temporary_requirement_for_resolution(filename)
if req.is_a?(Hash)
parsed_manifest[type][name]["version"] = updated_req
else
parsed_manifest[type][name] = updated_req
end
end
end
replace_req_on_target_specific_deps!(parsed_manifest, filename)
TomlRB.dump(parsed_manifest)
end
def replace_req_on_target_specific_deps!(parsed_manifest, filename)
parsed_manifest.fetch("target", {}).each do |target, _|
Cargo::FileParser::DEPENDENCY_TYPES.each do |type|
dependency_names = dependency_names_for_type_and_target(
parsed_manifest,
type,
target
)
dependency_names.each do |name|
req = parsed_manifest.dig("target", target, type, name)
updated_req = temporary_requirement_for_resolution(filename)
if req.is_a?(Hash)
parsed_manifest["target"][target][type][name]["version"] =
updated_req
else
parsed_manifest["target"][target][type][name] = updated_req
end
end
end
end
end
def replace_git_pin(content)
parsed_manifest = TomlRB.parse(content)
Cargo::FileParser::DEPENDENCY_TYPES.each do |type|
dependency_names_for_type(parsed_manifest, type).each do |name|
req = parsed_manifest.dig(type, name)
next unless req.is_a?(Hash)
next unless [req["tag"], req["rev"]].compact.uniq.count == 1
if req["tag"]
parsed_manifest[type][name]["tag"] = replacement_git_pin
end
if req["rev"]
parsed_manifest[type][name]["rev"] = replacement_git_pin
end
end
end
replace_git_pin_on_target_specific_deps!(parsed_manifest)
TomlRB.dump(parsed_manifest)
end
def replace_git_pin_on_target_specific_deps!(parsed_manifest)
parsed_manifest.fetch("target", {}).each do |target, _|
Cargo::FileParser::DEPENDENCY_TYPES.each do |type|
dependency_names = dependency_names_for_type_and_target(
parsed_manifest,
type,
target
)
dependency_names.each do |name|
req = parsed_manifest.dig("target", target, type, name)
next unless req.is_a?(Hash)
next unless [req["tag"], req["rev"]].compact.uniq.count == 1
if req["tag"]
parsed_manifest["target"][target][type][name]["tag"] =
replacement_git_pin
end
if req["rev"]
parsed_manifest["target"][target][type][name]["rev"] =
replacement_git_pin
end
end
end
end
end
def replace_ssh_urls(content)
parsed_manifest = TomlRB.parse(content)
Cargo::FileParser::DEPENDENCY_TYPES.each do |type|
(parsed_manifest[type] || {}).each do |_, details|
next unless details.is_a?(Hash)
next unless details["git"]
details["git"] = details["git"].
gsub(%r{ssh://git@(.*?)/}, 'https://\1/')
end
end
TomlRB.dump(parsed_manifest)
end
def temporary_requirement_for_resolution(filename)
original_req = dependency.requirements.
find { |r| r.fetch(:file) == filename }&.
fetch(:requirement)
lower_bound_req =
if original_req && !unlock_requirement?
original_req
else
">= #{lower_bound_version}"
end
unless latest_allowable_version &&
Cargo::Version.correct?(latest_allowable_version) &&
Cargo::Version.new(latest_allowable_version) >=
Cargo::Version.new(lower_bound_version)
return lower_bound_req
end
lower_bound_req + ", <= #{latest_allowable_version}"
end
# rubocop:disable Metrics/PerceivedComplexity
def lower_bound_version
@lower_bound_version ||=
if git_dependency? && git_dependency_version
git_dependency_version
elsif !git_dependency? && dependency.version
dependency.version
else
version_from_requirement =
dependency.requirements.map { |r| r.fetch(:requirement) }.
compact.
flat_map { |req_str| Cargo::Requirement.new(req_str) }.
flat_map(&:requirements).
reject { |req_array| req_array.first.start_with?("<") }.
map(&:last).
max&.to_s
version_from_requirement || 0
end
end
# rubocop:enable Metrics/PerceivedComplexity
def git_dependency_version
return unless lockfile
TomlRB.parse(lockfile.content).
fetch("package", []).
select { |p| p["name"] == dependency.name }.
find { |p| p["source"].end_with?(dependency.version) }.
fetch("version")
end
def dependency_names_for_type(parsed_manifest, type)
names = []
parsed_manifest.fetch(type, {}).each do |nm, req|
next unless dependency.name == name_from_declaration(nm, req)
names << nm
end
names
end
def dependency_names_for_type_and_target(parsed_manifest, type, target)
names = []
(parsed_manifest.dig("target", target, type) || {}).each do |nm, req|
next unless dependency.name == name_from_declaration(nm, req)
names << nm
end
names
end
def name_from_declaration(name, declaration)
return name if declaration.is_a?(String)
unless declaration.is_a?(Hash)
raise "Unexpected dependency declaration: #{declaration}"
end
declaration.fetch("package", name)
end
def manifest_files
@manifest_files ||=
dependency_files.select { |f| f.name.end_with?("Cargo.toml") }
raise "No Cargo.toml!" if @manifest_files.none?
@manifest_files
end
def lockfile
@lockfile ||= dependency_files.find { |f| f.name == "Cargo.lock" }
end
def toolchain
@toolchain ||=
dependency_files.find { |f| f.name == "rust-toolchain" }
end
def git_dependency?
GitCommitChecker.
new(dependency: dependency, credentials: []).
git_dependency?
end
end
end
end
end