Class: Fastlane::Helper::GodotVersions

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/godot/helper/godot_versions.rb

Overview

Read/write version numbers in export_presets.cfg. Godot stores them per-preset under platform-specific keys:

iOS:     application/short_version (name), application/version (code)
Android: version/name (name), version/code (code)

Constant Summary collapse

VERSION_NAME_KEYS =
['application/short_version', 'version/name'].freeze
VERSION_CODE_KEYS =
['application/version', 'version/code'].freeze

Class Method Summary collapse

Class Method Details

.bump(current, part) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fastlane/plugin/godot/helper/godot_versions.rb', line 52

def self.bump(current, part)
  UI.user_error!('Cannot bump: no current version name in any preset') if current.nil? || current.empty?
  segments = current.split('.').map(&:to_i)
  segments += [0] * (3 - segments.length) if segments.length < 3
  case part
  when 'major' then [segments[0] + 1, 0, 0]
  when 'minor' then [segments[0], segments[1] + 1, 0]
  when 'patch' then [segments[0], segments[1], segments[2] + 1]
  else UI.user_error!("Unknown bump '#{part}' — use major, minor, or patch")
  end.join('.')
end

.from_git(project_path) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/fastlane/plugin/godot/helper/godot_versions.rb', line 64

def self.from_git(project_path)
  tag = `git -C #{project_path.shellescape} describe --tags --abbrev=0 2>/dev/null`.strip
  UI.user_error!('git-derived versioning needs at least one tag (e.g. v1.0.0)') if tag.empty?
  commit_count = `git -C #{project_path.shellescape} rev-list --count HEAD 2>/dev/null`.strip
  UI.user_error!('Could not count commits — is this a git repository?') if commit_count.empty?
  { version_name: tag.sub(/\Av/, ''), version_code: commit_count.to_i }
end

.read(cfg_path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fastlane/plugin/godot/helper/godot_versions.rb', line 13

def self.read(cfg_path)
  UI.user_error!("No export_presets.cfg at #{cfg_path}") unless File.exist?(cfg_path)
  names = []
  codes = []
  File.foreach(cfg_path) do |line|
    key, value = split_assignment(line)
    next unless key
    names << value if VERSION_NAME_KEYS.include?(key)
    codes << value if VERSION_CODE_KEYS.include?(key)
  end
  UI.user_error!("No version keys found in #{cfg_path} — add application/short_version + application/version (iOS) or version/name + version/code (Android) to a preset") if names.empty? && codes.empty?
  warn_on_mismatch('version name', names)
  warn_on_mismatch('build number', codes)
  { version_name: names.first, version_code: codes.first&.to_i }
end

.write(cfg_path, version_name: nil, version_code: nil) ⇒ Object

Writes the given values into every preset that has the keys, keeping platforms in sync. Returns the number of lines changed.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fastlane/plugin/godot/helper/godot_versions.rb', line 31

def self.write(cfg_path, version_name: nil, version_code: nil)
  UI.user_error!("No export_presets.cfg at #{cfg_path}") unless File.exist?(cfg_path)
  changed = 0
  rewritten = File.foreach(cfg_path).map do |line|
    key, = split_assignment(line)
    if version_name && VERSION_NAME_KEYS.include?(key)
      changed += 1
      "#{key}=\"#{version_name}\"\n"
    elsif version_code && VERSION_CODE_KEYS.include?(key)
      changed += 1
      quoted = key.start_with?('application/') ? "\"#{version_code}\"" : version_code.to_s
      "#{key}=#{quoted}\n"
    else
      line
    end
  end
  UI.user_error!("No version keys found to update in #{cfg_path}") if changed.zero?
  File.write(cfg_path, rewritten.join)
  changed
end