Skip to content

Commit 5fa491b

Browse files
deivid-rodriguezhsbt
authored andcommitted
Normalize lockfile platforms
1 parent e61bb75 commit 5fa491b

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

lib/bundler/cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ def inject(name, version)
625625
method_option "full-index", type: :boolean, default: false, banner: "Fall back to using the single-file index of all gems"
626626
method_option "add-platform", type: :array, default: [], banner: "Add a new platform to the lockfile"
627627
method_option "remove-platform", type: :array, default: [], banner: "Remove a platform from the lockfile"
628+
method_option "normalize-platforms", type: :boolean, default: false, banner: "Normalize lockfile platforms"
628629
method_option "patch", type: :boolean, banner: "If updating, prefer updating only to next patch version"
629630
method_option "minor", type: :boolean, banner: "If updating, prefer updating only to next minor version"
630631
method_option "major", type: :boolean, banner: "If updating, prefer updating to next major version (default)"

lib/bundler/cli/lock.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def run
1414
exit 1
1515
end
1616

17+
check_for_conflicting_options
18+
1719
print = options[:print]
1820
previous_output_stream = Bundler.ui.output_stream
1921
Bundler.ui.output_stream = :stderr if print
@@ -60,6 +62,10 @@ def run
6062

6163
definition.resolve_remotely! unless options[:local]
6264

65+
if options["normalize-platforms"]
66+
definition.normalize_platforms
67+
end
68+
6369
if print
6470
puts definition.to_lock
6571
else
@@ -70,5 +76,17 @@ def run
7076

7177
Bundler.ui.output_stream = previous_output_stream
7278
end
79+
80+
private
81+
82+
def check_for_conflicting_options
83+
if options["normalize-platforms"] && options["add-platform"].any?
84+
raise InvalidOption, "--normalize-platforms can't be used with --add-platform"
85+
end
86+
87+
if options["normalize-platforms"] && options["remove-platform"].any?
88+
raise InvalidOption, "--normalize-platforms can't be used with --remove-platform"
89+
end
90+
end
7391
end
7492
end

lib/bundler/definition.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ def validate_platforms!
461461
"Add the current platform to the lockfile with\n`bundle lock --add-platform #{local_platform}` and try again."
462462
end
463463

464+
def normalize_platforms
465+
@platforms = resolve.normalize_platforms!(current_dependencies, platforms)
466+
467+
@resolve = SpecSet.new(resolve.for(current_dependencies, false, @platforms))
468+
end
469+
464470
def add_platform(platform)
465471
return if @platforms.include?(platform)
466472

lib/bundler/spec_set.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ def for(dependencies, check = false, platforms = [nil])
5252
specs.uniq
5353
end
5454

55+
def normalize_platforms!(deps, platforms)
56+
complete_platforms = add_extra_platforms!(platforms)
57+
58+
complete_platforms.map do |platform|
59+
next platform if platform == Gem::Platform::RUBY
60+
61+
begin
62+
Integer(platform.version)
63+
rescue ArgumentError, TypeError
64+
next platform
65+
end
66+
67+
less_specific_platform = Gem::Platform.new([platform.cpu, platform.os, nil])
68+
next platform if incomplete_for_platform?(deps, less_specific_platform)
69+
70+
less_specific_platform
71+
end.uniq
72+
end
73+
5574
def add_extra_platforms!(platforms)
5675
return platforms.concat([Gem::Platform::RUBY]).uniq if @specs.empty?
5776

@@ -133,11 +152,10 @@ def materialized_for_all_platforms
133152
def incomplete_for_platform?(deps, platform)
134153
return false if @specs.empty?
135154

136-
@incomplete_specs = []
137-
138-
self.for(deps, true, [platform])
155+
validation_set = self.class.new(@specs)
156+
validation_set.for(deps, true, [platform])
139157

140-
@incomplete_specs.any?
158+
validation_set.incomplete_specs.any?
141159
end
142160

143161
def missing_specs

0 commit comments

Comments
 (0)