# Customise this file, documentation can be found here:
# https://github.com/KrauseFx/fastlane/tree/master/docs

$:.unshift File.dirname(__FILE__)
require 'lib/utils.rb'

fastlane_version "1.0.0"

default_platform :ios

platform :ios do
  before_all do
     ENV['DELIVER_WHAT_TO_TEST'] = git_commit_log
     ensure_git_status_clean
  end

  desc "Runs linting (and eventually static analysis)"
  lane :analyze do
    return if test_disabled?
    make 'lint'
  end

  desc "Runs all the unit tests."
  lane :test do
    return if test_disabled?
    # TODO: lint & test JS code
    xctest(
      scheme: 'Wikipedia',
      destination: "platform=iOS Simulator,name=iPhone 6,OS=8.3",
      reports: [
        {
          report: "html",
          output: "build/reports/unit-tests.html"
        },
        {
          report: "junit",
          output: "build/reports/unit-tests.xml"
        }
      ],
      clean: nil
    )
  end

  desc "Bump the version, and submit a new **Wikipedia Alpha** Build to Apple TestFlight"
  lane :alpha do
    # snapshot
    sigh
    increment_build_number

    # uncomment when CI is able to push tags
    if ENV['WMF_BUMP']
      commit_version_bump
      plist_version = get_version_short_string File.expand_path(File.join(ENV['PWD'], 'Wikipedia/Wikipedia-Info.plist'))
      # tag must be added after the version bump is committed
      add_git_tag(tag: "#{plist_version}.#{Actions.lane_context[Actions::SharedValues::BUILD_NUMBER]}")
    end

    ipa(
      configuration: "Alpha",
      scheme: "Wikipedia Alpha",
    )
    hockey(
      notes: '',
      notify: '0', # Means do not notify
      status: '1', # Means do not make available for download
    )
    deliver skip_deploy: true, beta: true

    # uncomment when CI is able to push tags
    if ENV['WMF_BUMP']
      # only push after everything else has succeeded
      push_to_git_remote
    end
  end

  desc "Submit a new **Wikipedia Beta** build to Apple TestFlight"
  lane :beta do
    # snapshot
    sigh
    ipa(
      configuration: "Beta",
      scheme: "Wikipedia Beta",
    )
    hockey(
      notes: '',
      notify: '0', # Means do not notify
      status: '1', # Means do not make available for download
    )
    deliver skip_deploy: true, beta: true
  end

  desc "Deploy a new version to the App Store"
  lane :store do
    # snapshot
    sigh
    ipa(
      configuration: "Wikipedia",
      scheme: "Wikipedia",
    )
    hockey(
      notes: '',
      notify: '0', # Means do not notify
      status: '1', # Means do not make available for download
    )
    deliver skip_deploy: true, force: true
  end

  after_all do |lane|
  
  end

  error do |lane, exception|
  
  end
end