Commit 85f6e536 by Alex Chaffee

Merge branch 'master' of github.com:ctran/annotate_models

Conflicts: annotate.gemspec
parents e356e95f ddfba1f7
...@@ -9,8 +9,6 @@ Gem::Specification.new do |s| ...@@ -9,8 +9,6 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Cuong Tran", "Alex Chaffee", "Marcos Piccinini"] s.authors = ["Cuong Tran", "Alex Chaffee", "Marcos Piccinini"]
s.date = %q{2009-12-13}
s.default_executable = %q{annotate}
s.description = %q{Annotates Rails Models, routes, fixtures, and others based on the database schema.} s.description = %q{Annotates Rails Models, routes, fixtures, and others based on the database schema.}
s.email = ["alex@stinky.com", "ctran@pragmaquest.com", "x@nofxx.com"] s.email = ["alex@stinky.com", "ctran@pragmaquest.com", "x@nofxx.com"]
s.executables = ["annotate"] s.executables = ["annotate"]
...@@ -34,7 +32,7 @@ Gem::Specification.new do |s| ...@@ -34,7 +32,7 @@ Gem::Specification.new do |s|
"spec/spec_helper.rb", "spec/spec_helper.rb",
"tasks/migrate.rake" "tasks/migrate.rake"
] ]
s.homepage = %q{http://github.com/ctran/annotate} s.homepage = %q{http://github.com/ctran/annotate_models}
s.rdoc_options = ["--charset=UTF-8"] s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.rubyforge_project = %q{annotate} s.rubyforge_project = %q{annotate}
...@@ -46,6 +44,10 @@ Gem::Specification.new do |s| ...@@ -46,6 +44,10 @@ Gem::Specification.new do |s|
"spec/annotate_spec.rb", "spec/annotate_spec.rb",
"spec/spec_helper.rb" "spec/spec_helper.rb"
] ]
s.add_runtime_dependency('rake')
s.add_development_dependency('jeweler')
s.add_development_dependency('rspec', '~> 1.3.2')
s.add_development_dependency('activesupport', '>= 2.1.0')
if s.respond_to? :specification_version then if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
......
$:.unshift(File.dirname(__FILE__)) unless $:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
require 'yaml'
module Annotate module Annotate
def self.version def self.version
version_file = File.dirname(__FILE__) + "/../VERSION.yml" version_file = File.dirname(__FILE__) + "/../VERSION.yml"
...@@ -14,6 +16,7 @@ module Annotate ...@@ -14,6 +16,7 @@ module Annotate
def self.load_tasks def self.load_tasks
if File.exists?('Rakefile') if File.exists?('Rakefile')
require 'rake'
load 'Rakefile' load 'Rakefile'
Dir[File.join(File.dirname(__FILE__), 'tasks', '**/*.rake')].each { |rake| load rake } Dir[File.join(File.dirname(__FILE__), 'tasks', '**/*.rake')].each { |rake| load rake }
return true return true
......
...@@ -119,18 +119,24 @@ module AnnotateModels ...@@ -119,18 +119,24 @@ module AnnotateModels
old_content = File.read(file_name) old_content = File.read(file_name)
# Ignore the Schema version line because it changes with each migration # Ignore the Schema version line because it changes with each migration
header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/) header = Regexp.new(/(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?\n)/)
old_header = old_content.match(header).to_s old_header = old_content.match(header).to_s
new_header = info_block.match(header).to_s new_header = info_block.match(header).to_s
if old_header == new_header old_columns = old_header && old_header.scan(/#[\t\s]+([\w\d]+)[\t\s]+\:([\d\w]+)/).sort
new_columns = new_header && new_header.scan(/#[\t\s]+([\w\d]+)[\t\s]+\:([\d\w]+)/).sort
if old_columns == new_columns
false false
else else
# Remove old schema info # Replace the old schema info with the new schema info
old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '') new_content = old_content.sub(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, info_block)
# But, if there *was* no old schema info, we simply need to insert it
# Write it back if new_content == old_content
new_content = options[:position] == 'before' ? (info_block + old_content) : (old_content + "\n" + info_block) new_content = options[:position] == 'before' ?
(info_block + old_content) :
((old_content =~ /\n$/ ? old_content : old_content + '\n') + info_block)
end
File.open(file_name, "wb") { |f| f.puts new_content } File.open(file_name, "wb") { |f| f.puts new_content }
true true
...@@ -228,7 +234,7 @@ module AnnotateModels ...@@ -228,7 +234,7 @@ module AnnotateModels
# in subdirectories without namespacing. # in subdirectories without namespacing.
def get_model_class(file) def get_model_class(file)
require File.expand_path("#{model_dir}/#{file}") # this is for non-rails projects, which don't get Rails auto-require magic require File.expand_path("#{model_dir}/#{file}") # this is for non-rails projects, which don't get Rails auto-require magic
model = file.gsub(/\.rb$/, '').camelize model = ActiveSupport::Inflector.camelize(file.gsub(/\.rb$/, ''))
parts = model.split('::') parts = model.split('::')
begin begin
parts.inject(Object) {|klass, part| klass.const_get(part) } parts.inject(Object) {|klass, part| klass.const_get(part) }
......
desc "Add schema information (as comments) to model and fixture files" desc "Add schema information (as comments) to model and fixture files"
task :annotate_models => :environment do task :annotate_models => :environment do
require 'annotate/annotate_models' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'annotate', 'annotate_models'))
options={} options={}
options[:position_in_class] = ENV['position_in_class'] || ENV['position'] || :before options[:position_in_class] = ENV['position_in_class'] || ENV['position'] || :before
options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] || :before options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] || :before
...@@ -14,7 +14,7 @@ end ...@@ -14,7 +14,7 @@ end
desc "Remove schema information from model and fixture files" desc "Remove schema information from model and fixture files"
task :remove_annotation => :environment do task :remove_annotation => :environment do
require 'annotate/annotate_models' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'annotate', 'annotate_models'))
options={} options={}
options[:model_dir] = ENV['model_dir'] options[:model_dir] = ENV['model_dir']
AnnotateModels.remove_annotations(options) AnnotateModels.remove_annotations(options)
......
desc "Prepends the route map to the top of routes.rb" desc "Prepends the route map to the top of routes.rb"
task :annotate_routes do task :annotate_routes do
require 'annotate/annotate_routes' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'annotate', 'annotate_routes'))
AnnotateRoutes.do_annotate AnnotateRoutes.do_annotate
end end
require File.dirname(__FILE__) + '/../spec_helper.rb' require File.dirname(__FILE__) + '/../spec_helper.rb'
require 'annotate/annotate_models' require 'annotate/annotate_models'
require 'rubygems' require 'rubygems'
require 'activesupport' require 'active_support'
describe AnnotateModels do describe AnnotateModels do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment