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.
29 30 31 32 33 34 35 |
# File 'lib/baptize/application.rb', line 29 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.
27 28 29 |
# File 'lib/baptize/application.rb', line 27 def commands @commands end |
Instance Method Details
#bapfile_location(backtrace = caller) ⇒ Object
:nodoc:
144 145 146 147 148 149 150 151 |
# File 'lib/baptize/application.rb', line 144 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
45 46 47 48 49 |
# File 'lib/baptize/application.rb', line 45 def define_command(name, description=nil, &block) Command.new(name, description, &block).tap do |command| @commands[command.name] = command end end |
#dispatch ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/baptize/application.rb', line 90 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.
169 170 171 172 |
# File 'lib/baptize/application.rb', line 169 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)
176 177 178 |
# File 'lib/baptize/application.rb', line 176 def exit_because_of_exception(ex) # :nodoc: exit(false) end |
#find_bapfile_location ⇒ Object
:nodoc:
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/baptize/application.rb', line 118 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.
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/baptize/application.rb', line 132 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
99 100 101 102 103 |
# File 'lib/baptize/application.rb', line 99 def load_bapfile standard_exception_handling do raw_load_bapfile end end |
#parse_input ⇒ Object
51 52 53 54 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 |
# File 'lib/baptize/application.rb', line 51 def parse_input @arguments = OptionParser.new do |opts| opts. = "baptize [OPTIONS] COMMAND" opts.separator "" opts.separator "COMMANDS are ..." width = [commands.values.map(&:name).map(&:length), 31].flatten.max commands.values.each do |command| opts.separator sprintf(" %-#{width}s %s\n", command.name, 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:
114 115 116 |
# File 'lib/baptize/application.rb', line 114 def print_bapfile_directory(location) # :nodoc: $stderr.puts "(in #{Dir.pwd})" unless @original_dir == location end |
#raw_load_bapfile ⇒ Object
:nodoc:
105 106 107 108 109 110 111 112 |
# File 'lib/baptize/application.rb', line 105 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
37 38 39 40 41 42 43 |
# File 'lib/baptize/application.rb', line 37 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.
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/baptize/application.rb', line 154 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 |