Class: Yarrow::ConsoleRunner
- Inherits:
-
Object
- Object
- Yarrow::ConsoleRunner
- Defined in:
- lib/yarrow/console_runner.rb
Constant Summary collapse
- SUCCESS =
0
- FAILURE =
1
- ENABLED_OPTIONS =
{ :h => :help, :v => :version, :c => :config, :t => :theme, }
- ALLOWED_CONFIG_FILES =
[ '.yarrowdoc', 'Yarrowdoc' ]
Instance Method Summary collapse
- #config ⇒ Object
- #has_option?(option) ⇒ Boolean
-
#initialize(arguments, io = STDOUT) ⇒ ConsoleRunner
constructor
A new instance of ConsoleRunner.
- #is_option?(argument) ⇒ Boolean
- #normalize_theme_path ⇒ Object
- #print_error(e) ⇒ Object
- #print_footer ⇒ Object
- #print_header ⇒ Object
- #print_help ⇒ Object
- #process_arguments ⇒ Object
-
#process_configuration ⇒ Object
def load_configuration(path) ALLOWED_CONFIG_FILES.each do |filename| config_path = path + ‘/’ + filename if File.exists? config_path @config.deep_merge! Configuration.load(config_path) return end end end.
- #register_option(raw_option) ⇒ Object
- #run_application ⇒ Object
- #run_input_process ⇒ Object
- #run_output_process ⇒ Object
Constructor Details
#initialize(arguments, io = STDOUT) ⇒ ConsoleRunner
Returns a new instance of ConsoleRunner.
20 21 22 23 24 25 26 |
# File 'lib/yarrow/console_runner.rb', line 20 def initialize(arguments, io=STDOUT) @out = io @arguments = arguments @options = {} @targets = [] @config = Configuration.load_defaults end |
Instance Method Details
#config ⇒ Object
28 29 30 |
# File 'lib/yarrow/console_runner.rb', line 28 def config @config end |
#has_option?(option) ⇒ Boolean
138 139 140 |
# File 'lib/yarrow/console_runner.rb', line 138 def has_option?(option) @options.has_key? option end |
#is_option?(argument) ⇒ Boolean
134 135 136 |
# File 'lib/yarrow/console_runner.rb', line 134 def is_option?(argument) argument[0] == "-" end |
#normalize_theme_path ⇒ Object
102 103 104 105 |
# File 'lib/yarrow/console_runner.rb', line 102 def normalize_theme_path # noop end |
#print_error(e) ⇒ Object
158 159 160 |
# File 'lib/yarrow/console_runner.rb', line 158 def print_error(e) @out.puts "Error! " + e.to_s end |
#print_footer ⇒ Object
154 155 156 |
# File 'lib/yarrow/console_runner.rb', line 154 def @out.puts "Content generated at {path}!" end |
#print_header ⇒ Object
150 151 152 |
# File 'lib/yarrow/console_runner.rb', line 150 def print_header @out.puts Yarrow::APP_NAME + " " + Yarrow::VERSION end |
#print_help ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/yarrow/console_runner.rb', line 162 def print_help help = <<HELP See http://yarrowdoc.org for more information. Usage: $ yarrow [options] $ yarrow [options] <input> $ yarrow [options] <input input> <output> Arguments <input> - Path to source directory or an individual source file. If not supplied defaults to the current working directory. Multiple directories can be specified by repeating arguments, separated by whitespace. <output> - Path to the generated documentation. If not supplied, defaults to ./docs in the current working directory. If it does not exist, it is created. If it does exist it remains in place, but existing files are overwritten by new files with the same name. Options Use -o or --option for boolean switches and --option=value or --option:value for options that require an explicit value to be set. -h --help [switch] Display this help message and exit -v --version [switch] Display version header and exit -c --config [string] Path to a config properties file -p --packages [string] Defines package convention for the model -t --theme [string] Template theme for generated docs HELP @out.puts help end |
#process_arguments ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/yarrow/console_runner.rb', line 62 def process_arguments @arguments.each do |arg| if is_option?(arg) register_option(arg) else @targets << arg end end end |
#process_configuration ⇒ Object
def load_configuration(path)
ALLOWED_CONFIG_FILES.each do |filename|
config_path = path + '/' + filename
if File.exists? config_path
@config.deep_merge! Configuration.load(config_path)
return
end
end
end
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/yarrow/console_runner.rb', line 82 def process_configuration # load_configuration(Dir.pwd) # @targets.each do |input_path| # @config.deep_merge! load_configuration(input_path) # end if has_option?(:config) path = @options[:config] @config.deep_merge! Configuration.load(path) end @config. = @options.to_hash # normalize_theme_path # theme = @config.options.theme # @config.append load_configuration(theme) end |
#register_option(raw_option) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/yarrow/console_runner.rb', line 107 def register_option(raw_option) option = raw_option.gsub(":", "=") if option.include? "=" parts = option.split("=") option = parts[0] value = parts[1] else value = true end name = option.gsub("-", "") if option[0..1] == "--" if ENABLED_OPTIONS.has_value?(name.to_sym) @options[name.to_sym] = value return end else if ENABLED_OPTIONS.has_key?(name.to_sym) @options[ENABLED_OPTIONS[name.to_sym]] = value return end end raise "Unrecognized option: #{raw_option}" end |
#run_application ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/yarrow/console_runner.rb', line 32 def run_application print_header begin process_arguments if has_option?(:version) return SUCCESS end if has_option?(:help) print_help return SUCCESS end process_configuration run_input_process run_output_process SUCCESS rescue Exception => e print_error e FAILURE end end |
#run_input_process ⇒ Object
142 143 144 |
# File 'lib/yarrow/console_runner.rb', line 142 def run_input_process # noop end |
#run_output_process ⇒ Object
146 147 148 |
# File 'lib/yarrow/console_runner.rb', line 146 def run_output_process # noop end |