# 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.4.0"

default_platform :ios

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

    carthage(
      use_submodules: true,
      no_build: true
    )
  end

  desc "Generate app icons"
  lane :icons do
    sh "cd .. && export SOURCE_ROOT=`pwd` && ./Authenticator/Resources/GenerateIcons.sh"
  end

  desc "Generate screenshots"
  lane :screenshots do
    # Generate updated screenshots.
    snapshot
    # Compress the screenshots with pngcrush
    sh "for IMG in screenshots/**/*.png; do pngcrush -rem alla -ow \"$IMG\"; done"
  end

  desc "Build the app for release"
  lane :build do
    set_build_number
    gym
    clear_build_number

    # Add a git tag for this build.
    add_git_tag(
      tag: "build/#{build_number}"
    )
  end

  desc "Runs all the tests"
  lane :test do
    scan(scheme: "Authenticator")
  end

  desc "Submit a new beta build to TestFlight"
  lane :beta do
    # Ensure that the generated icons are up-to-date and committed.
    icons
    ensure_git_status_clean

    # Build and upload the new binary to TestFlight.
    build
    pilot
  end

  desc "Deploy a new version to the App Store"
  lane :release do
    # Ensure that the generated icons and screenshots are up-to-date and committed.
    icons
    screenshots
    ensure_git_status_clean

    # Build and upload the app metadata and binary to iTunes Connect.
    build
    deliver(
      overwrite_screenshots: true,
      submit_for_review: true,
      automatic_release: true,
      phased_release: true
    )

    # Add a git tag for this release.
    add_git_tag(
      tag: get_version_number
    )

    # After committing and tagging, bump the version number.
    increment_version_number
  end

  # You can define as many lanes as you want

  after_all do |lane|
    # This block is called, only if the executed lane was successful

    # slack(
    #   message: "Successfully deployed new App Update."
    # )
  end

  error do |lane, exception|
    # slack(
    #   message: exception.message,
    #   success: false
    # )
  end
end

def build_number
  # Generate a build number from the number of git commits.
  return number_of_commits.to_s
end

def set_build_number
  increment_build_number(
    build_number: build_number,
    skip_info_plist: true,
  )
end

def clear_build_number
  increment_build_number(
    build_number: 0,
    skip_info_plist: true,
  )
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/enhancer
