fastlane_version '2.82.0'

#### Release ####

desc 'Release a new version of VLC to the App Store'
desc ''
desc 'This action requires the following parameters:'
desc '- platform (iOS or tvOS)'
desc ''
desc 'This action does the following:'
desc '- Ensure a clean git status'
desc '- Clear derived data'
desc '- Set the version, bump the build number and commit the change'
desc '- Apply the privateConstants which include the credentials'
desc '- Install cocoapods dependencies'
desc '- Build and sign the app'
desc '- Update the changelog from the NEWS file'
desc '- Push the version bump'
lane :release do |options|
   platform = get_platform options
   version = get_version options
   ensure_git_status_clean
   clear_derived_data
   set_version_bump_build_and_commit(platform: platform, version: version)
   git_apply_private_constants
   cocoapods(repo_update: true)
   gym(scheme: "VLC-#{platform}")
   pilot(app_platform: platform == 'tvOS' ? 'appletvos' : 'ios')
   update_changelog
   push_to_git_remote
end

#### Private ####

desc 'Bump and commit app version and build number'
private_lane :set_version_bump_build_and_commit do |options|
  if options[:platform] == 'tvOS'
    increment_build_number_in_plist(VLC::infoPlistPath[:tvOS])
    set_version_number_in_plist(VLC::infoPlistPath[:tvOS], options[:version])
  elsif options[:platform] == 'iOS'
    increment_build_number_in_plist(VLC::infoPlistPath[:iOS])
    increment_build_number_in_plist(VLC::infoPlistPath[:watchKitExtension])
    increment_build_number_in_plist(VLC::infoPlistPath[:watchOS])
    set_version_number_in_plist(VLC::infoPlistPath[:iOS], options[:version])
    set_version_number_in_plist(VLC::infoPlistPath[:watchKitExtension], options[:version])
    set_version_number_in_plist(VLC::infoPlistPath[:watchOS], options[:version])
  end

  commit_version_bump(message: 'Version Bump by fastlane', force: true)
end

desc 'Update changelog in iTunes Connect with the content from Docs/NEWS'
private_lane :update_changelog do |options|
  # Splits the News by -------- get out the top notes
  changelog = File.read('../Docs/NEWS').split('-----------')[1].split('-----------').first
  temp_changelog = changelog.split("${options[:platform]}")
  if temp_changelog.count <= 1
    temp_changelog = changelog.split("tvOS")
  end
  changelog = temp_changelog[0..-2].join.strip
  set_changelog(app_identifier: 'org.videolan.vlc-ios', changelog: changelog, username: '*', team_name: 'VideoLAN')
end

desc 'Apply privateConstants patch including credentials'
private_lane :git_apply_private_constants do
  Dir.chdir('..') do
    gitapply = `xcrun git apply 0001-privateConstants.patch`
    if gitapply != ''
      puts("⚠️  There are conflicts. Please resolve the conflicts and update the privateConstants.patch before continuing.\n#{gitapply}")
      exit 1
    end
  end
end

desc 'Return the platform received as parameter, or ask for it if missing'
private_lane :get_platform do |options|
  platform = options[:platform]
  if !platform || platform.empty?
    platform = prompt(text: 'Platform [iOS, tvOS]: ')
  end
  if platform != 'iOS' && platform != 'tvOS'
    puts("⚠️  Platform '#{platform}' not supported")
    exit 1
  end
  platform
end
