Module: ArshCommands

Defined in:
lib/rubyeval.rb,
lib/cd.rb,
lib/rvm.rb,
lib/alias.rb,
lib/replacestring.rb

Overview

Evaluate ruby code typed into command line. Allow multiple lines of code before evaluting blcoks.

Class Method Summary (collapse)

Class Method Details

+ (Object) alias(args)



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/alias.rb', line 2

def self.alias(args)
  args = args.join(" ")
  alias_cmd, alias_body = args.split(/=/,2)
  alias_body_cmd, alias_body_params = alias_body.split(/ /,2)
  alias_cmd.strip!
  alias_body_cmd.strip!
  if alias_body_params != nil then
    alias_body_params.strip
  else
    alias_body_params = ""
  end
  new_alias =<<-EOS
    def self.#{alias_cmd}(args=[])
   	args = ["#{alias_body_params} "] + args
   	system("#{alias_body_cmd} \#{args.join(' ')}")
   end
 EOS
  ArshCommands.module_eval(new_alias)
end

+ (Object) cd(dir)

Change directory



3
4
5
6
7
8
9
10
11
12
# File 'lib/cd.rb', line 3

def self.cd(dir)
  dir = ["#{ENV['HOME']}"] if dir.nitems == 0
  if File.directory?(File.expand_path(dir.join(" ")))
    Dir.chdir(File.expand_path(dir.join(" ")))
  elsif dir =~ /~/
    dir.gsub("~","#{ENV['HOME']}")
  else
    puts "Invalid Directory #{dir.to_s}"
  end
end

+ (Object) replacestring(string)



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/replacestring.rb', line 2

def self.replacestring(string)
  @rep_new_string = string
  begin
    repstring = /<%(.*?)%>/.match(@rep_new_string)
    loop do
      rep_original = repstring[0]
      rep_new = eval(repstring[1]).to_s
      @rep_new_string = @rep_new_string.gsub(rep_original,rep_new)
      repstring = /<%(.*?)%>/.match(@rep_new_string)
      break if repstring == nil
    end if /<%(.*?)%>/.match(@rep_new_string) != nil
  rescue
    puts $!
    @rep_new_string.gsub!(repstring[0],"[ERR]")
  end
  return @rep_new_string
end

+ (Object) ruby_eval(input, blocks = true)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubyeval.rb', line 9

def self.ruby_eval(input,blocks=true)
 if blocks
    # keywords that start a block
    start_strings = ["class,","module","def","do","{","("]
    # Keywords that end a block
    end_strings = ["end","}",")"]
    # Add last line typed to
    @rubyeval_history << input
    start_strings.each {|string| @rubyeval_indent += 1 if input.include? string}
    end_strings.each {|string| @rubyeval_indent -=1 if input.include? string}
    #input.strip.each(" ") {|string| @rubyeval_indent += 1 if start_strings.include? string }
    #input.strip.each(" ") {|string| @rubyeval_indent -= 1 if end_strings.include? string }
    if @rubyeval_indent <= 0
      @rubyeval_indent = 0
      begin
        eval(@rubyeval_history.join("\n"),@rubyeval_proc.binding)
        @rubyeval_history = Array.new
      rescue ScriptError, StandardError
        @rubyeval_history = Array.new
        puts "#{$!}"
      end
    end
  else
    begin
      eval(input,@rubyeval_proc.binding)
    rescue ScriptError, StandardError
      puts "#{$!}"
    end
  end
end

+ (Object) rubyeval_indent



6
7
8
# File 'lib/rubyeval.rb', line 6

def self.rubyeval_indent
  return @rubyeval_indent
end

+ (Object) rvm(command)



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rvm.rb', line 2

def self.rvm(command)
  command = command.is_a?(Array) ? command.join(' ') : command
  case command
  when /^use /
    @output = %x{bash -c "source #{ENV['HOME']}/.rvm/scripts/rvm; rvm #{command}; env | grep -i rvm"}
    @output.split("\n").select { |l| l[/=/] }.each do |n|
      var, value = n.split(/=/,2)
      ENV[var] = value
    end
    puts "\e[32mUsing #{ENV['GEM_HOME']}\e[0m"
  else
    puts "\e[01;32mUsing Ctrl-C on this operation will push the operation to the background.\nThe operation will not stop\e[0m"
    pid = ""
    Process.fork do
      pid = Process.pid
      print "\n"
      exec %Q{bash -c "source #{ENV['HOME']}/.rvm/scripts/rvm; rvm #{command}"}
    end
    Process.waitpid(pid.to_i)
  end
end