#!/usr/bin/env ruby require 'optparse' require 'rake/dsl_definition' require 'rake' begin require "annotate" rescue LoadError here = File.expand_path(File.dirname __FILE__) $:<< "#{here}/../lib" require "annotate" end 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 ENV['position'] = 'before' # hack: make sure default position is "before" 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('--ignore-model-subdirects', "Ignore subdirectories of the models directory") do |dir| ENV['ignore_model_sub_dir'] = "yes" end opts.on('--sort', "Sort columns alphabetically, rather than in creation order") do |dir| ENV['sort'] = "yes" 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 opts.on('-f', '--format [bare|rdoc|markdown]', ['bare', 'rdoc', 'markdown'], 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt| ENV["format_#{fmt}"] = 'yes' end opts.on('--force', 'Force new annotations even if there are no changes.') do |force| ENV['force'] = 'yes' end opts.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |value| ENV['trace'] = 'yes' end end.parse! ENV['is_cli'] = '1' if Annotate.load_tasks Rake::Task[task].invoke else STDERR.puts "Can't find Rakefile. Are we in a Rails folder?" end