default_platform(:ios)

platform :ios do
  ios_swift_infoplist_path = "./Samples/iOS-Swift/iOS-Swift/Info.plist"
  ios_swift_clip_infoplist_path = "./Samples/iOS-Swift/iOS-SwiftClip/Info.plist"
  configuration = if is_ci then 'TestCI' else 'Test' end

  lane :bump_build_number do
    increment_build_number(
      build_number: ENV["FASTLANE_BUILD_NUMBER"],
      xcodeproj: "./Samples/iOS-Swift/iOS-Swift.xcodeproj"
    )
  end

  # The version for all Info.plist must be a period-separated list of at most three non-negative integers
  # for App Store Connect. If we are on a preview we to remove these suffixes.
  lane :remove_preview_version_suffixes do
    version = get_version_number(
      xcodeproj: "./Samples/iOS-Swift/iOS-Swift.xcodeproj",
      target: "iOS-Swift"
      )
    new_version = version.split("-", -1)[0]

    # We also need to replace the MARKETING_VERSION otherwise the build will fail with
    # error: The CFBundleShortVersionString of an App Clip ('8.9.0-beta.1') must match that of its containing parent app ('8.9.0').
    sh "sed -i '' 's/MARKETING_VERSION = #{version}/MARKETING_VERSION = #{new_version}/g' ../Samples/iOS-Swift/iOS-Swift.xcodeproj/project.pbxproj"

    set_info_plist_value(
      path: ios_swift_infoplist_path,
      key: "CFBundleShortVersionString",
      value: new_version
    )
    set_info_plist_value(
      path: ios_swift_clip_infoplist_path,
      key: "CFBundleShortVersionString",
      value: new_version
    )

    sentryInfoPlistPath = "./Sources/Resources/Info.plist"
    set_info_plist_value(
      path: sentryInfoPlistPath,
      key: "CFBundleShortVersionString",
      value: new_version
    )
    set_info_plist_value(
      path: sentryInfoPlistPath,
      key: "CFBundleVersion",
      value: new_version
    )
  end

  desc "Run match for local development"
    lane :match_local do
     match(
       type: "development",
       app_identifier: ["io.sentry.sample.iOS-Swift",
         "io.sentry.sample.iOS-Swift.Clip",
         "io.sentry.iOS-Swift-UITests.xctrunner",
         "io.sentry.cocoa.perf-test-app-plain",
         "io.sentry.*",
         "io.sentry.iOS-Benchmarking.xctrunner",
         "io.sentry.cocoa.perf-test-app-sentry"],
        readonly: true
     )
     match(
       type: "appstore",
       app_identifier: ["io.sentry.sample.iOS-Swift",
         "io.sentry.sample.iOS-Swift.Clip",
         "io.sentry.iOS-Swift-UITests.xctrunner",
         "io.sentry.cocoa.perf-test-app-plain",
         "io.sentry.*",
         "io.sentry.iOS-Benchmarking.xctrunner",
         "io.sentry.cocoa.perf-test-app-sentry"],
        readonly: true
     )
  end

  desc "Build iOS-Swift with Release"
  lane :build_ios_swift do

    setup_ci

    sync_code_signing(
      type: "appstore",
      readonly: true,
      app_identifier: ["io.sentry.sample.iOS-Swift",  "io.sentry.sample.iOS-Swift.Clip"],
      
      # Directly cloning the branch instead of using a shallow clone fixes the rare error:
      #   fatal: a branch named 'master' already exists in GitHub Action workflows.
      git_branch: "master",
      clone_branch_directly: true
    )

    build_app(
      workspace: "Sentry.xcworkspace",
      scheme: "iOS-Swift",
      include_bitcode: false,
      include_symbols: true,
      export_method: "app-store",
      archive_path: "iOS-Swift"
    )

    delete_keychain(name: "fastlane_tmp_keychain") unless is_ci
  end

  lane :build_ios_swift_for_tests do

    setup_ci(
      force: true
    )

    sync_code_signing(
      type: "development",
      readonly: true,
      app_identifier: ["io.sentry.sample.iOS-Swift", "io.sentry.sample.iOS-Swift.Clip"],
      
      # Directly cloning the branch instead of using a shallow clone fixes the rare error:
      #   fatal: a branch named 'master' already exists in GitHub Action workflows.
      git_branch: "master",
      clone_branch_directly: true
    )

    build_app(
      workspace: "Sentry.xcworkspace",
      scheme: "iOS-Swift",
      derived_data_path: "DerivedData",
      skip_archive: true
    )

    delete_keychain(name: "fastlane_tmp_keychain") unless is_ci
  end

  lane :build_ios_swift_ui_test do

    setup_ci(
      force: true
    )

    sync_code_signing(
      type: "development",
      readonly: true,
      app_identifier: ["io.sentry.sample.iOS-Swift", "io.sentry.sample.iOS-Swift.Clip", "io.sentry.iOS-Swift-UITests.xctrunner"],
      
      # Directly cloning the branch instead of using a shallow clone fixes the rare error:
      #   fatal: a branch named 'master' already exists in GitHub Action workflows.
      git_branch: "master",
      clone_branch_directly: true
    )

    # don't use gym here because it always appends a "build" command which fails, since this is a test target not configured for running
    sh "set -o pipefail && NSUnbufferedIO=YES xcodebuild -workspace ../Sentry.xcworkspace -scheme iOS-Swift-UITests -derivedDataPath ../DerivedData -destination 'generic/platform=iOS' -configuration Test build-for-testing 2>&1 | xcbeautify"

    delete_keychain(name: "fastlane_tmp_keychain") unless is_ci
  end

  lane :build_ios_benchmark_test do

    setup_ci(
      force: true
    )

    sync_code_signing(
      type: "development",
      readonly: true,
      app_identifier: ["io.sentry.sample.iOS-Swift", "io.sentry.sample.iOS-Swift.Clip", "io.sentry.iOS-Benchmarking.xctrunner"],
      
      
      # Directly cloning the branch instead of using a shallow clone fixes the rare error:
      #   fatal: a branch named 'master' already exists in GitHub Action workflows.
      git_branch: "master",
      clone_branch_directly: true
    )

    build_app(
      workspace: "Sentry.xcworkspace",
      scheme: "iOS-Swift-Benchmarking",
      xcargs: "build-for-testing",
      derived_data_path: "DerivedData",
      skip_archive: true
    )

    delete_keychain(name: "fastlane_tmp_keychain") unless is_ci
  end

  lane :build_ios_swift_ui_test_sample do

    setup_ci(
      force: true
    )

    build_app(
      workspace: "Sentry.xcworkspace",
      scheme: "SwiftUITestSample",
      configuration: "Debug",
      derived_data_path: "TestSamples/SwiftUITestSample/DerivedData",
      destination: "generic/platform=iOS Simulator",
      xcargs: "-sdk 'iphonesimulator'",
      skip_archive: true
    )
  end

  desc "Upload iOS-Swift to TestFlight and symbols to Sentry"
  lane :ios_swift_to_testflight do

    app_store_connect_api_key(
      key_id: ENV["APP_STORE_CONNECT_KEY_ID"],
      issuer_id: ENV["APP_STORE_CONNECT_ISSUER_ID"],
      key_content: ENV["APP_STORE_CONNECT_KEY"]
    )

    testflight(
      skip_waiting_for_build_processing: true,
    )

    sentry_upload_dif(
      auth_token: ENV["SENTRY_AUTH_TOKEN"],
      org_slug: 'sentry-sdks',
      project_slug: 'sentry-cocoa',
      include_sources: true
    )
  end

  lane :ui_tests_ios_swiftui do
    run_tests(
      workspace: "Sentry.xcworkspace",
      scheme: "iOS-SwiftUI",
      configuration: configuration
    )
  end
  
    lane :ui_tests_ios_swift6 do |options|
    run_tests(
      workspace: "Sentry.xcworkspace",
      scheme: "iOS-Swift6",
      configuration: configuration
    )
  end

  lane :ui_tests_ios_objc do
    run_tests(
      workspace: "Sentry.xcworkspace",
      scheme: "iOS-ObjectiveC",
      configuration: configuration
    )
  end

  lane :ui_tests_ios_swift do |options|
    run_tests(
      workspace: "Sentry.xcworkspace",
      scheme: "iOS-Swift",
      device: options[:device],
      address_sanitizer: options[:address_sanitizer],
      configuration: configuration
    )
  end

  lane :ui_tests_tvos_swift do
    run_tests(
      workspace: "Sentry.xcworkspace",
      scheme: "tvOS-Swift",
      configuration: configuration
    )
  end
  
  desc "Run Duplication test"
  lane :duplication_test do
    run_tests(
      project: "./Tests/DuplicatedSDKTest/DuplicatedSDKTest.xcodeproj",
      scheme: "DuplicatedSDKTest"
    )
  end

  desc "Build Perf-test app without Sentry"
  lane :build_perf_test_app_plain do
    setup_ci(
      force: true
    )

    sync_code_signing(
      type: "development",
      readonly: true,
      app_identifier: ["io.sentry.cocoa.perf-test-app-plain"],
      
      # Directly cloning the branch instead of using a shallow clone fixes the rare error:
      #   fatal: a branch named 'master' already exists in GitHub Action workflows.
      git_branch: "master",
      clone_branch_directly: true
    )

    build_app(
      project: "Tests/Perf/test-app-plain/test-app-plain.xcodeproj",
      scheme: "test-app-plain",
      include_bitcode: false,
      include_symbols: false,
      export_method: "development",
      output_directory: "Tests/Perf/",
      output_name: "test-app-plain.ipa"
    )

    delete_keychain(name: "fastlane_tmp_keychain") unless is_ci
  end

  desc "Build Perf-test app with Sentry"
  lane :build_perf_test_app_sentry do
    setup_ci(
      force: true
    )

    sync_code_signing(
      type: "development",
      readonly: true,
      app_identifier: ["io.sentry.cocoa.perf-test-app-sentry"],
      
      # Directly cloning the branch instead of using a shallow clone fixes the rare error:
      #   fatal: a branch named 'master' already exists in GitHub Action workflows.
      git_branch: "master",
      clone_branch_directly: true
    )

    build_app(
      project: "Tests/Perf/test-app-sentry/test-app-sentry.xcodeproj",
      scheme: "test-app-sentry",
      include_bitcode: false,
      include_symbols: false,
      export_method: "development",
      output_directory: "Tests/Perf/",
      output_name: "test-app-sentry.ipa"
    )

    delete_keychain(name: "fastlane_tmp_keychain") unless is_ci
  end

end
