From a2fb164a4fadf0f34055089b75ef9077bded8524 Mon Sep 17 00:00:00 2001 From: robertomiranda Date: Thu, 13 Mar 2014 19:16:49 -0500 Subject: [PATCH 1/8] Add Public Api for register new extensions for Rake Notes --- .../lib/rails/source_annotation_extractor.rb | 39 ++++++++++++------- railties/test/application/rake/notes_test.rb | 5 +++ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/railties/lib/rails/source_annotation_extractor.rb b/railties/lib/rails/source_annotation_extractor.rb index 83e28090f82bc..5863ad1ed7429 100644 --- a/railties/lib/rails/source_annotation_extractor.rb +++ b/railties/lib/rails/source_annotation_extractor.rb @@ -18,6 +18,22 @@ def self.directories @@directories ||= %w(app config db lib test) + (ENV['SOURCE_ANNOTATION_DIRECTORIES'] || '').split(',') end + def self.extensions + @@extensions ||= {} + end + + # Registers new Annotations File Extensions + # SourceAnnotationExtractor::Annotation.register_extensions("css", "scss", "sass", "less", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } + def self.register_extensions(*extensions, &block) + self.extensions[/\.(#{extensions.join("|")})$/] = block + end + + register_extensions("builder", "rb", "coffe", "rake") { |tag| /#\s*(#{tag}):?\s*(.*)$/ } + register_extensions("css", "scss", "sass", "less", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } + register_extensions("erb") { |tag| /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/ } + register_extensions("haml") { |tag| /-\s*#\s*(#{tag}):?\s*(.*)$/ } + register_extensions("slim") { |tag| /\/\s*\s*(#{tag}):?\s*(.*)$/ } + # Returns a representation of the annotation that looks like this: # # [126] [TODO] This algorithm is simple and clearly correct, make it faster. @@ -78,21 +94,14 @@ def find_in(dir) if File.directory?(item) results.update(find_in(item)) else - pattern = - case item - when /\.(builder|rb|coffee|rake)$/ - /#\s*(#{tag}):?\s*(.*)$/ - when /\.(css|scss|sass|less|js)$/ - /\/\/\s*(#{tag}):?\s*(.*)$/ - when /\.erb$/ - /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/ - when /\.haml$/ - /-\s*#\s*(#{tag}):?\s*(.*)$/ - when /\.slim$/ - /\/\s*\s*(#{tag}):?\s*(.*)$/ - else nil - end - results.update(extract_annotations_from(item, pattern)) if pattern + 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 end end diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index 05f6338b6846a..62ab28082db15 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -175,6 +175,11 @@ def teardown end end + test 'register a new extension' do + SourceAnnotationExtractor::Annotation.register_extensions(".test1", ".test2") { |tag| /#{tag}/ } + assert SourceAnnotationExtractor::Annotation.extensions[/(\.test1|\.test2)/] + end + private def boot_rails super From 810af6f6ee62d76e9ed529d93ea9686a45e5a81e Mon Sep 17 00:00:00 2001 From: robertomiranda Date: Fri, 14 Mar 2014 15:34:24 -0500 Subject: [PATCH 2/8] Remove .scss, .sass, .less, .haml, .slim, coffee from Rake Notes. Now we have an API for register it in the corresponding gems --- .../lib/rails/source_annotation_extractor.rb | 6 ++---- railties/test/application/rake/notes_test.rb | 16 +++------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/railties/lib/rails/source_annotation_extractor.rb b/railties/lib/rails/source_annotation_extractor.rb index 5863ad1ed7429..b5f1ca1602973 100644 --- a/railties/lib/rails/source_annotation_extractor.rb +++ b/railties/lib/rails/source_annotation_extractor.rb @@ -28,11 +28,9 @@ def self.register_extensions(*extensions, &block) self.extensions[/\.(#{extensions.join("|")})$/] = block end - register_extensions("builder", "rb", "coffe", "rake") { |tag| /#\s*(#{tag}):?\s*(.*)$/ } - register_extensions("css", "scss", "sass", "less", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } + register_extensions("builder", "rb", "rake") { |tag| /#\s*(#{tag}):?\s*(.*)$/ } + register_extensions("css", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } register_extensions("erb") { |tag| /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/ } - register_extensions("haml") { |tag| /-\s*#\s*(#{tag}):?\s*(.*)$/ } - register_extensions("slim") { |tag| /\/\s*\s*(#{tag}):?\s*(.*)$/ } # Returns a representation of the annotation that looks like this: # diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index 62ab28082db15..2191de32af621 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -1,4 +1,5 @@ require "isolation/abstract_unit" +require 'rails/source_annotation_extractor' module ApplicationTests module RakeTests @@ -18,14 +19,8 @@ def teardown test 'notes finds notes for certain file_types' do app_file "app/views/home/index.html.erb", "<% # TODO: note in erb %>" - app_file "app/views/home/index.html.haml", "-# TODO: note in haml" - app_file "app/views/home/index.html.slim", "/ TODO: note in slim" - app_file "app/assets/javascripts/application.js.coffee", "# TODO: note in coffee" app_file "app/assets/javascripts/application.js", "// TODO: note in js" app_file "app/assets/stylesheets/application.css", "// TODO: note in css" - app_file "app/assets/stylesheets/application.css.scss", "// TODO: note in scss" - app_file "app/assets/stylesheets/application.css.sass", "// TODO: note in sass" - app_file "app/assets/stylesheets/application.css.less", "// TODO: note in less" app_file "app/controllers/application_controller.rb", 1000.times.map { "" }.join("\n") << "# TODO: note in ruby" app_file "lib/tasks/task.rake", "# TODO: note in rake" app_file 'app/views/home/index.html.builder', '# TODO: note in builder' @@ -42,19 +37,13 @@ def teardown lines = output.scan(/\[([0-9\s]+)\](\s)/) assert_match(/note in erb/, output) - assert_match(/note in haml/, output) - assert_match(/note in slim/, output) assert_match(/note in ruby/, output) - assert_match(/note in coffee/, output) assert_match(/note in js/, output) assert_match(/note in css/, output) - assert_match(/note in scss/, output) - assert_match(/note in sass/, output) - assert_match(/note in less/, output) assert_match(/note in rake/, output) assert_match(/note in builder/, output) - assert_equal 12, lines.size + assert_equal 6, lines.size lines.each do |line| assert_equal 4, line[0].size @@ -178,6 +167,7 @@ def teardown test 'register a new extension' do SourceAnnotationExtractor::Annotation.register_extensions(".test1", ".test2") { |tag| /#{tag}/ } assert SourceAnnotationExtractor::Annotation.extensions[/(\.test1|\.test2)/] + assert_blank SourceAnnotationExtractor::Annotation.extensions[/(\.haml)/] end private From f43421cb65da635988f2bb5341d519c3c2748b74 Mon Sep 17 00:00:00 2001 From: robertomiranda Date: Fri, 14 Mar 2014 17:51:14 -0500 Subject: [PATCH 3/8] Supporting .ruby, .yml and .yaml Extension in Rake Notes --- railties/lib/rails/source_annotation_extractor.rb | 2 +- railties/test/application/rake/notes_test.rb | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/railties/lib/rails/source_annotation_extractor.rb b/railties/lib/rails/source_annotation_extractor.rb index b5f1ca1602973..af6d90b5bdb22 100644 --- a/railties/lib/rails/source_annotation_extractor.rb +++ b/railties/lib/rails/source_annotation_extractor.rb @@ -28,7 +28,7 @@ def self.register_extensions(*extensions, &block) self.extensions[/\.(#{extensions.join("|")})$/] = block end - register_extensions("builder", "rb", "rake") { |tag| /#\s*(#{tag}):?\s*(.*)$/ } + register_extensions("builder", "rb", "rake", "yml", "yaml", "ruby") { |tag| /#\s*(#{tag}):?\s*(.*)$/ } register_extensions("css", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } register_extensions("erb") { |tag| /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/ } diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index 2191de32af621..b6b1bd63f447f 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -24,6 +24,9 @@ def teardown app_file "app/controllers/application_controller.rb", 1000.times.map { "" }.join("\n") << "# TODO: note in ruby" app_file "lib/tasks/task.rake", "# TODO: note in rake" app_file 'app/views/home/index.html.builder', '# TODO: note in builder' + app_file 'config/locales/en.yml', '# TODO: note in yml' + app_file 'config/locales/en.yaml', '# TODO: note in yml' + app_file "app/views/home/index.ruby", "# TODO: note in ruby" boot_rails require 'rake' @@ -42,8 +45,10 @@ def teardown assert_match(/note in css/, output) assert_match(/note in rake/, output) assert_match(/note in builder/, output) + assert_match(/note in yml/, output) + assert_match(/note in yaml/, output) - assert_equal 6, lines.size + assert_equal 9, lines.size lines.each do |line| assert_equal 4, line[0].size From ce38a6b8b62e73596ec1062663e85726fbca8933 Mon Sep 17 00:00:00 2001 From: robertomiranda Date: Sun, 16 Mar 2014 14:32:08 -0500 Subject: [PATCH 4/8] Fix Shadowing extensions variable in Register Annotation Exentsions --- railties/lib/rails/source_annotation_extractor.rb | 4 ++-- railties/test/application/rake/notes_test.rb | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/railties/lib/rails/source_annotation_extractor.rb b/railties/lib/rails/source_annotation_extractor.rb index af6d90b5bdb22..201532d299bdf 100644 --- a/railties/lib/rails/source_annotation_extractor.rb +++ b/railties/lib/rails/source_annotation_extractor.rb @@ -24,8 +24,8 @@ def self.extensions # Registers new Annotations File Extensions # SourceAnnotationExtractor::Annotation.register_extensions("css", "scss", "sass", "less", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } - def self.register_extensions(*extensions, &block) - self.extensions[/\.(#{extensions.join("|")})$/] = block + def self.register_extensions(*exts, &block) + extensions[/\.(#{exts.join("|")})$/] = block end register_extensions("builder", "rb", "rake", "yml", "yaml", "ruby") { |tag| /#\s*(#{tag}):?\s*(.*)$/ } diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index b6b1bd63f447f..28efc84c573bf 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -25,7 +25,7 @@ def teardown app_file "lib/tasks/task.rake", "# TODO: note in rake" app_file 'app/views/home/index.html.builder', '# TODO: note in builder' app_file 'config/locales/en.yml', '# TODO: note in yml' - app_file 'config/locales/en.yaml', '# TODO: note in yml' + app_file 'config/locales/en.yaml', '# TODO: note in yaml' app_file "app/views/home/index.ruby", "# TODO: note in ruby" boot_rails @@ -40,13 +40,13 @@ def teardown lines = output.scan(/\[([0-9\s]+)\](\s)/) assert_match(/note in erb/, output) - assert_match(/note in ruby/, output) assert_match(/note in js/, output) assert_match(/note in css/, output) assert_match(/note in rake/, output) assert_match(/note in builder/, output) assert_match(/note in yml/, output) assert_match(/note in yaml/, output) + assert_match(/note in ruby/, output) assert_equal 9, lines.size @@ -170,9 +170,9 @@ def teardown end test 'register a new extension' do - SourceAnnotationExtractor::Annotation.register_extensions(".test1", ".test2") { |tag| /#{tag}/ } - assert SourceAnnotationExtractor::Annotation.extensions[/(\.test1|\.test2)/] - assert_blank SourceAnnotationExtractor::Annotation.extensions[/(\.haml)/] + SourceAnnotationExtractor::Annotation.register_extensions("test1", "test2"){ |tag| /#{tag}/ } + assert_not_nil SourceAnnotationExtractor::Annotation.extensions[/\.(test1|test2)$/] + assert_nil SourceAnnotationExtractor::Annotation.extensions[/\.(haml)$/] end private From 27e95727d7f1da623cd3f22b643ae5eb0d3a93b1 Mon Sep 17 00:00:00 2001 From: robertomiranda Date: Sun, 16 Mar 2014 14:57:21 -0500 Subject: [PATCH 5/8] Add config.annotations, in order to register new extensions for Rake notes at config level --- railties/lib/rails/application/configuration.rb | 4 ++++ railties/test/application/configuration_test.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 20e3de32aacbf..9aec2f973462e 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -1,6 +1,7 @@ require 'active_support/core_ext/kernel/reporting' require 'active_support/file_update_checker' require 'rails/engine/configuration' +require 'rails/source_annotation_extractor' module Rails class Application @@ -149,6 +150,9 @@ def session_store(*args) end end + def annotations + SourceAnnotationExtractor::Annotation + end end end end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index b39cd3747b78b..b11fd5517007e 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -793,5 +793,15 @@ def index assert ActiveRecord::Base.dump_schema_after_migration end + + test "config.annotations wrapping SourceAnnotationExtractor::Annotation class" do + make_basic_app do |app| + app.config.annotations.register_extensions("coffee") do |tag| + /#\s*(#{tag}):?\s*(.*)$/ + end + end + + assert_not_nil SourceAnnotationExtractor::Annotation.extensions[/\.(coffee)$/] + end end end From 8eac0a6b5817defabef6cee2f12baeda26e91d8f Mon Sep 17 00:00:00 2001 From: robertomiranda Date: Sun, 16 Mar 2014 15:08:16 -0500 Subject: [PATCH 6/8] Add Changelog Entry ref #14379 --- railties/CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 35b3a379aef6a..b52b80e8029c7 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,11 @@ +* Add public API to register new extensions for `rake notes`. + + Example: + + config.annotations.register_extensions("scss", "sass") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } + + *Roberto Miranda* + * Removed unnecessary `rails application` command. *Arun Agrawal* From 6b4793b1d2793e37a3cd314ab84edf5ccc2a2032 Mon Sep 17 00:00:00 2001 From: robertomiranda Date: Mon, 17 Mar 2014 10:31:21 -0500 Subject: [PATCH 7/8] Update command line guide --- guides/source/command_line.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/guides/source/command_line.md b/guides/source/command_line.md index 8949ef4c78ea8..57283f7c40004 100644 --- a/guides/source/command_line.md +++ b/guides/source/command_line.md @@ -411,7 +411,7 @@ The `doc:` namespace has the tools to generate documentation for your app, API d ### `notes` -`rake notes` will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is done in files with extension `.builder`, `.rb`, `.erb`, `.haml`, `.slim`, `.css`, `.scss`, `.js`, `.coffee`, `.rake`, `.sass` and `.less` for both default and custom annotations. +`rake notes` will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is done in files with extension `.builder`, `.rb`, `.rake`, `.yml`, `.yaml`, `.ruby`, `.css`, `.js` and `.erb` for both default and custom annotations. ```bash $ rake notes @@ -425,6 +425,12 @@ app/models/school.rb: * [ 17] [FIXME] ``` +You can add support for new file extensions using `config.annotations.register_extensions` option, which receives a list of the extensions with its corresponding regex to match it up. + +```ruby +config.annotations.register_extensions("scss", "sass", "less") { |annotation| /\/\/\s*(#{annotation}):?\s*(.*)$/ } +``` + If you are looking for a specific annotation, say FIXME, you can use `rake notes:fixme`. Note that you have to lower case the annotation's name. ```bash From 3b073ac19530b657d7585047027dcf78a452161c Mon Sep 17 00:00:00 2001 From: robertomiranda Date: Mon, 17 Mar 2014 15:20:46 -0500 Subject: [PATCH 8/8] Rake notes should picked up new Extensions registered in the config/application.rb file --- railties/test/application/rake/notes_test.rb | 22 +++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index 28efc84c573bf..df5615be1c626 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -170,9 +170,25 @@ def teardown end test 'register a new extension' do - SourceAnnotationExtractor::Annotation.register_extensions("test1", "test2"){ |tag| /#{tag}/ } - assert_not_nil SourceAnnotationExtractor::Annotation.extensions[/\.(test1|test2)$/] - assert_nil SourceAnnotationExtractor::Annotation.extensions[/\.(haml)$/] + add_to_config %q{ config.annotations.register_extensions("scss", "sass") { |annotation| /\/\/\s*(#{annotation}):?\s*(.*)$/ } } + app_file "app/assets/stylesheets/application.css.scss", "// TODO: note in scss" + app_file "app/assets/stylesheets/application.css.sass", "// TODO: note in sass" + + boot_rails + + require 'rake' + require 'rdoc/task' + require 'rake/testtask' + + Rails.application.load_tasks + + Dir.chdir(app_path) do + output = `bundle exec rake notes` + lines = output.scan(/\[([0-9\s]+)\]/).flatten + assert_match(/note in scss/, output) + assert_match(/note in sass/, output) + assert_equal 2, lines.size + end end private