Skip to content

Instantly share code, notes, and snippets.

@itsthejb
Forked from jfro/clean-sims.rb
Last active February 17, 2016 07:59
Show Gist options
  • Save itsthejb/fda94309a937ce304680 to your computer and use it in GitHub Desktop.
Save itsthejb/fda94309a937ce304680 to your computer and use it in GitHub Desktop.
Quick script to remove stale simulator device folders unused by Xcode, lists invalid devices if run without flags
#!/usr/bin/ruby
require 'fileutils'
require 'optparse'
xcodePath = %x{xcode-select -print-path}
puts "Checking simulator devices against Xcode: #{xcodePath}"
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: clean-sims.rb [options]"
opts.on("-d", "--delete", "Delete stale simulator devices") do |d|
options[:delete] = d
end
end.parse!
# Ask Xcode for sim device IDs
activeSims = %x{xcrun simctl list}
validDeviceIDs = {}
for line in activeSims.split("\n")
m = /(.*)\s\(([\w\d]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12})(?:(?!unavailable).)*$/m.match(line)
if m
validDeviceIDs[m[2]] = m[1].strip
end
end
def getDeviceInfo(devicePath, key)
return %x{/usr/libexec/PlistBuddy -c "Print :#{key}" #{devicePath}/device.plist}.strip
end
# find all on-disk devices, compare against Xcode's list
for deviceFolder in Dir.glob(File.join(Dir.home, "/Library/Developer/CoreSimulator/Devices/*"))
deviceID = File.basename(deviceFolder)
isValid = false
if not validDeviceIDs.has_key? deviceID
name = getDeviceInfo(deviceFolder, 'name')
runtime = getDeviceInfo(deviceFolder, 'runtime')
puts "#{deviceID}(#{name}, #{runtime}) is invalid"
if options[:delete]
FileUtils.remove_entry_secure(deviceFolder)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment