Class: Deliver::SyncScreenshots

Inherits:
Object
  • Object
show all
Defined in:
deliver/lib/deliver/sync_screenshots.rb

Defined Under Namespace

Classes: DeleteScreenshotJob, UploadResult, UploadScreenshotJob

Instance Method Summary collapse

Constructor Details

#initialize(app:, platform:) ⇒ SyncScreenshots

Returns a new instance of SyncScreenshots.



32
33
34
35
# File 'deliver/lib/deliver/sync_screenshots.rb', line 32

def initialize(app:, platform:)
  @app = app
  @platform = platform
end

Instance Method Details

#do_replace_screenshots(iterator, screenshots, delete_worker, upload_worker) ⇒ Object

This is a testable method that focuses on figuring out what to update



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'deliver/lib/deliver/sync_screenshots.rb', line 101

def do_replace_screenshots(iterator, screenshots, delete_worker, upload_worker)
  remote_screenshots = iterator.each_app_screenshot.map do |localization, app_screenshot_set, app_screenshot|
    ScreenshotComparable.create_from_remote(app_screenshot: app_screenshot, locale: localization.locale)
  end

  local_screenshots = iterator.each_local_screenshot(screenshots.group_by(&:language)).map do |localization, app_screenshot_set, screenshot, index|
    if index >= 10
      UI.user_error!("Found #{localization.locale} has more than 10 screenshots for #{app_screenshot_set.screenshot_display_type}. "\
                     "Make sure containts only necessary screenshots.")
    end
    ScreenshotComparable.create_from_local(screenshot: screenshot, app_screenshot_set: app_screenshot_set)
  end

  # Thanks to `Array#-` API and `ScreenshotComparable`, working out diffs between local screenshot directory and App Store Connect
  # is as easy as you can see below. The former one finds what is missing in local and the latter one is visa versa.
  screenshots_to_delete = remote_screenshots - local_screenshots
  screenshots_to_upload = local_screenshots - remote_screenshots

  delete_jobs = screenshots_to_delete.map { |x| DeleteScreenshotJob.new(x.context[:app_screenshot], x.context[:locale]) }
  delete_worker.batch_enqueue(delete_jobs)
  delete_worker.start

  upload_jobs = screenshots_to_upload.map { |x| UploadScreenshotJob.new(x.context[:app_screenshot_set], x.context[:screenshot].path) }
  upload_worker.batch_enqueue(upload_jobs)
  upload_worker.start
end

#enable_localizations(locales) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'deliver/lib/deliver/sync_screenshots.rb', line 71

def enable_localizations(locales)
  localizations = fetch_localizations
  locales_to_enable = locales - localizations.map(&:locale)
  Helper.show_loading_indicator("Activating localizations for #{locales_to_enable.join(', ')}...")
  locales_to_enable.each do |locale|
    version.create_app_store_version_localization(attributes: { locale: locale })
  end
  Helper.hide_loading_indicator
end

#replace_screenshots(iterator, screenshots, retries = 3) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'deliver/lib/deliver/sync_screenshots.rb', line 81

def replace_screenshots(iterator, screenshots, retries = 3)
  # delete and upload screenshots to get App Store Connect in sync
  do_replace_screenshots(iterator, screenshots, create_delete_worker, create_upload_worker)

  # wait for screenshots to be processed on App Store Connect end and
  # ensure the number of uploaded screenshots matches the one in local
  result = wait_for_complete(iterator)
  return if !result.processing? && result.screenshot_count == screenshots.count

  if retries.zero?
    UI.crash!("Retried uploading screenshots #{retries} but there are still failures of processing screenshots." \
              "Check App Store Connect console to work out which screenshots processed unsuccessfully.")
  end

  # retry with deleting failing screenshots
  result.failing_screenshots.each(&:delete!)
  replace_screenshots(iterator, screenshots, retries - 1)
end

#sort_screenshots(iterator) ⇒ Object



153
154
155
156
157
158
159
# File 'deliver/lib/deliver/sync_screenshots.rb', line 153

def sort_screenshots(iterator)
  Helper.show_loading_indicator("Sorting screenshots uploaded...")
  sort_worker = create_sort_worker
  sort_worker.batch_enqueue(iterator.each_app_screenshot_set.to_a.map { |_, set| set })
  sort_worker.start
  Helper.hide_loading_indicator
end

#sync(screenshots) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'deliver/lib/deliver/sync_screenshots.rb', line 43

def sync(screenshots)
  UI.important('This is currently a beta feature in fastlane. This may cause some errors on your environment.')

  unless FastlaneCore::Feature.enabled?('FASTLANE_ENABLE_BETA_DELIVER_SYNC_SCREENSHOTS')
    UI.user_error!('Please set a value to "FASTLANE_ENABLE_BETA_DELIVER_SYNC_SCREENSHOTS" environment variable ' \
                   'if you acknowleage the risk and try this out.')
  end

  UI.important("Will begin uploading snapshots for '#{version.version_string}' on App Store Connect")

  # enable localizations that will be used
  screenshots_per_language = screenshots.group_by(&:language)
  enable_localizations(screenshots_per_language.keys)

  # create iterator
  localizations = fetch_localizations
  iterator = Deliver::AppScreenshotIterator.new(localizations)

  # sync local screenshots with remote settings by deleting and uploading
  UI.message("Starting with the upload of screenshots...")
  replace_screenshots(iterator, screenshots)

  # ensure screenshots within screenshot sets are sorted in right order
  sort_screenshots(iterator)

  UI.important('Screenshots are synced successfully!')
end

#sync_from_path(screenshots_path) ⇒ Object



37
38
39
40
41
# File 'deliver/lib/deliver/sync_screenshots.rb', line 37

def sync_from_path(screenshots_path)
  # load local screenshots
  screenshots = Deliver::Loader.load_app_screenshots(screenshots_path, true)
  sync(screenshots)
end

#wait_for_complete(iterator) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'deliver/lib/deliver/sync_screenshots.rb', line 128

def wait_for_complete(iterator)
  retry_count = 0
  Helper.show_loading_indicator("Waiting for all the screenshots processed...")
  loop do
    failing_screenshots = []
    state_counts = iterator.each_app_screenshot.map { |_, _, app_screenshot| app_screenshot }.each_with_object({}) do |app_screenshot, hash|
      state = app_screenshot.asset_delivery_state['state']
      hash[state] ||= 0
      hash[state] += 1
      failing_screenshots << app_screenshot if app_screenshot.error?
    end

    result = UploadResult.new(asset_delivery_state_counts: state_counts, failing_screenshots: failing_screenshots)
    return result unless result.processing?

    # sleep with exponential backoff
    interval = 5 + (2**retry_count)
    UI.message("There are still incomplete screenshots. Will check the states again in #{interval} secs - #{state_counts}")
    sleep(interval)
    retry_count += 1
  end
ensure
  Helper.hide_loading_indicator
end