Class: Fastlane::Helper::FlutterVersioncodeBumpHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/flutter_versioncode_bump/helper/flutter_versioncode_bump_helper.rb

Class Method Summary collapse

Class Method Details

.bump_version_code(full_version, increment) ⇒ Object



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

def self.bump_version_code(full_version, increment)
  unless full_version.include?('+')
    raise 'No version code to bump :( ! Add a version code (example: 1.0.0+0001)'
  end

  splitted_version = full_version.split('+')

  version_name = splitted_version[0]
  version_code = splitted_version[1]

  # TODO: Add check for negative value!

  if version_code.to_i < 0 || version_code.to_s.length == 0
    raise 'Version code is invalid! Should be 0 or greater.'
  end

  incremented_version_code = version_code.to_i + increment
  new_version_code = format("%0#{version_code.to_s.length}d", incremented_version_code.to_s)

  "#{version_name}+#{new_version_code}"
end

.retrieve_full_version(pubspec_location) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/fastlane/plugin/flutter_versioncode_bump/helper/flutter_versioncode_bump_helper.rb', line 9

def self.retrieve_full_version(pubspec_location)
  begin
    pubspec = YAML.load_file(pubspec_location)
  rescue StandardError
    raise 'Reading the pubspec failed!'
  end

  pubspec['version']
end

.write_full_version(pubspec_location, version) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fastlane/plugin/flutter_versioncode_bump/helper/flutter_versioncode_bump_helper.rb', line 19

def self.write_full_version(pubspec_location, version)
  # To retain the comments and whitespace(s) in the file replace the version in the text.
  pubspec_as_text = File.read(pubspec_location)

  # Apply regex to edit the version line
  pubspec_new_content = pubspec_as_text.sub(/version:.*/, "version: #{version}")

  # Write updated content to pubspec
  File.open(pubspec_location, 'w') { |file| file.puts(pubspec_new_content) }
rescue StandardError
  raise "Could not write the updated version to the pubspec"
end