Permalink
Browse files

Merge branch 'master' of git://github.com/tjsheehy/script-refactor

Conflicts:
	README
  • Loading branch information...
2 parents 1789900 + abb7cf5 commit 430e9f070cf0bccc57bd2433be175cb9f82a5f56 @hiroshi committed Apr 2, 2009
View
@@ -2,7 +2,7 @@ script/refactor
===============
This plugin provides a simple script. It helps you refactor rails applications.
Say, you have 'user' resource, and you want to change the name of the resource to 'person',
-the script replaces name of files, directories, constants, variables and so force.
+the script replaces name of files, directories, constants, variables and so forth.
== INSTALL
First of all, if you don't install this via:
@@ -0,0 +1,45 @@
+require 'active_support'
+
+class MigrationGenerator
+
+ @rails_root
+ def initialize(rails_root)
+ @rails_root = rails_root
+ end
+
+ def generate_rename_table_migration(old_class_name, new_class_name)
+
+ migration_content = File.read(File.dirname(__FILE__) + '/rename_migration_template.rb')
+
+ to_replace = {
+ 'migration_name' => migration_name(old_class_name, new_class_name),
+ 'old_table_name' => table_name(old_class_name),
+ 'new_table_name' => table_name(new_class_name)
+ }
+ to_replace.each do |key, value|
+ migration_content.gsub!("{#{key}}", value)
+ end
+
+ file_name = "#{@rails_root}/db/migrate/#{Time.now.strftime('%Y%m%d%H%M%S')}_#{to_replace['migration_name'].underscore}.rb"
+ open(file_name, "w") do |out|
+ out.print migration_content
+ end
+ return File.new(file_name)
+ end
+
+ private
+
+ def migration_name(old_class_name, new_class_name)
+ "Rename#{remove_namepsace_delim(old_class_name)}To#{remove_namepsace_delim(new_class_name)}"
+ end
+
+ def remove_namepsace_delim(value)
+ value.sub('::', '')
+ end
+
+ def table_name(value)
+ value.gsub(/.*::/, '').underscore.pluralize
+ end
+
+
+end
@@ -0,0 +1,9 @@
+class {migration_name} < ActiveRecord::Migration
+ def self.up
+ rename_table :{old_table_name}, :{new_table_name}
+ end
+
+ def self.down
+ rename_table :{new_table_name}, :{old_table_name}
+ end
+end
@@ -0,0 +1,96 @@
+require "find"
+require "rubygems"
+require "active_support"
+
+require File.dirname(__FILE__) + '/migration_generator'
+
+class RenameRefactoring
+
+ def initialize(rails_root, from, to)
+ @rails_root = rails_root
+ @from = from
+ @to = to
+ end
+
+ def apply
+ # scm
+ case
+ when File.directory?(".git")
+ scm = :git
+ def rename_cmd(src, dst); "git mv #{src} #{dst}"; end
+ when File.directory?(".svn")
+ scm = :svn
+ def rename_cmd(src, dst); "svn mv #{src} #{dst}"; end
+ else
+ def rename_cmd(src, dst); "mv #{src} #{dst}"; end
+ end
+
+ renames = {
+ "test/unit/#{@from}_test.rb" => "test/unit/#{@to}_test.rb",
+ "test/functional/#{@from.pluralize}_controller_test.rb" => "test/functional/#{@to.pluralize}_controller_test.rb",
+ "test/fixtures/#{@from.pluralize}.yml" => "test/fixtures/#{@to.pluralize}.yml",
+ "app/views/#{@from.pluralize}" => "app/views/#{@to.pluralize}",
+ "app/models/#{@from}.rb" => "app/models/#{@to}.rb",
+ "app/helpers/#{@from.pluralize}_helper.rb" => "app/helpers/#{@to.pluralize}_helper.rb",
+ "app/controllers/#{@from.pluralize}_controller.rb" => "app/controllers/#{@to.pluralize}_controller.rb",
+ }
+
+ puts "Renaming files and directories:"
+ renames.each do |src, dst|
+ if File.exist? src
+ cmd = rename_cmd(src, dst)
+ puts cmd
+ `#{cmd}`
+ end
+ end
+
+ puts "\nReplacing class and variables:"
+ replaces = {
+ @from => @to,
+ @from.classify => @to.classify,
+ @from.pluralize => @to.pluralize,
+ @from.classify.pluralize => @to.classify.pluralize,
+ }
+ replaces.each do |f,t|
+ puts "#{f} -> #{t}"
+ end
+ pattern = "(\\b|_)(#{replaces.keys.join("|")})(\\b|[_A-Z])"
+ puts "pattern: /#{pattern}/"
+
+ Find.find(".") do |path|
+ Find.prune if path =~ /\/(vendor|log|script|tmp|migrate|\.(git|svn))$/
+
+ if File.file? path
+ content = File.read(path)
+ # print replacing lines
+ content.each_with_index do |line, idx|
+ line.scan(/#{pattern}/).each do
+ puts "#{path}:#{idx+1}: #{line}"
+ end
+ end
+
+ replaced = content.gsub!(/#{pattern}/){ "#{$1}#{replaces[$2]}#{$3}"}
+ unless replaced.nil?
+ # replace file content
+ open(path, "w") do |out|
+ out.print content
+ end
+ end
+ end
+ end
+
+ puts 'generating rename migration'
+ migraton_generator = MigrationGenerator.new(@rails_root)
+ migraton_generator.generate_rename_table_migration(@from.camelize, @to.camelize)
+
+ puts "\nNOTE: If you want to revert them:" if scm
+ case scm
+ when :git
+ puts " git reset --hard"
+ when :svn
+ puts " svn revert -R ."
+ end
+
+ end
+
+end
@@ -1,7 +1,8 @@
+require File.dirname(__FILE__) + '/rename_refactoring'
+
=begin
TODO:
* Please refactor me
-* Ignore db directory and create migration?
=end
module Refactor
VERSION = "0.1"
@@ -34,80 +35,7 @@ module Refactor
exit 1
end
-# main
-require "find"
-require "rubygems"
-require "activesupport"
-
# arguments
type, from, to = ARGV
-# scm
-case
-when File.directory?(".git")
- scm = :git
- def rename_cmd(src, dst); "git mv #{src} #{dst}"; end
-when File.directory?(".svn")
- scm = :svn
- def rename_cmd(src, dst); "svn mv #{src} #{dst}"; end
-else
- def rename_cmd(src, dst); "mv #{src} #{dst}"; end
-end
-
-renames = {
- "test/unit/#{from}_test.rb" => "test/unit/#{to}_test.rb",
- "test/functional/#{from.pluralize}_controller_test.rb" => "test/functional/#{to.pluralize}_controller_test.rb",
- "test/fixtures/#{from.pluralize}.yml" => "test/fixtures/#{to.pluralize}.yml",
- "app/views/#{from.pluralize}" => "app/views/#{to.pluralize}",
- "app/models/#{from}.rb" => "app/models/#{to}.rb",
- "app/helpers/#{from.pluralize}_helper.rb" => "app/helpers/#{to.pluralize}_helper.rb",
- "app/controllers/#{from.pluralize}_controller.rb" => "app/controllers/#{to.pluralize}_controller.rb",
-}
-
-puts "Renamming files and directories:"
-renames.each do |src, dst|
- cmd = rename_cmd(src, dst)
- if File.exist? src
- puts cmd
- `#{cmd}`
- end
-end
-
-puts "\nReplacing class and variables:"
-replaces = {
- from => to,
- from.classify => to.classify,
- from.pluralize => to.pluralize,
- from.classify.pluralize => to.classify.pluralize,
-}
-replaces.each do |f,t|
- puts "#{f} -> #{t}"
-end
-pattern = "(\\b|_)(#{replaces.keys.join("|")})(\\b|[_A-Z])"
-puts "pettern: /#{pattern}/"
-puts ""
-
-Find.find(".") do |path|
- Find.prune if path =~ /\/(vendor|log|script|tmp|\.(git|svn))$/
-
- if File.file? path
- input = File.read(path)
- # print replacing lines
- input.each_with_index do |line, idx|
- line.scan(/#{pattern}/).each do
- puts "#{path}:#{idx+1}: #{line}"
- end
- end
- # replace file content
- open(path, "w") do |out|
- out.print input.gsub(/#{pattern}/){ "#{$1}#{replaces[$2]}#{$3}"}
- end
- end
-end
-
-puts "\nNOTE: If you want to revert them:" if scm
-case scm
-when :git
- puts " git reset --hard"
-when :svn
- puts " svn revert -R ."
-end
+rename = RenameRefactoring.new(File.expand_path('.'), from, to)
+rename.apply
@@ -0,0 +1,50 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe MigrationGenerator do
+
+ before(:each) do
+ @rails_root = File.expand_path(File.dirname(__FILE__) + '/../tmp')
+ setup_temp_migration_folder(@rails_root)
+ @generator = MigrationGenerator.new(@rails_root)
+ @migration = @generator.generate_rename_table_migration('BlogPost', 'WeblogEntry')
+ end
+
+ it "should save the migration to RAILS_ROOT/db/migrate" do
+ File.dirname(@migration.path).should == @rails_root + '/db/migrate'
+ end
+
+ it "should generae a filename of the format yyyymmddhhmmss_rename_blog_post_to_weblog_entry.rb" do
+ File.basename(@migration.path).should =~ /^\d{14}_rename_blog_post_to_weblog_entry.rb$/
+ end
+
+
+ it "should generate a migration file to rename the models tables" do
+ @migration.read.should == <<-eos
+class RenameBlogPostToWeblogEntry < ActiveRecord::Migration
+ def self.up
+ rename_table :blog_posts, :weblog_entries
+ end
+
+ def self.down
+ rename_table :weblog_entries, :blog_posts
+ end
+end
+ eos
+ end
+
+ it "should handle namespaces in class names" do
+ migration = @generator.generate_rename_table_migration('Foo::BlogPost', 'Bar::WeblogEntry')
+ migration.read.should == <<-eos
+class RenameFooBlogPostToBarWeblogEntry < ActiveRecord::Migration
+ def self.up
+ rename_table :blog_posts, :weblog_entries
+ end
+
+ def self.down
+ rename_table :weblog_entries, :blog_posts
+ end
+end
+ eos
+ end
+
+end
@@ -0,0 +1,2 @@
+require '../lib/util'
+
View
@@ -0,0 +1,6 @@
+require File.dirname(__FILE__) + '/../lib/migration_generator'
+
+def setup_temp_migration_folder(rails_root)
+ FileUtils.rm_rf(rails_root) if File.exists?(rails_root)
+ FileUtils.mkdir_p(rails_root + '/db/migrate')
+end

0 comments on commit 430e9f0

Please sign in to comment.