fastlane_version "1.86.0"

lane :release do |options|
  if options[:skip_git_checks] != true
    ensure_git_branch(branch: "master")
    ensure_git_status_clean
  else
    UI.message "Skipping the 'git status clean' and branch check!".yellow
  end

  git_pull

  libraryVersionUsed = read_podspec['dependencies']['R.swift.Library'].first
  unless prompt(text: "R.swift.Library #{libraryVersionUsed} is defined as dependency.\nAdjust podspec now if needed. Ready to continue?".green, boolean: true, ci_input: "")
    raise "Aborted by user".red
  end

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

  isPrerelease = false
  bumpType = prompt(text: "What kind of release is this? (major/minor/patch/custom)".green, boolean: false, ci_input: "")
  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

  newVersion = version_get_podspec()
  changelog = prompt(text: "Please provide release notes:".green, boolean: false, ci_input: "", multi_line_end_keyword: "FIN")
  af_insert_text_into_file(
    file_path: "Changelog.md",
    text: "## #{newVersion}\n\n#{changelog}\n\n",
    insert_at_bottom: false
  )

  # Sync version.swift with version in podspec
  filepath = '../Sources/rswift/Rswift.swift'
  newVersion = version_get_podspec()

  content = File.read(filepath).sub(/static let version = "(.*)"/, "static let version = \"#{newVersion}\"")
  File.open(filepath, 'wb') { |file| file.write(content) }

  if options[:skip_tests] != true
    runalltests
  else
    UI.message "Skipping tests!".yellow
  end

  sh "cd .. && swift build -c release -Xswiftc \"-target\" -Xswiftc \"x86_64-apple-macosx10.11\""

  zipPath = "/tmp/rswift-#{newVersion}.zip"
  sh "rm -f #{zipPath}"

  junk_zip(
    paths: ["./.build/release/rswift", "./License"],
    output_path: zipPath
  )

  unless prompt(text: "#{newVersion} has been build and 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

  unless `git status --porcelain`.empty?
    git_commit(
      path: ".",
      message: "Preparing for the #{newVersion} release"
    )
  end

  push_to_git_remote

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

  af_upload_asset_for_github_release(
    file_path: zipPath
  )

  pod_push
end

desc "Runs all the tests"
lane :runalltests do
  cocoapods(repo_update: false)

  sh "cd .. && swift test -Xswiftc \"-target\" -Xswiftc \"x86_64-apple-macosx10.11\" && mkdir -p build/debug && cp .build/debug/rswift build/debug"

  scan(
    workspace: "ResourceApp.xcworkspace",
    scheme: "ResourceApp",
    clean: true
  )
  scan(
    workspace: "ResourceApp.xcworkspace",
    scheme: "ResourceApp-tvOS",
    clean: true
  )
end

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