fastlane_version "2.129.0"

default_platform :ios


platform :ios do

  library_name = "MockingKit"
  main_branch = "master"
  build_folder_docs = ".build/docs"


  # Version ================

  desc "Create a new version"
  lane :version do |options|
    version_validate
    docc

    bump_type = options[:type]
    version = version_bump_podspec(
      path: 'Version',
      bump_type: bump_type)

    git_commit(path: "*", message: "Bump to #{version}")
    add_git_tag(tag: version)
    push_git_tags()
    push_to_git_remote()
  end
  
  desc "Validate that the repo is valid for release"
  lane :version_validate do
    ensure_git_status_clean
    ensure_git_branch(branch: main_branch)
    swiftlint(strict: true)
    sh("swift test")
  end


  # Docs =======================

  desc "Build documentation for all platforms"
  lane :docc do
    sh('cd .. && rm -rf ' + build_folder_docs)
    
    docc_platform(destination: 'iOS', name: 'ios')
    docc_platform(destination: 'OS X', name: 'osx')
    docc_platform(destination: 'tvOS', name: 'tvos')
    docc_platform(destination: 'watchOS', name: 'watchos')
    
    docc_web_platform(name: 'ios')
    docc_web_platform(name: 'osx')
    docc_web_platform(name: 'tvos')
    docc_web_platform(name: 'watchos')
  end

  desc "Build documentation for a single platform"
  lane :docc_platform do |values|
    sh('cd .. && mkdir -p ' + build_folder_docs)
    docc_delete_derived_data(lib: library_name)
    sh('cd .. && xcodebuild docbuild \
      -scheme ' + library_name + ' \
      -destination \'generic/platform=' + values[:destination] + '\'')
    sh('cd .. && \
      find ~/Library/Developer/Xcode/DerivedData \
      -name "' + library_name + '.doccarchive" \
      -exec cp -R {} ' + build_folder_docs + ' \;')
    sh('cd .. && \
      mv ' + build_folder_docs + '/' + library_name + '.doccarchive ' + build_folder_docs + '/' + library_name + '_' + values[:name] + '.doccarchive')
  end

  desc "Delete documentation derived data (may be historic duplicates)"
  lane :docc_delete_derived_data do
    sh('find ~/Library/Developer/Xcode/DerivedData \
      -name "' + library_name + '.doccarchive" \
      -exec rm -Rf {} \; || true')
  end

  desc "Build static documentation website for a single platform"
  lane :docc_web_platform do |values|
    sh('cd .. && $(xcrun --find docc) process-archive \
      transform-for-static-hosting ' + build_folder_docs + '/' + library_name + '_' + values[:name] + '.doccarchive \
      --output-path  ' + build_folder_docs + '/web_' + values[:name] + ' \
      --hosting-base-path ' + library_name + '')
  end

end
