Skip to content

Commit c10d185

Browse files
committed
Move implementation of unparser gem to Unparser::CLI
* Rename executable from test-unparser to unparser * Add a "emitter style" preprocessor * Use procto gem for method objects
1 parent 471625a commit c10d185

9 files changed

Lines changed: 370 additions & 159 deletions

File tree

bin/test-unparser

Lines changed: 0 additions & 158 deletions
This file was deleted.

bin/unparser

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env ruby
2+
3+
trap('INT') do |status|
4+
exit! 128 + status
5+
end
6+
7+
require 'unparser/cli'
8+
9+
Unparser::CLI.run(ARGV)

config/flay.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
---
22
threshold: 13
3-
total_score: 407
3+
total_score: 463

lib/unparser.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'abstract_type'
2+
require 'procto'
23
require 'concord'
34
require 'parser/all'
45

lib/unparser/cli.rb

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)