# Release lanes, run on the machine holding the release key:
#
#   bundle install
#   bundle exec fastlane play     # Play Store bundle, added to production pending changes
#   bundle exec fastlane github   # GitHub release APK, attached to a draft release for the pushed vX.Y tag
#
# Env: ETCHDROID_KEYSTORE, ETCHDROID_KEYSTORE_PASSWORD, ETCHDROID_KEY_ALIAS, ETCHDROID_KEY_PASSWORD,
# SENTRY_AUTH_TOKEN (uploads the R8 mapping so Sentry stack traces are readable),
# SUPPLY_JSON_KEY (Play service account JSON, play lane only).
# Both lanes need the real app/google-services.json.

skip_docs
default_platform(:android)

platform :android do
  def build(task, properties = {})
    ENV.fetch("SENTRY_AUTH_TOKEN") # fail fast: a release without its mapping has unreadable Sentry traces
    gradle(
      task: task,
      flavor: "Gplay",
      build_type: "Release",
      properties: {
        "android.injected.signing.store.file" => ENV.fetch("ETCHDROID_KEYSTORE"),
        "android.injected.signing.store.password" => ENV.fetch("ETCHDROID_KEYSTORE_PASSWORD"),
        "android.injected.signing.key.alias" => ENV.fetch("ETCHDROID_KEY_ALIAS"),
        "android.injected.signing.key.password" => ENV.fetch("ETCHDROID_KEY_PASSWORD"),
      }.merge(properties),
      print_command: false # would print the passwords
    )
  end

  # fastlane's GRADLE_*_OUTPUT_PATH is just the newest file under build/outputs, which can be a
  # stale debug build, so pick the gplayRelease output by its path.
  def output(key, suffix)
    lane_context[key].find { |path| path.end_with?(suffix) } or UI.user_error!("No #{suffix} built")
  end

  desc "Upload a signed bundle to Google Play, leaving it in pending changes to send for review manually"
  lane :play do |options|
    build("bundle")
    upload_to_play_store(
      package_name: "eu.depau.etchdroid",
      track: options[:track] || "production",
      aab: output(SharedValues::GRADLE_ALL_AAB_OUTPUT_PATHS, "/gplayRelease/app-gplay-release.aab"),
      skip_upload_apk: true, # would otherwise default to every APK under build/outputs
      changes_not_sent_for_review: true,
      skip_upload_metadata: true,
      skip_upload_images: true,
      skip_upload_screenshots: true
    )
  end

  desc "Build the signed GitHub release APK (donation links kept) and attach it to a draft release"
  lane :github do
    version = File.read("../app/build.gradle.kts")[/versionName = "(.+)"/, 1]
    build("assemble", "etchdroid.githubRelease" => true)
    apk = output(SharedValues::GRADLE_ALL_APK_OUTPUT_PATHS, "/gplay/release/app-gplay-release.apk")
    sh("gh", "release", "create", "v#{version}", apk,
       "--draft", "--verify-tag", "--title", "v#{version}", "--generate-notes")
  end
end
