default_platform :ios

platform :ios do
  desc "Runs all the tests"
  lane :test do
    run_tests()
  end

  desc "Submit a new Beta Build to Apple TestFlight"
  desc "This will also make sure the profile is up to date"
  lane :beta do
    create_keychain(
      name: ENV["MATCH_KEYCHAIN_NAME"],
      password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      default_keychain: true,
      unlock: true,
      timeout: 3600,
      add_to_search_list: true
    )
    match(
      type: "appstore",
      app_identifier: "me.mssun.passforios",
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      readonly: true
    )
    match(
      type: "appstore",
      app_identifier: "me.mssun.passforios.find-login-action-extension",
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      readonly: true
    )
    match(
      type: "appstore",
      app_identifier: "me.mssun.passforios.auto-fill-credential-extension",
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      readonly: true
    )
    increment_build_number(
      build_number: latest_testflight_build_number(version: get_version_number(target: "pass"), initial_build_number: 0) + 1,
      xcodeproj: "pass.xcodeproj"
    )
    gym(
      scheme: "pass",
      verbose: true
    )
    pilot(
      skip_waiting_for_build_processing: true
    )
    mailgun(
      postmaster: ENV["MY_POSTMASTER"],
      apikey: ENV["MY_API_KEY"],
      to: ENV["DESTINATION_EMAIL"],
      from: ENV["EMAIL_FROM_NAME"],
      reply_to: ENV["EMAIL_REPLY_TO"],
      subject: "A new build of Pass for iOS has been uploaded",
      success: true,
      app_link: "https://github.com/mssun/passforios",
      ci_build_link: "https://travis-ci.org/mssun/passforios",
      message: "A new build has been uploaded by Travis CI.",
    )
  end
end
