lib/pdd.rb in pdd-0.20.7 vs lib/pdd.rb in pdd-0.20.8

- old
+ new

@@ -50,15 +50,16 @@ }.freeze # Get logger. def self.log unless defined?(@logger) - @logger = Logger.new(STDOUT) + @logger = Logger.new($stdout) @logger.formatter = proc { |severity, _, _, msg| - if severity == 'ERROR' + case severity + when 'ERROR' "#{Rainbow(severity).red}: #{msg}\n" - elsif severity == 'WARN' + when 'WARN' "#{Rainbow(severity).orange}: #{msg}\n" else "#{msg}\n" end } @@ -85,24 +86,19 @@ 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}" + dir = @opts[:source] || Dir.pwd + PDD.log.info "Reading from root dir #{dir}" require_relative 'pdd/sources' - sources = Sources.new(dir) - @opts[:include]&.each do |p| - sources = sources.include(p) - end - @opts[:exclude]&.each do |p| - sources = sources.exclude(p) - PDD.log.info "Excluding #{p}" - end + sources = Sources.new(File.expand_path(dir)) + sources.exclude((@opts[:exclude] || []) + (@opts['skip-gitignore'] || [])) + sources.include(@opts[:include]) sanitize( rules( - Nokogiri::XML::Builder.new do |xml| + Nokogiri::XML::Builder.new(encoding: 'UTF-8') 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]} " \ @@ -150,20 +146,23 @@ total = 0 list = @opts[:rule] || [] unless list.select { |r| r.start_with?('max-duplicates:') }.empty? 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? + 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.zero? + xml end def sanitize(xml) xsd = Nokogiri::XML::Schema( @@ -171,9 +170,10 @@ ) errors = xsd.validate(Nokogiri::XML(xml)).map(&:message) errors.each { |e| PDD.log.error e } PDD.log.error(xml) unless errors.empty? raise SchemaError, errors.join('; ') unless errors.empty? + xml end end end