github-changelog-generator/lib/github_changelog_generator/parser_file.rb

107 lines
3.2 KiB
Ruby
Raw Permalink Normal View History

2016-09-22 17:16:29 +00:00
# frozen_string_literal: true
2016-02-25 10:00:45 +00:00
require "pathname"
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
# <tt>.github_changelog_generator</tt> to override defaults.
2016-03-19 14:50:05 +00:00
#
# Example:
# header_label=# My Super Changelog
# ; Comments are allowed
2016-03-19 14:50:05 +00:00
# future-release=5.0.0
# # 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)
@options = options
@file = file
end
2016-03-19 14:50:05 +00:00
# Sets options using configuration file content
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
end
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)
return if non_configuration_line?(line)
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
raise ParserError, "Failed on line ##{line_number}: \"#{line.gsub(/[\n\r]+/, '')}\""
end
# Returns true if the line starts with a pound sign or a semi-colon.
def non_configuration_line?(line)
line =~ /^[\#;]/ || line =~ /^[\s]+$/
end
# Returns a the option name 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.tr("-", "_").to_sym, value.gsub(/[\n\r]+/, "")]
end
KNOWN_ARRAY_KEYS = %i[exclude_labels include_labels bug_labels
enhancement_labels breaking_labels issue_line_labels between_tags exclude_tags]
KNOWN_INTEGER_KEYS = [:max_issues]
def convert_value(value, option_name)
if KNOWN_ARRAY_KEYS.include?(option_name)
value.split(",")
elsif KNOWN_INTEGER_KEYS.include?(option_name)
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
IRREGULAR_OPTIONS = {
bugs_label: :bug_prefix,
enhancement_label: :enhancement_prefix,
issues_label: :issue_prefix,
header_label: :header,
front_matter: :frontmatter,
pr_label: :merge_prefix,
breaking_label: :breaking_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
}
def option_key_for(option_name)
IRREGULAR_OPTIONS.fetch(option_name) { option_name }
2016-03-17 20:08:17 +00:00
end
end
end