Skip to content

Commit

Permalink
Normalize lockfile platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez authored and hsbt committed Oct 25, 2024
1 parent 0ebe987 commit f046cc4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ def inject(name, version)
method_option "full-index", type: :boolean, default: false, banner: "Fall back to using the single-file index of all gems"
method_option "add-platform", type: :array, default: [], banner: "Add a new platform to the lockfile"
method_option "remove-platform", type: :array, default: [], banner: "Remove a platform from the lockfile"
method_option "normalize-platforms", type: :boolean, default: false, banner: "Normalize lockfile platforms"
method_option "patch", type: :boolean, banner: "If updating, prefer updating only to next patch version"
method_option "minor", type: :boolean, banner: "If updating, prefer updating only to next minor version"
method_option "major", type: :boolean, banner: "If updating, prefer updating to next major version (default)"
Expand Down
18 changes: 18 additions & 0 deletions lib/bundler/cli/lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def run
exit 1
end

check_for_conflicting_options

print = options[:print]
previous_output_stream = Bundler.ui.output_stream
Bundler.ui.output_stream = :stderr if print
Expand Down Expand Up @@ -60,6 +62,10 @@ def run

definition.resolve_remotely! unless options[:local]

if options["normalize-platforms"]
definition.normalize_platforms
end

if print
puts definition.to_lock
else
Expand All @@ -70,5 +76,17 @@ def run

Bundler.ui.output_stream = previous_output_stream
end

private

def check_for_conflicting_options
if options["normalize-platforms"] && options["add-platform"].any?
raise InvalidOption, "--normalize-platforms can't be used with --add-platform"
end

if options["normalize-platforms"] && options["remove-platform"].any?
raise InvalidOption, "--normalize-platforms can't be used with --remove-platform"
end
end
end
end
6 changes: 6 additions & 0 deletions lib/bundler/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ def validate_platforms!
"Add the current platform to the lockfile with\n`bundle lock --add-platform #{local_platform}` and try again."
end

def normalize_platforms
@platforms = resolve.normalize_platforms!(current_dependencies, platforms)

@resolve = SpecSet.new(resolve.for(current_dependencies, false, @platforms))
end

def add_platform(platform)
return if @platforms.include?(platform)

Expand Down
26 changes: 22 additions & 4 deletions lib/bundler/spec_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ def for(dependencies, check = false, platforms = [nil])
specs.uniq
end

def normalize_platforms!(deps, platforms)
complete_platforms = add_extra_platforms!(platforms)

complete_platforms.map do |platform|
next platform if platform == Gem::Platform::RUBY

begin
Integer(platform.version)
rescue ArgumentError, TypeError
next platform
end

less_specific_platform = Gem::Platform.new([platform.cpu, platform.os, nil])
next platform if incomplete_for_platform?(deps, less_specific_platform)

less_specific_platform
end.uniq
end

def add_extra_platforms!(platforms)
return platforms.concat([Gem::Platform::RUBY]).uniq if @specs.empty?

Expand Down Expand Up @@ -133,11 +152,10 @@ def materialized_for_all_platforms
def incomplete_for_platform?(deps, platform)
return false if @specs.empty?

@incomplete_specs = []

self.for(deps, true, [platform])
validation_set = self.class.new(@specs)
validation_set.for(deps, true, [platform])

@incomplete_specs.any?
validation_set.incomplete_specs.any?
end

def missing_specs
Expand Down

0 comments on commit f046cc4

Please sign in to comment.