Skip to content

Commit

Permalink
add only option to parse specific files, it is really useful to debug
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Feb 20, 2012
1 parent 7328b64 commit 622e397
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -50,6 +50,8 @@ By default rails_best_practices will do parse codes in vendor, spec, test and fe
--features include features files
-x, --exclude PATTERNS Don't analyze files matching a pattern
(comma-separated regexp list)
-o, --only PATTERNS analyze files only matching a pattern
(comma-separated regexp list)
-g, --generate Generate configuration yaml
-v, --version Show this version
-h, --help Show this message
Expand Down
13 changes: 13 additions & 0 deletions lib/rails_best_practices/analyzer.rb
Expand Up @@ -46,6 +46,7 @@ def generate
# @param [Hash] options
def analyze
@options["exclude"] ||= []
@options["only"] ||= []
@options["output-file"] ||= "rails_best_practices_output.html"

Core::Runner.base_path = @path
Expand Down Expand Up @@ -98,6 +99,10 @@ def parse_files
files = expand_dirs_to_files(@path)
files = file_sort(files)

if @options["only"].present?
files = file_accept(files, @options["only"])
end

# By default, tmp, vender, spec, test, features are ignored.
["vendor", "spec", "test", "features", "tmp"].each do |pattern|
files = file_ignore(files, "#{pattern}/") unless @options[pattern]
Expand Down Expand Up @@ -157,6 +162,14 @@ def file_ignore files, pattern
files.reject { |file| file.index(pattern) }
end

# accept specific files.
#
# @param [Array] files
# @param [Regexp] patterns, files match any pattern will be accepted
def file_accept files, patterns
files.reject { |file| !patterns.any? { |pattern| file =~ pattern } }
end

# output errors on terminal.
def output_terminal_errors
@runner.errors.each { |error| plain_output(error.to_s, 'red') }
Expand Down
12 changes: 11 additions & 1 deletion lib/rails_best_practices/command.rb
Expand Up @@ -19,6 +19,8 @@
# --features include features files
# -x, --exclude PATTERNS don't analyze files matching a pattern
# (comma-separated regexp list)
# -o, --only PATTERNS analyze files only matching a pattern
# (comma-separated regexp list)
# -g, --generate generate configuration yaml
# -v, --version show this version
# -h, --help show this message
Expand Down Expand Up @@ -94,12 +96,20 @@

opts.on("-x", "--exclude PATTERNS", "Don't analyze files matching a pattern", "(comma-separated regexp list)") do |list|
begin
options["exclude"] = list.split(/,/).map{|x| Regexp.new x}
options["exclude"] = list.split(",").map{|x| Regexp.new x}
rescue RegexpError => e
raise OptionParser::InvalidArgument, e.message
end
end

opts.on("-o", "--only PATTERNS", "Analyze files only matching a pattern", "(comma-separated regexp list)") do |list|
begin
options["only"] = list.split(",").map { |x| Regexp.new x }
rescue RegexpError => e
raise OptionParser::InvalidArgument e.message
end
end

opts.on("-g", "--generate", "Generate configuration yaml") do
options["generate"] = true
end
Expand Down

0 comments on commit 622e397

Please sign in to comment.