rhtml -> html.erb

手で一個ずつ変更してもいいんですけど、どこかで同じようなことやりそうだなーって思って、スクリプトを書きました。RAILS_ROOT/scriptにrhtml_to_html_erb.rbというファイル名で保存してください。使い方はこんな感じっす。

RAILS_ROOT> ruby script/rhtml_to_html_erb.rb -h
Usage: ruby script/rhtml_to_html_erb.rb [options]

    -p, --pretend                    Run but do not make any changes.
    -s, --ignore_svn                 Don't use svn command even if .svn directory exists.

    -h, --help                       Show this help message.

RAILS_ROOT> 

オプションとしては、-p で何も変更せずに実行。-sで.svnディレクトリがある場合でもsvn mvコマンドを使わずにリネームします。

#!/usr/bin/env ruby
RAILS_ROOT = File.join(File.dirname(__FILE__), '..')

require 'fileutils'
require 'optparse'

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("-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

target_files = Dir.glob('app/**/*.rhtml')
target_files.each do |target_file|
  use_svn = !(runtime_options[:ignore_svn] or !File.exist?(File.join(File.dirname(target_file), '.svn')))
  dest_filename = target_file.gsub(/\.rhtml$/, '.html.erb')
  if use_svn
    cmd_str = "svn mv #{target_file} #{dest_filename}"
    puts cmd_str
    unless runtime_options[:pretend]
      system cmd_str
    end
  else
    FileUtils.mv(target_file, dest_filename, :noop => runtime_options[:pretend], :verbose => true)
  end
end

2009/05/31 git対応してみました

http://d.hatena.ne.jp/akm/20090530#1243706273