mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	
							parent
							
								
									bfe18f4a77
								
							
						
					
					
						commit
						1d436fbb94
					
				
					 5 changed files with 53 additions and 25 deletions
				
			
		
							
								
								
									
										2
									
								
								LICENSE
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								LICENSE
									
										
									
									
									
								
							| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
The MIT License (MIT)
 | 
					The MIT License (MIT)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Copyright (c) 2017 Vitalii Elenhaupt
 | 
					Copyright (c) 2018 Vitalii Elenhaupt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
				
			||||||
of this software and associated documentation files (the "Software"), to deal
 | 
					of this software and associated documentation files (the "Software"), to deal
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,10 +3,6 @@ require "../../../src/ameba/cli/cmd"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module Ameba::Cli
 | 
					module Ameba::Cli
 | 
				
			||||||
  describe "Cmd" do
 | 
					  describe "Cmd" do
 | 
				
			||||||
    it "has a list of available formatters" do
 | 
					 | 
				
			||||||
      AVAILABLE_FORMATTERS.should_not be_nil
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    describe ".run" do
 | 
					    describe ".run" do
 | 
				
			||||||
      it "runs ameba" do
 | 
					      it "runs ameba" do
 | 
				
			||||||
        r = Cli.run %w(-f silent file.cr)
 | 
					        r = Cli.run %w(-f silent file.cr)
 | 
				
			||||||
| 
						 | 
					@ -58,7 +54,7 @@ module Ameba::Cli
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      it "allows args to be blank" do
 | 
					      it "allows args to be blank" do
 | 
				
			||||||
        c = Cli.parse_args [] of String
 | 
					        c = Cli.parse_args [] of String
 | 
				
			||||||
        c.formatter.should eq :progress
 | 
					        c.formatter.should be_nil
 | 
				
			||||||
        c.files.should be_nil
 | 
					        c.files.should be_nil
 | 
				
			||||||
        c.only.should be_nil
 | 
					        c.only.should be_nil
 | 
				
			||||||
        c.except.should be_nil
 | 
					        c.except.should be_nil
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,6 +4,10 @@ module Ameba
 | 
				
			||||||
  describe Config do
 | 
					  describe Config do
 | 
				
			||||||
    config_sample = "config/ameba.yml"
 | 
					    config_sample = "config/ameba.yml"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it "should have a list of available formatters" do
 | 
				
			||||||
 | 
					      Config::AVAILABLE_FORMATTERS.should_not be_nil
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    describe ".load" do
 | 
					    describe ".load" do
 | 
				
			||||||
      it "loads custom config" do
 | 
					      it "loads custom config" do
 | 
				
			||||||
        config = Config.load config_sample
 | 
					        config = Config.load config_sample
 | 
				
			||||||
| 
						 | 
					@ -45,6 +49,17 @@ module Ameba
 | 
				
			||||||
        config.formatter = formatter
 | 
					        config.formatter = formatter
 | 
				
			||||||
        config.formatter.should eq formatter
 | 
					        config.formatter.should eq formatter
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "allows to set formatter using a name" do
 | 
				
			||||||
 | 
					        config.formatter = :progress
 | 
				
			||||||
 | 
					        config.formatter.should_not be_nil
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "raises an error if not available formatter is set" do
 | 
				
			||||||
 | 
					        expect_raises(Exception) do
 | 
				
			||||||
 | 
					          config.formatter = :no_such_formatter
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    describe "#update_rule" do
 | 
					    describe "#update_rule" do
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,13 +5,6 @@ require "option_parser"
 | 
				
			||||||
module Ameba::Cli
 | 
					module Ameba::Cli
 | 
				
			||||||
  extend self
 | 
					  extend self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  AVAILABLE_FORMATTERS = {
 | 
					 | 
				
			||||||
    progress: Formatter::DotFormatter,
 | 
					 | 
				
			||||||
    todo:     Formatter::TODOFormatter,
 | 
					 | 
				
			||||||
    flycheck: Formatter::FlycheckFormatter,
 | 
					 | 
				
			||||||
    silent:   Formatter::BaseFormatter,
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def run(args)
 | 
					  def run(args)
 | 
				
			||||||
    opts = parse_args args
 | 
					    opts = parse_args args
 | 
				
			||||||
    config = Config.load opts.config
 | 
					    config = Config.load opts.config
 | 
				
			||||||
| 
						 | 
					@ -28,7 +21,7 @@ module Ameba::Cli
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private class Opts
 | 
					  private class Opts
 | 
				
			||||||
    property config = Config::PATH
 | 
					    property config = Config::PATH
 | 
				
			||||||
    property formatter : String | Symbol = :progress
 | 
					    property formatter : Symbol | String | Nil
 | 
				
			||||||
    property files : Array(String)?
 | 
					    property files : Array(String)?
 | 
				
			||||||
    property only : Array(String)?
 | 
					    property only : Array(String)?
 | 
				
			||||||
    property except : Array(String)?
 | 
					    property except : Array(String)?
 | 
				
			||||||
| 
						 | 
					@ -49,7 +42,7 @@ module Ameba::Cli
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      parser.on("-f", "--format FORMATTER",
 | 
					      parser.on("-f", "--format FORMATTER",
 | 
				
			||||||
        "Choose an output formatter: #{formatters}") do |formatter|
 | 
					        "Choose an output formatter: #{Config.formatter_names}") do |formatter|
 | 
				
			||||||
        opts.formatter = formatter
 | 
					        opts.formatter = formatter
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -86,17 +79,11 @@ module Ameba::Cli
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private def configure_formatter(config, opts)
 | 
					  private def configure_formatter(config, opts)
 | 
				
			||||||
    if cls = AVAILABLE_FORMATTERS[opts.formatter]?
 | 
					    if name = opts.formatter
 | 
				
			||||||
      config.formatter = cls.new
 | 
					      config.formatter = name
 | 
				
			||||||
    else
 | 
					 | 
				
			||||||
      raise "Unknown formatter `#{opts.formatter}`. Use any of #{formatters}."
 | 
					 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private def formatters
 | 
					 | 
				
			||||||
    AVAILABLE_FORMATTERS.keys.join("|")
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  private def print_version
 | 
					  private def print_version
 | 
				
			||||||
    puts VERSION
 | 
					    puts VERSION
 | 
				
			||||||
    exit 0
 | 
					    exit 0
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,6 +12,13 @@ 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
 | 
				
			||||||
 | 
					  AVAILABLE_FORMATTERS = {
 | 
				
			||||||
 | 
					    progress: Formatter::DotFormatter,
 | 
				
			||||||
 | 
					    todo:     Formatter::TODOFormatter,
 | 
				
			||||||
 | 
					    flycheck: Formatter::FlycheckFormatter,
 | 
				
			||||||
 | 
					    silent:   Formatter::BaseFormatter,
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  PATH = ".ameba.yml"
 | 
					  PATH = ".ameba.yml"
 | 
				
			||||||
  setter formatter : Formatter::BaseFormatter?
 | 
					  setter formatter : Formatter::BaseFormatter?
 | 
				
			||||||
  setter files : Array(String)?
 | 
					  setter files : Array(String)?
 | 
				
			||||||
| 
						 | 
					@ -22,6 +29,10 @@ class Ameba::Config
 | 
				
			||||||
  # `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)
 | 
					    @rules = Rule.rules.map &.new(config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if @config.as_h? && (name = @config["Formatter"]?.try &.["Name"]?)
 | 
				
			||||||
 | 
					      self.formatter = name.to_s
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Loads YAML configuration file by `path`.
 | 
					  # Loads YAML configuration file by `path`.
 | 
				
			||||||
| 
						 | 
					@ -33,8 +44,12 @@ class Ameba::Config
 | 
				
			||||||
  def self.load(path = PATH)
 | 
					  def self.load(path = PATH)
 | 
				
			||||||
    content = File.exists?(path) ? File.read path : ""
 | 
					    content = File.exists?(path) ? File.read path : ""
 | 
				
			||||||
    Config.new YAML.parse(content)
 | 
					    Config.new YAML.parse(content)
 | 
				
			||||||
  rescue
 | 
					  rescue e
 | 
				
			||||||
    raise "Config file is invalid"
 | 
					    raise "Config file is invalid: #{e.message}"
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def self.formatter_names
 | 
				
			||||||
 | 
					    AVAILABLE_FORMATTERS.keys.join("|")
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Returns a list of paths (with wildcards) to files.
 | 
					  # Returns a list of paths (with wildcards) to files.
 | 
				
			||||||
| 
						 | 
					@ -64,6 +79,21 @@ class Ameba::Config
 | 
				
			||||||
    @formatter ||= default_formatter
 | 
					    @formatter ||= default_formatter
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # Sets formatter by name.
 | 
				
			||||||
 | 
					  #
 | 
				
			||||||
 | 
					  # ```
 | 
				
			||||||
 | 
					  # config = Ameba::Config.load
 | 
				
			||||||
 | 
					  # config.formatter = :progress
 | 
				
			||||||
 | 
					  # ```
 | 
				
			||||||
 | 
					  #
 | 
				
			||||||
 | 
					  def formatter=(name : String | Symbol)
 | 
				
			||||||
 | 
					    if f = AVAILABLE_FORMATTERS[name]?
 | 
				
			||||||
 | 
					      @formatter = f.new
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					      raise "Unknown formatter `#{name}`. Use one of #{Config.formatter_names}."
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Updates rule properties.
 | 
					  # Updates rule properties.
 | 
				
			||||||
  #
 | 
					  #
 | 
				
			||||||
  # ```
 | 
					  # ```
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue