SourceAnnotationExtractor
Implements the logic behind the rake tasks for annotations like
rails notes rails notes:optimize
and friends. See rails -T notes and railties/lib/rails/tasks/annotations.rake.
Annotation objects are triplets :line, :tag, :text that represent the line where the annotation lives, its tag, and its text. Note the filename is not stored.
Annotations are looked for in comments and modulus whitespace they have to start with the tag optionally followed by a colon. Everything up to the end of the line (or closing ERB comment tag) is considered to be their text.
Constants
Annotation = Struct.new(:line, :tag, :text) do\ndef self.directories\n@@directories ||= %w(app config db lib test) + (ENV["SOURCE_ANNOTATION_DIRECTORIES"] || "").split(",")\nend\n\n# Registers additional directories to be included\n# SourceAnnotationExtractor::Annotation.register_directories("spec", "another")\ndef self.register_directories(*dirs)\ndirectories.push(*dirs)\nend\n\ndef self.extensions\n@@extensions ||= {}\nend\n\n# Registers new Annotations File Extensions\n# SourceAnnotationExtractor::Annotation.register_extensions("css", "scss", "sass", "less", "js") { |tag| /\\/\\/\\s*(#{tag}):?\\s*(.*)$/ }\ndef self.register_extensions(*exts, &block)\nextensions[/\\.(#{exts.join("|")})$/] = block\nend\n\nregister_extensions("builder", "rb", "rake", "yml", "yaml", "ruby") { |tag| /#\\s*(#{tag}):?\\s*(.*)$/ }\nregister_extensions("css", "js") { |tag| /\\/\\/\\s*(#{tag}):?\\s*(.*)$/ }\nregister_extensions("erb") { |tag| /<%\\s*#\\s*(#{tag}):?\\s*(.*?)\\s*%>/ }\n\n# Returns a representation of the annotation that looks like this:\n#\n# [126] [TODO] This algorithm is simple and clearly correct, make it faster.\n#\n# If +options+ has a flag <tt>:tag</tt> the tag is shown as in the example above.\n# Otherwise the string contains just line and text.\ndef to_s(options = {})\ns = "[#{line.to_s.rjust(options[:indent])}] ".dup\ns << "[#{tag}] " if options[:tag]\ns << text\nend\nend
Attributes
| [R] | tag |
Files
- railties/lib/rails/source_annotation_extractor.rb
2Notes
Custom annotation types
For group work you may need something more than FIXME, OPTIMIZE and TODO. Just create new rake file and place it to lib/tasks:
require 'source_annotation_extractor'
task :notes do
SourceAnnotationExtractor.enumerate "WTF|OMG", :tag => true
end
namespace :notes do
desc "Enumerate all WTF annotations"
task :wtf do
SourceAnnotationExtractor.enumerate "WTF"
end
desc "Enumerate all OMG annotations"
task :omg do
SourceAnnotationExtractor.enumerate "OMG"
end
end
or create an array of new types and generate tasks dynamicaly.
Add Rspec files to the annotations
By default the annotations search the 'test' folder, but not the 'spec' folder if you are using Rspec. To get those specs involved do this:
require 'source_annotation_extractor'
class SourceAnnotationExtractor
def find(dirs=%w(app lib spec))
dirs.inject({}) { |h, dir| h.update(find_in(dir)) }
end
end
If you have other folders you want to check, just add them to the dirs list.