mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	
							parent
							
								
									db815a2e30
								
							
						
					
					
						commit
						bc552d0730
					
				
					 3 changed files with 35 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -6,8 +6,9 @@ module Ameba::Cli
 | 
			
		|||
  extend self
 | 
			
		||||
 | 
			
		||||
  private class Opts
 | 
			
		||||
    property config : String?
 | 
			
		||||
    property config = Ameba::Config::PATH
 | 
			
		||||
    property silent : Bool = false
 | 
			
		||||
    property generate : Bool = false
 | 
			
		||||
    property files : Array(String)?
 | 
			
		||||
    property only : Array(String)?
 | 
			
		||||
    property except : Array(String)?
 | 
			
		||||
| 
						 | 
				
			
			@ -33,6 +34,10 @@ module Ameba::Cli
 | 
			
		|||
      parser.on("--except RULE1,RULE2,...", "Disable the given rules") do |rules|
 | 
			
		||||
        opts.except = rules.split ","
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      parser.on("--gen-config", "Generate a configuration file acting as a TODO list") do
 | 
			
		||||
        opts.generate = true
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    run_ameba opts
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +50,9 @@ module Ameba::Cli
 | 
			
		|||
 | 
			
		||||
    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
 | 
			
		||||
    puts "Error: #{e.message}"
 | 
			
		||||
    exit 255
 | 
			
		||||
| 
						 | 
				
			
			@ -64,6 +71,16 @@ module Ameba::Cli
 | 
			
		|||
    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
 | 
			
		||||
    puts Ameba::VERSION
 | 
			
		||||
    exit 0
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,6 +12,7 @@ require "yaml"
 | 
			
		|||
# By default config loads `.ameba.yml` file in a current directory.
 | 
			
		||||
#
 | 
			
		||||
class Ameba::Config
 | 
			
		||||
  PATH = ".ameba.yml"
 | 
			
		||||
  setter formatter : Formatter::BaseFormatter?
 | 
			
		||||
  setter files : Array(String)?
 | 
			
		||||
  getter rules : Array(Rule::Base)
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +20,7 @@ class Ameba::Config
 | 
			
		|||
  # Creates a new instance of `Ameba::Config` based on YAML parameters.
 | 
			
		||||
  #
 | 
			
		||||
  # `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)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -29,10 +30,11 @@ class Ameba::Config
 | 
			
		|||
  # config = Ameba::Config.load
 | 
			
		||||
  # ```
 | 
			
		||||
  #
 | 
			
		||||
  def self.load(path = nil)
 | 
			
		||||
    path ||= ".ameba.yml"
 | 
			
		||||
  def self.load(path = 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
 | 
			
		||||
 | 
			
		||||
  # Returns a list of paths (with wildcards) to files.
 | 
			
		||||
| 
						 | 
				
			
			@ -78,6 +80,15 @@ class Ameba::Config
 | 
			
		|||
    @rules[index] = rule
 | 
			
		||||
  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
 | 
			
		||||
    Dir["**/*.cr"].reject(&.starts_with? "lib/")
 | 
			
		||||
  end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,7 +15,7 @@ module Ameba
 | 
			
		|||
    @rules : Array(Rule::Base)
 | 
			
		||||
 | 
			
		||||
    # A list of sources to run inspection on.
 | 
			
		||||
    @sources : Array(Source)
 | 
			
		||||
    getter sources : Array(Source)
 | 
			
		||||
 | 
			
		||||
    # A formatter to prepare report.
 | 
			
		||||
    @formatter : Formatter::BaseFormatter
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue