[rubygems/rubygems] Centralize logic using `Pathname#relative_path_from`

To avoid potential crashes when trying to jump from a drive to another
on Windows, and take the change refactor things a bit.

https://github.com/rubygems/rubygems/commit/7c9a9a431a
This commit is contained in:
Ry Biesemeyer 2021-12-02 16:09:36 +00:00 коммит произвёл git
Родитель a812f1fed0
Коммит bece880874
7 изменённых файлов: 24 добавлений и 16 удалений

Просмотреть файл

@ -29,7 +29,7 @@ module Bundler
Bundler.ui.warn "Install missing gems with `bundle install`"
exit 1
elsif !Bundler.default_lockfile.file? && Bundler.frozen_bundle?
Bundler.ui.error "This bundle has been frozen, but there is no #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} present"
Bundler.ui.error "This bundle has been frozen, but there is no #{SharedHelpers.relative_lockfile_path} present"
exit 1
else
Bundler.load.lock(:preserve_unknown_sections => true) unless options[:"dry-run"]

Просмотреть файл

@ -29,7 +29,7 @@ module Bundler
flag ||= "--frozen flag" if options[:frozen]
flag ||= "deployment setting"
raise ProductionError, "The #{flag} requires a lockfile. Please make " \
"sure you have checked your #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} into version control " \
"sure you have checked your #{SharedHelpers.relative_lockfile_path} into version control " \
"before deploying."
end

Просмотреть файл

@ -405,13 +405,13 @@ module Bundler
msg << "\n\nYou have added to the Gemfile:\n" << added.join("\n") if added.any?
msg << "\n\nYou have deleted from the Gemfile:\n" << deleted.join("\n") if deleted.any?
msg << "\n\nYou have changed in the Gemfile:\n" << changed.join("\n") if changed.any?
msg << "\n\nRun `bundle install` elsewhere and add the updated #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} to version control.\n"
msg << "\n\nRun `bundle install` elsewhere and add the updated #{SharedHelpers.relative_gemfile_path} to version control.\n"
unless explicit_flag
suggested_command = unless Bundler.settings.locations("frozen").keys.include?(:env)
"bundle config set frozen false"
end
msg << "If this is a development machine, remove the #{Bundler.default_gemfile.relative_path_from(SharedHelpers.pwd)} " \
msg << "If this is a development machine, remove the #{SharedHelpers.relative_lockfile_path} " \
"freeze by running `#{suggested_command}`." if suggested_command
end

Просмотреть файл

@ -40,11 +40,11 @@ module Bundler
out << "\n## Gemfile\n"
gemfiles.each do |gemfile|
out << "\n### #{Pathname.new(gemfile).relative_path_from(SharedHelpers.pwd)}\n\n"
out << "\n### #{SharedHelpers.relative_path_to(gemfile)}\n\n"
out << "```ruby\n" << read_file(gemfile).chomp << "\n```\n"
end
out << "\n### #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}\n\n"
out << "\n### #{SharedHelpers.relative_path_to(Bundler.default_lockfile)}\n\n"
out << "```\n" << read_file(Bundler.default_lockfile).chomp << "\n```\n"
end

Просмотреть файл

@ -55,20 +55,13 @@ module Bundler
if spec.source.instance_of?(Source::Path) && spec.source.path.absolute?
full_path
else
relative_path_from(Bundler.root.join(bundler_path), :to => full_path) || full_path
SharedHelpers.relative_path_to(full_path, :from => Bundler.root.join(bundler_path))
end
rescue TypeError
error_message = "#{spec.name} #{spec.version} has an invalid gemspec"
raise Gem::InvalidSpecificationException.new(error_message)
end
def relative_path_from(source, to:)
Pathname.new(to).relative_path_from(source).to_s
rescue ArgumentError
# on Windows, if source and destination are on different drivers, there's no relative path from one to the other
nil
end
def define_path_helpers
<<~'END'
unless defined?(Gem)

Просмотреть файл

@ -68,7 +68,7 @@ module Bundler
if lockfile.match?(/<<<<<<<|=======|>>>>>>>|\|\|\|\|\|\|\|/)
raise LockfileError, "Your lockfile contains merge conflicts.\n" \
"Run `git checkout HEAD -- #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}` first to get a clean lock."
"Run `git checkout HEAD -- #{SharedHelpers.relative_lockfile_path}` first to get a clean lock."
end
lockfile.split(/(?:\r?\n)+/) do |line|
@ -92,7 +92,7 @@ module Bundler
@specs = @specs.values.sort_by!(&:full_name)
rescue ArgumentError => e
Bundler.ui.debug(e)
raise LockfileError, "Your lockfile is unreadable. Run `rm #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}` " \
raise LockfileError, "Your lockfile is unreadable. Run `rm #{SharedHelpers.relative_lockfile_path}` " \
"and then `bundle install` to generate a new lockfile."
end

Просмотреть файл

@ -197,6 +197,21 @@ module Bundler
filesystem_access(gemfile_path) {|g| File.open(g, "w") {|file| file.puts contents } }
end
def relative_gemfile_path
relative_path_to(Bundler.default_gemfile)
end
def relative_lockfile_path
relative_path_to(Bundler.default_lockfile)
end
def relative_path_to(destination, from: pwd)
Pathname.new(destination).relative_path_from(from).to_s
rescue ArgumentError
# on Windows, if source and destination are on different drivers, there's no relative path from one to the other
destination
end
private
def validate_bundle_path