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.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 173

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



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

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 102

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



94
95
96
97
98
99
100
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 94

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

.current_version(tag_format:, update:, system:, target:) ⇒ Object



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

def self.current_version(tag_format:, update:, system:, target:)
  if update
    Helper::SemanticVersioningHelper.previous_version(tag_format: tag_format)
  else
    Helper::SemanticVersioningHelper.version_number(system: system, target: target)
  end
end

.ensure_info_plist(path) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 239

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

  File.write(path, "    <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n    <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n    <plist version=\"1.0\">\n    <dict>\n    </dict>\n    </plist>\n  PLIST\n\n  info_plist = main_group.new_file(\"Info.plist\")\n  info_plist.include_in_index = nil\n  info_plist.set_last_known_file_type(\"text.plist\")\n  main_target.build_configurations.each do |config|\n    config.build_settings[\"INFOPLIST_FILE\"] = info_plist.full_path.to_s\n  end\nend\n")

.formatted_tag(tag, format) ⇒ Object



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

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

.gitObject



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

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



64
65
66
67
68
69
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 64

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)


221
222
223
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 221

def self.git_tag_exists?(tag)
  git.tags.map(&:name).include?(tag)
end

.group_commits(commits:, allowed_types:) ⇒ Object



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

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



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

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



235
236
237
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 235

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

.main_targetObject



231
232
233
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 231

def self.main_target
  project.targets.first
end

.manual_bump_type(skip_manual:) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 123

def self.manual_bump_type(skip_manual:)
  return if skip_manual

  UI.message("No new version to bump")
  if !Helper.test? && UI.interactive?
    return UI.select("If you do want to create a new version, select which type:", [:major, :minor, :patch, nil])
  end

  nil
end

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 71

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



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

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



225
226
227
228
229
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 225

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



52
53
54
55
56
57
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 52

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



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 197

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