Class: EasyChangelog::Utility

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

Class Method Summary collapse

Class Method Details

.attr_from_entry(var_name, entry) ⇒ Object



38
39
40
# File 'lib/easy_changelog/utility.rb', line 38

def attr_from_entry(var_name, entry)
  EasyChangelog.configuration.entry_row_match_regexp.match(entry)[var_name.to_sym]
end

.attr_from_path(var_name, path) ⇒ Object



34
35
36
# File 'lib/easy_changelog/utility.rb', line 34

def attr_from_path(var_name, path)
  EasyChangelog.configuration.entry_path_match_regexp.match(path)[var_name.to_sym]
end

.discover_card_id(branch_name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/easy_changelog/utility.rb', line 50

def discover_card_id(branch_name)
  return if EasyChangelog.configuration.card_id_regex.nil?

  branch_name ||= `git rev-parse --abbrev-ref HEAD`
  return if branch_name == EasyChangelog.configuration.main_branch

  id = EasyChangelog.configuration.card_id_regex.match(branch_name)&.named_captures&.fetch('card_id', nil)

  normalize_card_id(id)
end

.ensure_entries_dir_existsObject



6
7
8
9
# File 'lib/easy_changelog/utility.rb', line 6

def ensure_entries_dir_exists
  dir_name = EasyChangelog.configuration.entries_path
  FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name)
end

.extract_id(body) ⇒ Object



61
62
63
# File 'lib/easy_changelog/utility.rb', line 61

def extract_id(body)
  /^\[Fix(?:es)? #(\d+)\] (.*)/.match(body)&.captures || [nil, body]
end

.parse_changelog(content) ⇒ Object

rubocop:disable Metrics/MethodLength



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/easy_changelog/utility.rb', line 12

def parse_changelog(content)
  ss = StringScanner.new(content)

  header = ss.scan_until(EasyChangelog.configuration.unreleased_header)
  unreleased = ss.scan_until(/\n(?=## )/m)

  if unreleased.nil?
    unreleased = parse_unreleased_entries(ss.rest)
    rest = ''
  else
    unreleased = parse_unreleased_entries(unreleased)
    rest = ss.rest
  end

  [header, unreleased, rest]
end

.section_for(path) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/easy_changelog/utility.rb', line 42

def section_for(path)
  entry_type = attr_from_path(:type, path).to_sym

  return '' if EasyChangelog.configuration.loose?

  EasyChangelog.configuration.type_mapping[entry_type][:title]
end

.str_to_filename(str) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/easy_changelog/utility.rb', line 65

def str_to_filename(str)
  str
    .split
    .reject(&:empty?)
    .map { |s| prettify(s) }
    .inject do |result, word|
      s = "#{result}_#{word}"
      return result if s.length > EasyChangelog.configuration.filename_max_length

      s
    end
end

.update_changelog(content) ⇒ Object

rubocop:enable Metrics/MethodLength



30
31
32
# File 'lib/easy_changelog/utility.rb', line 30

def update_changelog(content)
  File.write(EasyChangelog.configuration.changelog_filename, content)
end