annotate 1.8 KB
#!/usr/bin/env ruby

require 'optparse'
require 'annotate'

task = :annotate_models

OptionParser.new do |opts|
  opts.banner = "Usage: annotate [options] [model_file]*"

  opts.on('-d', '--delete', 
  "Remove annotations from all model files") do
    task = :remove_annotation
  end

  opts.on('-p', '--position [before|after]', ['before', 'after'], 
  "Place the annotations at the top (before) or the bottom (after) of the model file") do |p| 
    ENV['position'] = p
  end

  opts.on('-r', '--routes', 
  "Annotate routes.rb with the output of 'rake routes'") do
    task = :annotate_routes
  end

  opts.on('-v', '--version', 
  "Show the current version of this gem") do 
    puts "annotate v#{Annotate.version}"; exit
  end

  opts.on('-m', '--show-migration', 
  "Include the migration version number in the annotation") do
    ENV['include_version'] = "yes"
  end

  opts.on('-i', '--show-indexes', 
  "List the table's database indexes in the annotation") do   
    ENV['show_indexes'] = "yes"
  end

  opts.on('-s', '--simple-indexes',
  "Concat the column's related indexes in the annotation") do
    ENV['simple_indexes'] = "yes"
  end

  opts.on('--model-dir dir', 
  "Annotate model files stored in dir rather than app/models") do |dir|
    ENV['model_dir'] = dir
  end

  opts.on('-R', '--require path',
  "Additional files to require before loading models") do |path|
    if ENV['require']
      ENV['require'] = ENV['require'] + ",#{path}"
    else
      ENV['require'] = path
    end
  end

  opts.on('-e', '--exclude [tests,fixtures]', Array, "Do not annotate fixtures, test files, or both") do |exclusions|
    exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
  end

end.parse!

if Annotate.load_tasks
  Rake::Task[task].invoke
else
  STDERR.puts "Can't find Rakefile. Are we in a Rails folder?"
end