rubocopfmt 0.1.0.beta2 → 0.1.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5964e7d80d992e05a65799d94271249cfe563e36
4
- data.tar.gz: 97623211f9bf546a16654aecda5117b2a3f9b1bd
3
+ metadata.gz: 670799ba9de4c1e7446930905a3decb4212e6ae9
4
+ data.tar.gz: 2f02bc7f06cad9d23c8f74dc476d53d50cad8d77
5
5
  SHA512:
6
- metadata.gz: f38caac15512b59497dd4b07dda77beb1da67bd854cddd4e90c9b6f36dc82a9e84479f23c8ec4acdf79096926bf7c64e5266d2b042fe909bee8c268bfe55f753
7
- data.tar.gz: 85d365454150441eb9cd9aea763e2ef9f7f7819086b47c5610eed1d769d9363129b7ff90882daf90696ee5d5ffde268c1a1e407b73a9967a27396894997369c3
6
+ metadata.gz: 3c64289852893568c1eefb3edff608ca6e70cfc84436e789dcbabd5f01ca2d7bbf9cc3adc7eede5973af4e0ded3559147afe51077045d10b881532fc8310b442
7
+ data.tar.gz: 580314ca1bb4c094313d002bc8d522f315c6bdb11831640358cd3696ca56f55f2e271c2374d7e1571a7ea72222ea3eacce3c1bbcc29c967b2278cec6a9f8b07c
data/README.md CHANGED
@@ -18,12 +18,14 @@ Usage: rubocopfmt [options] [path ...]
18
18
  Reads from STDIN if no path is given.
19
19
 
20
20
  Options:
21
- -d, --diff Display diffs instead of rewriting files.
22
- -l, --list List files whose formatting is incorrect.
23
- -w, --write Write result to (source) file instead of STDOUT.
24
-
25
- -v, --version Show version.
26
- -h, --help Show this message.
21
+ -d, --diff Display diffs instead of rewriting files.
22
+ -l, --list List files whose formatting is incorrect.
23
+ -w, --write Write result to (source) file instead of STDOUT.
24
+ -F, --stdin-file=<s> Optionally provide file path when using STDIN.
25
+ -D, --diff-format=<s> Display diffs using format: unified, rcs, context
26
+
27
+ -v, --version Print version and exit
28
+ -h, --help Show this message
27
29
  ```
28
30
 
29
31
  ## Configure
@@ -1,4 +1,5 @@
1
- require 'rubocopfmt/options_parser'
1
+ require 'rubocop'
2
+
2
3
  require 'rubocopfmt/formatter'
3
4
 
4
5
  module RuboCopFMT
@@ -25,7 +26,7 @@ module RuboCopFMT
25
26
  end
26
27
 
27
28
  def correct
28
- Rainbow.enabled = false
29
+ Rainbow.enabled = false if defined?(Rainbow)
29
30
  @runner = RuboCop::Runner.new(options, config_store)
30
31
  @runner.run(paths)
31
32
  options[:stdin]
@@ -1,7 +1,6 @@
1
- require 'diffy'
2
- require 'rubocop'
3
-
1
+ require 'rubocopfmt/diff'
4
2
  require 'rubocopfmt/errors'
3
+ require 'rubocopfmt/options_parser'
5
4
  require 'rubocopfmt/source'
6
5
 
7
6
  module RuboCopFMT
@@ -32,10 +31,6 @@ module RuboCopFMT
32
31
 
33
32
  private
34
33
 
35
- def auto_correct_sources
36
- sources.map(&:auto_correct)
37
- end
38
-
39
34
  def require_real_files(flag)
40
35
  return unless @options.paths.empty?
41
36
 
@@ -45,16 +40,14 @@ module RuboCopFMT
45
40
 
46
41
  def print_corrected_list
47
42
  require_real_files('--list')
48
- auto_correct_sources
49
43
 
50
- sources.each { |c| puts c.path if c.corrected? }
44
+ for_corrected_source do |source|
45
+ puts source.path
46
+ end
51
47
  end
52
48
 
53
49
  def print_diff_of_corrections
54
- auto_correct_sources
55
-
56
- sources.each do |source|
57
- next unless source.corrected?
50
+ for_corrected_source do |source|
58
51
  puts "diff #{source.path} rubocopfmt/#{source.path}" if source.path
59
52
  puts diff_source(source)
60
53
  end
@@ -62,17 +55,16 @@ module RuboCopFMT
62
55
 
63
56
  def write_corrected_source
64
57
  require_real_files('--write')
65
- auto_correct_sources
66
58
 
67
- sources.each do |source|
68
- File.write(source.path, source.output) if source.corrected?
59
+ for_corrected_source do |source|
60
+ File.write(source.path, source.output)
69
61
  end
70
62
  end
71
63
 
72
64
  def print_corrected_source
73
- auto_correct_sources
74
-
75
- sources.each { |source| print source.output }
65
+ for_corrected_source(skip_unchanged: false) do |source|
66
+ print source.output
67
+ end
76
68
  end
77
69
 
78
70
  def sources
@@ -87,14 +79,18 @@ module RuboCopFMT
87
79
  end
88
80
  end
89
81
 
90
- def diff_source(source)
91
- diff = Diffy::Diff.new(
92
- source.input, source.output,
93
- include_diff_info: true,
94
- diff: '-U 3'
95
- )
82
+ def for_corrected_source(skip_unchanged: true)
83
+ sources.each do |source|
84
+ source.auto_correct
85
+ next if skip_unchanged && !source.corrected?
96
86
 
97
- diff.to_s(:text)
87
+ yield(source)
88
+ end
89
+ end
90
+
91
+ def diff_source(source)
92
+ diff = Diff.new(source)
93
+ diff.render(options.diff_format)
98
94
  end
99
95
 
100
96
  def new_source_from_stdin(path = nil)
@@ -0,0 +1,40 @@
1
+ require 'diffy'
2
+
3
+ module RuboCopFMT
4
+ class Diff
5
+ FORMATS = {
6
+ unified: '-U 3',
7
+ rcs: '-n',
8
+ context: '-C 3'
9
+ }.freeze
10
+
11
+ DEFAULT_FORMAT = :unified
12
+
13
+ attr_reader :source
14
+
15
+ def self.valid_format?(format)
16
+ FORMATS.key?(format.downcase.to_sym)
17
+ end
18
+
19
+ def initialize(source)
20
+ @source = source
21
+ end
22
+
23
+ def render(format = nil)
24
+ diff = Diffy::Diff.new(
25
+ source.input, source.output,
26
+ include_diff_info: true,
27
+ diff: diff_opts(format)
28
+ )
29
+
30
+ diff.to_s(:text)
31
+ end
32
+
33
+ private
34
+
35
+ def diff_opts(format = nil)
36
+ return FORMATS[format.downcase.to_sym] if format
37
+ FORMATS[DEFAULT_FORMAT]
38
+ end
39
+ end
40
+ end
@@ -3,6 +3,7 @@ require 'rubocopfmt/options_parser'
3
3
  module RuboCopFMT
4
4
  class Options
5
5
  attr_accessor :stdin_file
6
+ attr_accessor :diff_format
6
7
 
7
8
  def paths
8
9
  @paths.nil? ? [] : @paths
@@ -37,16 +37,26 @@ module RuboCopFMT
37
37
  opt :list, 'List files whose formatting is incorrect.'
38
38
  opt :write, 'Write result to (source) file instead of STDOUT.'
39
39
  opt :stdin_file, 'Optionally provide file path when using STDIN.',
40
- short: 'f', type: :string
40
+ short: 'F', type: :string
41
+ opt :diff_format, 'Display diffs using format: unified, rcs, context',
42
+ short: 'D', type: :string
41
43
  banner ''
42
44
 
43
45
  conflicts :diff, :list, :write
46
+ conflicts :diff_format, :list, :write
47
+ end
48
+
49
+ if opts[:diff_format] && !Diff.valid_format?(opts[:diff_format])
50
+ Trollop.die :diff_format,
51
+ "does not support \"#{opts[:diff_format]}\" format"
52
+ end
53
+
54
+ opts[:diff] = true if opts[:diff_format] && !opts[:diff]
55
+
56
+ opts.each do |k, v|
57
+ options.send("#{k}=", v) if options.respond_to?("#{k}=")
44
58
  end
45
59
 
46
- options.diff = opts[:diff]
47
- options.list = opts[:list]
48
- options.write = opts[:write]
49
- options.stdin_file = opts[:stdin_file]
50
60
  options
51
61
  end
52
62
  end
@@ -1,3 +1,3 @@
1
1
  module RuboCopFMT
2
- VERSION = '0.1.0.beta2'.freeze
2
+ VERSION = '0.1.0.beta3'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocopfmt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.beta2
4
+ version: 0.1.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Myhrberg
@@ -130,6 +130,7 @@ files:
130
130
  - lib/rubocopfmt/auto_corrector.rb
131
131
  - lib/rubocopfmt/cli.rb
132
132
  - lib/rubocopfmt/core_ext/string.rb
133
+ - lib/rubocopfmt/diff.rb
133
134
  - lib/rubocopfmt/errors.rb
134
135
  - lib/rubocopfmt/formatter.rb
135
136
  - lib/rubocopfmt/options.rb