#!/usr/bin/env ruby # 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 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. STDOUT.sync = true require 'slop' require 'nokogiri' require 'rainbow' require_relative '../lib/pdd' require_relative '../lib/pdd/version' require_relative '../lib/pdd/source' begin args = [] if File.exist?('.pdd') cfg = File.new('.pdd') body = File.read(cfg) extra = body.split(/\s+/).map(&:strip) args += extra puts "Found #{body.split(/\n/).length} lines in #{File.absolute_path(cfg)}" end args += ARGV begin opts = Slop.parse(args, strict: true, help: true) do |o| o.banner = "Usage (#{PDD::VERSION}): pdd [options]" o.bool '-h', '--help', 'Show these instructions' o.bool '-v', '--verbose', 'Enable verbose mode (a lot of logging)' o.bool '-q', '--quiet', 'Enable quiet mode (almost no logging)' o.bool '--skip-gitignore', 'Don\'t look into .gitignore for excludes' o.bool '-i', '--version', 'Show current version' do puts PDD::VERSION exit end o.string '-s', '--source', 'Source directory to parse ("." by default)' o.string '-f', '--file', 'File to save XML into' o.array '-e', '--exclude', 'Glob pattern to exclude, e.g. "**/*.jpg"', default: [] o.string '-t', '--format', 'Format of the report (xml|html)' o.array( '-r', '--rule', 'Rule to apply (can be used many times)', delimiter: ';' ) end rescue Slop::Error => ex raise StandardError, "#{ex.message}, try --help" end if opts.help? puts opts puts "This is our README to learn more: \ https://github.com/yegor256/pdd/blob/master/README.md" exit end if opts.verbose? && !opts.file? raise '-f is mandatory when using -v, try --help for more information' end if opts['skip-gitignore'] raise 'For --skip-gitignore see https://github.com/yegor256/pdd/issues/80' end Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 file = opts.file? ? File.new(opts[:file], 'w') : STDOUT output = PDD::Base.new(opts).xml if opts[:format] if opts[:format] == 'html' xslt = File.join( File.dirname(File.dirname(__FILE__)), 'assets', 'puzzles.xsl' ) output = Nokogiri::XSLT(File.read(xslt)).transform(Nokogiri::XML(output)) elsif opts[:format] != 'xml' raise 'Invalid format, use html or xml' end end file << output rescue SystemExit => ex puts ex.message unless ex.success? exit(ex.status) rescue PDD::Error => ex puts "#{Rainbow('ERROR').red}: #{ex.message}. \ If you can't understand the cause of this issue or you don't know \ how to fix it, please submit a GitHub issue, we will try to help you: \ https://github.com/yegor256/pdd/issues. This tool is still in its beta \ version and we will appreciate your feedback. Here is where you can find \ more documentation: https://github.com/yegor256/pdd/blob/master/README.md." exit(1) rescue StandardError => ex puts "#{Rainbow('ERROR').red} (#{ex.class.name}): #{ex.message}" exit(-1) end