Created
March 21, 2014 12:42
-
-
Save regularfry/9685345 to your computer and use it in GitHub Desktop.
microgems
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
#!/usr/bin/env ruby | |
# Take a Gemfile.lock and a GEM_PATH. Create a new GEM_HOME at | |
# the given path, with only the selected gems symlinked in. | |
# Ignores groups. | |
new_gem_home = ARGV.shift or | |
raise "Specify the new GEM_HOME" | |
current_gem_path = ENV['GEM_PATH'].to_s.split(":").first | |
if current_gem_path.empty? | |
# This probably isn't correct, but the methods on Gem don't give | |
# much of a clue | |
current_gem_path = Gem.default_dir | |
end | |
gems_to_link = parse_gemfile( STDIN ) | |
make_gem_home( gems_to_link, | |
current_gem_path, | |
new_gem_home ) | |
BEGIN { | |
require 'fileutils' | |
def parse_gemfile( io ) | |
io.read.scan(/^ (\w+) \((\S+?)\)/).map{|name,ver| | |
{name:name,ver:ver} | |
} | |
end | |
def make_gem_home( gems_to_link, | |
current_gem_path, | |
new_gem_home) | |
full_current_path = File.expand_path current_gem_path | |
FileUtils.mkdir_p new_gem_home + "/gems" | |
gems_to_link.each do |hsh| | |
link_basename = "#{hsh[:name]}" | |
target_basename = "#{hsh[:name]}-#{hsh[:ver]}" | |
link_path = File.join new_gem_home, "gems", link_basename | |
target_path = File.join full_current_path, "gems", target_basename | |
FileUtils.ln_sf target_path, link_path | |
end | |
end | |
} |
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
# encoding: utf-8 | |
# Smallest possible gem loader that might ever work. | |
# Assumes one version of each gem per $GEM_PATH, to go with link-gems. | |
# Assumes that the only code you want to require is in lib/. | |
# Only patches $LOAD_PATH, doesn't touch #require. | |
module Microgems | |
def self.setup( gem_path, load_path ) | |
lib_glob = File.join( gem_path, "gems", "*", "lib" ) | |
Dir[lib_glob].each do |lib_path| | |
load_path << lib_path | |
end | |
end | |
end | |
Microgems.setup( ENV.fetch('GEM_PATH',"").split(":").first, | |
$LOAD_PATH ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment