Created
January 23, 2009 12:23
-
-
Save h-lame/50997 to your computer and use it in GitHub Desktop.
Script for updating view files to the new <name>.<output_format>.<renderer> naming scheme of Rails 2.x (useful for upgrading 1.x apps).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
options = { :use_svn => true } | |
OptionParser.new do |opt| | |
opt.banner = "Usage: upgrade_views [options]" | |
opt.on('-f', '--file_rename', 'Use file renaming rather than svn move to update the files.') { |v| options[:use_svn] = false } | |
opt.parse!(ARGV) | |
end | |
def do_file_rename_with_filesystem(old_file_parts, new_file_parts) | |
File.rename(old_file_parts.join('.'), new_file_parts.join('.')) | |
end | |
def do_file_rename_with_svn(old_file_parts, new_file_parts) | |
`svn move #{old_file_parts.join('.')} #{new_file_parts.join('.')}` | |
end | |
if options[:use_svn] | |
alias :do_file_rename :do_file_rename_with_svn | |
else | |
alias :do_file_rename :do_file_rename_with_filesystem | |
end | |
def move_view_file(view_file) | |
puts "..processing: #{Dir.pwd}/#{view_file}" | |
bits = view_file.split('.') | |
the_ext = bits.last | |
case the_ext | |
when /^rhtml$/ | |
do_file_rename(bits, [bits[0..-2], 'html.erb'].flatten) | |
when /^rjs$/ | |
do_file_rename(bits, [bits[0..-2], 'js.rjs'].flatten) | |
when /^rxls$/ | |
do_file_rename(bits, [bits[0..-2], 'xls.rxls'].flatten) | |
else | |
puts "Oh! I don't know what to do to!" | |
end | |
end | |
def walk_dir(dir) | |
puts "Entering #{Dir.pwd}/#{dir}" | |
Dir.chdir(dir) do | |
Dir['*'].each do |file| | |
if File.directory?(file) | |
walk_dir(file) | |
else | |
move_view_file(file) | |
end | |
end | |
end | |
end | |
walk_dir('app/views') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment