Class: Genomer::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/genomer/runtime.rb

Constant Summary collapse

MESSAGES =
{
  :error  => {
    :init_again =>
      "This directory contains a 'Gemfile' and already appears to be a genomer project."
},
  :output => {
    :version => "Genomer version #{Genomer::VERSION}",
    :not_project =>
      "Use `genomer init NAME` to create a new genomer project called NAME",
    :simple_help =>
      "genomer COMMAND [options]\nrun `genomer help` for a list of available commands",
    :man =>
      "genomer man COMMAND\nrun `genomer help` for a list of available commands"
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Runtime

Returns a new instance of Runtime.



36
37
38
39
40
# File 'lib/genomer/runtime.rb', line 36

def initialize(settings)
  @command   = settings.rest.shift
  @arguments = settings.rest
  @flags     = settings
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



10
11
12
# File 'lib/genomer/runtime.rb', line 10

def arguments
  @arguments
end

#commandObject (readonly)

Returns the value of attribute command.



9
10
11
# File 'lib/genomer/runtime.rb', line 9

def command
  @command
end

#flagsObject (readonly)

Returns the value of attribute flags.



11
12
13
# File 'lib/genomer/runtime.rb', line 11

def flags
  @flags
end

Instance Method Details

#execute!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/genomer/runtime.rb', line 42

def execute!
  return message :output, :version if flags[:version]

  if genomer_project?
    case command
    when nil    then message :output, :simple_help
    when "help" then help
    when "init" then message :error, :init_again
    when "man"  then man
    else             run_plugin
    end
  else
    case command
    when "init" then init
    else             message :output, :not_project
    end
  end
end

#genomer_project?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/genomer/runtime.rb', line 146

def genomer_project?
  File.exists?('Gemfile')
end

#groffed_man_file(original_man_file) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/genomer/runtime.rb', line 110

def groffed_man_file(original_man_file)
  converted_man = Tempfile.new("genome-manpage-")
  File.open(converted_man.path,'w') do |out|
    out.puts render_man(File.read(original_man_file))
  end
  converted_man
end

#helpObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/genomer/runtime.rb', line 61

def help
  msg =<<-EOF
    genomer COMMAND [options]

    Available commands:
      init        Create a new genomer project
      man         View man page for the specified plugin
  EOF
  msg.unindent!

  if File.exists?('Gemfile')
    msg << Genomer::Plugin.plugins.inject(String.new) do |str,p|
      str << '  '
      str << p.name.gsub("genomer-plugin-","").ljust(12)
      str << p.summary
      str << "\n"
    end
  end
  msg.strip
end

#initObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/genomer/runtime.rb', line 118

def init
  project_name = arguments.first
  if File.exists?(project_name)
    raise Genomer::Error, "Directory '#{project_name}' already exists."
  end

  require 'genomer/files'

  Dir.mkdir project_name
  Dir.mkdir File.join(project_name,'assembly')

  File.open(File.join(project_name,'Gemfile'),'w') do |file|
    file.print Genomer::Files.gemfile
  end

  ['scaffold.yml','sequence.fna','annotations.gff'].each do |name|
    File.open(File.join(project_name,'assembly',name),'w') do |file|
      file.print Genomer::Files.send(name.gsub('.','_').to_sym)
    end
  end

  "Genomer project '#{project_name}' created.\n"
end

#manObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/genomer/runtime.rb', line 82

def man
  if not arguments.empty?
    location = if arguments.first == 'init'
                 File.expand_path File.dirname(__FILE__) + '/../../man/genomer-init.1.ronn'
               else
                 man_file(arguments.clone)
               end

    unless File.exists?(location)
      raise Genomer::Error, "No manual entry for command '#{arguments.join(' ')}'"
    end

    Kernel.exec "man #{groffed_man_file(location).path}"
  else
    message :output, :man
  end
end

#man_file(arguments) ⇒ Object



100
101
102
103
104
# File 'lib/genomer/runtime.rb', line 100

def man_file(arguments)
  plugin = arguments.first
  page = arguments.unshift("genomer").join('-') << ".ronn"
  File.join(Genomer::Plugin.fetch(plugin).full_gem_path, 'man', page)
end

#message(type, msg) ⇒ Object



30
31
32
33
# File 'lib/genomer/runtime.rb', line 30

def message(type,msg)
  content = MESSAGES[type][msg]
  type == :error ? raise(Genomer::Error, content) : content
end

#render_man(input) ⇒ Object



106
107
108
# File 'lib/genomer/runtime.rb', line 106

def render_man(input)
  Md2Man::ENGINE.render input
end

#run_pluginObject



142
143
144
# File 'lib/genomer/runtime.rb', line 142

def run_plugin
  Genomer::Plugin[command].new(arguments,flags).run
end