root/trunk/tools/annotation_extract.rb

Revision 195, 1.9 kB (checked in by e.@brainspl.at, 2 years ago)

fix up rendering slightly. fixed layout bug, thanks atmos. Haml templates have caching enabled now. REST routes can be nested arbitrary levels of nestedness without complaining. Improved rest compatability. More to come

Line 
1 # thanks Jamis!
2 class SourceAnnotationExtractor
3   class Annotation < Struct.new(:line, :tag, :text)
4     def to_s(options={})
5       s = "[%3d] " % line
6       s << "[#{tag}] " if options[:tag]
7       s << text
8     end
9   end
10
11   def self.enumerate(tag, options={})
12     extractor = new(tag)
13     extractor.display(extractor.find, options)
14   end
15
16   attr_reader :tag
17
18   def initialize(tag)
19     @tag = tag
20   end
21
22   def find(dirs=%w(app lib test))
23     dirs.inject({}) { |h, dir| h.update(find_in(dir)) }
24   end
25
26   def find_in(dir)
27     results = {}
28
29     Dir.glob("#{dir}/*") do |item|
30       next if File.basename(item)[0] == ?.
31
32       if File.directory?(item)
33         results.update(find_in(item))
34       elsif item =~ /\.(?:rb|rxml|xerb|builder|mab|haml)$/
35         results.update(extract_annotations_from(item, /#\s*(#{tag}):?\s*(.*)$/))
36       elsif item =~ /\.herb|\.jerb|\.erb$/
37         results.update(extract_annotations_from(item, /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/))
38       end
39     end
40
41     results
42   end
43
44   def extract_annotations_from(file, pattern)
45     lineno = 0
46     result = File.readlines(file).inject([]) do |list, line|
47       lineno += 1
48       next list unless line =~ pattern
49       list << Annotation.new(lineno, $1, $2)
50     end
51     result.empty? ? {} : { file => result }
52   end
53
54   def display(results, options={})
55     results.keys.sort.each do |file|
56       puts "#{file}:"
57       results[file].each do |note|
58         puts "  * #{note.to_s(options)}"
59       end
60       puts
61     end
62   end
63 end
64
65 desc "Enumerate all annotations"
66 task :notes do
67   SourceAnnotationExtractor.enumerate "OPTIMIZE|FIXME|TODO", :tag => true
68 end
69
70 namespace :notes do
71   desc "Enumerate all OPTIMIZE annotations"
72   task :optimize do
73     SourceAnnotationExtractor.enumerate "OPTIMIZE"
74   end
75
76   desc "Enumerate all FIXME annotations"
77   task :fixme do
78     SourceAnnotationExtractor.enumerate "FIXME"
79   end
80
81   desc "Enumerate all TODO annotations"
82   task :todo do
83     SourceAnnotationExtractor.enumerate "TODO"
84   end
85 end
Note: See TracBrowser for help on using the browser.