Class: Bio::Shell::Setup

Inherits:
Object show all
Defined in:
lib/bio/shell/setup.rb

Instance Method Summary collapse

Constructor Details

#initializeSetup

Returns a new instance of Setup.



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
39
40
# File 'lib/bio/shell/setup.rb', line 14

def initialize
  check_ruby_version

  # command line options
  getoptlong

  # setup working directory
  savedir = setup_savedir

  # load configuration and plugins
  Bio::Shell.configure(savedir)

  # set default to irb mode
  Bio::Shell.cache[:mode] = ((defined? @mode) && @mode) || :irb

  case Bio::Shell.cache[:mode]
  when :web
    # setup rails server
    Bio::Shell::Web.new
  when :irb
    # setup irb server
    Bio::Shell::Irb.new
  when :script
    # run bioruby shell script
    Bio::Shell::Script.new(@script)
  end
end

Instance Method Details

#check_ruby_versionObject



42
43
44
45
46
# File 'lib/bio/shell/setup.rb', line 42

def check_ruby_version
  if RUBY_VERSION < "1.8.2"
    raise "BioRuby shell runs on Ruby version >= 1.8.2"
  end
end

#getoptlongObject

command line argument (working directory or bioruby shell script file)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bio/shell/setup.rb', line 49

def getoptlong
  opts = GetoptLong.new
  opts.set_options(
    [ '--rails',   '-r',  GetoptLong::NO_ARGUMENT ],
    [ '--web',     '-w',  GetoptLong::NO_ARGUMENT ],
    [ '--console', '-c',  GetoptLong::NO_ARGUMENT ],
    [ '--irb',     '-i',  GetoptLong::NO_ARGUMENT ]
  )
  opts.each_option do |opt, arg|
    case opt
    when /--rails/, /--web/
      @mode = :web
    when /--console/, /--irb/
      @mode = :irb
    end
  end
end

#install_savedir(savedir) ⇒ Object



104
105
106
# File 'lib/bio/shell/setup.rb', line 104

def install_savedir(savedir)
  FileUtils.makedirs(savedir)
end

#setup_savedirObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bio/shell/setup.rb', line 67

def setup_savedir
  arg = ARGV.shift

  # Options after the '--' argument are not parsed by GetoptLong and
  # are passed to irb or rails.  This hack preserve the first option
  # when working directory of the project is not given.
  if arg and arg[/^-/]
    ARGV.unshift arg
    arg = nil
  end

  if arg.nil?
    # run in the current directory
    if File.exist?(Bio::Shell::Core::HISTORY)
      savedir = Dir.pwd
    else
      savedir = File.join(ENV['HOME'].to_s, ".bioruby")
      install_savedir(savedir)
    end
  elsif File.file?(arg)
    # run file as a bioruby shell script
    savedir = File.join(File.dirname(arg), "..")
    @script = arg
    @mode = :script
  else
    # run in new or existing directory
    if arg[/^#{File::SEPARATOR}/]
      savedir = arg
    else
      savedir = File.join(Dir.pwd, arg)
    end
    install_savedir(savedir)
  end

  return savedir
end