def check_superseded_tracks(apk_version_codes)
      UI.message("Checking superseded tracks, uploading '#{apk_version_codes}' to '#{Supply.config[:track]}'...")
      max_apk_version_code = apk_version_codes.max
      max_tracks_version_code = nil

      tracks = ["production", "rollout", "beta", "alpha", "internal"]
      config_track_index = tracks.index(Supply.config[:track])

      # Custom "closed" tracks are now allowed (https://support.google.com/googleplay/android-developer/answer/3131213)
      # Custom tracks have an equal level with alpha (alpha is considered a closed track as well)
      # If a track index is not found, we will assume is a custom track so an alpha index is given
      config_track_index = tracks.index("alpha") unless config_track_index

      tracks.each_index do |track_index|
        track = tracks[track_index]
        track_version_codes = client.track_version_codes(track).sort
        UI.verbose("Found '#{track_version_codes}' on track '#{track}'")

        next if track_index.eql?(config_track_index)
        next if track_version_codes.empty?

        if max_tracks_version_code.nil?
          max_tracks_version_code = track_version_codes.max
        end

        removed_version_codes = track_version_codes.take_while do |v|
          v < max_tracks_version_code || (v < max_apk_version_code && track_index > config_track_index)
        end

        next if removed_version_codes.empty?

        keep_version_codes = track_version_codes - removed_version_codes
        max_tracks_version_code = keep_version_codes[0] unless keep_version_codes.empty?
        client.update_track(track, 1.0, keep_version_codes)
        UI.message("Superseded track '#{track}', removed '#{removed_version_codes}'")
      end
    end
def check_superseded_tracks(apk_version_codes)
      UI.message("Checking superseded tracks, uploading '#{apk_version_codes}' to '#{Supply.config[:track]}'...")
      max_apk_version_code = apk_version_codes.max
      max_tracks_version_code = nil

      tracks = ["production", "rollout", "beta", "alpha", "internal"]
      config_track_index = tracks.index(Supply.config[:track])

      # Custom "closed" tracks are now allowed (https://support.google.com/googleplay/android-developer/answer/3131213)
      # Custom tracks have an equal level with alpha (alpha is considered a closed track as well)
      # If a track index is not found, we will assume is a custom track so an alpha index is given
      config_track_index = tracks.index("alpha") unless config_track_index

      tracks.each_index do |track_index|
        track = tracks[track_index]
        track_version_codes = client.track_version_codes(track).sort
        UI.verbose("Found '#{track_version_codes}' on track '#{track}'")

        next if track_index.eql?(config_track_index)
        next if track_version_codes.empty?

        if max_tracks_version_code.nil?
          max_tracks_version_code = track_version_codes.max
        end

        removed_version_codes = track_version_codes.take_while do |v|
          v < max_tracks_version_code || (v < max_apk_version_code && track_index > config_track_index)
        end

        next if removed_version_codes.empty?

        keep_version_codes = track_version_codes - removed_version_codes
        max_tracks_version_code = keep_version_codes[0] unless keep_version_codes.empty?
        client.update_track(track, 1.0, keep_version_codes)
        UI.message("Superseded track '#{track}', removed '#{removed_version_codes}'")
      end
    end
Remove any version codes that is:
  - Lesser than the greatest of any later (i.e. production) track
  - Or lesser than the currently being uploaded if it's in an earlier (i.e. alpha) track