2016-02-25 10:00:45 +00:00
|
|
|
require "pathname"
|
2016-02-23 22:46:23 +00:00
|
|
|
|
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
|
2016-02-23 22:54:20 +00:00
|
|
|
FILENAME = ".github_changelog_generator"
|
|
|
|
|
2015-09-14 04:02:52 +00:00
|
|
|
def initialize(options)
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2016-02-23 22:54:20 +00:00
|
|
|
# Destructively change @options using data in configured options file.
|
2015-09-22 19:45:16 +00:00
|
|
|
def parse!
|
2016-02-23 22:54:20 +00:00
|
|
|
file.each_line { |line| parse_line!(line) } if file.exist?
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
|
|
|
|
2015-09-22 19:45:16 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def file
|
2016-02-23 22:54:20 +00:00
|
|
|
@file ||= Pathname(File.expand_path(@options[:params_file] || FILENAME))
|
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-03-17 20:08:17 +00:00
|
|
|
@options[translate_option_name(key_sym)] = convert_value(value, key_sym)
|
2015-09-22 19:45:16 +00:00
|
|
|
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
|
2016-03-17 19:39:49 +00:00
|
|
|
|
|
|
|
KNOWN_ARRAY_KEYS = [:exclude_labels, :include_labels, :bug_labels,
|
|
|
|
:enhancement_labels, :between_tags, :exclude_tags]
|
|
|
|
KNOWN_INTEGER_KEYS = [:max_issues]
|
|
|
|
|
|
|
|
def convert_value(value, key_sym)
|
|
|
|
if KNOWN_ARRAY_KEYS.include?(key_sym)
|
|
|
|
value.split(",")
|
|
|
|
elsif KNOWN_INTEGER_KEYS.include?(key_sym)
|
|
|
|
value.to_i
|
|
|
|
elsif value =~ /^(true|t|yes|y|1)$/i
|
|
|
|
true
|
|
|
|
elsif value =~ /^(false|f|no|n|0)$/i
|
|
|
|
false
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2016-03-17 20:08:17 +00:00
|
|
|
|
|
|
|
def translate_option_name(key_sym)
|
|
|
|
{
|
|
|
|
bugs_label: :bug_prefix,
|
|
|
|
enhancement_label: :enhancement_prefix,
|
|
|
|
issues_label: :issue_prefix,
|
|
|
|
header_label: :header,
|
|
|
|
front_matter: :frontmatter,
|
|
|
|
pr_label: :merge_prefix,
|
|
|
|
issues_wo_labels: :add_issues_wo_labels,
|
|
|
|
pr_wo_labels: :add_pr_wo_labels,
|
|
|
|
pull_requests: :pulls,
|
|
|
|
filter_by_milestone: :filter_issues_by_milestone,
|
|
|
|
github_api: :github_endpoint
|
|
|
|
}.fetch(key_sym) { key_sym }
|
|
|
|
end
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
|
|
|
end
|