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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 126

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



162
163
164
165
166
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 162

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



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

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



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

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_versionObject



188
189
190
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 188

def self.current_version
  @current_version ||= main_target.build_configurations.first.build_settings["MARKETING_VERSION"] || "1.0"
end

.ensure_info_plist(path) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 196

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>
          <key>CFBundleVersion</key>
          <string>#{current_version}</string>
          <key>CFBundleShortVersionString</key>
          <string>#{current_version}</string>
        </dict>
        </plist>
  PLIST

  info_plist = main_group.new_file("Info.plist")
  main_target.add_file_references([info_plist])
  Fastlane::UI.success("Successfully created the file '#{path}' for agvtool")
end

.formatted_tag(tag, format) ⇒ Object



24
25
26
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 24

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

.gitObject



168
169
170
171
172
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 168

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



29
30
31
32
33
34
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 29

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)


174
175
176
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 174

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

.group_commits(commits:, allowed_types:) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 88

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



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

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



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

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

.main_targetObject



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

def self.main_target
  project.targets.first
end

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 36

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



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

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

.show_messageObject

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



16
17
18
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 16

def self.show_message
  UI.message("Hello from the semantic_versioning plugin helper!")
end

.version_numberObject



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

def self.version_number
  Actions::GetVersionNumberAction.run({})
end

.write_changelog(path:, changelog:) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/fastlane/plugin/semantic_versioning/helper/semantic_versioning_helper.rb', line 150

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