|
| 1 | +require 'unparser' |
| 2 | +require 'mutant' |
| 3 | +require 'optparse' |
| 4 | + |
| 5 | +require 'unparser/cli/preprocessor' |
| 6 | +require 'unparser/cli/source' |
| 7 | + |
| 8 | +module Unparser |
| 9 | + # Unparser CLI implementation |
| 10 | + class CLI |
| 11 | + |
| 12 | + # Unparser CLI specific differ |
| 13 | + class Differ < Mutant::Differ |
| 14 | + |
| 15 | + def self.run(*arguments) |
| 16 | + new(*arguments).colorized_diff |
| 17 | + end |
| 18 | + |
| 19 | + # Return source diff |
| 20 | + # |
| 21 | + # @return [String] |
| 22 | + # if there is a diff |
| 23 | + # |
| 24 | + # @return [nil] |
| 25 | + # otherwise |
| 26 | + # |
| 27 | + # @api private |
| 28 | + # |
| 29 | + def diff |
| 30 | + diffs.map do |piece| |
| 31 | + Diff::LCS::Hunk.new(old, new, piece, max_length, old.length - new.length).diff(:unified) << "\n" |
| 32 | + end.join |
| 33 | + end |
| 34 | + memoize :diff |
| 35 | + |
| 36 | + end # Differ |
| 37 | + |
| 38 | + # Run CLI |
| 39 | + # |
| 40 | + # @param [Array<String>] arguments |
| 41 | + # |
| 42 | + # @return [Fixnum] |
| 43 | + # the exit status |
| 44 | + # |
| 45 | + # @api private |
| 46 | + # |
| 47 | + def self.run(*arguments) |
| 48 | + new(*arguments).exit_status |
| 49 | + end |
| 50 | + |
| 51 | + # Initialize object |
| 52 | + # |
| 53 | + # @param [Array<String>] arguments |
| 54 | + # |
| 55 | + # @return [undefined] |
| 56 | + # |
| 57 | + # @api private |
| 58 | + # |
| 59 | + def initialize(arguments) |
| 60 | + @sources = [] |
| 61 | + |
| 62 | + opts = OptionParser.new do |builder| |
| 63 | + add_options(builder) |
| 64 | + end |
| 65 | + |
| 66 | + file_names = |
| 67 | + begin |
| 68 | + opts.parse!(arguments) |
| 69 | + rescue OptionParser::ParseError => error |
| 70 | + raise(Error, error.message, error.backtrace) |
| 71 | + end |
| 72 | + |
| 73 | + file_names.each do |file_name| |
| 74 | + @sources << Source::File.new(file_name) |
| 75 | + end |
| 76 | + |
| 77 | + @exit_success = true |
| 78 | + end |
| 79 | + |
| 80 | + # Add options |
| 81 | + # |
| 82 | + # @param [Optparse::Builder] builder |
| 83 | + # |
| 84 | + # @return [undefined] |
| 85 | + # |
| 86 | + # @api private |
| 87 | + # |
| 88 | + def add_options(builder) |
| 89 | + builder.banner = 'usage: unparse [options] FILE [FILE]' |
| 90 | + builder.separator('').on('-e', '--evaluate SOURCE') do |original_source| |
| 91 | + @sources << Source::String.new(original_source) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + # Return exit status |
| 96 | + # |
| 97 | + # @return [Fixnum] |
| 98 | + # |
| 99 | + # @api private |
| 100 | + # |
| 101 | + def exit_status |
| 102 | + @sources.each do |source| |
| 103 | + process_source(source) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + private |
| 108 | + |
| 109 | + # Process source |
| 110 | + # |
| 111 | + # @param [CLI::Source] |
| 112 | + # |
| 113 | + # @return [undefined] |
| 114 | + # |
| 115 | + # @api private |
| 116 | + # |
| 117 | + def process_source(source) |
| 118 | + if source.success? |
| 119 | + puts "Success: #{source.identification}" |
| 120 | + else |
| 121 | + puts source.error_report |
| 122 | + puts "Error: #{source.identification}" |
| 123 | + @exit_success = false |
| 124 | + end |
| 125 | + end |
| 126 | + end # CLI |
| 127 | +end # Unparser |
0 commit comments