Class: EasyChangelog::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_changelog/configuration.rb

Constant Summary collapse

CONFIG_PATHS =
%w[
  ./.easy_changelog.rb
  ./config/initializers/easy_changelog.rb
  ./config/easy_changelog.rb
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

rubocop:disable Metrics/AbcSize



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/easy_changelog/configuration.rb', line 22

def initialize
  @entries_path = 'changelog/'
  @entries_order = :asc
  @changelog_filename = 'CHANGELOG.md'

  @main_branch = 'master'
  @entry_path_format = '<type>_<name>_<timestamp>.md'
  @entry_row_format = '* <ref>: <card_ref> <title> (<username>)'
  @unreleased_header = /## #{Regexp.escape("#{@main_branch} (unreleased)")}/m
  @user_signature = Regexp.new(format(Regexp.escape('[@%<user>s][]'), user: '([\w-]+)'))

  @filename_max_length = 50
  @type_mapping = {
    breaking: { title: 'Breaking Changes', level: :major },
    feature: { title: 'New features', level: :minor },
    fix: { title: 'Bug fixes', level: :patch }
  }
  @include_empty_card_id = false

  @repo_url = ENV.fetch('REPOSITORY_URL', nil)
  @cards_url = ENV.fetch('CARDS_URL', nil)
  @card_id_regex = %r{(?<card_id>[^/]+)/(?:.+)}
  @release_message_template = -> { "## #{EasyChangelog::VERSION} (#{Date.today.iso8601})" }
end

Instance Attribute Details

#card_id_normalizerObject

Returns the value of attribute card_id_normalizer.



10
11
12
# File 'lib/easy_changelog/configuration.rb', line 10

def card_id_normalizer
  @card_id_normalizer
end

#card_id_regexObject (readonly)

Returns the value of attribute card_id_regex.



10
11
12
# File 'lib/easy_changelog/configuration.rb', line 10

def card_id_regex
  @card_id_regex
end

#cards_urlObject

Returns the value of attribute cards_url.



8
9
10
# File 'lib/easy_changelog/configuration.rb', line 8

def cards_url
  @cards_url
end

#changelog_filenameObject

Returns the value of attribute changelog_filename.



8
9
10
# File 'lib/easy_changelog/configuration.rb', line 8

def changelog_filename
  @changelog_filename
end

#entries_orderObject

Returns the value of attribute entries_order.



10
11
12
# File 'lib/easy_changelog/configuration.rb', line 10

def entries_order
  @entries_order
end

#entries_pathObject

Returns the value of attribute entries_path.



10
11
12
# File 'lib/easy_changelog/configuration.rb', line 10

def entries_path
  @entries_path
end

#entry_path_formatObject (readonly)

Returns the value of attribute entry_path_format.



10
11
12
# File 'lib/easy_changelog/configuration.rb', line 10

def entry_path_format
  @entry_path_format
end

#entry_row_formatObject

Returns the value of attribute entry_row_format.



8
9
10
# File 'lib/easy_changelog/configuration.rb', line 8

def entry_row_format
  @entry_row_format
end

#filename_max_lengthObject

Returns the value of attribute filename_max_length.



8
9
10
# File 'lib/easy_changelog/configuration.rb', line 8

def filename_max_length
  @filename_max_length
end

#include_empty_card_idObject

Returns the value of attribute include_empty_card_id.



8
9
10
# File 'lib/easy_changelog/configuration.rb', line 8

def include_empty_card_id
  @include_empty_card_id
end

#main_branchObject

Returns the value of attribute main_branch.



8
9
10
# File 'lib/easy_changelog/configuration.rb', line 8

def main_branch
  @main_branch
end

#release_message_templateObject

Raises:



54
55
56
57
58
59
60
61
62
# File 'lib/easy_changelog/configuration.rb', line 54

def release_message_template
  raise ConfigurationError, 'release_message_template must be set' unless @release_message_template

  return @release_message_template unless @release_message_template.respond_to?(:call)

  message = @release_message_template.call
  message = "## #{message}" unless message.start_with?('## ')
  message
end

#repo_urlObject

rubocop:enable Metrics/AbcSize

Raises:



48
49
50
51
52
# File 'lib/easy_changelog/configuration.rb', line 48

def repo_url
  raise ConfigurationError, 'repo_url must be set' unless @repo_url

  @repo_url
end

#type_mappingObject

Returns the value of attribute type_mapping.



10
11
12
# File 'lib/easy_changelog/configuration.rb', line 10

def type_mapping
  @type_mapping
end

#unreleased_headerObject

Returns the value of attribute unreleased_header.



10
11
12
# File 'lib/easy_changelog/configuration.rb', line 10

def unreleased_header
  @unreleased_header
end

#user_signatureObject

Returns the value of attribute user_signature.



10
11
12
# File 'lib/easy_changelog/configuration.rb', line 10

def user_signature
  @user_signature
end

Instance Method Details

#card_regex=(value) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
# File 'lib/easy_changelog/configuration.rb', line 64

def card_regex=(value)
  raise ArgumentError, 'card_regex must be a Regexp' unless value.is_a?(Regexp)

  @card_regex = value
end

#changelog_typesObject



109
110
111
112
113
# File 'lib/easy_changelog/configuration.rb', line 109

def changelog_types
  return [] if @type_mapping == :loose

  @type_mapping.keys
end

#entry_path_match_regexpObject



121
122
123
124
125
126
127
128
# File 'lib/easy_changelog/configuration.rb', line 121

def entry_path_match_regexp
  formula = @entry_path_format.gsub(/<(\w+)>/) do |match|
    matcher = match == '<type>' ? '[^_]' : '.'
    "(?#{match}#{matcher}+)"
  end

  Regexp.new("(?:#{entries_path})?#{formula}")
end

#entry_path_templateObject



130
131
132
# File 'lib/easy_changelog/configuration.rb', line 130

def entry_path_template
  File.join(entries_path, @entry_path_format.gsub(/<(\w+)>/) { |_match| "%<#{Regexp.last_match(1)}>s" })
end

#entry_row_match_regexpObject



134
135
136
137
138
139
# File 'lib/easy_changelog/configuration.rb', line 134

def entry_row_match_regexp
  with_optional_card_ref = Regexp.escape(@entry_row_format).gsub('<card_ref>\\ ') { |match| "(?:#{match})?" }
  formula = with_optional_card_ref.gsub(/<(\w+)>/) { |match| "(?#{match}.+)" }

  Regexp.new(formula)
end

#entry_row_templateObject



141
142
143
# File 'lib/easy_changelog/configuration.rb', line 141

def entry_row_template
  @entry_row_format.gsub(/<(\w+)>/) { |_match| "%<#{Regexp.last_match(1)}>s" }
end

#load_configObject



145
146
147
148
149
150
151
152
# File 'lib/easy_changelog/configuration.rb', line 145

def load_config
  paths = CONFIG_PATHS.map { |file_path| File.expand_path(file_path) }
  path = paths.select { |file_path| File.exist?(file_path) }.first

  return unless path

  load(path)
end

#loose?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/easy_changelog/configuration.rb', line 99

def loose?
  @type_mapping == :loose
end

#sectionsObject



115
116
117
118
119
# File 'lib/easy_changelog/configuration.rb', line 115

def sections
  return [''] if @type_mapping == :loose

  @type_mapping.values.map { |v| v[:title] }
end