Method: Xccleanup.remove_old_archives

Defined in:
lib/xccleanup.rb

.remove_old_archives(manually) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/xccleanup.rb', line 169

def self.remove_old_archives(manually)
	saved_bytes = 0

	arch_folder = File.expand_path('~/Library/Developer/Xcode/Archives/')
	arch_date_folders = get_folders_in_dir(arch_folder)

	bundle_ids = {}

	arch_date_folders.each do |arch_date_folder|
		date_name = arch_date_folder.split('/').last

		archives = get_folders_in_dir(arch_date_folder)


		if archives.length == 0
			puts "- Empty archives subfolder #{date_name}, removing..."
			FileUtils.rm_rf(arch_date_folder)
		else
			archives.each do |archive|
				plist_path = File.join(archive,'Info.plist')
				if File.file?(plist_path)
					plist = File.read(plist_path)

					bundle_id_pos = plist.index('<key>CFBundleIdentifier</key>')
					unless bundle_id_pos.nil?
						bundle_id_pos = plist.index('<string>', bundle_id_pos)
						unless bundle_id_pos.nil?
							bundle_id_pos = bundle_id_pos + 8 # <string>
							bundle_id_end = plist.index('</string>', bundle_id_pos)
							unless bundle_id_end.nil?
								bundle_id = plist[bundle_id_pos, bundle_id_end - bundle_id_pos]
								if bundle_ids[bundle_id].nil?
									bundle_ids[bundle_id] = {date_name => archive }
								else
									bundle_ids[bundle_id][date_name] = archive
								end
							end
						end
					end
				end
			end
		end
	end

	skip_single_archives = false
	if manually
		skip_single_archives = prompt_bool("Skip all bundle id's for which only a single archive is present?")
	end

	bundle_ids.each do |bundle_id, dates|
		if dates.length > 1
			dates = dates.sort_by { |date, archive| Date.parse(date) }.reverse
			puts "#{dates.length} archives for \"#{bundle_id}\":"
			arch_index = 1
			dates.each do |date, archive|
				archive_size = get_byte_size(archive)
				puts "  #{arch_index}: #{date} (#{pbs(archive_size)})"
				arch_index += 1
			end
			
			recent = 0
			if manually
				recent = prompt("> KEEP how many most recent? ").to_i
			end

			if recent < dates.length
				kept = 0
				dates.each do |date, archive|
					if kept >= recent
						archive_size = get_byte_size(archive)
						puts "- Removing #{date}"
						FileUtils.rm_rf(archive)
						saved_bytes += archive_size
					else
						puts "- Keeping #{date}"
					end
					kept += 1
				end
			end
		elsif !skip_single_archives
			date, archive = dates.first
			archive_size = get_byte_size(archive)

			remove = !manually
			if manually
				remove = prompt_bool("• 1 archive for \"#{bundle_id}\" (#{date}, #{pbs(archive_size)})\n> REMOVE it?")
			else
				puts "• Removing 1 archive for \"#{bundle_id}\" (#{date}, #{pbs(archive_size)})"
			end

			if remove
				FileUtils.rm_rf(archive)
				saved_bytes += archive_size
			end
		end
	end

	return saved_bytes
end