Commit e51c3418 by Phil

render Schema Information as RDoc

parent 9c68c8c9
This is a crude hacked version of Annotate, which accomplishes one
task: I renders the Schema Information as RDoc Format, so that a
subsequent `rake doc:app` will include the Schema Information in the
generated documentation.
This version is incompatible with previous Versions of Annotate and
doesn't not come with any helpers to bridge that gap.
== Annotate (aka AnnotateModels) == Annotate (aka AnnotateModels)
Add a comment summarizing the current schema to the top or bottom of each of your... Add a comment summarizing the current schema to the top or bottom of each of your...
......
...@@ -30,17 +30,17 @@ rescue LoadError ...@@ -30,17 +30,17 @@ rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler" puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end end
require 'spec/rake/spectask' # require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec| # Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec' # spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb'] # spec.spec_files = FileList['spec/**/*_spec.rb']
end # end
Spec::Rake::SpecTask.new(:rcov) do |spec| # Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec' # spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb' # spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true # spec.rcov = true
end # end
require 'rake/rdoctask' require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc| Rake::RDocTask.new do |rdoc|
......
--- ---
:major: 2 :major: 3
:minor: 4 :minor: 0
:patch: 0 :patch: 0
# Generated by jeweler # Generated by jeweler
# DO NOT EDIT THIS FILE # DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec` # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{annotate} s.name = %q{annotate}
s.version = "2.4.0" s.version = "3.0.0"
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-10-23} s.date = %q{2010-06-17}
s.default_executable = %q{annotate} 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"]
...@@ -37,13 +37,14 @@ Gem::Specification.new do |s| ...@@ -37,13 +37,14 @@ Gem::Specification.new do |s|
s.homepage = %q{http://github.com/ctran/annotate} s.homepage = %q{http://github.com/ctran/annotate}
s.rdoc_options = ["--charset=UTF-8"] s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5} s.rubyforge_project = %q{annotate}
s.rubygems_version = %q{1.3.6}
s.summary = %q{Annotates Rails Models, routes, fixtures, and others based on the database schema.} s.summary = %q{Annotates Rails Models, routes, fixtures, and others based on the database schema.}
s.test_files = [ s.test_files = [
"spec/annotate/annotate_models_spec.rb", "spec/spec_helper.rb",
"spec/annotate/annotate_models_spec.rb",
"spec/annotate/annotate_routes_spec.rb", "spec/annotate/annotate_routes_spec.rb",
"spec/annotate_spec.rb", "spec/annotate_spec.rb"
"spec/spec_helper.rb"
] ]
if s.respond_to? :specification_version then if s.respond_to? :specification_version then
...@@ -56,3 +57,4 @@ Gem::Specification.new do |s| ...@@ -56,3 +57,4 @@ Gem::Specification.new do |s|
else else
end end
end end
$:.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"
......
module AnnotateModels module AnnotateModels
class << self class << self
# Annotate Models plugin use this header # Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info"
PREFIX = "== Schema Information" PREFIX = "== Schema Information"
END_MARK = "== Schema Information End"
PATTERN = /^##{PREFIX}\n(#.*\n)*#--\n##{END_MARK}\n#\+\+\n/
FIXTURE_DIRS = ["test/fixtures","spec/fixtures"] FIXTURE_DIRS = ["test/fixtures","spec/fixtures"]
# File.join for windows reverse bar compat? # File.join for windows reverse bar compat?
...@@ -42,10 +44,10 @@ module AnnotateModels ...@@ -42,10 +44,10 @@ module AnnotateModels
# each column. The line contains the column name, # each column. The line contains the column name,
# the type (and length), and any optional attributes # the type (and length), and any optional attributes
def get_schema_info(klass, header, options = {}) def get_schema_info(klass, header, options = {})
info = "# #{header}\n#\n" info = "##{header}\n#\n"
info << "# Table name: #{klass.table_name}\n#\n" info << "#Table name: #{klass.table_name}\n#\n"
max_size = klass.column_names.collect{|name| name.size}.max + 1 max_size = klass.column_names.collect{|name| name.size}.max + 5
klass.columns.each do |col| klass.columns.each do |col|
attrs = [] attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil? attrs << "default(#{quote(col.default)})" unless col.default.nil?
...@@ -77,14 +79,17 @@ module AnnotateModels ...@@ -77,14 +79,17 @@ module AnnotateModels
end end
end end
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n" a = ([col_type] + attrs).join(", ")
line = sprintf("#%-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col.name}*::", a).rstrip + "\n"
# puts line
info << line
end end
if options[:show_indexes] if options[:show_indexes]
info << get_index_info(klass) info << get_index_info(klass)
end end
info << "#\n\n" info << "#--\n#== Schema Information End\n#++\n"
end end
def get_index_info(klass) def get_index_info(klass)
...@@ -119,15 +124,18 @@ module AnnotateModels ...@@ -119,15 +124,18 @@ 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(#.*\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
# puts "old_header: #{old_header}"
# puts "new_header: #{new_header}"
if old_header == new_header if old_header == new_header
false false
else else
# Remove old schema info # Remove old schema info
old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '') old_content.sub!(PATTERN, '')
# Write it back # Write it back
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" + info_block)
...@@ -142,7 +150,7 @@ module AnnotateModels ...@@ -142,7 +150,7 @@ module AnnotateModels
if File.exist?(file_name) if File.exist?(file_name)
content = File.read(file_name) content = File.read(file_name)
content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '') content.sub!(PATTERN, '')
File.open(file_name, "wb") { |f| f.puts content } File.open(file_name, "wb") { |f| f.puts content }
end end
...@@ -263,6 +271,7 @@ module AnnotateModels ...@@ -263,6 +271,7 @@ module AnnotateModels
annotated = [] annotated = []
get_model_files.each do |file| get_model_files.each do |file|
# puts "---------------- file: #{file}"
begin begin
klass = get_model_class(file) klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class? if klass < ActiveRecord::Base && !klass.abstract_class?
......
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