Permalink
Browse files

* results of script/generate plugin script-refactor

* move main part of the first version of script/refactor into plugin's tree
  • Loading branch information...
0 parents commit c9dd0627b609a619b5fb45282125d05c68e1e633 @hiroshi committed Jan 27, 2009
Showing with 177 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +13 −0 README
  3. +23 −0 Rakefile
  4. +1 −0 init.rb
  5. +1 −0 install.rb
  6. +103 −0 lib/script_refactor.rb
  7. +4 −0 tasks/script_refactor_tasks.rake
  8. +8 −0 test/script_refactor_test.rb
  9. +3 −0 test/test_helper.rb
  10. +1 −0 uninstall.rb
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 README
@@ -0,0 +1,13 @@
+Script-refactor
+===============
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2009 [name of plugin creator], released under the MIT license
23 Rakefile
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the script_refactor plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the script_refactor plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Script-refactor'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1 init.rb
@@ -0,0 +1 @@
+# Include hook code here
1 install.rb
@@ -0,0 +1 @@
+# Install hook code here
103 lib/script_refactor.rb
@@ -0,0 +1,103 @@
+=begin
+TODO:
+* Please refactor me
+* Ignore db directory and create migration?
+=end
+
+# usage
+if ARGV.size != 3
+ puts <<-USAGE
+Refactoring application.
+* Renaming files with git, svn aware manner
+* Replacing class names, variable names as possible
+
+THIS SCRIPT MAY DESTRACT FILES AND/OR DIRECTORIES. BE SURE YOUR CHANGES ARE COMMITED OR BACKED UP!
+
+Usage:
+ #{$0} type from to
+
+Possible types:
+ resource: Replace resouce name, including file names.
+
+Examples:
+ #{$0} resource user person
+
+ USAGE
+ exit
+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
+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:"
+case scm
+when :git
+ puts " git reset --hard"
+when :svn
+ puts " svn revert -R ."
+end
4 tasks/script_refactor_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :script_refactor do
+# # Task goes here
+# end
8 test/script_refactor_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class Script-refactorTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
3 test/test_helper.rb
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
1 uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit c9dd062

Please sign in to comment.