# Customise this file, documentation can be found here:
# https://github.com/fastlane/fastlane/tree/master/fastlane/docs
# All available actions: https://docs.fastlane.tools/actions
# can also be listed using the `fastlane actions` command

# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`

# If you want to automatically update fastlane if a new version is available:
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.107.0"

default_platform :ios

platform :ios do
  $lane_active = ""
  before_all do
    # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
    skip_docs
  end

  desc "Runs all the tests"
  lane :test do
    $lane_active = "Test"
    scan(
      workspace: ENV["XCODE_WORKSPACE"],
      scheme: ENV["XCODE_SCHEME"],
      clean: true,
      code_coverage: true
    )
  end

  desc "Run code coverage with xcov"
  lane :coverage do
    $lane_active = "Coverage"
    xcov(
      workspace: ENV["XCODE_WORKSPACE"],
      scheme: ENV["XCODE_SCHEME"],
      output_directory: "coverage",
      html_report: false,
      json_report: true,
      only_project_targets: true
    )
    xcov_report    
  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
    $lane_active = "Beta"
    # Fetch provisioning profile
    # sigh
    sigh(
      adhoc: false
    )
  
    # Build
    gym(
      scheme: ENV["XCODE_SCHEME"],
      configuration: "Beta",
      sdk: "iphoneos12.0",
      clean: true,
      include_bitcode: true,
      include_symbols: true,
      export_method: "app-store",
      buildlog_path: 'fastlane/logs'
    )

    # TestFlight upload
    pilot(
      app_identifier: ENV["APP_IDENTIFIER"],
      skip_waiting_for_build_processing: true
    )
  end

  desc "builds and packages iOS apps"
  lane :archive do
    $lane_active = "Archive"
    # Fetch provisioning profile
    # sigh
    sigh(
      adhoc: false
    )
    # Build
    gym(
      scheme: ENV["XCODE_SCHEME"],
      configuration: "Release",
      sdk: "iphoneos12.0",
      clean: true,
      include_bitcode: true,
      include_symbols: true,
      export_method: "app-store",
      buildlog_path: 'fastlane/logs'
    )
  end

  desc "publish new version in App Store"
  lane :publish do
    $lane_active = "Publish"
    deliver(
      force: true, 
      skip_metadata: true,
      ignore_language_directory_validation: true
    )
  end

  after_all do |lane|
    telegram(
        url_horn: ENV["TELEGRAM_WEBHOOKS"],
        message: $lane_active + " Fastlane is OK"
    )
  end

  error do |lane, exception|
    telegram(
        url_horn: ENV["TELEGRAM_WEBHOOKS"],
        message: "`ERROR` " + $lane_active + " Fastlane FAIL"
    )
  end
end


# More information about multiple platforms in fastlane: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
# All available actions: https://docs.fastlane.tools/actions

# fastlane reports which actions are used. No personal data is recorded. 
# Learn more at https://github.com/fastlane/fastlane#metrics
