Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dav/324bfe77021c440fae9f to your computer and use it in GitHub Desktop.
Save dav/324bfe77021c440fae9f to your computer and use it in GitHub Desktop.
A simple ruby script that backs up the original Xcode file templates and then changes the comment block at the top of each new file in Xcode with a different template.
require 'fileutils'
class ModifyXcodeFileTemplateCommentSections
XCODE_TEMPLATE_DIR="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates"
ANNOYING_COMMENT_BLOCK = <<-EOB
//
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//
EOB
PREFERRED_COMMENT_BLOCK = <<-EOP
// ___FILENAME___
EOP
def self.validate_write_access!
unless File.writable? XCODE_TEMPLATE_DIR
puts
puts "You need to run with sudo."
exit
end
end
def self.run
validate_write_access!
backup_tarball = "#{ENV['HOME']}/XcodeFileTemplateOriginals.tgz"
if File.exists? backup_tarball
STDERR.puts "#{backup_tarball} already exists. Aborting."
exit
end
system("tar czvf #{backup_tarball} \"#{XCODE_TEMPLATE_DIR}\"")
puts "Backed up originals to #{backup_tarball}"
puts "Modifying Xcode File Templates..."
Dir.glob "#{XCODE_TEMPLATE_DIR}/**/___FILEBASENAME___.[hm]" do |filename|
puts "#{filename}"
File.open(filename, "r") do |file|
text = file.read
new_text = text.gsub(/#{ANNOYING_COMMENT_BLOCK}/, PREFERRED_COMMENT_BLOCK)
File.open(filename, "w") do |file|
file.write(new_text)
end
end
end
end
end
ModifyXcodeFileTemplateCommentSections.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment