Skip to content

Instantly share code, notes, and snippets.

@jeromedalbert
Created July 18, 2024 09:23
Show Gist options
  • Save jeromedalbert/ebc5af3b0b5a28d186e0629f62cb825d to your computer and use it in GitHub Desktop.
Save jeromedalbert/ebc5af3b0b5a28d186e0629f62cb825d to your computer and use it in GitHub Desktop.
Proof of Concept - Declare all dependencies in Gemfile, not in the gemspec
require_relative 'lib/my_gem/version'
# Proof of concept monkey-patch
class Gem::Specification
def add_gemfile_dependencies(*groups)
require 'bundler'
groups = [:default] if groups.empty?
# Quick hack to remove any gemspec call from the Gemfile to prevent a
# circular infinite call loop. A better solution could be to patch
# `gemspec` to do a no-op if `add_gemfile_dependencies` is running
gemfile_content_without_gemspec = File.read('Gemfile').gsub('gemspec', '')
dependencies = Bundler::Dsl.new.eval_gemfile('Gemfile', gemfile_content_without_gemspec)
dependencies.select! do |dependency|
dependency.name != name && dependency.groups.any? { |group| groups.include?(group) }
end
dependencies.each { |dependency| add_dependency(dependency.name, dependency.requirement) }
end
end
Gem::Specification.new do |spec|
# ...
spec.add_gemfile_dependencies
# or spec.add_gemfile_dependencies(:default, :release) or whatever
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment