Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

got generator working

  • Loading branch information...
commit 33d308bd26df0bc4911c92a7b64dfc9d039664be 1 parent 7c908f6
@orangewolf orangewolf authored
View
2  Rakefile
@@ -12,6 +12,8 @@ begin
gem.authors = ["Patrick Crowley and Rob Kaufman"]
gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_development_dependency "yard", ">= 0"
+ gem.files = FileList["[A-Z]*", "{generators,lib,rails}/**/*"]
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
View
1  generators/truck/USAGE
@@ -0,0 +1 @@
+script/generate truck
View
56 generators/truck/lib/insert_commands.rb
@@ -0,0 +1,56 @@
+# Mostly pinched from http://github.com/ryanb/nifty-generators/tree/master
+
+Rails::Generator::Commands::Base.class_eval do
+ def file_contains?(relative_destination, line)
+ File.read(destination_path(relative_destination)).include?(line)
+ end
+end
+
+Rails::Generator::Commands::Create.class_eval do
+ def insert_before(file, line, stop='^(class|module) .+$')
+ logger.insert "#{line} into #{file}"
+ unless options[:pretend] || file_contains?(file, line)
+ gsub_file file, /^#{stop}/ do |match|
+ "#{line}\n#{match}"
+ end
+ end
+ end
+
+ def insert_after(file, line, stop='(class|module)')
+ logger.insert "#{line} into #{file}"
+ unless options[:pretend] || file_contains?(file, line)
+ gsub_file file, /#{stop}/ do |match|
+ "#{match}\n #{line}"
+ end
+ end
+ end
+
+end
+
+Rails::Generator::Commands::Destroy.class_eval do
+ def insert_before(file, line, stop='')
+ logger.remove "#{line} from #{file}"
+ unless options[:pretend]
+ gsub_file file, "\n #{line}", ''
+ end
+ end
+
+ def insert_after(file, line, stop='')
+ logger.remove "#{line} from #{file}"
+ unless options[:pretend]
+ gsub_file file, "\n #{line}", ''
+ end
+ end
+
+end
+
+Rails::Generator::Commands::List.class_eval do
+ def insert_before(file, line, stop='')
+ logger.insert "#{line} into #{file}"
+ end
+
+ def insert_after(file, line, stop='')
+ logger.insert "#{line} into #{file}"
+ end
+
+end
View
5 generators/truck/templates/legacy_adapter.erb
@@ -1,5 +0,0 @@
-legacy:
- adapter: mysql
- database: <%= RAILS_ROOT.split('/').last %>_legacy
- username:
- password:
View
0  generators/truck/templates/legacy_base.erb → generators/truck/templates/legacy_base.rb
File renamed without changes
View
4 generators/truck/templates/legacy_model.erb
@@ -1,5 +1,5 @@
-class Legacy<%= model_name %> < LegacyBase
- set_table_name "<%= model_name %>"
+class Legacy<%= model_name.classify %> < LegacyBase
+ set_table_name "<%= model_name.pluralize %>"
def map
{
View
6 generators/truck/templates/legacy_task.erb
@@ -2,9 +2,9 @@ namespace :db do
namespace :migrate do
<% legacy_models.each do |model_name| %>
- desc 'Migrates <%= pluralize(model_name).downcase %>'
- task :<%= pluralize(model_name).downcase %> => :environment do
- migrate :<%= pluralize(model_name).downcase %>
+ desc 'Migrates <%= model_name.pluralize.downcase %>'
+ task :<%= model_name.pluralize.downcase %> => :environment do
+ migrate :<%= model_name.pluralize.downcase %>
end
<% end %>
View
11 generators/truck/truck_generate.rb
@@ -1,11 +0,0 @@
-class TruckGenerator < Rails::Generator::Base
-
- def manifest
- record do |m|
-
-
-
- end
- end
-
-end
View
31 generators/truck/truck_generator.rb
@@ -0,0 +1,31 @@
+require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
+
+class TruckGenerator < Rails::Generator::Base
+
+ def manifest
+ record do |m|
+ @legacy_models = Dir.glob(RAILS_ROOT + '/app/models/*.rb').collect { |model_path| File.basename(model_path).gsub('.rb', '') }
+
+ m.directory 'app/models/legacy'
+ m.file 'legacy_base.rb', 'app/models/legacy/legacy_base.rb'
+
+ @legacy_models.each do |model_name|
+ m.template 'legacy_model.erb', "app/models/legacy/#{model_name.downcase}.rb", :assigns => { :model_name => model_name }
+ end
+
+ m.directory 'lib/tasks'
+ m.template 'legacy_task.erb', 'lib/tasks/legacy.rake', :assigns => { :legacy_models => @legacy_models }
+ snippet = <<EOS
+legacy:
+ adapter: mysql
+ database: #{RAILS_ROOT.split('/').last}_legacy
+ username:
+ password:
+EOS
+
+ m.insert_before "config/database.yml", snippet, "production:"
+
+ end
+ end
+
+end
View
23 trucker.gemspec
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Patrick Crowley and Rob Kaufman"]
- s.date = %q{2010-07-10}
+ s.date = %q{2010-07-14}
s.description = %q{Trucker is a gem for migrating legacy data into a Rails app}
s.email = %q{patrick@mokolabs.com}
s.extra_rdoc_files = [
@@ -17,22 +17,25 @@ Gem::Specification.new do |s|
"README.rdoc"
]
s.files = [
- ".document",
- ".gitignore",
- "BACKGROUND.markdown",
+ "BACKGROUND.markdown",
"INSTALL.markdown",
"LICENSE",
"README.rdoc",
"Rakefile",
- "lib/trucker.rb",
- "spec/spec.opts",
- "spec/spec_helper.rb",
- "spec/trucker_spec.rb"
+ "VERSION.yml",
+ "generators/truck/USAGE",
+ "generators/truck/lib/insert_commands.rb",
+ "generators/truck/templates/legacy_base.rb",
+ "generators/truck/templates/legacy_initializer.erb",
+ "generators/truck/templates/legacy_model.erb",
+ "generators/truck/templates/legacy_task.erb",
+ "generators/truck/truck_generator.rb",
+ "lib/trucker.rb"
]
s.homepage = %q{http://github.com/mokolabs/trucker}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
- s.rubygems_version = %q{1.3.7}
+ s.rubygems_version = %q{1.3.6}
s.summary = %q{Bring your legacy along}
s.test_files = [
"spec/spec_helper.rb",
@@ -43,7 +46,7 @@ Gem::Specification.new do |s|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
s.add_development_dependency(%q<yard>, [">= 0"])
else
Please sign in to comment.
Something went wrong with that request. Please try again.