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_generation_process ⇒ Object
Constructor Details
#initialize(arguments, io = STDOUT) ⇒ ConsoleRunner
Returns a new instance of ConsoleRunner.
19 20 21 22 23 24 25 |
# File 'lib/yarrow/console_runner.rb', line 19 def initialize(arguments, io=STDOUT) @out = io @arguments = arguments = {} @targets = [] @config = Configuration.load_defaults end |
Instance Method Details
#config ⇒ Object
27 28 29 |
# File 'lib/yarrow/console_runner.rb', line 27 def config @config end |
#has_option?(option) ⇒ Boolean
136 137 138 |
# File 'lib/yarrow/console_runner.rb', line 136 def has_option?(option) .has_key? option end |
#is_option?(argument) ⇒ Boolean
132 133 134 |
# File 'lib/yarrow/console_runner.rb', line 132 def is_option?(argument) argument[0] == "-" end |
#normalize_theme_path ⇒ Object
101 102 103 |
# File 'lib/yarrow/console_runner.rb', line 101 def normalize_theme_path # noop end |
#print_error(e) ⇒ Object
155 156 157 |
# File 'lib/yarrow/console_runner.rb', line 155 def print_error(e) @out.puts "Error! " + e.to_s end |
#print_footer ⇒ Object
151 152 153 |
# File 'lib/yarrow/console_runner.rb', line 151 def @out.puts "Content generated at {path}!" end |
#print_header ⇒ Object
147 148 149 |
# File 'lib/yarrow/console_runner.rb', line 147 def print_header @out.puts Yarrow::APP_NAME + " " + Yarrow::VERSION end |
#print_help ⇒ Object
159 160 161 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 |
# File 'lib/yarrow/console_runner.rb', line 159 def print_help help = " See http://yarrow.maetl.net for more information.\n\n Usage:\n\n $ yarrow [options]\n $ yarrow [options] <input>\n $ yarrow [options] <input input> <output>\n\n Arguments\n\n <input> - Path to source directory or an individual source file. If not supplied\n defaults to the current working directory. Multiple directories\n can be specified by repeating arguments, separated by whitespace.\n\n <output> - Path to the generated documentation. If not supplied, defaults to\n ./docs in the current working directory. If it does not exist, it\n is created. If it does exist it remains in place, but existing\n files are overwritten by new files with the same name.\n\n Options\n\n Use -o or --option for boolean switches and --option=value or --option:value\n for options that require an explicit value to be set.\n\n-h --help [switch] Display this help message and exit\n-v --version [switch] Display version header and exit\n-c --config [string] Path to a config properties file\n-p --packages [string] Defines package convention for the model\n-t --theme [string] Template theme for generated docs\n\n\n" @out.puts help end |
#process_arguments ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/yarrow/console_runner.rb', line 61 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/yarrow/console_runner.rb', line 81 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 = [:config] @config.deep_merge! Configuration.load(path) end @config. = .to_hash # normalize_theme_path # theme = @config.options.theme # @config.append load_configuration(theme) end |
#register_option(raw_option) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/yarrow/console_runner.rb', line 105 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) [name.to_sym] = value return end else if ENABLED_OPTIONS.has_key?(name.to_sym) [ENABLED_OPTIONS[name.to_sym]] = value return end end raise "Unrecognized option: #{raw_option}" end |
#run_application ⇒ Object
31 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 |
# File 'lib/yarrow/console_runner.rb', line 31 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_generation_process ⇒ Object
140 141 142 143 144 145 |
# File 'lib/yarrow/console_runner.rb', line 140 def run_generation_process generator = Generator.new(@config) generator.process do |manifest| p manifest end end |