Except & only cli flags

This commit is contained in:
Vitalii Elenhaupt 2017-11-23 19:49:45 +02:00
parent 34707903b8
commit dceafd26e5
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
7 changed files with 99 additions and 70 deletions

View file

@ -46,13 +46,5 @@ module Ameba
config.formatter.should eq formatter config.formatter.should eq formatter
end end
end end
describe "#subconfig" do
it "returns a specific subconfig" do
config = Config.load config_sample
config.subconfig("NoSuchConfig").should be_nil
config.subconfig(Rule::LineLength.new.name).should_not be_nil
end
end
end end
end end

View file

@ -48,10 +48,11 @@ module Ameba
end end
it "returns false if there are invalid sources" do it "returns false if there are invalid sources" do
rules = Rule.rules.map &.new
s = Source.new %q( s = Source.new %q(
WrongConstant = 5 WrongConstant = 5
) )
Runner.new([s], formatter).run.success?.should be_false Runner.new(rules, [s], formatter).run.success?.should be_false
end end
end end
end end

76
src/ameba/cli/cmd.cr Normal file
View file

@ -0,0 +1,76 @@
require "../../ameba"
require "option_parser"
# :nodoc:
module Ameba::Cli
extend self
private class Opts
property config : String?
property silent : Bool = false
property files : Array(String)?
property only : Array(String)?
property except : Array(String)?
end
def run(args, opts = Opts.new)
OptionParser.parse(args) do |parser|
parser.banner = "Usage: ameba [options] [file1 file2 ...]"
parser.on("-v", "--version", "Print version") { print_version }
parser.on("-h", "--help", "Show this help") { show_help parser }
parser.on("-s", "--silent", "Disable output") { opts.silent = true }
parser.unknown_args { |f| files = f if f.any? }
parser.on("-c PATH", "--config PATH", "Specify configuration file") do |f|
opts.config = f
end
parser.on("--only RULE1,RULE2,...", "Specify a list of rules") do |rules|
opts.only = rules.split ","
end
parser.on("--except RULE1,RULE2,...", "Disable the given rules") do |rules|
opts.except = rules.split ","
end
end
run_ameba opts
end
def run_ameba(opts)
config = Ameba::Config.load opts.config
config.files = opts.files
config.formatter = Ameba::Formatter::BaseFormatter.new if opts.silent
configure_rules(config, opts)
exit 1 unless Ameba.run(config).success?
rescue e
puts "Error: #{e.message}"
exit 255
end
private def configure_rules(config, opts)
if only = opts.only
config.rules.map! { |r| r.enabled = false; r }
only.each do |rule_name|
config.update_rule(rule_name, enabled: true)
end
end
opts.except.try &.each do |rule_name|
config.update_rule(rule_name, enabled: false)
end
end
private def print_version
puts Ameba::VERSION
exit 0
end
private def show_help(parser)
puts parser
exit 0
end
end

View file

@ -14,11 +14,13 @@ require "yaml"
class Ameba::Config class Ameba::Config
setter formatter : Formatter::BaseFormatter? setter formatter : Formatter::BaseFormatter?
setter files : Array(String)? setter files : Array(String)?
getter rules : Array(Rule::Base)
# Creates a new instance of `Ameba::Config` based on YAML parameters. # Creates a new instance of `Ameba::Config` based on YAML parameters.
# #
# `Config.load` uses this constructor to instantiate new config by YAML file. # `Config.load` uses this constructor to instantiate new config by YAML file.
protected def initialize(@config : YAML::Any) protected def initialize(config : YAML::Any)
@rules = Rule.rules.map &.new(config)
end end
# Loads YAML configuration file by `path`. # Loads YAML configuration file by `path`.
@ -60,16 +62,20 @@ class Ameba::Config
@formatter ||= default_formatter @formatter ||= default_formatter
end end
# Returns a subconfig of a fully loaded configuration. # Updates rule properties.
# This is used to get a config for a specific rule.
# #
# ``` # ```
# config = Ameba::Config.load # config = Ameba::Config.load
# config.subconfig "LineLength" # config.update_rule "MyRuleName", enabled: false
# ``` # ```
# #
def subconfig(name) def update_rule(name, enabled = true)
@config[name]? index = @rules.index { |r| r.name == name }
raise ArgumentError.new("Rule `#{name}` does not exist") unless index
rule = @rules[index]
rule.enabled = enabled
@rules[index] = rule
end end
private def default_files private def default_files
@ -81,7 +87,7 @@ class Ameba::Config
end end
# :nodoc: # :nodoc:
module Rule module RuleConfig
macro properties(&block) macro properties(&block)
{% definitions = [] of NamedTuple %} {% definitions = [] of NamedTuple %}
{% if block.body.is_a? Assign %} {% if block.body.is_a? Assign %}
@ -140,8 +146,8 @@ class Ameba::Config
# allow creating rules without properties # allow creating rules without properties
properties {} properties {}
def self.new(config : Ameba::Config? = nil) def self.new(config = nil)
yaml = config.try &.subconfig(class_name).try &.to_yaml || "{}" yaml = config.try &.[class_name]?.try &.to_yaml || "{}"
from_yaml yaml from_yaml yaml
end end
end end

View file

@ -21,7 +21,7 @@ module Ameba::Rule
# that are tested by this rule, it should add an error. # that are tested by this rule, it should add an error.
# #
abstract struct Base abstract struct Base
include Config::Rule include Config::RuleConfig
# This method is designed to test the source passed in. If source has issues # This method is designed to test the source passed in. If source has issues
# that are tested by this rule, it should add an error. # that are tested by this rule, it should add an error.

View file

@ -31,19 +31,13 @@ module Ameba
# ``` # ```
# #
def initialize(config : Config) def initialize(config : Config)
@rules = load_rules(config)
@sources = load_sources(config) @sources = load_sources(config)
@formatter = config.formatter @formatter = config.formatter
@rules = config.rules.select &.enabled
end end
# Instantiates a runner using a list of sources and a formatter. # :nodoc:
# protected def initialize(@rules, @sources, @formatter)
# ```
# runner = Ameba::Runner.new sources, formatter
# ```
#
def initialize(@sources, @formatter)
@rules = load_rules nil
end end
# Performs the inspection. Iterates through all sources and test it using # Performs the inspection. Iterates through all sources and test it using
@ -91,9 +85,5 @@ module Ameba
.flatten .flatten
.map { |path| Source.new File.read(path), path } .map { |path| Source.new File.read(path), path }
end end
private def load_rules(config)
Rule.rules.map { |r| r.new config }.select &.enabled
end
end end
end end

View file

@ -1,39 +1,3 @@
require "option_parser" require "./ameba/cli/cmd"
require "./ameba"
files, formatter, config_path = nil, nil, nil Ameba::Cli.run(ARGV)
OptionParser.parse(ARGV) do |parser|
parser.banner = "Usage: ameba [options] [file1 file2 ...]"
parser.on("-v", "--version", "Print version") do
puts Ameba::VERSION
exit 0
end
parser.on("-h", "--help", "Show this help") do
puts parser
exit 0
end
parser.on("-s", "--silent", "Disable output") do
formatter = Ameba::Formatter::BaseFormatter.new
end
# parser.on("-f FORMATTER", "--format FORMATTER", "Specify formatter") do |f|
# end
parser.on("-c PATH", "--config PATH", "Specify configuration file") do |f|
config_path = f
end
parser.unknown_args do |f|
files = f if f.any?
end
end
config = Ameba::Config.load config_path
config.formatter = formatter
config.files = files
exit(1) unless Ameba.run(config).success?