skip_docs

VERSION = read_json( json_path: "./package.json" )[:version]
GITHUB_RELEASE = "v#{VERSION}"
ENVIRONMENT = ENV["ENVIRONMENT"]
BUILD_NUMBER = "#{Time.now.strftime("%y%m%d")}#{ENV["GITHUB_RUN_NUMBER"]}"
GITHUB_REPO = "tooot-app/app"

XCODEPROJ = "./ios/tooot.xcodeproj"
INFO_PLIST = "./ios/tooot/Info.plist"

desc "ANDROID: Prepare play store"
private_lane :prepare_playstore_android do
  android_set_version_name( version_name: VERSION, gradle_file: "./android/app/build.gradle" )
  android_set_version_code( version_code: BUILD_NUMBER, gradle_file: "./android/app/build.gradle" )
end

desc "Build and deploy iOS app"
private_lane :build_ios do
  BUILD_DIRECTORY = "./ios/build"
  IPA_FILE = "#{BUILD_DIRECTORY}/tooot.ipa"
  DSYM_FILE = "#{BUILD_DIRECTORY}/tooot.app.dSYM.zip"

  setup_ci
  set_info_plist_value( path: INFO_PLIST, key: "CFBundleShortVersionString", value: VERSION )
  set_info_plist_value(
    path: INFO_PLIST,
    key: "NSAppTransportSecurity",
    value: {}
  )
  increment_build_number( xcodeproj: XCODEPROJ, build_number: BUILD_NUMBER )
  app_store_connect_api_key
  match( type: "appstore", readonly: true )

  build_ios_app(
    export_method: "app-store",
    include_symbols: true,
    output_directory: BUILD_DIRECTORY,
    silent: true
  )

  case ENVIRONMENT
  when "candidate"
    sentry_upload_dsym(
      org_slug: ENV["SENTRY_ORGANIZATION"],
      project_slug: ENV["SENTRY_PROJECT"],
      dsym_path: DSYM_FILE
    )
    upload_to_testflight(
      skip_submission: true,
      ipa: IPA_FILE,
      demo_account_required: true,
      distribute_external: true,
      groups: "尝鲜",
      changelog: "感谢帮忙测试 tooot 🙏"
    )
  when "release"
    upload_to_app_store(
      ipa: IPA_FILE,
      app_version: VERSION
    )
    download_dsyms(
      version: VERSION,
      build_number: BUILD_NUMBER,
      wait_for_dsym_processing: true
    )
    sentry_upload_dsym(
      org_slug: ENV["SENTRY_ORGANIZATION"],
      project_slug: ENV["SENTRY_PROJECT"],
    )
  end
end

desc "Build and deploy Android app"
private_lane :build_android do
  sh("echo #{ENV["ANDROID_KEYSTORE"]} | base64 -d | tee #{File.expand_path('..', Dir.pwd)}/android/tooot.jks >/dev/null", log: false)

  prepare_playstore_android

  build_android_app(
    task: 'clean bundle',
    build_type: 'release',
    project_dir: "./android",
    print_command: true,
    print_command_output: true,
    properties: {
      "android.injected.signing.store.file" => "#{File.expand_path('..', Dir.pwd)}/android/tooot.jks",
      "android.injected.signing.store.password" => ENV["ANDROID_KEYSTORE_PASSWORD"],
      "android.injected.signing.key.alias" => ENV["ANDROID_KEYSTORE_ALIAS"],
      "android.injected.signing.key.password" => ENV["ANDROID_KEYSTORE_KEY_PASSWORD"],
    }
  )

  case ENVIRONMENT
  when "candidate"
    upload_to_play_store(
      track: "alpha",
      skip_upload_metadata: true,
      skip_upload_changelogs: true,
      skip_upload_images: true,
      skip_upload_screenshots: true
    )
  when "release"
    upload_to_play_store(
      track: "production",
      skip_upload_changelogs: true
    )
  end
end

desc "Build Android apk"
private_lane :build_android_apk do
  sh("echo #{ENV["ANDROID_KEYSTORE"]} | base64 -d | tee #{File.expand_path('..', Dir.pwd)}/android/tooot.jks >/dev/null", log: false)

  prepare_playstore_android

  build_android_app(
    task: 'assemble',
    build_type: 'release',
    project_dir: "./android",
    print_command: true,
    print_command_output: true,
    properties: {
      "android.injected.signing.store.file" => "#{File.expand_path('..', Dir.pwd)}/android/tooot.jks",
      "android.injected.signing.store.password" => ENV["ANDROID_KEYSTORE_PASSWORD"],
      "android.injected.signing.key.alias" => ENV["ANDROID_KEYSTORE_ALIAS"],
      "android.injected.signing.key.password" => ENV["ANDROID_KEYSTORE_KEY_PASSWORD"],
    }
  )

  sh "mv #{lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]} #{File.expand_path('..', Dir.pwd)}/tooot-#{GITHUB_RELEASE}.apk"
end

lane :ios do
  cocoapods(clean_install: true, podfile: "./ios/Podfile")
  build_ios
  rocket
end

lane :android do
  build_android
  rocket
end

lane :release do
  if ENVIRONMENT == 'release'
    build_android_apk
    set_github_release(
      repository_name: GITHUB_REPO,
      name: GITHUB_RELEASE,
      tag_name: GITHUB_RELEASE,
      description: "No changelog provided",
      commitish: git_branch,
      is_prerelease: false,
      upload_assets: ["#{File.expand_path('..', Dir.pwd)}/tooot-#{GITHUB_RELEASE}.apk"]
    )
  end
  rocket
end
