lib/pdd.rb in pdd-0.17.9 vs lib/pdd.rb in pdd-0.18

- old
+ new

@@ -1,7 +1,5 @@ -# encoding: utf-8 -# # Copyright (c) 2014-2017 Yegor Bugayenko # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the 'Software'), to deal # in the Software without restriction, including without limitation the rights @@ -21,10 +19,11 @@ # SOFTWARE. require 'nokogiri' require 'logger' require 'time' +require 'rainbow' require_relative 'pdd/sources' require_relative 'pdd/version' require_relative 'pdd/rule/estimates' require_relative 'pdd/rule/text' require_relative 'pdd/rule/duplicates' @@ -51,14 +50,18 @@ 'available-roles' => PDD::Rule::Roles::Available }.freeze # Get logger. def self.log - unless @logger + unless defined?(@logger) @logger = Logger.new(STDOUT) @logger.formatter = proc { |severity, _, _, msg| - "#{severity}: #{msg.dump}\n" + if severity == 'ERROR' + "#{Rainbow(severity).red}: #{msg}\n" + else + "#{msg}\n" + end } @logger.level = Logger::ERROR end @logger end @@ -73,31 +76,33 @@ # +opts+:: Options def initialize(opts) @opts = opts PDD.log.level = Logger::INFO if @opts[:verbose] PDD.log.level = Logger::ERROR if @opts[:quiet] - PDD.log.info "my version is #{PDD::VERSION}" + PDD.log.info "My version is #{PDD::VERSION}" PDD.log.info "Ruby version is #{RUBY_VERSION} at #{RUBY_PLATFORM}" end # Generate XML. def xml dir = @opts[:source] ? @opts[:source] : Dir.pwd - PDD.log.info "reading #{dir}" + PDD.log.info "Reading #{dir}" sources = Sources.new(dir) - @opts[:exclude].each do |p| - sources = sources.exclude(p) - PDD.log.info "excluding #{p}" - end unless @opts[:exclude].nil? + unless @opts[:exclude].nil? + @opts[:exclude].each do |p| + sources = sources.exclude(p) + PDD.log.info "Excluding #{p}" + end + end sanitize( rules( Nokogiri::XML::Builder.new do |xml| xml << "<?xml-stylesheet type='text/xsl' href='#{xsl}'?>" xml.puzzles(attrs) do sources.fetch.each do |source| source.puzzles.each do |puzzle| - PDD.log.info "puzzle #{puzzle.props[:id]} " \ + PDD.log.info "Puzzle #{puzzle.props[:id]} " \ "#{puzzle.props[:estimate]}/#{puzzle.props[:role]}" \ " at #{puzzle.props[:file]}" render puzzle, xml end end @@ -143,16 +148,16 @@ raise PDD::Error, 'You can\'t modify max-duplicates, it\'s always 1' end list.push('max-duplicates:1').map do |r| name, value = r.split(':') rule = RULES[name] - raise "rule '#{name}' doesn't exist" if rule.nil? + raise "Rule '#{name}' doesn't exist" if rule.nil? rule.new(doc, value).errors.each do |e| PDD.log.error e total += 1 end end - raise PDD::Error, "#{total} errors, see log above" unless total == 0 + raise PDD::Error, "#{total} errors, see log above" unless total.zero? xml end def sanitize(xml) xsd = Nokogiri::XML::Schema(