Created
May 30, 2009 17:56
-
-
Save akm/120579 to your computer and use it in GitHub Desktop.
rhtml_to_html_erb
This file contains hidden or 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 | |
# -*- coding: utf-8 -*- | |
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) | |
require 'fileutils' | |
require 'optparse' | |
FileUtils.cd(RAILS_ROOT) | |
runtime_options = {} | |
ARGV.options do |opts| | |
script_name = File.basename($0) | |
opts.banner = "Usage: ruby script/#{script_name} [options]" | |
opts.separator "" | |
opts.on("-p", "--pretend", | |
"Run but do not make any changes.") { |v| runtime_options[:pretend] = true } | |
opts.on("-g", "--ignore-git", | |
"Don't use git command even if .git directory exists in ancestors of app/views") { |v| runtime_options[:ignore_git] = true } | |
opts.on("-s", "--ignore-svn", | |
"Don't use svn command even if .svn directory exists.") { |v| runtime_options[:ignore_svn] = true } | |
opts.separator "" | |
opts.on("-h", "--help", "Show this help message.") { puts opts; exit } | |
opts.parse! | |
end | |
MAX_DIR_DEPTH = 100 | |
use_git = false | |
unless runtime_options[:ignore_git] | |
dir = File.expand_path('app/views') | |
depth = 0 | |
while true do | |
parent_dir = File.dirname(dir) | |
break if (depth += 1) > MAX_DIR_DEPTH | |
break if parent_dir.nil? || (parent_dir == dir) | |
dir = parent_dir | |
if File.exist?(File.join(dir, '.git')) | |
use_git = true | |
break | |
end | |
end | |
end | |
target_files = Dir.glob('app/views/**/*.rhtml') | |
target_files.each do |target_file| | |
use_svn = use_git ? false : | |
!runtime_options[:ignore_svn] && !File.exist?(File.join(File.dirname(target_file), '.svn')) | |
dest_filename = File.join(File.dirname(target_file), File.basename(target_file, '.rhtml') << '.html.erb') | |
if use_svn | |
cmd_str = "svn mv #{target_file} #{dest_filename}" | |
puts cmd_str | |
system(cmd_str) unless runtime_options[:pretend] | |
elsif use_git | |
cmd_str = "git mv #{target_file} #{dest_filename}" | |
puts cmd_str | |
system(cmd_str) unless runtime_options[:pretend] | |
else | |
FileUtils.mv(target_file, dest_filename, :noop => runtime_options[:pretend], :verbose => true) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment