Sha256: 27b8cbdaa3d7b89fdde1e86b5a89c47e815412d4f2d9985a036469b0b8aa99ed

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require 'readline'
require 'rails/sh/rake'
require 'rails/sh/command'
require 'rails/sh/commands'

module Rails
  module Sh
    RAILS_SUB_COMMANDS = ['generate', 'destroy', 'plugin', 'benchmarker', 'profiler', 
                          'console', 'server', 'dbconsole', 'application', 'runner']

    class << self
      def start
        Rails::Sh::Rake.init
        puts "Rails.env: #{Rails.env}"
        puts "type `help` to print help"
        setup_readline
        while buf = Readline.readline("rails> ", true)
          line = buf.strip
          next if line.empty?
          begin
            execute(line)
          rescue SystemExit
            raise
          rescue Exception => e
            puts "\e[41m#{e.message}\n#{e.backtrace.join("\n")}\e[0m"
          end
          setup_readline
        end
      end

      def setup_readline
        Readline.basic_word_break_characters = ""
        Readline.completion_proc = lambda do |word|
          (
            Command.command_names.map { |name| name.to_s } +
            RAILS_SUB_COMMANDS +
            Rails::Sh::Rake.task_names.map { |name| "rake #{name}" }
          ).grep(/#{Regexp.quote(word)}/)
        end
      end

      def execute(line)
        start = Time.now
        if command = Command.find(line)
          arg = line.split(/\s+/, 2)[1] rescue nil
          command.call(arg)
        else
          execute_rails_command(line)
        end
        puts "\e[34m#{Time.now - start}sec\e[0m"
      end

      def execute_rails_command(line)
        pid = fork do
          ARGV.clear
          ARGV.concat line.split(/\s+/)
          puts "\e[42m$ rails #{ARGV.join(" ")}\e[0m"
          require 'rails/commands'
        end
        Process.waitpid(pid)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails-sh-1.2.5 lib/rails/sh.rb