Class: Fastlane::Helper::SemanticVersioningHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb

Class Method Summary collapse

Class Method Details

.build_changelog(version:, commits:, type_map:, name: nil) ⇒ Object

Builds and returns th changelog for the upcoming release.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 154

def self.build_changelog(version:, commits:, type_map:, name: nil)
  lines = []

  title = [version, name, Time.now.strftime("(%F)")].compact.join(" ")
  lines << "## #{title}"
  lines << ""

  grouped_commits = group_commits(commits: commits, allowed_types: type_map.keys)
  grouped_commits.each do |key, section_commits|
    next unless section_commits.any?

    lines << "### #{type_map[key]}:" if key != :none
    lines << ""

    section_commits.each do |commit|
      lines << "- #{key == :breaking ? commit[:breaking] : commit[:subject]}"
    end

    lines << ""
  end

  "#{lines.join("\n")}\n"
end

.bump_message(format) ⇒ Object



190
191
192
193
194
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 190

def self.bump_message(format)
  format("bump: %s", format
    .sub("$current_version", Actions.lane_context[Actions::SharedValues::SEMVER_CURRENT_VERSION])
    .sub("$new_version", Actions.lane_context[Actions::SharedValues::SEMVER_NEW_VERSION]))
end

.bump_type(commits:, force_type: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 94

def self.bump_type(commits:, force_type: nil)
  return force_type if force_type == :major # can't go any higher

  result = force_type

  commits.each do |commit|
    return :major if commit[:major]

    bump_type = commit[:bump]
    if bump_type == :major
      return :major
    elsif bump_type == :minor
      result = :minor
    elsif bump_type == :patch && result.nil?
      result = :patch
    end
  end

  result
end

.commit_bump_type(commit:, bump_map:) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 86

def self.commit_bump_type(commit:, bump_map:)
  return :major if commit[:major]

  return bump_map[:breaking] if commit[:breaking]

  bump_map[commit[:type]]
end

.ensure_info_plist(path) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 220

def self.ensure_info_plist(path)
  return if File.exist?(path)

  File.write(path, <<-PLIST)
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    </dict>
    </plist>
  PLIST

  info_plist = main_group.new_file("Info.plist")
  info_plist.include_in_index = nil
  info_plist.set_last_known_file_type("text.plist")
  main_target.build_configurations.each do |config|
    config.build_settings["INFOPLIST_FILE"] = info_plist.full_path.to_s
  end
end

.formatted_tag(tag, format) ⇒ Object



51
52
53
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 51

def self.formatted_tag(tag, format)
  format.sub("$version", tag)
end

.gitObject



196
197
198
199
200
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 196

def self.git
  # rubocop:disable Style/ClassVars
  @@git ||= Git.open(".")
  # rubocop:enable Style/ClassVars
end

.git_commits(from:, allowed_types:, bump_map:) ⇒ Object

Retrieves git commits and returns them grouped by type



56
57
58
59
60
61
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 56

def self.git_commits(from:, allowed_types:, bump_map:)
  logs = from ? git.log(-1).between(from) : git.log(-1)
  logs.reverse_each.filter_map { |commit|
    parse_conventional_commit(commit: commit, allowed_types: allowed_types, bump_map: bump_map)
  }
end

.git_tag_exists?(tag) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 202

def self.git_tag_exists?(tag)
  git.tags.include?(tag)
end

.group_commits(commits:, allowed_types:) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 115

def self.group_commits(commits:, allowed_types:)
  result = allowed_types.to_h { |type| [type, []] }
  result[:none] = []

  commits.each do |commit|
    result[:breaking] << commit if commit[:breaking] && allowed_types.include?(:breaking)

    if commit[:major] && !allowed_types.include?(commit[:type])
      result[:none] << commit
      next
    end

    next unless allowed_types.include?(commit[:type])

    result[commit[:type]] << commit
  end

  result
end

.increase_version(current_version:, bump_type:) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 135

def self.increase_version(current_version:, bump_type:)
  version_array = current_version.split(".").map(&:to_i)
  version_array = version_array.unshift(0, 0)[-3..] # pad from left with zeros when version is not 3-digits.

  case bump_type
  when :major
    version_array[0] += 1
    version_array[1] = version_array[2] = 0
  when :minor
    version_array[1] += 1
    version_array[2] = 0
  when :patch
    version_array[2] += 1
  end

  version_array.join(".")
end

.main_group(name = nil) ⇒ Object



216
217
218
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 216

def self.main_group(name = nil)
  project.main_group[name || main_target.name]
end

.main_targetObject



212
213
214
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 212

def self.main_target
  project.targets.first
end

.parse_conventional_commit(commit:, allowed_types:, bump_map:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 63

def self.parse_conventional_commit(commit:, allowed_types:, bump_map:)
  types = allowed_types.join("|")
  commit.message.match(/^(?<type>#{types})(\((?<scope>\S+)\))?(?<major>!)?:\s+(?<subject>[^\n\r]+)(\z|\n\n(?<body>.*\z))/m) do |match|
    cc = {
      type: match[:type].to_sym,
      major: !match[:major].nil?,
      scope: match[:scope],
      subject: match[:subject],
      body: match[:body],
      breaking: nil,
      original_message: commit.message
    }

    match[:body]&.match(/^BREAKING CHANGE?: (.+)\z/) do |breaking|
      cc[:breaking] = breaking[1]
    end

    cc[:bump] = commit_bump_type(commit: cc, bump_map: bump_map)

    return cc
  end
end

.previous_version(tag_format:) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 34

def self.previous_version(tag_format:)
  tag = Fastlane::Actions::LastGitTagAction.run(pattern: tag_format.sub("$version", "[0-9].[0-9].[0-9]"))
  return "0.0.0" if tag.empty?

  regex = tag_format.sub("$version", "(?<version>\\d+\\.\\d+\\.\\d+)")
  tag.match(regex) do |match|
    return match[:version]
  end
end

.project(path = nil) ⇒ Object



206
207
208
209
210
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 206

def self.project(path = nil)
  # rubocop:disable Style/ClassVars
  @@project ||= Xcodeproj::Project.open(path)
  # rubocop:enable Style/ClassVars
end

.set_version_number(version_number:, system:) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 26

def self.set_version_number(version_number:, system:)
  if system == "apple-generic"
    Fastlane::Actions::IncrementVersionNumberAction.run(version_number: version_number)
  else
    Actions::IncrementVersionNumberInXcodeprojAction.run(version_number: version_number)
  end
end

.verify_versioning_system(value) ⇒ Object



44
45
46
47
48
49
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 44

def self.verify_versioning_system(value)
  allowed = %w[apple-generic manual]
  return if allowed.include?(value)

  UI.user_error!("'versioning_system' must be one of #{allowed}")
end

.version_number(system:, target:) ⇒ Object

class methods that you define here become available in your action as ‘Helper::SemanticVersioningHelper.your_method`



18
19
20
21
22
23
24
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 18

def self.version_number(system:, target:)
  if system == "apple-generic"
    Actions::GetVersionNumberAction.run({})
  else
    Actions::GetVersionNumberFromXcodeprojAction.run(target: target)
  end
end

.write_changelog(path:, changelog:) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 178

def self.write_changelog(path:, changelog:)
  old_changelog = File.new(path).read if File.exist?(path)

  File.open(path, "w") do |file|
    file.write(changelog)
    if old_changelog
      file.write("\n")
      file.write(old_changelog)
    end
  end
end