2016-09-22 17:16:29 +00:00
|
|
|
# frozen_string_literal: true
|
2017-05-24 22:11:02 +00:00
|
|
|
|
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)
|
|
|
|
|
2016-03-19 14:50:05 +00:00
|
|
|
# ParserFile is a configuration file reader which sets options in the
|
|
|
|
# given Hash.
|
|
|
|
#
|
|
|
|
# In your project's root, you can put a file named
|
2016-03-19 15:42:36 +00:00
|
|
|
# <tt>.github_changelog_generator</tt> to override defaults.
|
2016-03-19 14:50:05 +00:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# header_label=# My Super Changelog
|
2016-03-19 15:42:36 +00:00
|
|
|
# ; Comments are allowed
|
2016-03-19 14:50:05 +00:00
|
|
|
# future-release=5.0.0
|
2016-03-19 15:42:36 +00:00
|
|
|
# # Ruby-style comments, too
|
2016-03-19 14:50:05 +00:00
|
|
|
# since-tag=1.0.0
|
|
|
|
#
|
|
|
|
# The configuration format is <tt>some-key=value</tt> or <tt>some_key=value</tt>.
|
|
|
|
#
|
2015-09-15 18:38:41 +00:00
|
|
|
class ParserFile
|
2016-03-19 14:50:05 +00:00
|
|
|
# @param options [Hash] options to be configured from file contents
|
|
|
|
# @param file [nil,IO] configuration file handle, defaults to opening `.github_changelog_generator`
|
2016-03-19 15:09:34 +00:00
|
|
|
def initialize(options, file = open_settings_file)
|
2015-09-14 04:02:52 +00:00
|
|
|
@options = options
|
2016-03-19 12:59:58 +00:00
|
|
|
@file = file
|
|
|
|
end
|
|
|
|
|
2016-03-19 14:50:05 +00:00
|
|
|
# Sets options using configuration file content
|
2015-09-22 19:45:16 +00:00
|
|
|
def parse!
|
2016-03-19 14:13:12 +00:00
|
|
|
return unless @file
|
2016-03-19 14:18:20 +00:00
|
|
|
@file.each_with_index { |line, i| parse_line!(line, i + 1) }
|
2016-03-19 14:13:12 +00:00
|
|
|
@file.close
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
|
|
|
|
2015-09-22 19:45:16 +00:00
|
|
|
private
|
|
|
|
|
2016-03-19 14:50:05 +00:00
|
|
|
FILENAME = ".github_changelog_generator"
|
|
|
|
|
2016-03-19 15:09:34 +00:00
|
|
|
def open_settings_file
|
2016-03-19 14:50:05 +00:00
|
|
|
path = Pathname(File.expand_path(FILENAME))
|
|
|
|
File.open(path) if path.exist?
|
|
|
|
end
|
|
|
|
|
2016-03-19 14:18:20 +00:00
|
|
|
def parse_line!(line, line_number)
|
2016-03-19 15:42:36 +00:00
|
|
|
return if non_configuration_line?(line)
|
2016-03-19 08:40:42 +00:00
|
|
|
option_name, value = extract_pair(line)
|
|
|
|
@options[option_key_for(option_name)] = convert_value(value, option_name)
|
2017-09-18 19:04:10 +00:00
|
|
|
rescue StandardError
|
2016-03-19 15:17:10 +00:00
|
|
|
raise ParserError, "Failed on line ##{line_number}: \"#{line.gsub(/[\n\r]+/, '')}\""
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
|
|
|
|
2016-03-19 15:42:36 +00:00
|
|
|
# Returns true if the line starts with a pound sign or a semi-colon.
|
|
|
|
def non_configuration_line?(line)
|
2016-03-19 16:12:30 +00:00
|
|
|
line =~ /^[\#;]/ || line =~ /^[\s]+$/
|
2016-03-19 15:42:36 +00:00
|
|
|
end
|
|
|
|
|
2016-03-19 08:40:42 +00:00
|
|
|
# Returns a the option name as a symbol and its string value sans newlines.
|
2015-09-22 19:45:16 +00:00
|
|
|
#
|
|
|
|
# @param line [String] unparsed line from config file
|
|
|
|
# @return [Array<Symbol, String>]
|
|
|
|
def extract_pair(line)
|
|
|
|
key, value = line.split("=", 2)
|
2016-11-04 23:14:34 +00:00
|
|
|
[key.tr("-", "_").to_sym, value.gsub(/[\n\r]+/, "")]
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
2016-03-17 19:39:49 +00:00
|
|
|
|
2017-05-24 22:11:02 +00:00
|
|
|
KNOWN_ARRAY_KEYS = %i[exclude_labels include_labels bug_labels
|
2017-10-10 18:27:23 +00:00
|
|
|
enhancement_labels breaking_labels issue_line_labels between_tags exclude_tags]
|
2016-03-17 19:39:49 +00:00
|
|
|
KNOWN_INTEGER_KEYS = [:max_issues]
|
|
|
|
|
2016-03-19 08:40:42 +00:00
|
|
|
def convert_value(value, option_name)
|
|
|
|
if KNOWN_ARRAY_KEYS.include?(option_name)
|
2016-03-17 19:39:49 +00:00
|
|
|
value.split(",")
|
2016-03-19 08:40:42 +00:00
|
|
|
elsif KNOWN_INTEGER_KEYS.include?(option_name)
|
2016-03-17 19:39:49 +00:00
|
|
|
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
|
|
|
|
2016-03-19 08:40:42 +00:00
|
|
|
IRREGULAR_OPTIONS = {
|
|
|
|
bugs_label: :bug_prefix,
|
|
|
|
enhancement_label: :enhancement_prefix,
|
|
|
|
issues_label: :issue_prefix,
|
|
|
|
header_label: :header,
|
|
|
|
front_matter: :frontmatter,
|
|
|
|
pr_label: :merge_prefix,
|
2017-10-10 18:27:23 +00:00
|
|
|
breaking_label: :breaking_prefix,
|
2016-03-19 08:40:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
def option_key_for(option_name)
|
|
|
|
IRREGULAR_OPTIONS.fetch(option_name) { option_name }
|
2016-03-17 20:08:17 +00:00
|
|
|
end
|
2015-09-14 04:02:52 +00:00
|
|
|
end
|
|
|
|
end
|