diff --git a/README.md b/README.md index b9fb2407..7216cbd3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/rails_best_practices/analyzer.rb b/lib/rails_best_practices/analyzer.rb index 6e2c3cd3..a028f73e 100644 --- a/lib/rails_best_practices/analyzer.rb +++ b/lib/rails_best_practices/analyzer.rb @@ -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 @@ -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] @@ -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') } diff --git a/lib/rails_best_practices/command.rb b/lib/rails_best_practices/command.rb index 02b114ee..f7fbf9d9 100644 --- a/lib/rails_best_practices/command.rb +++ b/lib/rails_best_practices/command.rb @@ -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 @@ -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