#!/usr/bin/env ruby require 'rubygems' require 'fileutils' require 'tempfile' require 'optparse' require 'prompter' require 'dye' prompter = Prompter.new do |pr| def pr.say_echo(result, opts={}) out = sprintf " #> %s", result say out, :style => :yellow end end version = File.read(File.expand_path('../../VERSION', __FILE__)).strip copy = Dye.dye "irt #{version} (c) 2010-2011 Domizio Demichelis", :blue, :bold options = {} optparse = OptionParser.new do |opts| opts.banner = < 1 prompter.yes? %(The template file has been modified, do you want to save it?) do prompter.choose %(Enter the file path to save:), /[\w0-9_]/ do |as_file| IRT::Utils.save_as(tmp_path, as_file, prompter){ system(ENV['IRT_COMMAND']) } end end end end [ tmp_path ] else ARGV.map {|p| File.expand_path(p) } end files = paths.map do |path| unless File.exists?(path) next if prompter.no? %(Do you want to create the file "#{path}"?), :hint => '[ '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 if files.empty? 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 = File.read('Gemfile') unless gemfile.match(/\bgem\b.+\birt\b/) prompter.say_warning %(The Gemfile doesn't look to include any 'gem "irt"' statement.\nIRT will probably not work until you add it!) prompter.yes?("Do you want to add irt to your Gemfile?", :hint => '[enter=y|n]', :default => 'y') do File.open('Gemfile', 'a') do |f| f.puts %(gem "irt") end end end 'rails c %s %s %s' elsif File.exists?('./script/console') 'ruby script/console --irb="irt_rails2 %s"' end else 'irt_irb %s %s' end if cmd_format.match(/^ruby script\/console/) ENV['RAILS_ENV'] ||= options[:rails_env] end ENV['IRT_INTERACTIVE_EOF'] = options[:interactive_eof].inspect if options[:interactive_eof] files.each do |file| ENV['IRT_COMMAND'] = sprintf cmd_format, file, options[:irb_options], options[:rails_env] unless system(ENV['IRT_COMMAND']) puts Dye.sgr(:clear) if Dye.color? exit(1) end end