2015-09-14 04:02:52 +00:00
|
|
|
module GitHubChangelogGenerator
|
2015-10-21 21:13:42 +00:00
|
|
|
ParserError = Class.new(StandardError)
|
|
|
|
|
2015-09-15 18:38:41 +00:00
|
|
|
class ParserFile
|
2015-09-14 04:02:52 +00:00
|
|
|
def initialize(options)
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2015-09-22 19:45:16 +00:00
|
|
|
def parse!
|
|
|
|
return unless File.exist?(file)
|
|
|
|
|
|
|
|
File.readlines(file).each { |line| parse_line!(line) }
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
|
|
|
|
2015-09-22 19:45:16 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def file
|
|
|
|
@file ||= File.expand_path(@options[:params_file] || ".github_changelog_generator")
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
|
|
|
|
2015-09-22 19:45:16 +00:00
|
|
|
def parse_line!(line)
|
|
|
|
key_sym, value = extract_pair(line)
|
2016-02-23 15:18:36 +00:00
|
|
|
value = true if value =~ /^(true|t|yes|y|1)$/i
|
|
|
|
value = false if value =~ /^(false|f|no|n|0)$/i
|
2015-09-22 19:45:16 +00:00
|
|
|
@options[key_sym] = value
|
|
|
|
rescue
|
2015-10-21 21:13:42 +00:00
|
|
|
raise ParserError, "Config file #{file} is incorrect in line \"#{line.gsub(/[\n\r]+/, '')}\""
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
|
|
|
|
2015-09-22 19:45:16 +00:00
|
|
|
# Returns a the setting as a symbol and its string value sans newlines.
|
|
|
|
#
|
|
|
|
# @param line [String] unparsed line from config file
|
|
|
|
# @return [Array<Symbol, String>]
|
|
|
|
def extract_pair(line)
|
|
|
|
key, value = line.split("=", 2)
|
|
|
|
[key.sub("-", "_").to_sym, value.gsub(/[\n\r]+/, "")]
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|