# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
update_fastlane

default_platform(:android)

platform :android do
  lane :set_build_code_internal do
    versions = google_play_track_version_codes(
        track: "internal",
        json_key: "./keys.json"
      )
    last_version = versions[0].to_i
    Dir.chdir("../..") do
      re = /version:\s([0-9]*\.[0-9]*\.[0-9]*)\+[0-9]*/i
      config = File.read("./pubspec.yaml")
      version_name = config.match(re).captures

      subst = "version: #{version_name[0]}+#{last_version+2}"

      result = config.gsub(re, subst)

      File.open("./pubspec.yaml", 'w') { |file| file.write(result) }
    end
  end

  lane :deploy_internal_test do
    versions = google_play_track_version_codes(
        track: "internal",
        json_key: "./keys.json"
      )
    last_version = versions[0].to_i
    upload_to_play_store(track: 'internal', aab: '../build/app/outputs/bundle/release/app-release.aab', version_code: "#{last_version+1}")
  end

  lane :deploy_release do
    upload_to_play_store(track: 'internal', track_promote_to: "production", deactivate_on_promote: false, skip_upload_changelogs: true)
  end
end