lib/genomer/runtime.rb in genomer-0.0.8 vs lib/genomer/runtime.rb in genomer-0.0.9

- old
+ new

@@ -1,54 +1,82 @@ require 'unindent' require 'tempfile' require 'md2man' +require 'genomer/version' + class Genomer::Runtime attr :command attr :arguments attr :flags + 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" + } + } + + + def message(type,msg) + content = MESSAGES[type][msg] + type == :error ? raise(Genomer::Error, content) : content + end + + def initialize(settings) @command = settings.rest.shift @arguments = settings.rest @flags = settings end def execute! - case command - when nil then short_help - when "help" then help - when "init" then init - when "man" then man - else run_plugin + 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 - def short_help - msg =<<-EOF - genomer COMMAND [options] - run `genomer help` for a list of available commands - EOF - msg.unindent - end - 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! - msg << Genomer::Plugin.plugins.inject(String.new) do |str,p| - str << ' ' - str << p.name.gsub("genomer-plugin-","").ljust(12) - str << p.summary - str << "\n" + 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 def man @@ -63,16 +91,11 @@ raise Genomer::Error, "No manual entry for command '#{arguments.join(' ')}'" end Kernel.exec "man #{groffed_man_file(location).path}" else - msg =<<-EOF - genomer man COMMAND - run `genomer help` for a list of available commands - EOF - msg.unindent! - msg.strip + message :output, :man end end def man_file(arguments) plugin = arguments.first @@ -108,13 +131,16 @@ file.print Genomer::Files.send(name.gsub('.','_').to_sym) end end "Genomer project '#{project_name}' created.\n" - end def run_plugin Genomer::Plugin[command].new(arguments,flags).run + end + + def genomer_project? + File.exists?('Gemfile') end end