default_platform :ios

lane :prepare do
  carthage(cache_builds: true, platform: "iOS")
  sh "./scripts/gopenpgp_build.sh"
end

lane :reset_build_number do
    increment_build_number(
      build_number: 0,
      xcodeproj: "pass.xcodeproj"
    )
end

lane :match_development do
    app_identifier = "me.mssun.passforios"
    match(
      type: "development",
      git_url: "https://github.com/mssun/certificates-passforios",
      git_basic_authorization: ENV['MATCH_GIT_BASIC_AUTHORIZATION'],
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      app_identifier: [app_identifier,
                       "#{app_identifier}.find-login-action-extension",
		       "#{app_identifier}.auto-fill-credential-extension",
		       "#{app_identifier}.shortcuts"],
      readonly: true
    )
end

lane :match_appstore do
    app_identifier = "me.mssun.passforios"
    match(
      type: "appstore",
      git_url: "https://github.com/mssun/certificates-passforios",
      git_basic_authorization: ENV['MATCH_GIT_BASIC_AUTHORIZATION'],
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      app_identifier: [app_identifier,
                       "#{app_identifier}.find-login-action-extension",
		       "#{app_identifier}.auto-fill-credential-extension",
		       "#{app_identifier}.shortcuts"],
      readonly: true
    )
end

lane :match_appstore_beta do
    app_identifier = "me.mssun.passforiosbeta"
    match(
      type: "appstore",
      git_url: "https://github.com/mssun/certificates-passforios",
      git_basic_authorization: ENV['MATCH_GIT_BASIC_AUTHORIZATION'],
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      app_identifier: [app_identifier,
                       "#{app_identifier}.find-login-action-extension",
		       "#{app_identifier}.auto-fill-credential-extension",
		       "#{app_identifier}.shortcuts"],
      readonly: true
    )
end

platform :ios do
  desc "Runs all tests"
  lane :test do
    run_tests(scheme: "pass")
  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
    app_identifier = "me.mssun.passforiosbeta"
    api_key = app_store_connect_api_key(
      key_id: ENV["APP_STORE_CONNECT_API_KEY_KEY_ID"],
      issuer_id: ENV["APP_STORE_CONNECT_API_KEY_ISSUER_ID"],
      is_key_content_base64: true,
      key_content: ENV["APP_STORE_CONNECT_API_KEY_KEY_CONTENT"]
    )

    if is_ci?
      create_keychain(
        name: ENV["MATCH_KEYCHAIN_NAME"],
        password: ENV["MATCH_KEYCHAIN_PASSWORD"],
        default_keychain: false,
        unlock: true,
        timeout: 3600,
        add_to_search_list: true
      )
      match_appstore_beta
    end
    increment_build_number(
      build_number: latest_testflight_build_number(
        version: get_version_number(target: "pass"),
        app_identifier: app_identifier,
        api_key: api_key,
        initial_build_number: 0
      ) + 1,
      xcodeproj: "pass.xcodeproj"
    )
    build_app(
      scheme: "pass",
      configuration: "Beta",
      skip_profile_detection: true,
      export_method: "app-store",
      export_options: {
        method: "app-store",
        provisioningProfiles: {
          app_identifier => "match AppStore #{app_identifier}",
          "#{app_identifier}.find-login-action-extension" => "match AppStore #{app_identifier}.find-login-action-extension",
          "#{app_identifier}.auto-fill-credential-extension" => "match AppStore #{app_identifier}.auto-fill-credential-extension",
          "#{app_identifier}.shortcuts" => "match AppStore #{app_identifier}.shortcuts",
        },
      },
      verbose: true
    )
    upload_to_testflight(
      app_identifier: app_identifier,
      api_key: api_key,
      skip_waiting_for_build_processing: true
    )
    increment_build_number(
      build_number: 0,
      xcodeproj: "pass.xcodeproj"
    )
    if is_ci?
      notify_dev
    end
  end

  desc "Submit a new build to AppStore"
  lane :release do
    app_identifier = "me.mssun.passforios"
    api_key = app_store_connect_api_key(
      key_id: ENV["APP_STORE_CONNECT_API_KEY_KEY_ID"],
      issuer_id: ENV["APP_STORE_CONNECT_API_KEY_ISSUER_ID"],
      is_key_content_base64: true,
      key_content: ENV["APP_STORE_CONNECT_API_KEY_KEY_CONTENT"]
    )

    if is_ci?
      create_keychain(
        name: ENV["MATCH_KEYCHAIN_NAME"],
        password: ENV["MATCH_KEYCHAIN_PASSWORD"],
        default_keychain: false,
        unlock: true,
        timeout: 3600,
        add_to_search_list: true
      )
      match_appstore
    end
    increment_build_number(
      build_number: latest_testflight_build_number(
        version: get_version_number(target: "pass"),
        app_identifier: app_identifier,
        api_key: api_key,
        initial_build_number: 0
      ) + 1,
      xcodeproj: "pass.xcodeproj"
    )
    build_app(
      scheme: "pass",
      configuration: "Release",
      skip_profile_detection: true,
      export_options: {
        method: "app-store",
        provisioningProfiles: {
          app_identifier => "match AppStore #{app_identifier}",
          "#{app_identifier}.find-login-action-extension" => "match AppStore #{app_identifier}.find-login-action-extension",
          "#{app_identifier}.auto-fill-credential-extension" => "match AppStore #{app_identifier}.auto-fill-credential-extension",
          "#{app_identifier}.shortcuts" => "match AppStore #{app_identifier}.shortcuts",
        },
      },
      verbose: true
    )
    upload_to_testflight(
      app_identifier: app_identifier,
      api_key: api_key,
      skip_waiting_for_build_processing: true
    )
    increment_build_number(
      build_number: 0,
      xcodeproj: "pass.xcodeproj"
    )
    if is_ci?
      notify_dev
    end
  end

  desc 'Notify developers'
  lane :notify_dev do
    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://github.com/mssun/passforios/actions",
      message: "A new build has been uploaded by GitHub Action.",
    )
  end
end
