Skip to content

Instantly share code, notes, and snippets.

@tamc
Created April 16, 2019 13:10
Show Gist options
  • Save tamc/b266cee82bd20943be3370f5adf80624 to your computer and use it in GitHub Desktop.
Save tamc/b266cee82bd20943be3370f5adf80624 to your computer and use it in GitHub Desktop.
Script to set the photos and videos in the gallery to be the same in all iOS simulators
#!/usr/bin/env ruby
require 'json'
require 'optparse'
$shutDownSimulators = false
$bootSimulators = false
$leaveExisting = false
$alterDates = true
OptionParser.new do |opts|
opts.banner = "Usage #{$0} [options] folder-of-media"
opts.on("-s", "--[no-]-shutdown-simulators", "Shutdown simulators before adding media. Default #{$shutDownSimulators}") do |b|
$shutDownSimulators = b
end
opts.on("-b", "--[no-]-boot-simulators", "Boot simulators after adding media. Default #{$bootSimulators}") do |b|
$bootSimulators = b
end
opts.on("-l", "--[no-]-leave-existing-media", "Leave existing media. Default #{$leaveExisting ? 'leave' : 'delete'}") do |b|
$leaveExisting = b
end
opts.on("-d", "--[no-]-alter-dates", "Alters the dates of the added media to one each day, starting today, working backwards. Default #{$alterDates}") do |b|
$alterDates = b
end
end.parse!
$newMediaFolder = ARGV[0]
unless $newMediaFolder
puts "First argument must be the path to a folder of media to add to the simulators"
exit
end
simulators = []
JSON.parse(`xcrun simctl list devices --json`)["devices"].each do |runtime, s|
simulators.concat(s) if runtime =~ /iOS/
end
ids = simulators.map { |json| json["udid"] }
def r(s)
return if s.empty?
puts s
end
def shutDownSimulators
return unless $shutDownSimulators
r `xcrun simctl shutdown all`
end
def removeExistingMedia(id)
return if $leaveExisting
r `rm -fr ~/Library/Developer/CoreSimulator/Devices/#{id}/data/Media/DCIM/100APPLE/*`
r `rm -fr ~/Library/Developer/CoreSimulator/Devices/#{id}/data/Media/PhotoData`
end
def bootSimulator(id)
return unless $bootSimulators
r `xcrun simctl boot #{id}`
end
def addNewMedia(id)
r `cp #{File.join($newMediaFolder, "*")} ~/Library/Developer/CoreSimulator/Devices/#{id}/data/Media/DCIM/100APPLE/`
end
class Integer
def minutes
self * 60
end
def hours
minutes * 60
end
def days
hours * 24
end
end
def setNewMediaDates(id)
return unless $alterDates
time = Time.now() - rand(1..3).round.hours
Dir[File.expand_path("~/Library/Developer/CoreSimulator/Devices/#{id}/data/Media/DCIM/100APPLE/*")].entries.each do |p|
r `touch -t #{time.strftime('%Y%m%d%H%M')} #{p}`
time = time - rand(1..3).round.days + rand(1..12).hours
end
end
shutDownSimulators
ids.each do |id|
removeExistingMedia(id)
addNewMedia(id)
setNewMediaDates(id)
bootSimulator(id)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment