2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2008-05-16 18:01:32 -04:00
|
|
|
# Implements the logic behind the rake tasks for annotations like
|
|
|
|
#
|
2016-07-02 05:11:42 -04:00
|
|
|
# rails notes
|
|
|
|
# rails notes:optimize
|
2008-05-16 18:01:32 -04:00
|
|
|
#
|
2016-07-02 05:11:42 -04:00
|
|
|
# and friends. See <tt>rails -T notes</tt> and <tt>railties/lib/rails/tasks/annotations.rake</tt>.
|
2008-05-16 18:01:32 -04:00
|
|
|
#
|
|
|
|
# Annotation objects are triplets <tt>:line</tt>, <tt>:tag</tt>, <tt>:text</tt> 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
|
2011-04-02 23:47:51 -04:00
|
|
|
# of the line (or closing ERB comment tag) is considered to be their text.
|
2007-09-28 13:02:40 -04:00
|
|
|
class SourceAnnotationExtractor
|
2017-01-13 01:09:56 -05:00
|
|
|
Annotation = Struct.new(:line, :tag, :text) do
|
2012-02-25 16:01:40 -05:00
|
|
|
def self.directories
|
2016-08-06 13:15:47 -04:00
|
|
|
@@directories ||= %w(app config db lib test) + (ENV["SOURCE_ANNOTATION_DIRECTORIES"] || "").split(",")
|
2012-02-25 16:01:40 -05:00
|
|
|
end
|
2008-05-16 18:01:32 -04:00
|
|
|
|
2016-07-04 20:10:24 -04:00
|
|
|
# Registers additional directories to be included
|
2017-08-22 17:27:16 -04:00
|
|
|
# SourceAnnotationExtractor::Annotation.register_directories("spec", "another")
|
2016-07-04 20:10:24 -04:00
|
|
|
def self.register_directories(*dirs)
|
|
|
|
directories.push(*dirs)
|
|
|
|
end
|
|
|
|
|
2014-03-13 20:16:49 -04:00
|
|
|
def self.extensions
|
|
|
|
@@extensions ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Registers new Annotations File Extensions
|
|
|
|
# SourceAnnotationExtractor::Annotation.register_extensions("css", "scss", "sass", "less", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ }
|
2014-03-16 15:32:08 -04:00
|
|
|
def self.register_extensions(*exts, &block)
|
|
|
|
extensions[/\.(#{exts.join("|")})$/] = block
|
2014-03-13 20:16:49 -04:00
|
|
|
end
|
|
|
|
|
2014-03-14 18:51:14 -04:00
|
|
|
register_extensions("builder", "rb", "rake", "yml", "yaml", "ruby") { |tag| /#\s*(#{tag}):?\s*(.*)$/ }
|
2014-03-14 16:34:24 -04:00
|
|
|
register_extensions("css", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ }
|
2014-03-13 20:16:49 -04:00
|
|
|
register_extensions("erb") { |tag| /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/ }
|
|
|
|
|
2008-05-16 18:01:32 -04:00
|
|
|
# Returns a representation of the annotation that looks like this:
|
|
|
|
#
|
|
|
|
# [126] [TODO] This algorithm is simple and clearly correct, make it faster.
|
|
|
|
#
|
|
|
|
# If +options+ has a flag <tt>:tag</tt> the tag is shown as in the example above.
|
|
|
|
# Otherwise the string contains just line and text.
|
2016-10-28 23:05:58 -04:00
|
|
|
def to_s(options = {})
|
2017-07-22 08:45:53 -04:00
|
|
|
s = "[#{line.to_s.rjust(options[:indent])}] ".dup
|
2007-09-28 13:02:40 -04:00
|
|
|
s << "[#{tag}] " if options[:tag]
|
|
|
|
s << text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-06 18:13:47 -05:00
|
|
|
# Prints all annotations with tag +tag+ under the root directories +app+,
|
2013-01-15 13:56:10 -05:00
|
|
|
# +config+, +db+, +lib+, and +test+ (recursively).
|
|
|
|
#
|
|
|
|
# Additional directories may be added using a comma-delimited list set using
|
|
|
|
# <tt>ENV['SOURCE_ANNOTATION_DIRECTORIES']</tt>.
|
|
|
|
#
|
|
|
|
# Directories may also be explicitly set using the <tt>:dirs</tt> key in +options+.
|
|
|
|
#
|
|
|
|
# SourceAnnotationExtractor.enumerate 'TODO|FIXME', dirs: %w(app lib), tag: true
|
|
|
|
#
|
|
|
|
# If +options+ has a <tt>:tag</tt> flag, it will be passed to each annotation's +to_s+.
|
|
|
|
#
|
|
|
|
# See <tt>#find_in</tt> for a list of file extensions that will be taken into account.
|
2008-05-16 18:01:32 -04:00
|
|
|
#
|
|
|
|
# This class method is the single entry point for the rake tasks.
|
2016-10-28 23:05:58 -04:00
|
|
|
def self.enumerate(tag, options = {})
|
2007-09-28 13:02:40 -04:00
|
|
|
extractor = new(tag)
|
2013-01-15 13:56:10 -05:00
|
|
|
dirs = options.delete(:dirs) || Annotation.directories
|
|
|
|
extractor.display(extractor.find(dirs), options)
|
2007-09-28 13:02:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :tag
|
|
|
|
|
|
|
|
def initialize(tag)
|
|
|
|
@tag = tag
|
|
|
|
end
|
|
|
|
|
2008-05-16 18:01:32 -04:00
|
|
|
# Returns a hash that maps filenames under +dirs+ (recursively) to arrays
|
2011-12-11 11:54:50 -05:00
|
|
|
# with their annotations.
|
2013-01-15 13:56:10 -05:00
|
|
|
def find(dirs)
|
2007-09-28 13:02:40 -04:00
|
|
|
dirs.inject({}) { |h, dir| h.update(find_in(dir)) }
|
|
|
|
end
|
|
|
|
|
2008-05-16 18:01:32 -04:00
|
|
|
# Returns a hash that maps filenames under +dir+ (recursively) to arrays
|
2012-10-08 07:41:31 -04:00
|
|
|
# with their annotations. Only files with annotations are included. Files
|
2014-11-08 21:53:51 -05:00
|
|
|
# with extension +.builder+, +.rb+, +.rake+, +.yml+, +.yaml+, +.ruby+,
|
|
|
|
# +.css+, +.js+ and +.erb+ are taken into account.
|
2007-09-28 13:02:40 -04:00
|
|
|
def find_in(dir)
|
|
|
|
results = {}
|
|
|
|
|
|
|
|
Dir.glob("#{dir}/*") do |item|
|
|
|
|
next if File.basename(item)[0] == ?.
|
|
|
|
|
|
|
|
if File.directory?(item)
|
|
|
|
results.update(find_in(item))
|
2013-01-15 13:47:12 -05:00
|
|
|
else
|
2014-03-13 20:16:49 -04:00
|
|
|
extension = Annotation.extensions.detect do |regexp, _block|
|
|
|
|
regexp.match(item)
|
|
|
|
end
|
|
|
|
|
|
|
|
if extension
|
|
|
|
pattern = extension.last.call(tag)
|
|
|
|
results.update(extract_annotations_from(item, pattern)) if pattern
|
|
|
|
end
|
2007-09-28 13:02:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2008-05-16 18:01:32 -04:00
|
|
|
# If +file+ is the filename of a file that contains annotations this method returns
|
|
|
|
# a hash with a single entry that maps +file+ to an array of its annotations.
|
|
|
|
# Otherwise it returns an empty hash.
|
2007-09-28 13:02:40 -04:00
|
|
|
def extract_annotations_from(file, pattern)
|
|
|
|
lineno = 0
|
2016-05-12 15:21:07 -04:00
|
|
|
result = File.readlines(file, encoding: Encoding::BINARY).inject([]) do |list, line|
|
2007-09-28 13:02:40 -04:00
|
|
|
lineno += 1
|
|
|
|
next list unless line =~ pattern
|
|
|
|
list << Annotation.new(lineno, $1, $2)
|
|
|
|
end
|
|
|
|
result.empty? ? {} : { file => result }
|
|
|
|
end
|
|
|
|
|
2008-05-16 18:01:32 -04:00
|
|
|
# Prints the mapping from filenames to annotations in +results+ ordered by filename.
|
|
|
|
# The +options+ hash is passed to each annotation's +to_s+.
|
2016-10-28 23:05:58 -04:00
|
|
|
def display(results, options = {})
|
2014-03-03 22:23:29 -05:00
|
|
|
options[:indent] = results.flat_map { |f, a| a.map(&:line) }.max.to_s.size
|
2007-09-28 13:02:40 -04:00
|
|
|
results.keys.sort.each do |file|
|
|
|
|
puts "#{file}:"
|
|
|
|
results[file].each do |note|
|
|
|
|
puts " * #{note.to_s(options)}"
|
|
|
|
end
|
|
|
|
puts
|
|
|
|
end
|
|
|
|
end
|
2011-04-02 23:47:51 -04:00
|
|
|
end
|