Class: Baptize::Application
- Inherits:
-
Object
- Object
- Baptize::Application
- Defined in:
- lib/baptize/application.rb
Overview
Most of this is copy-paste fom Rake::Application github.com/ruby/rake/blob/master/lib/rake/application.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
Instance Method Summary collapse
-
#bapfile_location(backtrace = caller) ⇒ Object
:nodoc:.
- #define_command(name, description = nil, &block) ⇒ Object
- #dispatch ⇒ Object
-
#display_error_message(ex) ⇒ Object
Display the error message that caused the exception.
-
#exit_because_of_exception(ex) ⇒ Object
Exit the program because of an unhandled exception.
-
#find_bapfile_location ⇒ Object
:nodoc:.
-
#have_bapfile ⇒ Object
True if one of the files in BAPFILES is in the current directory.
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #load_bapfile ⇒ Object
- #parse_input ⇒ Object
-
#print_bapfile_directory(location) ⇒ Object
:nodoc:.
-
#raw_load_bapfile ⇒ Object
:nodoc:.
- #run ⇒ Object
-
#standard_exception_handling ⇒ Object
Provide standard exception handling for the given block.
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
33 34 35 36 37 38 39 |
# File 'lib/baptize/application.rb', line 33 def initialize @original_dir = Dir.pwd @bapfiles = %w(bapfile Bapfile bapfile.rb Bapfile.rb) @commands = {} Baptize.application = self load 'baptize/commands.rb' end |
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
31 32 33 |
# File 'lib/baptize/application.rb', line 31 def commands @commands end |
Instance Method Details
#bapfile_location(backtrace = caller) ⇒ Object
:nodoc:
148 149 150 151 152 153 154 155 |
# File 'lib/baptize/application.rb', line 148 def bapfile_location(backtrace=caller) # :nodoc: backtrace.map { |t| t[/([^:]+):/, 1] } re = /^#{@bapfile}$/ re = /#{re.source}/i if windows? backtrace.find { |str| str =~ re } || '' end |
#define_command(name, description = nil, &block) ⇒ Object
49 50 51 52 53 |
# File 'lib/baptize/application.rb', line 49 def define_command(name, description=nil, &block) Command.new(name, description, &block).tap do |command| @commands[command.name] = command end end |
#dispatch ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/baptize/application.rb', line 94 def dispatch args = @arguments.dup command_name = args.shift&.to_sym fail "No command given. Try baptize --help" if command_name.nil? command = commands[command_name] fail "Invalid command #{command_name}" unless command command.invoke(*args) end |
#display_error_message(ex) ⇒ Object
Display the error message that caused the exception.
173 174 175 176 |
# File 'lib/baptize/application.rb', line 173 def (ex) # :nodoc: $stderr.puts "Error during processing: #{$!}" $stderr.puts ex.backtrace.join("\n\t") end |
#exit_because_of_exception(ex) ⇒ Object
Exit the program because of an unhandled exception. (may be overridden by subclasses)
180 181 182 |
# File 'lib/baptize/application.rb', line 180 def exit_because_of_exception(ex) # :nodoc: exit(false) end |
#find_bapfile_location ⇒ Object
:nodoc:
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/baptize/application.rb', line 122 def find_bapfile_location # :nodoc: here = Dir.pwd until (fn = have_bapfile) Dir.chdir("..") return nil if Dir.pwd == here # || options.nosearch here = Dir.pwd end [fn, here] ensure Dir.chdir(@original_dir) end |
#have_bapfile ⇒ Object
True if one of the files in BAPFILES is in the current directory. If a match is found, it is copied into @bapfile.
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/baptize/application.rb', line 136 def have_bapfile # :nodoc: @bapfiles.each do |fn| if File.exist?(fn) others = Dir.glob(fn, File::FNM_CASEFOLD).sort return others.size == 1 ? others.first : fn elsif fn == '' return fn end end return nil end |
#load_bapfile ⇒ Object
103 104 105 106 107 |
# File 'lib/baptize/application.rb', line 103 def load_bapfile standard_exception_handling do raw_load_bapfile end end |
#parse_input ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 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 |
# File 'lib/baptize/application.rb', line 55 def parse_input @arguments = OptionParser.new do |opts| opts. = "baptize [OPTIONS] COMMAND" opts.separator "" opts.separator "COMMANDS are ..." width = [commands.values.map(&:name_with_parameters).map(&:length), 31].flatten.max commands.values.each do |command| opts.separator sprintf(" %-#{width}s %s\n", command.name_with_parameters, command.description) end opts.separator "" opts.separator "OPTIONS are ..." opts.on_tail("-h", "--help", "-H", "Display this help message.") do puts opts exit end opts.on('--bapfile', '-f [FILENAME]', "Use FILENAME as the bapfile to search for.") do |value| value ||= '' @bapfiles.clear @bapfiles << value end opts.on('--verbose', '-v', "Log message to standard output.") do |value| Registry.execution_scope.set :verbose, true end opts.on('--version', '-V', "Display the program version.") do |value| puts "baptize, version #{Baptize::VERSION}" exit end end.parse(ARGV) end |
#print_bapfile_directory(location) ⇒ Object
:nodoc:
118 119 120 |
# File 'lib/baptize/application.rb', line 118 def print_bapfile_directory(location) # :nodoc: $stderr.puts "(in #{Dir.pwd})" unless @original_dir == location end |
#raw_load_bapfile ⇒ Object
:nodoc:
109 110 111 112 113 114 115 116 |
# File 'lib/baptize/application.rb', line 109 def raw_load_bapfile # :nodoc: bapfile, location = find_bapfile_location fail "No Bapfile found (looking for: #{@bapfiles.join(', ')})" if bapfile.nil? @bapfile = bapfile Dir.chdir(location) print_bapfile_directory(location) load(File.(@bapfile)) if @bapfile && @bapfile != '' end |
#run ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/baptize/application.rb', line 41 def run standard_exception_handling do parse_input load_bapfile dispatch end end |
#standard_exception_handling ⇒ Object
Provide standard exception handling for the given block.
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/baptize/application.rb', line 158 def standard_exception_handling # :nodoc: yield rescue SystemExit # Exit silently with current status raise rescue OptionParser::InvalidOption => ex $stderr.puts ex. exit(false) rescue Exception => ex # Exit with error message (ex) exit_because_of_exception(ex) end |