Permalink
Browse files

Added 'minigem rever GEM_NAME' command to revert back to rubygems

  • Loading branch information...
fabien committed Sep 16, 2008
1 parent 267893f commit 2304761c39e68352b0beb1dd57c53b546d3588c3
Showing with 42 additions and 6 deletions.
  1. +4 −0 README
  2. +9 −0 bin/minigem
  3. +1 −1 lib/minigems/executable_wrapper
  4. +28 −5 lib/minigems/script_helper.rb
View
4 README
@@ -21,6 +21,10 @@ to use minigems:
sudo minigem prepare merb-core
+And to revert back to rubygems:
+
+sudo minigem revert merb-core
+
To use minigems in your own scripts, use the following construct, instead of the
common 'require "rubygems"' statement:
View
@@ -11,6 +11,7 @@ if ARGV.empty? || %w[-H --help].detect { |o| ARGV.index(o) }
puts " #{program} setup"
puts " #{program} remove"
puts " #{program} prepare GEMNAME [GEMNAME ...]"
+ puts " #{program} revert GEMNAME [GEMNAME ...]"
puts " #{program} install GEMNAME [GEMNAME ...] [options] -- --build-flags [options]"
puts " #{program} update GEMNAME [GEMNAME ...] [options]"
puts " #{program} <command> GEMNAME [GEMNAME ...] [options]"
@@ -61,6 +62,14 @@ elsif ARGV[0] == 'prepare'
next unless (gem_spec = Gem.source_index.find_name(gem_name).sort_by { |g| g.version }.last)
adapt_executables_for(gem_spec)
end
+elsif ARGV[0] == 'revert'
+ # Adapt a gems' executable wrapper to use rubygems.
+ ARGV.shift # skip prepare command argument
+ ensure_in_load_path!(ARGV.empty?)
+ ARGV.each do |gem_name|
+ next unless (gem_spec = Gem.source_index.find_name(gem_name).sort_by { |g| g.version }.last)
+ revert_executables_for(gem_spec)
+ end
elsif ARGV[0] == 'install' || ARGV[0] == 'update'
# Install or update a rubygem and prepare it for minigems.
command = ARGV.shift.to_sym # skip command argument
@@ -5,7 +5,7 @@ SHEBANG
# The application 'EXECUTABLE_NAME' is installed as part of a gem (GEM_NAME),
# and this file is here to facilitate running it.
-require 'minigems'
+require 'GEM_MODE'
version = ">= 0"
@@ -60,14 +60,36 @@ def adapt_executables_for(gemspec)
next if executable == 'minigem' # better not modify minigem itself
if File.exists?(wrapper_path = File.join(Gem.bindir, executable))
wrapper_code = interpolate_wrapper(gemspec.name, executable)
- if File.open(wrapper_path, 'w') { |f| f.write(wrapper_code) }
- puts "Adapted #{wrapper_path} to use minigems instead of rubygems."
- else
+ begin
+ if File.open(wrapper_path, 'w') { |f| f.write(wrapper_code) }
+ puts "Adapted #{wrapper_path} to use minigems instead of rubygems."
+ else
+ puts "Failed to adapt #{wrapper_path} - maybe you need sudo permissions?"
+ end
+ rescue Errno::EACCES => e
puts "Failed to adapt #{wrapper_path} - maybe you need sudo permissions?"
- end
+ end
end
end
end
+
+ def revert_executables_for(gemspec)
+ gemspec.executables.each do |executable|
+ next if executable == 'minigem' # better not modify minigem itself
+ if File.exists?(wrapper_path = File.join(Gem.bindir, executable))
+ wrapper_code = interpolate_wrapper(gemspec.name, executable, 'rubygems')
+ begin
+ if File.open(wrapper_path, 'w') { |f| f.write(wrapper_code) }
+ puts "Reverted #{wrapper_path} to use rubygems instead of minigems."
+ else
+ puts "Failed to revert #{wrapper_path} - maybe you need sudo permissions?"
+ end
+ rescue Errno::EACCES => e
+ puts "Failed to revert #{wrapper_path} - maybe you need sudo permissions?"
+ end
+ end
+ end
+ end
def ensure_in_load_path!(force = false)
install_path = File.join(Gem::ConfigMap[:sitelibdir], 'minigems.rb')
@@ -100,10 +122,11 @@ def remove_minigems!
end
end
- def interpolate_wrapper(gem_name, executable_name)
+ def interpolate_wrapper(gem_name, executable_name, mode = 'minigems')
@template_code ||= File.read(File.join(minigems_path, 'lib', 'minigems', 'executable_wrapper'))
vars = { 'GEM_NAME' => gem_name, 'EXECUTABLE_NAME' => executable_name }
vars['SHEBANG'] = "#!/usr/bin/env " + Gem::ConfigMap[:ruby_install_name]
+ vars['GEM_MODE'] = mode
vars.inject(@template_code) { |str,(k,v)| str.gsub(k,v) }
end

0 comments on commit 2304761

Please sign in to comment.