Class: Yarrow::ConsoleRunner

Inherits:
Object
  • Object
show all
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

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(File.dirname(__FILE__) + "/defaults.yml")
end

Instance Method Details

#configObject



28
29
30
# File 'lib/yarrow/console_runner.rb', line 28

def config
  @config
end

#has_option?(option) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/yarrow/console_runner.rb', line 137

def has_option?(option)
  @options.has_key? option
end

#is_option?(argument) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/yarrow/console_runner.rb', line 133

def is_option?(argument)
  argument[0] == "-"
end

#normalize_theme_pathObject



102
103
104
# File 'lib/yarrow/console_runner.rb', line 102

def normalize_theme_path
  
end


157
158
159
# File 'lib/yarrow/console_runner.rb', line 157

def print_error(e)
  @out.puts "Error! " + e.to_s
end


153
154
155
# File 'lib/yarrow/console_runner.rb', line 153

def print_footer
  @out.puts "Content generated at {path}!"
end


149
150
151
# File 'lib/yarrow/console_runner.rb', line 149

def print_header
  @out.puts Yarrow::APP_NAME + " " + Yarrow::VERSION
end


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
195
196
# File 'lib/yarrow/console_runner.rb', line 161

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_argumentsObject



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_configurationObject

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 = @options.to_hash

  #normalize_theme_path
  
  #theme = @config.options.theme
  #@config.append load_configuration(theme)
end

#register_option(raw_option) ⇒ Object



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
131
# File 'lib/yarrow/console_runner.rb', line 106

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_applicationObject



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
    
    print_footer
    
    SUCCESS
  rescue Exception => e
    print_error e
    FAILURE
  end
end

#run_input_processObject



141
142
143
# File 'lib/yarrow/console_runner.rb', line 141

def run_input_process
  
end

#run_output_processObject



145
146
147
# File 'lib/yarrow/console_runner.rb', line 145

def run_output_process
  
end