#!/usr/bin/env ruby at_exit{ Dye.print_reset_colors } require 'rubygems' require 'fileutils' require 'optparse' require 'irt/utils' copy = IRT.copyright options = {} optparse = OptionParser.new do |opts| opts.banner = < '[=y|n]', :default => 'y' options[:interactive_eof] = true dirname = File.dirname(path) FileUtils.mkdir_p(dirname) unless File.directory?(dirname) File.open(path, 'w') {|f| f.puts "\n" } # one empty line makes irb of 1.9.2 happy end File.directory?(path) ? Dir.glob(File.join(path, '**/*.irt')) : path end.flatten.compact if files.empty? IRT::Prompter.say_notice 'No *.irt files to run' exit end cmd_format = if File.exists?('./config/environment.rb') && !options[:no_rails] if File.exists?('./script/rails') gemfile_path = ENV['BUNDLE_GEMFILE'] || 'Gemfile' gemfile = File.read(gemfile_path) unless gemfile.match(/\bgem\b.+\birt\b/) IRT::Prompter.say_warning %(The Gemfile doesn't look to include any 'gem "irt"' statement.\nIRT will probably not work until you add it!) IRT::Prompter.yes?("Do you want to add irt to your Gemfile?", :hint => '[=y|n]', :default => 'y') do File.open(gemfile_path, 'a') do |f| f.puts %(\ngem "irt") end end end minor, patch = gemfile.match(/\bgem\b.+\brails\b.+3\.(\d+)\.(\d+)/).captures rails_version = (minor.to_i > 0 || patch.to_i > 9) ? :new : :old 'rails c %s %s %s' elsif File.exists?('./script/console') rails_version = :old 'ruby script/console --irb="irt_rails2 %2$s" %1$s %3$s' end else 'irt_irb %s %s' end ENV['IRT_INTERACTIVE_EOF'] = options[:interactive_eof].inspect if options[:interactive_eof] files.each do |file| ENV['IRT_COMMAND'] = case rails_version when :new sprintf cmd_format, options[:rails_env], options[:irb_options], file when :old sprintf cmd_format, options[:irb_options], file, options[:rails_env] else sprintf cmd_format, options[:irb_options], file end exit(1) unless system ENV['IRT_COMMAND'] end