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",
  "yarrow.yml"
]

Instance Method Summary collapse

Constructor Details

#initialize(args, io = STDOUT) ⇒ ConsoleRunner

Returns a new instance of ConsoleRunner.



19
20
21
22
23
24
# File 'lib/yarrow/console_runner.rb', line 19

def initialize(args, io=STDOUT)
  @arguments = args
  @out = io
  @options = {}
  @targets = []
end

Instance Method Details

#configObject



26
27
28
# File 'lib/yarrow/console_runner.rb', line 26

def config
  @config ||= Configuration.load_defaults
end

#has_option?(option) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#is_option?(argument) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#normalize_theme_pathObject



110
111
112
# File 'lib/yarrow/console_runner.rb', line 110

def normalize_theme_path
  # noop
end


162
163
164
# File 'lib/yarrow/console_runner.rb', line 162

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


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

def print_footer
  @out.puts "Content generated at #{@config.output_dir}"
end


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

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


166
167
168
169
# File 'lib/yarrow/console_runner.rb', line 166

def print_help
  help_path = Pathname.new(__dir__) + "help.txt"
  @out.puts(help_path.read)
end

#process_argumentsObject



58
59
60
61
62
63
64
65
66
# File 'lib/yarrow/console_runner.rb', line 58

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/yarrow/console_runner.rb', line 78

def process_configuration
  # load_configuration(Dir.pwd)
  default_config = Yarrow::Configuration.load_defaults

  if @targets.empty?
    @config = default_config
  else
    @config = Yarrow::Config::Instance.new(
      output_dir: default_config.output_dir,
      source_dir: @targets.first,
      meta: default_config.meta,
      server: default_config.meta
    )
  end

  # @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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/yarrow/console_runner.rb', line 114

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



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

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_generation_process

    print_footer

    SUCCESS
  rescue Exception => e
    print_error e
    FAILURE
  end
end

#run_generation_processObject



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

def run_generation_process
  generator = Generator.new(@config)
  generator.generate
end