Generate config

closes #17
This commit is contained in:
Vitalii Elenhaupt 2017-11-23 23:59:59 +02:00 committed by V. Elenhaupt
parent db815a2e30
commit bc552d0730
3 changed files with 35 additions and 7 deletions

View File

@ -6,8 +6,9 @@ module Ameba::Cli
extend self extend self
private class Opts private class Opts
property config : String? property config = Ameba::Config::PATH
property silent : Bool = false property silent : Bool = false
property generate : Bool = false
property files : Array(String)? property files : Array(String)?
property only : Array(String)? property only : Array(String)?
property except : Array(String)? property except : Array(String)?
@ -33,6 +34,10 @@ module Ameba::Cli
parser.on("--except RULE1,RULE2,...", "Disable the given rules") do |rules| parser.on("--except RULE1,RULE2,...", "Disable the given rules") do |rules|
opts.except = rules.split "," opts.except = rules.split ","
end end
parser.on("--gen-config", "Generate a configuration file acting as a TODO list") do
opts.generate = true
end
end end
run_ameba opts run_ameba opts
@ -45,7 +50,9 @@ module Ameba::Cli
configure_rules(config, opts) configure_rules(config, opts)
exit 1 unless Ameba.run(config).success? runner = Ameba.run(config)
generate_config_file(config, runner, opts) if opts.generate
exit 1 unless runner.success?
rescue e rescue e
puts "Error: #{e.message}" puts "Error: #{e.message}"
exit 255 exit 255
@ -64,6 +71,16 @@ module Ameba::Cli
end end
end end
private def generate_config_file(config, runner, opts)
failed_rules =
runner.sources
.map { |s| s.errors.map &.rule.name }
.flatten
.uniq!
failed_rules.each { |rule| config.update_rule rule, enabled: false }
File.write(opts.config, config.to_yaml)
end
private def print_version private def print_version
puts Ameba::VERSION puts Ameba::VERSION
exit 0 exit 0

View File

@ -12,6 +12,7 @@ require "yaml"
# By default config loads `.ameba.yml` file in a current directory. # By default config loads `.ameba.yml` file in a current directory.
# #
class Ameba::Config class Ameba::Config
PATH = ".ameba.yml"
setter formatter : Formatter::BaseFormatter? setter formatter : Formatter::BaseFormatter?
setter files : Array(String)? setter files : Array(String)?
getter rules : Array(Rule::Base) getter rules : Array(Rule::Base)
@ -19,7 +20,7 @@ class Ameba::Config
# 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 : Hash(YAML::Type, YAML::Type))
@rules = Rule.rules.map &.new(config) @rules = Rule.rules.map &.new(config)
end end
@ -29,10 +30,11 @@ class Ameba::Config
# config = Ameba::Config.load # config = Ameba::Config.load
# ``` # ```
# #
def self.load(path = nil) def self.load(path = PATH)
path ||= ".ameba.yml"
content = File.exists?(path) ? File.read path : "{}" content = File.exists?(path) ? File.read path : "{}"
Config.new YAML.parse(content) Config.new YAML.parse(content).as_h
rescue e
raise "Config file is invalid"
end end
# Returns a list of paths (with wildcards) to files. # Returns a list of paths (with wildcards) to files.
@ -78,6 +80,15 @@ class Ameba::Config
@rules[index] = rule @rules[index] = rule
end end
def to_yaml(yaml : YAML::Builder)
yaml.mapping do
rules.each do |rule|
rule.name.to_yaml(yaml)
rule.to_yaml(yaml)
end
end
end
private def default_files private def default_files
Dir["**/*.cr"].reject(&.starts_with? "lib/") Dir["**/*.cr"].reject(&.starts_with? "lib/")
end end

View File

@ -15,7 +15,7 @@ module Ameba
@rules : Array(Rule::Base) @rules : Array(Rule::Base)
# A list of sources to run inspection on. # A list of sources to run inspection on.
@sources : Array(Source) getter sources : Array(Source)
# A formatter to prepare report. # A formatter to prepare report.
@formatter : Formatter::BaseFormatter @formatter : Formatter::BaseFormatter