From b2c624cf77ecd412eb9a854fb21410b62f7bb271 Mon Sep 17 00:00:00 2001 From: Jan Lelis Date: Sat, 14 May 2011 20:30:59 +0200 Subject: [PATCH] gemify general helpers/rvm stuff ("every_day_irb", "rvm_loader") --- README.rdoc | 12 +--- Rakefile | 30 ++++---- every_day_irb.gemspec | 16 +++++ irbtools.gemspec | 15 ++-- lib/{irbtools/general.rb => every_day_irb.rb} | 58 ++-------------- lib/irbtools.rb | 9 +-- lib/irbtools/configure.rb | 4 +- lib/irbtools/libraries.rb | 69 ++++++++++++++++--- lib/irbtools/rvm.rb | 8 --- 9 files changed, 112 insertions(+), 109 deletions(-) create mode 100644 every_day_irb.gemspec rename lib/{irbtools/general.rb => every_day_irb.rb} (56%) delete mode 100644 lib/irbtools/rvm.rb diff --git a/README.rdoc b/README.rdoc index 393edc2..40548bd 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,5 +1,5 @@ = irbtools -This is a meta gem which installs useful irb gems and configures your irb. It has a modular structure and supports multiple library-loading mechanisms like via autoload or threads. +This is a meta gem that installs useful irb gems and configures your irb. It has a modular structure and supports multiple library-loading mechanisms like via autoload or threads. == Setup gem install irbtools @@ -62,6 +62,7 @@ See http://rbjl.net/40-irbtools-release-the-power-of-irb or read the commented s * wirb[https://github.com/janlelis/wirb/] colorize output * hirb[http://tagaholic.me/2009/03/13/hirb-irb-on-the-good-stuff.html] (active record) tables and custom views for specific objects * fancy_irb[https://github.com/janlelis/fancy_irb] put result as comment and more colorization +* every_day_irb[https://github.com/janlelis/irbtools/tree/master/lib/every_day_irb.rb] helper methods that might be useful in every-day irb usage, e.g.: ls, cat, rq, rrq, ld, session_history, reset!, clear, dbg * clipboard[http://github.com/janlelis/clipboard] easy clipboard access (copy & paste) * interactive_editor[https://github.com/jberkel/interactive_editor] lets you open vim, hack something, and it gets loaded into the current session, also possible: yaml object editing * sketches[http://sketches.rubyforge.org/] another, more flexible "start editor and it gets loaded into your irb session" plugin @@ -76,15 +77,6 @@ See http://rbjl.net/40-irbtools-release-the-power-of-irb or read the commented s * guessmethod[http://guessmethod.rubyforge.org/] automatically corrects typos (method_missing hook) -=== Helper methods -See the source for a description. These methods are defined directly by irbtools. Each gem may add its own helper methods. - -*General*: ls, cat, rq, rrq/rerequire, ld, session_history, reset!, clear, use_ruby/use, rubies, use_gemset/gemset, gemsets, dbg - -*Clipboard*: copy, paste, copy_input, copy_output - -*CodeRay*: colorize, ray - ==== RVM If you have RVM installed, you can switch your gemset or ruby with gemset and use (use gemsets and rubies to get a list). The gemset can be changed within the session, while changing ruby starts a new one. Please also note that you need to have installed irbtools for that ruby version. I recommend installing it in the global gemset. diff --git a/Rakefile b/Rakefile index 3a3fca3..090ccd2 100644 --- a/Rakefile +++ b/Rakefile @@ -2,37 +2,37 @@ require 'rake' require 'rake/rdoctask' require 'fileutils' -def gemspec - @gemspec ||= eval(File.read('irbtools.gemspec'), binding, 'irbtools.gemspec') +def gemspec1 + @gemspec1 ||= eval(File.read('irbtools.gemspec'), binding, 'irbtools.gemspec') +end + +def gemspec2 + @gemspec2 ||= eval(File.read('every_day_irb.gemspec'), binding, 'every_day_irb.gemspec') end desc "Build the gem" task :gem => :gemspec do sh "gem build irbtools.gemspec" + sh "gem build every_day_irb.gemspec" FileUtils.mkdir_p 'pkg' - FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg' + FileUtils.mv "#{gemspec1.name}-#{gemspec1.version}.gem", 'pkg' + FileUtils.mv "#{gemspec2.name}-#{gemspec2.version}.gem", 'pkg' end desc "Install the gem locally (without docs)" task :install => :gem do - sh %{gem install pkg/#{gemspec.name}-#{gemspec.version} --no-rdoc --no-ri} + sh %{gem install pkg/#{gemspec2.name}-#{gemspec2.version} --no-rdoc --no-ri} + sh %{gem install pkg/#{gemspec1.name}-#{gemspec1.version} --no-rdoc --no-ri} end desc "Generate the gemspec" task :generate do - puts gemspec.to_ruby + puts gemspec1.to_ruby + puts gemspec2.to_ruby end desc "Validate the gemspec" task :gemspec do - gemspec.validate -end - -Rake::RDocTask.new do |rdoc| - version = File.exist?('VERSION') ? File.read('VERSION').chomp : "" - - rdoc.rdoc_dir = 'doc' - rdoc.title = "irbtools #{version}" - rdoc.rdoc_files.include('README*') - rdoc.rdoc_files.include('lib/**/*.rb') + gemspec1.validate + gemspec2.validate end diff --git a/every_day_irb.gemspec b/every_day_irb.gemspec new file mode 100644 index 0000000..3800877 --- /dev/null +++ b/every_day_irb.gemspec @@ -0,0 +1,16 @@ +# -*- encoding: utf-8 -*- +require 'rubygems' unless defined? Gem + +Gem::Specification.new do |s| + s.name = 'every_day_irb' + s.version = File.read('VERSION').chomp + + s.authors = ["Jan Lelis"] + s.summary = 'every_day_irb defines some helper methods that might be useful in irb.' + s.description = 'every_day_irb defines some helper methods that might be useful in every-day irb usage, e.g.: ls, cat, rq, rrq, ld, session_history, reset!, clear, dbg' + s.email = 'mail@janlelis.de' + s.extra_rdoc_files = %w[LICENSE] + s.files = Dir.glob(%w[lib/every_day_irb.rb VERSION every_day_irb.gemspec]) + s.homepage = 'http://github.com/janlelis/irbtools' + s.required_ruby_version = '>= 1.8.7' +end diff --git a/irbtools.gemspec b/irbtools.gemspec index 331af3a..fdacf74 100644 --- a/irbtools.gemspec +++ b/irbtools.gemspec @@ -6,13 +6,12 @@ Gem::Specification.new do |s| s.version = File.read('VERSION').chomp s.authors = ["Jan Lelis"] - s.date = %q{2011-03-28} - s.summary = %q{irbtools is a meta gem which installs some useful irb gems and configures your irb.} - s.description = %q{irbtools is a meta gem which installs some useful irb gems and configures your irb. Simply put a require 'irbtools' in the .irbrc file in your home directory.} - s.email = %q{mail@janlelis.de} + s.summary = 'irbtools is a meta gem that installs useful irb gems and configures your irb.' + s.description = 'irbtools is a meta gem that installs useful irb gems and configures your irb. Simply put a require "irbtools" in the .irbrc file in your home directory.' + s.email = 'mail@janlelis.de' s.extra_rdoc_files = %w[LICENSE README.rdoc] - s.files = Dir.glob(%w[lib/**/*.rb ]) + %w{VERSION CHANGELOG Rakefile irbtools.gemspec} - s.homepage = %q{http://github.com/janlelis/irbtools} + s.files = %w[lib/irbtools.rb lib/irbtools/configure.rb lib/irbtools/libraries.rb VERSION CHANGELOG Rakefile irbtools.gemspec] + s.homepage = 'http://github.com/janlelis/irbtools' s.required_ruby_version = '>= 1.8.7' s.add_dependency %q, ">= 0.6.5" @@ -22,10 +21,12 @@ Gem::Specification.new do |s| s.add_dependency %q, ">= 0.9.7" s.add_dependency %q, "~> 0.9" s.add_dependency %q, ">= 0.3.3" - s.add_dependency %q, ">= 0.2.4" + s.add_dependency %q, ">= 0.2.5" s.add_dependency %q, ">= 0.0.8" s.add_dependency %q, "~> 0.1.0" s.add_dependency %q, ">= 0" s.add_dependency %q, ">= 0" s.add_dependency %q, ">= 1.2.1" + s.add_dependency %q, ">= 1.0.0" + s.add_dependency %q, ">= #{ s.version }" end diff --git a/lib/irbtools/general.rb b/lib/every_day_irb.rb similarity index 56% rename from lib/irbtools/general.rb rename to lib/every_day_irb.rb index c76f4e3..7400520 100644 --- a/lib/irbtools/general.rb +++ b/lib/every_day_irb.rb @@ -1,3 +1,9 @@ +# every_day_irb defines some helper methods that might be useful in every-day irb usage + +module EveryDayIrb + VERSION = File.read( File.dirname(__FILE__) + '/../VERSION' ).chomp +end + # shows the contents of your current directory (more such commands available by FileUtils) def ls(path='.') Dir[ File.join( path, '*' )].map{|res| res =~ /^#{path}\/?/; $' } @@ -60,58 +66,6 @@ def clear system 'clear' end -# change ruby version (requires rvm) -autoload :RVM, 'irbtools/rvm' - -def rubies - RVM.current.list_strings -end - -def use(which = nil) # TODO with gemsets? - # show current ruby if called without options - if !which - return RVM.current.environment_name[/^.*@|.*$/].chomp('@') - end - - # start ruby :) - begin - RVM.use! which.to_s - rescue RVM::IncompatibleRubyError => err - err.message =~ /requires (.*?) \(/ - rubies = RVM.current.list_strings - if rubies.include? $1 - # remember history... - run_irb = proc{ exec "#{ $1 } -S #{ $0 }" } - if defined?(Ripl) && Ripl.instance_variable_get(:@shell) # ripl is running - Ripl.shell.write_history if Ripl.shell.respond_to? :write_history - run_irb.call - else - at_exit(&run_irb) - exit - end - else - warn "Sorry, that Ruby version could not be found (see rubies)!" - end - end -end -alias use_ruby use - -def gemsets - RVM.current.gemset.list -end - -def gemset(which = nil) - if which - if RVM.current.gemset.list.include? which.to_s - RVM.use! RVM.current.environment_name.gsub /(@.*?$)|$/, "@#{ which }" - else - warn "Sorry, that gemset could not be found (see gemsets)!" - end - end - RVM.current.gemset_name -end -alias use_gemset gemset - # load debugger, inspired by rdp def dbg begin diff --git a/lib/irbtools.rb b/lib/irbtools.rb index 550194a..1391428 100644 --- a/lib/irbtools.rb +++ b/lib/irbtools.rb @@ -39,7 +39,7 @@ # load: start load_libraries_proc[ Irbtools.libraries[:start] ] - # load : after_rc / sub-session + # load: after_rc / sub-session if defined?(Ripl) && Ripl.started? if defined? Ripl::AfterRc Ripl.after_rcs += Irbtools.libraries[:after_rc] @@ -54,18 +54,13 @@ } end - # load: autoload + # load: autoload hooks Irbtools.libraries[:autoload].each{ |constant, lib| gem lib autoload constant, lib Irbtools.send :library_loaded, lib } - - # # # # # - # general shortcuts & helper methods - require File.expand_path('irbtools/general', File.dirname(__FILE__) ) - # # # # # # irb options unless defined? Ripl diff --git a/lib/irbtools/configure.rb b/lib/irbtools/configure.rb index c7ba6cf..c7ac91f 100644 --- a/lib/irbtools/configure.rb +++ b/lib/irbtools/configure.rb @@ -15,6 +15,8 @@ # # # # # # define module methods module Irbtools + VERSION = File.read( File.dirname(__FILE__) + '/../../VERSION' ).chomp + @lib_hooks = Hash.new{|h,k| h[k] = [] } @libs = { :start => [], :after_rc => [], :autoload => [], :thread => {} } @packages = [] @@ -89,8 +91,6 @@ def init end alias start init end - - VERSION = ( File.read File.expand_path( '../../VERSION', File.dirname(__FILE__)) ).chomp end # # # # # diff --git a/lib/irbtools/libraries.rb b/lib/irbtools/libraries.rb index c68cdf0..8306d13 100644 --- a/lib/irbtools/libraries.rb +++ b/lib/irbtools/libraries.rb @@ -24,7 +24,9 @@ Boson.start :verbose => false end -Irbtools.add_library :fileutils, :thread => 40 do # cd, pwd, ln_s, mv, rm, mkdir, touch ... ;) +Irbtools.add_library 'every_day_irb', :thread => 40 # ls, cat, rq, rrq, ld, session_history, reset!, clear, dbg, ... + +Irbtools.add_library :fileutils, :thread => 50 do # cd, pwd, ln_s, mv, rm, mkdir, touch ... ;) include FileUtils::Verbose # patch cd so that it also shows the current directory @@ -44,19 +46,19 @@ def cd( path = File.expand_path('~') ) end end -Irbtools.add_library 'zucker/debug', :thread => 50 # nice debug printing (q, o, c, .m, .d) +Irbtools.add_library 'zucker/debug', :thread => 60 # nice debug printing (q, o, c, .m, .d) -Irbtools.add_library 'ap', :thread => 60 # nice debug printing (ap) +Irbtools.add_library 'ap', :thread => 70 # nice debug printing (ap) -Irbtools.add_library 'wirb/wp', :thread => 70 # ap alternative (wp) +Irbtools.add_library 'wirb/wp', :thread => 80 # ap alternative (wp) -Irbtools.add_library 'g', :thread => 80 if OS.mac? # nice debug printing (g) - MacOS only :/ +Irbtools.add_library 'g', :thread => 90 if OS.mac? # nice debug printing (g) - MacOS only :/ -Irbtools.add_library 'interactive_editor', :thread => 90 # lets you open vim (or your favourite editor), hack something, save it, and it's loaded in the current irb session +Irbtools.add_library 'interactive_editor', :thread => 100 # lets you open vim (or your favourite editor), hack something, save it, and it's loaded in the current irb session -Irbtools.add_library 'sketches', :thread => 100 # another, more flexible "start editor and it gets loaded into your irb session" plugin +Irbtools.add_library 'sketches', :thread => 110 # another, more flexible "start editor and it gets loaded into your irb session" plugin -Irbtools.add_library :ori, :thread => 110 do # object oriented ri method +Irbtools.add_library :ori, :thread => 120 do # object oriented ri method class Object # patch ori to also allow shell-like "Array#slice" syntax def ri(*args) @@ -125,4 +127,55 @@ def mf(*args, &block) end end +Irbtools.add_library 'rvm_loader', :autoload => :RVM do + def rubies + RVM.current.list_strings + end + + def use(which = nil) # TODO with gemsets? + # show current ruby if called without options + if !which + return RVM.current.environment_name[/^.*@|.*$/].chomp('@') + end + + # start ruby :) + begin + RVM.use! which.to_s + rescue RVM::IncompatibleRubyError => err + err.message =~ /requires (.*?) \(/ + rubies = RVM.current.list_strings + if rubies.include? $1 + # remember history... + run_irb = proc{ exec "#{ $1 } -S #{ $0 }" } + if defined?(Ripl) && Ripl.instance_variable_get(:@shell) # ripl is running + Ripl.shell.write_history if Ripl.shell.respond_to? :write_history + run_irb.call + else + at_exit(&run_irb) + exit + end + else + warn "Sorry, that Ruby version could not be found (see rubies)!" + end + end + end + alias use_ruby use + + def gemsets + RVM.current.gemset.list + end + + def gemset(which = nil) + if which + if RVM.current.gemset.list.include? which.to_s + RVM.use! RVM.current.environment_name.gsub /(@.*?$)|$/, "@#{ which }" + else + warn "Sorry, that gemset could not be found (see gemsets)!" + end + end + RVM.current.gemset_name + end + alias use_gemset gemset +end + # J-_-L diff --git a/lib/irbtools/rvm.rb b/lib/irbtools/rvm.rb deleted file mode 100644 index b708674..0000000 --- a/lib/irbtools/rvm.rb +++ /dev/null @@ -1,8 +0,0 @@ -# test if installed -unless rvm_path = ENV['rvm_path'] - raise 'Ruby Version Manager must be installed to use this command' -end - -# load rvm ruby api -$LOAD_PATH.unshift File.join(rvm_path, 'lib') -require 'rvm'