Module: IRT::Utils

Included in:
IRT
Defined in:
lib/irt/utils.rb,
lib/irt/extensions/rails_server.rb

Instance Method Summary collapse

Instance Method Details

#ask_run_new_file(new_file_path, source_path, tmp) ⇒ Object

skips asking to run the save file if it is a tmp file in a server session because the server is exiting so no rerun is possible



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/irt/extensions/rails_server.rb', line 62

def ask_run_new_file(new_file_path, source_path, tmp)
  if IRT::Prompter.yes?( %(Do you want to run the file "#{File.basename(new_file_path)}" now?) )
    # if we are saving a tmp_file from a save_as command (not from an at_exit block)
    if ENV['IRT_TMP_PATH'] && IRT.respond_to?(:irt_file) && IRT.irt_file == Pathname.new(ENV['IRT_TMP_PATH']).realpath
      ENV.delete('IRT_TMP_PATH')
      # reset tmp file content so check_save_tmp_file will be skipped
      File.open(source_path, 'w'){|f| f.puts "\n"}
    end
    if tmp && IRT.cli?
      ENV['IRT_COMMAND'] = ENV['IRT_COMMAND'].sub(/#{Regexp.quote(source_path)}/, new_file_path)
      exec ENV['IRT_COMMAND']
    else
      IRT::Session.run_file new_file_path
    end
  end
end


59
60
61
# File 'lib/irt/utils.rb', line 59

def copyright
  @copyrignt ||= Dye.dye "irt #{version} (c) 2010-2013 Domizio Demichelis", :blue, :bold
end

#create_tmp_fileObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/irt/utils.rb', line 27

def create_tmp_file()
  require 'tempfile'
  tmp_file = Tempfile.new(['', '.irt'])
  tmp_file << "\n" # one empty line makes irb of 1.9.2 happy
  tmp_file.flush
  # ENV used because with IRT.cli? 2 different processes need to access the same path
  ENV['IRT_TMP_PATH'] = tmp_file.path
  at_exit { check_save_tmp_file(tmp_file) }
  ENV['IRT_TMP_PATH']
end

#edit_with(editor, file, line = nil) ⇒ Object



49
50
51
52
53
# File 'lib/irt/utils.rb', line 49

def edit_with(editor, file, line=nil)
  cmd_format = IRT.send("#{editor}_command_format".to_sym)
  raise IRT::NotImplementedError, "#{cmd}_command_format missing" unless cmd_format
  system sprintf(cmd_format, file, line||0)
end

#original_ask_run_new_fileObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/irt/extensions/rails_server.rb', line 59

def ask_run_new_file(new_file_path, source_path, tmp)
  if IRT::Prompter.yes?( %(Do you want to run the file "#{File.basename(new_file_path)}" now?) )
    # if we are saving a tmp_file from a save_as command (not from an at_exit block)
    if ENV['IRT_TMP_PATH'] && IRT.respond_to?(:irt_file) && IRT.irt_file == Pathname.new(ENV['IRT_TMP_PATH']).realpath
      ENV.delete('IRT_TMP_PATH')
      # reset tmp file content so check_save_tmp_file will be skipped
      File.open(source_path, 'w'){|f| f.puts "\n"}
    end
    if tmp && IRT.cli?
      ENV['IRT_COMMAND'] = ENV['IRT_COMMAND'].sub(/#{Regexp.quote(source_path)}/, new_file_path)
      exec ENV['IRT_COMMAND']
    else
      IRT::Session.run_file new_file_path
    end
  end
end

#save_as(file_path, source_path = IRT.irt_file, tmp = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/irt/utils.rb', line 38

def save_as(file_path, source_path=IRT.irt_file, tmp=false)
  new_file_path = File.expand_path(file_path)
  if File.exists?(new_file_path)
    return false if IRT::Prompter.no? %(Do you want to overwrite "#{new_file_path}"?), :hint => '[y|<enter=n]', :default => 'n'
  end
  dirname = File.dirname(new_file_path)
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
  FileUtils.cp source_path, new_file_path
  ask_run_new_file new_file_path, source_path, tmp
end

#versionObject



55
56
57
# File 'lib/irt/utils.rb', line 55

def version
  @version ||= File.read(File.expand_path('../../../VERSION', __FILE__)).strip
end