# 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(:ios)

before_each do |_lane, _options|
  ENV['USER_NAME'] = 'mlch911@icloud.com'
  ENV['BUNDLE_ID'] = 'tech.mluoc.hyzpYbqx'
  ENV['TEAM_ID'] = '124688302'
  ENV['WORKSPACE'] = 'Runner.xcworkspace'
  ENV['SCHEME'] = 'Runner'
  ENV['']
end

platform :ios do
  lane :release do
    ensure_git_status_clean
    begin
      bump_build
      build_app
      app_store_connect_api_key(
        key_id: 'NFBM52M2PY',
        issuer_id: '22e070fb-ba6d-4f93-bf6f-1a6415f583fe',
        key_filepath: '~/AuthKey_NFBM52M2PY.p8',
        duration: 1200,
        in_house: false
      )
      deliver(
        ipa: lane_context[:IPA_OUTPUT_PATH],
        username: ENV['USER_NAME'],
        team_id: ENV['TEAM_ID'],
        app_identifier: ENV['BUNDLE_ID'],
        submit_for_review: true,
        automatic_release: true,
        phased_release: false,
        force: true, # Skip HTMl report verification
        skip_metadata: false,
        skip_screenshots: false,
        overwrite_screenshots: true,
        skip_binary_upload: false,
        precheck_include_in_app_purchases: false,
        submission_information: { add_id_info_uses_idfa: false, export_compliance_uses_encryption: false }
      )
      clean_build_artifacts

      add_git_tag(tag: 'release-' + tag_string)
    rescue => exception
      revert_bump_build
      `git checkout . && git clean -df`
      clean_build_artifacts
      raise exception
    end
  end

  lane :testflight do
    ensure_git_status_clean

    begin
      bump_build
      build_app

      app_store_connect_api_key(
        key_id: 'NFBM52M2PY',
        issuer_id: '22e070fb-ba6d-4f93-bf6f-1a6415f583fe',
        key_filepath: '~/AuthKey_NFBM52M2PY.p8',
        duration: 1200,
        in_house: false
      )
      upload_to_testflight(
        ipa: lane_context[:IPA_OUTPUT_PATH],
        team_id: ENV['TEAM_ID'],
        app_identifier: ENV['BUNDLE_ID'],
        distribute_external: true,
        changelog: '【优化】修复bug，优化体验',
        notify_external_testers: true,
        groups: ['dev', 'test']
      )

      clean_build_artifacts
      add_git_tag(tag: 'testflight-' + tag_string)
    rescue => exception
      revert_bump_build
      clean_build_artifacts
      `git checkout . && git clean -df`
      raise exception
    end
  end

  private_lane :build_app do
    build_ios_app(
      workspace: ENV['WORKSPACE'],
      configuration: 'Release',
      scheme: ENV['SCHEME'],
      clean: true,
      xcargs: '-allowProvisioningUpdates',
      include_symbols: true
    )
  end

  lane :bump_version do
    ensure_git_status_clean

    increment_version_number_in_plist(
      bump_type: 'patch',
      target: lane_context[:TARGET_NAME]
    )
    info_plist_path = get_info_plist_path(scheme: lane_context[:SCHEME])
    git_commit(
      path: [info_plist_path],
      message: 'Bump to next version',
      skip_git_hooks: true
    )
  end

  private_lane :bump_build do
    increment_build_number_in_plist(scheme: lane_context[:SCHEME])
    info_plist_path = get_info_plist_path(scheme: lane_context[:SCHEME])
    git_commit(
      path: [info_plist_path],
      message: 'Increase Build Number',
      skip_git_hooks: true
    )
  end

  private_lane :tag_string do
    unless lane_context[:TAG_STRING]
      tag = "#{version_string}.#{build_string}"
      lane_context[:TAG_STRING] = tag
    end
    lane_context[:TAG_STRING]
  end

  private_lane :version_build_string do
    "#{version_string}.#{build_string}"
  end

  private_lane :version_string do
    unless lane_context[:VERSION_STRING]
      version = get_version_number_from_plist(target: lane_context[:TARGET_NAME])
      lane_context[:VERSION_STRING] = version
    end
    lane_context[:VERSION_STRING]
  end

  private_lane :build_string do
    unless lane_context[:BUILD_STRING]
      build = get_build_number_from_plist(target: lane_context[:TARGET_NAME])
      lane_context[:BUILD_STRING] = build
    end
    lane_context[:BUILD_STRING]
  end

  private_lane :revert_bump_build do
    last_commit = `git --no-pager log --pretty=format:%H -1`
    last_commit_message = `git --no-pager log --pretty=format:%s -1 last_commit`
    if last_commit_message == 'Increase Build Number'
      `git tag -d #{tag_string}`
      `git reset --soft #{last_commit}^`
    end
  end
end
