Created
November 9, 2022 15:05
-
-
Save geoffyoungs/aa817c17851cf33825c4f91c956a26ff to your computer and use it in GitHub Desktop.
Side load a gem that's not in Gemfile
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
module GemSideLoad | |
class << self | |
attr_accessor :last | |
end | |
module Gem | |
VERSION = '1.0.0' | |
Requirement = Struct.new(:wat) | |
Version = Struct.new(:version) | |
class Version | |
def >=(other) | |
self.version >= other.version | |
end | |
end | |
class Specification < OpenStruct | |
attr_reader :depends | |
def initialize(&block) | |
super | |
@depends = [] | |
::GemSideLoad.last = self | |
yield self | |
end | |
def add_dependency(wat, *args) | |
@depends << wat | |
end | |
def add_runtime_dependency(wat, *args) | |
@depends << wat | |
end | |
def add_development_dependency(*_args) | |
end | |
end | |
end | |
def self.add(gem_name) | |
return unless gem_name | |
gems = ::Gem.default_path.last | |
specs = gems + '/specifications' | |
eval(File.read(specs + '/' + gem_name + '.gemspec'), binding) | |
spec = GemSideLoad.last | |
$:.push(*(dirs=Dir.glob("#{gems}/gems/#{gem_name}/{#{spec.require_paths.join(',')}}"))) | |
$:.uniq! | |
GemSideLoad.last | |
end | |
end | |
def add_gem_path(name) | |
gems = Gem.default_path.last | |
dir = Dir.entries(gems+'/gems').grep(/#{name}-[0-9]/).sort.last | |
unless dir | |
warn "#{name} not found in #{gems}/gems" | |
return | |
end | |
spec = GemSideLoad.add(dir) | |
spec.depends # auto-load deps? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment