Updating Xcode plug-ins

:  ~ 2 min read

I recently saw Joe's post about updating Xcode plug-ins, but since I'm really lazy, updating them one by one didn't suffice, so here's my take on it. In case you want to skip straight to the desert, here's the gist.

First, we change our script's directory:

# You might need these.
# require 'pathname'
# require 'fileutils'

# In case we want to also print the already updated plug-ins
DISPLAY_ALREADY_UPDATED = false

plugin_path = File.expand_path('~/Library/Application Support/Developer/Shared/Xcode/Plug-ins')

Dir.chdir(plugin_path)
files =  Dir['*.xcplugin']

Then we iterate through the files:

files.each do |plugin|
  # Lazy, lazy :)
  xcode_uuid_key    = 'DVTPlugInCompatibilityUUID'
  plugin_uuids_key  = xcode_uuid_key + 's'
  # This path needs to escape the backslash, so we can't reuse the one above :(
  plugin_plist      = "~/Library/Application\\ Support/Developer/Shared/Xcode/Plug-ins/#{plugin}/Contents/Info.plist"
  plugin_uuids      = `defaults read #{plugin_plist} #{plugin_uuids_key}`
  latest_xcode_uuid = `defaults read /Applications/Xcode.app/Contents/Info #{xcode_uuid_key}`.gsub! "\n", ''
  
  # If the value is already there, skip and optionally notify
  if plugin_uuids.include? latest_xcode_uuid
	uptodate_plugins += 1
    puts "#{green}#{plugin} is already up to date." if DISPLAY_ALREADY_UPDATED
    next
  end
  
  puts "#{yellow}Updating #{plugin}..."
  
  system "defaults write #{plugin_plist} #{plugin_uuids_key} -array-add #{latest_xcode_uuid}"
  
  updated_plugins += 1
end

Then, at the end, the finishing touches:

# For a bit of flavor
red    = "\e[31m"
green  = "\e[32m"
yellow = "\e[33m"
white  = "\e[0m"

if updated_plugins == 0
  if uptodate_plugins == 1
    puts "\n#{green}You have only one pluging, and it was already up to date.#{white}"
  else
    puts "\n#{green}All #{uptodate_plugins} plugins were already up to date.#{white}"
  end
elsif uptodate_plugins == 0
  if updated_plugins == 1
    puts "\n#{green}You have only one plugin, and it has been updated."
  else
    puts "\n#{green}All #{updated_plugins} plugins have been updated."
  end
elsif updated_plugins == 1 && uptodate_plugins == 1
  puts "\n#{green}#{updated_plugins} plugin has been updated, #{uptodate_plugins} plugin was already up to date.#{white}"
elsif updated_plugins == 1
  puts "\n#{green}#{updated_plugins} plugin has been updated, #{uptodate_plugins} plugins were already up to date.#{white}"
elsif uptodate_plugins == 1
  puts "\n#{green}#{updated_plugins} plugins have been updated, #{uptodate_plugins} plugin was already up to date.#{white}"
else
  puts "\n#{green}#{updated_plugins} plugins have been updated, #{uptodate_plugins} plugins were already up to date.#{white}"
end

Thanks for the inspiration, Joe!