From e51c3418ca67fc4e833d31db52f440f8d0e0d3b5 Mon Sep 17 00:00:00 2001 From: Phil Date: Tue, 22 Jun 2010 14:41:47 +0200 Subject: [PATCH] render Schema Information as RDoc --- README.rdoc | 9 +++++++++ Rakefile | 20 ++++++++++---------- VERSION.yml | 4 ++-- annotate.gemspec | 18 ++++++++++-------- lib/annotate.rb | 2 ++ lib/annotate/annotate_models.rb | 27 ++++++++++++++++++--------- 6 files changed, 51 insertions(+), 29 deletions(-) diff --git a/README.rdoc b/README.rdoc index e9bf79039..a98db42cf 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,3 +1,12 @@ +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) Add a comment summarizing the current schema to the top or bottom of each of your... diff --git a/Rakefile b/Rakefile index a5244a199..d8a6dfb10 100644 --- a/Rakefile +++ b/Rakefile @@ -30,17 +30,17 @@ rescue LoadError puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler" end -require 'spec/rake/spectask' -Spec::Rake::SpecTask.new(:spec) do |spec| - spec.libs << 'lib' << 'spec' - spec.spec_files = FileList['spec/**/*_spec.rb'] -end +# require 'spec/rake/spectask' +# Spec::Rake::SpecTask.new(:spec) do |spec| +# spec.libs << 'lib' << 'spec' +# spec.spec_files = FileList['spec/**/*_spec.rb'] +# end -Spec::Rake::SpecTask.new(:rcov) do |spec| - spec.libs << 'lib' << 'spec' - spec.pattern = 'spec/**/*_spec.rb' - spec.rcov = true -end +# Spec::Rake::SpecTask.new(:rcov) do |spec| +# spec.libs << 'lib' << 'spec' +# spec.pattern = 'spec/**/*_spec.rb' +# spec.rcov = true +# end require 'rake/rdoctask' Rake::RDocTask.new do |rdoc| diff --git a/VERSION.yml b/VERSION.yml index 7200e300c..4a7aafe83 100644 --- a/VERSION.yml +++ b/VERSION.yml @@ -1,4 +1,4 @@ --- - :major: 2 - :minor: 4 + :major: 3 + :minor: 0 :patch: 0 diff --git a/annotate.gemspec b/annotate.gemspec index c3b182290..1a449b218 100644 --- a/annotate.gemspec +++ b/annotate.gemspec @@ -1,15 +1,15 @@ # Generated by jeweler -# DO NOT EDIT THIS FILE -# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec` +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command # -*- encoding: utf-8 -*- Gem::Specification.new do |s| 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.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.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"] @@ -37,13 +37,14 @@ Gem::Specification.new do |s| s.homepage = %q{http://github.com/ctran/annotate} s.rdoc_options = ["--charset=UTF-8"] 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.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_spec.rb", - "spec/spec_helper.rb" + "spec/annotate_spec.rb" ] if s.respond_to? :specification_version then @@ -56,3 +57,4 @@ Gem::Specification.new do |s| else end end + diff --git a/lib/annotate.rb b/lib/annotate.rb index 32284ec29..a7d11b81c 100644 --- a/lib/annotate.rb +++ b/lib/annotate.rb @@ -1,6 +1,8 @@ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) +require 'yaml' + module Annotate def self.version version_file = File.dirname(__FILE__) + "/../VERSION.yml" diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index fc863320b..e311ac75c 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -1,8 +1,10 @@ module AnnotateModels class << self # Annotate Models plugin use this header - COMPAT_PREFIX = "== Schema Info" + PREFIX = "== Schema Information" + END_MARK = "== Schema Information End" + PATTERN = /^##{PREFIX}\n(#.*\n)*#--\n##{END_MARK}\n#\+\+\n/ FIXTURE_DIRS = ["test/fixtures","spec/fixtures"] # File.join for windows reverse bar compat? @@ -42,10 +44,10 @@ def quote(value) # each column. The line contains the column name, # the type (and length), and any optional attributes def get_schema_info(klass, header, options = {}) - info = "# #{header}\n#\n" - info << "# Table name: #{klass.table_name}\n#\n" + info = "##{header}\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| attrs = [] attrs << "default(#{quote(col.default)})" unless col.default.nil? @@ -77,14 +79,17 @@ def get_schema_info(klass, header, options = {}) 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%s", "*#{col.name}*::", a).rstrip + "\n" + # puts line + info << line end if options[:show_indexes] info << get_index_info(klass) end - info << "#\n\n" + info << "#--\n#== Schema Information End\n#++\n" end def get_index_info(klass) @@ -119,15 +124,18 @@ def annotate_one_file(file_name, info_block, options={}) old_content = File.read(file_name) # 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 new_header = info_block.match(header).to_s + # puts "old_header: #{old_header}" + # puts "new_header: #{new_header}" + if old_header == new_header false else # Remove old schema info - old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '') + old_content.sub!(PATTERN, '') # Write it back new_content = options[:position] == 'before' ? (info_block + old_content) : (old_content + "\n" + info_block) @@ -142,7 +150,7 @@ def remove_annotation_of_file(file_name) if File.exist?(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 } end @@ -263,6 +271,7 @@ def do_annotations(options={}) annotated = [] get_model_files.each do |file| + # puts "---------------- file: #{file}" begin klass = get_model_class(file) if klass < ActiveRecord::Base && !klass.abstract_class?