实现 Rails::Command::NotesCommand
背后的逻辑。有关用法信息,请参阅 rails notes --help
。
Annotation
对象是三元组 :line
、:tag
、:text
,表示注释所在的行、其标签及其文本。请注意,文件名未存储。
在注释和模数空白中查找注释,它们必须以标签开头,后面可以跟冒号。行尾(或结束 ERB
注释标签)之前的所有内容都被视为其文本。
- 类 Rails::SourceAnnotationExtractor::Annotation
- 类 Rails::SourceAnnotationExtractor::ParserExtractor
- 类 Rails::SourceAnnotationExtractor::PatternExtractor
属性
[R] | tag |
类公共方法
enumerate(tag = nil, options = {}) 链接
打印根目录 app
、config
、db
、lib
和 test
(递归)下带有标签 tag
的所有注释。
如果 tag
为 nil
,则打印带有默认标签或已注册标签的注释。
可以使用 options
中的 :dirs
键显式设置特定目录。
Rails::SourceAnnotationExtractor.enumerate 'TODO|FIXME', dirs: %w(app lib), tag: true
如果 options
有 :tag
标志,则会将其传递给每个注释的 to_s
。
请参阅 SourceAnnotationExtractor#find_in
以了解将考虑的文件扩展名列表。
此类方法是 rails notes
命令的单一入口点。
源代码:显示 | 在 GitHub 上
# File railties/lib/rails/source_annotation_extractor.rb, line 128 def self.enumerate(tag = nil, options = {}) tag ||= Annotation.tags.join("|") extractor = new(tag) dirs = options.delete(:dirs) || Annotation.directories extractor.display(extractor.find(dirs), options) end
new(tag) 链接
源代码:显示 | 在 GitHub 上
# File railties/lib/rails/source_annotation_extractor.rb, line 137 def initialize(tag) @tag = tag end
实例公共方法
display(results, options = {}) 链接
打印从文件名到 results
中注释的映射,按文件名排序。options
哈希被传递到每个注释的 to_s
中。
源代码:显示 | 在 GitHub 上
# File railties/lib/rails/source_annotation_extractor.rb, line 186 def display(results, options = {}) options[:indent] = results.flat_map { |f, a| a.map(&:line) }.max.to_s.size results.keys.sort.each do |file| puts "#{file}:" results[file].each do |note| puts " * #{note.to_s(options)}" end puts end end
find(dirs) 链接
返回一个哈希,将 dirs
(递归)下的文件名映射到包含其注释的数组。
源代码:显示 | 在 GitHub 上
# File railties/lib/rails/source_annotation_extractor.rb, line 143 def find(dirs) dirs.inject({}) { |h, dir| h.update(find_in(dir)) } end
find_in(dir) 链接
返回一个哈希,将 dir
(递归)下的文件名映射到包含其注释的数组。注册在 Rails::SourceAnnotationExtractor::Annotation.extensions
中的扩展名的文件会被考虑在内。仅包含注释的文件会被包含。
源代码:显示 | 在 GitHub 上
# File railties/lib/rails/source_annotation_extractor.rb, line 151 def find_in(dir) results = {} Dir.glob("#{dir}/*") do |item| next if File.basename(item).start_with?(".") if File.directory?(item) results.update(find_in(item)) else extension = Annotation.extensions.detect do |regexp, _block| regexp.match(item) end if extension pattern = extension.last.call(tag) # In case a user-defined pattern returns nothing for the given set # of tags, we exit early. next unless pattern # If a user-defined pattern returns a regular expression, we will # wrap it in a PatternExtractor to keep the same API. pattern = PatternExtractor.new(pattern) if pattern.is_a?(Regexp) annotations = pattern.annotations(item) results.update(item => annotations) if annotations.any? end end end results end