-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3315 from dependabot/feelepxyz/bundler2-file-parser
Bundler 2: [Prerelease] Add native helpers for file parsing
- Loading branch information
Showing
15 changed files
with
532 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/.bundle/* | ||
!/.bundle/config | ||
/.bundle | ||
/.env | ||
/tmp | ||
/dependabot-*.gem | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
module Functions | ||
class FileParser | ||
def initialize(lockfile_name:) | ||
@lockfile_name = lockfile_name | ||
end | ||
|
||
attr_reader :lockfile_name | ||
|
||
def parsed_gemfile(gemfile_name:) | ||
Bundler::Definition.build(gemfile_name, nil, {}). | ||
dependencies.select(&:current_platform?). | ||
reject { |dep| dep.source.is_a?(Bundler::Source::Gemspec) }. | ||
map(&method(:serialize_bundler_dependency)) | ||
end | ||
|
||
def parsed_gemspec(gemspec_name:) | ||
Bundler.load_gemspec_uncached(gemspec_name). | ||
dependencies. | ||
map(&method(:serialize_bundler_dependency)) | ||
end | ||
|
||
private | ||
|
||
def lockfile | ||
return @lockfile if defined?(@lockfile) | ||
|
||
@lockfile = | ||
begin | ||
return unless lockfile_name && File.exist?(lockfile_name) | ||
|
||
File.read(lockfile_name) | ||
end | ||
end | ||
|
||
def parsed_lockfile | ||
return unless lockfile | ||
|
||
@parsed_lockfile ||= Bundler::LockfileParser.new(lockfile) | ||
end | ||
|
||
def source_from_lockfile(dependency_name) | ||
parsed_lockfile&.specs.find { |s| s.name == dependency_name }&.source | ||
end | ||
|
||
def source_for(dependency) | ||
source = dependency.source | ||
if lockfile && default_rubygems?(source) | ||
# If there's a lockfile and the Gemfile doesn't have anything | ||
# interesting to say about the source, check that. | ||
source = source_from_lockfile(dependency.name) | ||
end | ||
raise "Bad source: #{source}" unless sources.include?(source.class) | ||
|
||
return nil if default_rubygems?(source) | ||
|
||
details = { type: source.class.name.split("::").last.downcase } | ||
if source.is_a?(Bundler::Source::Git) | ||
details.merge!(git_source_details(source)) | ||
end | ||
if source.is_a?(Bundler::Source::Rubygems) | ||
details[:url] = source.remotes.first.to_s | ||
end | ||
details | ||
end | ||
|
||
# TODO: Remove default `master` branch | ||
def git_source_details(source) | ||
{ | ||
url: source.uri, | ||
branch: source.branch || "master", | ||
ref: source.ref || "master" | ||
} | ||
end | ||
|
||
def default_rubygems?(source) | ||
return true if source.nil? | ||
return false unless source.is_a?(Bundler::Source::Rubygems) | ||
|
||
source.remotes.any? { |r| r.to_s.include?("rubygems.org") } | ||
end | ||
|
||
def serialize_bundler_dependency(dependency) | ||
{ | ||
name: dependency.name, | ||
requirement: dependency.requirement, | ||
groups: dependency.groups, | ||
source: source_for(dependency), | ||
type: dependency.type | ||
} | ||
end | ||
|
||
# Can't be a constant because some of these don't exist in bundler | ||
# 1.15, which used to cause issues on Heroku (causing exception on boot). | ||
# TODO: Check if this will be an issue with multiple bundler versions | ||
def sources | ||
[ | ||
NilClass, | ||
Bundler::Source::Rubygems, | ||
Bundler::Source::Git, | ||
Bundler::Source::Path, | ||
Bundler::Source::Gemspec, | ||
Bundler::Source::Metadata | ||
] | ||
end | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
bundler/helpers/v2/monkey_patches/definition_bundler_version_patch.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/definition" | ||
|
||
# Ignore the Bundler version specified in the Gemfile (since the only Bundler | ||
# version available to us is the one we're using). | ||
module BundlerDefinitionBundlerVersionPatch | ||
def expanded_dependencies | ||
@expanded_dependencies ||= | ||
expand_dependencies(dependencies + metadata_dependencies, @remote). | ||
reject { |d| d.name == "bundler" } | ||
end | ||
end | ||
|
||
Bundler::Definition.prepend(BundlerDefinitionBundlerVersionPatch) |
20 changes: 20 additions & 0 deletions
20
bundler/helpers/v2/monkey_patches/definition_ruby_version_patch.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/definition" | ||
|
||
module BundlerDefinitionRubyVersionPatch | ||
def index | ||
@index ||= super.tap do | ||
if ruby_version | ||
requested_version = ruby_version.to_gem_version_with_patchlevel | ||
sources.metadata_source.specs << | ||
Gem::Specification.new("ruby\0", requested_version) | ||
end | ||
|
||
sources.metadata_source.specs << | ||
Gem::Specification.new("ruby\0", "2.5.3p105") | ||
end | ||
end | ||
end | ||
|
||
Bundler::Definition.prepend(BundlerDefinitionRubyVersionPatch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/source" | ||
|
||
module Bundler | ||
class Source | ||
class Git | ||
class GitProxy | ||
private | ||
|
||
# Bundler allows ssh authentication when talking to GitHub but there's | ||
# no way for Dependabot to do so (it doesn't have any ssh keys). | ||
# Instead, we convert all `[email protected]:` URLs to use HTTPS. | ||
def configured_uri_for(uri) | ||
uri = uri.gsub(%r{git@(.*?):/?}, 'https://\1/') | ||
if /https?:/ =~ uri | ||
remote = Bundler::URI(uri) | ||
config_auth = Bundler.settings[remote.to_s] || Bundler.settings[remote.host] | ||
remote.userinfo ||= config_auth | ||
remote.to_s | ||
else | ||
uri | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
module Bundler | ||
class Source | ||
class Git < Path | ||
private | ||
|
||
def serialize_gemspecs_in(destination) | ||
original_load_paths = $LOAD_PATH.dup | ||
reduced_load_paths = original_load_paths. | ||
reject { |p| p.include?("/gems/") } | ||
|
||
$LOAD_PATH.shift until $LOAD_PATH.empty? | ||
reduced_load_paths.each { |p| $LOAD_PATH << p } | ||
|
||
if destination.relative? | ||
destination = destination.expand_path(Bundler.root) | ||
end | ||
Dir["#{destination}/#{@glob}"].each do |spec_path| | ||
# Evaluate gemspecs and cache the result. Gemspecs | ||
# in git might require git or other dependencies. | ||
# The gemspecs we cache should already be evaluated. | ||
spec = Bundler.load_gemspec(spec_path) | ||
next unless spec | ||
|
||
Bundler.rubygems.set_installed_by_version(spec) | ||
Bundler.rubygems.validate(spec) | ||
File.open(spec_path, "wb") { |file| file.write(spec.to_ruby) } | ||
end | ||
$LOAD_PATH.shift until $LOAD_PATH.empty? | ||
original_load_paths.each { |p| $LOAD_PATH << p } | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.