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.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 142

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



178
179
180
181
182
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 178

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 83

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



75
76
77
78
79
80
81
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 75

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



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 208

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



40
41
42
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 40

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

.gitObject



184
185
186
187
188
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 184

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



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

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)


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

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

.group_commits(commits:, allowed_types:) ⇒ Object



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

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 124

def self.increase_version(current_version:, bump_type:)
  version_array = current_version.split(".").map(&:to_i)

  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



204
205
206
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 204

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

.main_targetObject



200
201
202
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 200

def self.main_target
  project.targets.first
end

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 52

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

.project(path = nil) ⇒ Object



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

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



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

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



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

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:) ⇒ Object

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



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

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

.write_changelog(path:, changelog:) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 166

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