Skip to content

Instantly share code, notes, and snippets.

@kreeger
Created March 21, 2013 12:14
Show Gist options
  • Select an option

  • Save kreeger/5212605 to your computer and use it in GitHub Desktop.

Select an option

Save kreeger/5212605 to your computer and use it in GitHub Desktop.
Renamespaces all Obective-C-based files recursively through a directory, including both filenames and inside the file.
#!/usr/bin/env ruby
# ruby renamespace.rb OLD NEW ./dir
# Works with headers, implementations, and plists.
old_prefix, prefix, root = ARGV
def update_file(filename, opts={})
puts "Updating #{filename}."
data = File.open(filename, 'r') { |f| f.read }
data.gsub!(opts[:old], opts[:prefix])
destination = filename.sub(opts[:old], opts[:prefix])
File.open(destination, 'w') { |f| f.write data }
File.delete(filename)
end
def scan_directory(directory, opts={})
puts "Scanning #{directory}."
Dir.entries(directory).each do |file|
next if file =~ /^\./
full_file = File.join(directory, file)
puts "Checking #{full_file}."
scan_directory(full_file, opts) if File.directory?(full_file)
update_file(full_file, opts) if full_file =~ /\.(h|m|plist)$/
end
end
scan_directory(File.absolute_path(root), old: old_prefix, prefix: prefix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment