fastlane_version "1.86.0"

lane :release do |options|
  if options[:skip_branch_check] != true
    ensure_git_branch(branch: "master")
  end

  if options[:allow_dirty_branch] != true
    ensure_git_status_clean
  else
    UI.message "Skipping the 'git status clean' check!".yellow
  end

  git_pull

  runalltests

  unless is_ci
    notification(
      title: "R.swift.Library release",
      message: "💡 Needs your attention."
    )
  end

  currentVersion = version_get_podspec()
  UI.message "Current R.swift.Library podspec version is #{currentVersion}"

  bumpType = prompt(text: "What kind of release is this? (major/minor/patch/custom)".green, boolean: false, ci_input: "")
  isPrerelease = false
  case bumpType
  when "major", "minor", "patch"
    version_bump_podspec(bump_type: bumpType)
  when "custom"
    newVersion = prompt(text: "What is the new custom version number?".green, boolean: false, ci_input: "")
    version_bump_podspec(version_number: newVersion)

    isPrerelease = prompt(text: "Is this a prerelease version?".green, boolean: true, ci_input: "")
  else
    raise "Invalid release type: #{bumpType}".red
  end

  changelog = prompt(text: "Please provide release notes:".green, boolean: false, ci_input: "", multi_line_end_keyword: "FIN")

  newVersion = version_get_podspec()
  unless prompt(text: "#{newVersion} has been prepped for release. If you have any additional changes you would like to make, please do those before continuing. Would you like to commit, tag, push and release #{newVersion} including all uncommitted changes?".green, boolean: true, ci_input:"y")
    raise "Aborted by user".red
  end

  git_commit(
    path: ".",
    message: "Preparing for the #{newVersion} release"
  )

  push_to_git_remote

  af_create_github_release(
    owner: "mac-cain13",
    repository: "r.swift.library",
    tag_name: "v#{newVersion}",
    target_commitish: "master",
    name: "#{newVersion}",
    body: "#{changelog}",
    prerelease: isPrerelease
  )

  pod_push

  unless is_ci
    notification(
      title: "R.swift.Library release",
      message: "🎉 Version #{newVersion} is released."
    )
  end
end

lane :runalltests do
  scan(
    project: "R.swift.Library.xcodeproj",
    scheme: "Rswift-iOS",
    device: "iPhone 8",
    clean: true
  )
  scan(
    project: "R.swift.Library.xcodeproj",
    scheme: "Rswift-tvOS",
    device: "Apple TV 4K",
    clean: true
  )
end

error do |lane, exception|
  unless is_ci
    notification(
      title: "R.swift.Library #{lane}",
      message: "❌ Failed with an exception."
    )
  end
end
