# This file contains the fastlane.tools configuration for the PediFoods iOS app.
# You can find the documentation at https://docs.fastlane.tools

default_platform(:ios)

lane :tests do
  run_tests(
    scheme: "PediFoods",
    project: "PediFoods.xcodeproj",
    clean: true,
    code_coverage: true
  )
end

lane :assemble do |options|
  # Manual signing (team/identity/profile) for the "PediFoods" target is
  # baked directly into PediFoods.xcodeproj's own project settings - not
  # overridden here.
  # agvtool needs VERSIONING_SYSTEM = apple-generic, which this project doesn't
  # set, so it silently no-ops - pass CURRENT_PROJECT_VERSION directly instead.
  xcargs = "-skipPackagePluginValidation -skipMacroValidation"
  xcargs += " CURRENT_PROJECT_VERSION=#{ENV['BUILD_NUMBER']}" if ENV["BUILD_NUMBER"]

  build_app(
    scheme: "PediFoods",
    sdk: "iphoneos",
    export_method: "app-store",
    xcconfig: "fastlane/AppStore.xcconfig",
    xcargs: xcargs,
    derived_data_path: ".build/DerivedData",
    output_directory: ".build/fastlane",
    skip_archive: ENV["FASTLANE_SKIP_ARCHIVE"] == "YES",
    skip_codesigning: ENV["FASTLANE_SKIP_CODESIGNING"] == "YES"
  )
end

lane :beta do |options|
  desc "Build and upload to TestFlight"

  # force: true - without it, sigh just downloads whatever profile
  # already exists on Apple's servers as-is. This CI keychain's
  # distribution certificate is a different one than whatever the
  # existing profile was last generated against, so a plain (non-forced)
  # fetch produced a profile that doesn't include this machine's cert -
  # "Provisioning profile ... doesn't include signing certificate
  # 'iPhone Distribution: Loverde Company LTDA (K4E5BZMM4V)'" at archive
  # time. Forcing a renewal makes sigh regenerate the profile against
  # the team's currently valid certificates instead of reusing a stale
  # one.
  get_provisioning_profile(api_key_path: "fastlane/apikey.json", force: true)

  assemble

  upload_to_testflight(
    api_key_path: "fastlane/apikey.json",
    skip_waiting_for_build_processing: true
  )
end

lane :release do |options|
  desc "Build and release app"

  # see https://docs.fastlane.tools/uploading-app-privacy-details/
  #upload_app_privacy_details_to_app_store(json_path: "fastlane/app_privacy_details.json")

  # if you have an apikey.json file (https://developer.apple.com/documentation/appstoreconnectapi/creating-api-keys-for-app-store-connect-api), fastlane can automatically fetch certificates and the ASC authentication information
  #get_certificates(api_key_path: "fastlane/apikey.json")
  # force: true - without it, sigh just downloads whatever profile
  # already exists on Apple's servers as-is. This CI keychain's
  # distribution certificate is a different one than whatever the
  # existing profile was last generated against, so a plain (non-forced)
  # fetch produced a profile that doesn't include this machine's cert -
  # "Provisioning profile ... doesn't include signing certificate
  # 'iPhone Distribution: Loverde Company LTDA (K4E5BZMM4V)'" at archive
  # time. Forcing a renewal makes sigh regenerate the profile against
  # the team's currently valid certificates instead of reusing a stale
  # one.
  get_provisioning_profile(api_key_path: "fastlane/apikey.json", force: true)

  assemble

  upload_to_app_store(
    api_key_path: "fastlane/apikey.json",
    app_rating_config_path: "fastlane/metadata/rating.json",
    release_notes: { default: "Fixes and improvements." },
    submit_for_review: false
  )
end

