rubocopfmt 0.1.0.beta4 → 0.1.0.beta5

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: 4caf033607e3f55dc1e4dce78e4f5ffeded58566
4
- data.tar.gz: ef5a0d55ffa56e6e2103dfdbb6e90aeffb0d731b
3
+ metadata.gz: 0fa9af7cf679b86a5ef27cf338e97c13b8bda718
4
+ data.tar.gz: bc0456f52025e46daa569daa4c6057a38c95f853
5
5
  SHA512:
6
- metadata.gz: 3d8618a439c6691bf5b52ecdee3a9e67513985d5dc5364c3e1d185d80c9b3dd9285422763e20ced467efac3de92eb22f10c72cf38b4b17f5e357da1a5984c16c
7
- data.tar.gz: 7205b46d71e7fc7382bfe4baa5599003ed769a6250243827f6b45b9acf49f2f56b8aa1726a9efb4afbcd4654a4bcd989d7ed2ad4fcd22a6f2fffc62c659ea78f
6
+ metadata.gz: 6a761e3295527c8d8f39ce6e31b6ddd29263cd5d79ebba0bb9616e5765be22957c87ea428a3a7f022f77945fd780a55c121aeb029c3ecdad22467bb780c64ba5
7
+ data.tar.gz: 717eeb9ee0c8134293c07876f8c7e2eac7e729ab75d2ad611f87858627317d7c6cd7e4f9bbe161aa60392d05896d41ff3fac8354e4d2618df0d3d1a49b2567d1
@@ -1,6 +1,6 @@
1
1
  require 'rubocop'
2
2
 
3
- require 'rubocopfmt/formatter'
3
+ require 'rubocopfmt/rubocop_formatter'
4
4
 
5
5
  module RuboCopFMT
6
6
  class AutoCorrector
@@ -13,7 +13,7 @@ module RuboCopFMT
13
13
  RUBOCOP_ARGV = [
14
14
  '--auto-correct',
15
15
  '--cache', 'false',
16
- '--format', 'RuboCopFMT::Formatter',
16
+ '--format', 'RuboCopFMT::RubocopFormatter',
17
17
  '--except', DISABLED_COPS.join(',')
18
18
  ].freeze
19
19
 
@@ -27,7 +27,7 @@ module RuboCopFMT
27
27
 
28
28
  def correct
29
29
  Rainbow.enabled = false if defined?(Rainbow)
30
- @runner = RuboCop::Runner.new(options, config_store)
30
+ @runner = ::RuboCop::Runner.new(options, config_store)
31
31
  @runner.run(paths)
32
32
  options[:stdin]
33
33
  end
@@ -37,7 +37,7 @@ module RuboCopFMT
37
37
  def config_store
38
38
  return @config_store if @config_store
39
39
 
40
- @config_store = RuboCop::ConfigStore.new
40
+ @config_store = ::RuboCop::ConfigStore.new
41
41
  @config_store.options_config = options[:config] if options[:config]
42
42
  @config_store.force_default_config! if options[:force_default_config]
43
43
  @config_store
@@ -59,7 +59,7 @@ module RuboCopFMT
59
59
 
60
60
  def set_options_and_paths
61
61
  argv = RUBOCOP_ARGV + [@path || 'fake.rb']
62
- @options, @paths = RuboCop::Options.new.parse(argv)
62
+ @options, @paths = ::RuboCop::Options.new.parse(argv)
63
63
  @options[:stdin] = source
64
64
 
65
65
  [@options, @paths]
@@ -1,7 +1,7 @@
1
1
  require 'rubocopfmt/diff'
2
2
  require 'rubocopfmt/errors'
3
- require 'rubocopfmt/options_parser'
4
- require 'rubocopfmt/source'
3
+ require 'rubocopfmt/cli_options'
4
+ require 'rubocopfmt/formatter'
5
5
 
6
6
  module RuboCopFMT
7
7
  class CLI
@@ -10,12 +10,17 @@ module RuboCopFMT
10
10
  end
11
11
 
12
12
  attr_reader :options
13
+ attr_reader :sources
13
14
 
14
15
  def initialize(args)
15
- @options = OptionsParser.parse(args)
16
+ @options = CLIOptions.parse(args)
16
17
  end
17
18
 
18
19
  def run
20
+ runner = Formatter.new(options)
21
+ runner.run
22
+ @sources = runner.sources
23
+
19
24
  if @options.list
20
25
  print_corrected_list
21
26
  elsif @options.diff
@@ -31,23 +36,16 @@ module RuboCopFMT
31
36
 
32
37
  private
33
38
 
34
- def require_real_files(flag)
35
- return unless @options.paths.empty?
36
-
37
- $stderr.puts "ERROR: To use #{flag} you must specify one or more files"
38
- exit 1
39
- end
40
-
41
39
  def print_corrected_list
42
- require_real_files('--list')
43
-
44
- for_corrected_source do |source|
45
- puts source.path
40
+ sources.each do |source|
41
+ puts source.path if source.corrected?
46
42
  end
47
43
  end
48
44
 
49
45
  def print_diff_of_corrections
50
- for_corrected_source do |source|
46
+ sources.each do |source|
47
+ next unless source.corrected?
48
+
51
49
  if source.path && sources.size > 1
52
50
  puts "diff #{source.path} rubocopfmt/#{source.path}"
53
51
  end
@@ -56,37 +54,14 @@ module RuboCopFMT
56
54
  end
57
55
 
58
56
  def write_corrected_source
59
- require_real_files('--write')
60
-
61
- for_corrected_source do |source|
62
- File.write(source.path, source.output)
57
+ sources.each do |source|
58
+ File.write(source.path, source.output) if source.corrected?
63
59
  end
64
60
  end
65
61
 
66
62
  def print_corrected_source
67
- for_corrected_source(skip_unchanged: false) do |source|
68
- print source.output
69
- end
70
- end
71
-
72
- def sources
73
- return @sources if @sources
74
-
75
- if options.paths.empty?
76
- @sources = [new_source_from_stdin(options.stdin_file)]
77
- else
78
- @sources = options.paths.map do |path|
79
- new_source_from_file(path)
80
- end
81
- end
82
- end
83
-
84
- def for_corrected_source(skip_unchanged: true)
85
63
  sources.each do |source|
86
- source.auto_correct
87
- next if skip_unchanged && !source.corrected?
88
-
89
- yield(source)
64
+ print source.output
90
65
  end
91
66
  end
92
67
 
@@ -94,16 +69,5 @@ module RuboCopFMT
94
69
  diff = Diff.new(source)
95
70
  diff.render(options.diff_format)
96
71
  end
97
-
98
- def new_source_from_stdin(path = nil)
99
- Source.new($stdin.binmode.read, path)
100
- end
101
-
102
- def new_source_from_file(path)
103
- raise FileNotFound, "File not found: #{path}" unless File.exist?(path)
104
-
105
- source = File.read(path, mode: 'rb')
106
- Source.new(source, path)
107
- end
108
72
  end
109
73
  end
@@ -6,22 +6,25 @@ require 'rubocopfmt/options'
6
6
  require 'rubocopfmt/version'
7
7
 
8
8
  module RuboCopFMT
9
- class OptionsParser
9
+ class CLIOptions
10
10
  class << self
11
- def parse(args, options = nil)
11
+ def parse(args)
12
12
  args = args.clone
13
- options ||= Options.new
14
13
 
15
- parse_flags(args, options)
16
- options.paths = args unless args.empty?
14
+ opts = parse_args(args)
15
+ opts[:paths] = args
16
+ opts[:diff] = true if opts[:diff_format] && !opts[:diff]
17
+
18
+ validate_diff_format(opts)
19
+ validate_paths(opts)
17
20
 
18
- options
21
+ opts
19
22
  end
20
23
 
21
24
  private
22
25
 
23
- def parse_flags(args, options)
24
- opts = Trollop.options(args) do
26
+ def parse_args(args)
27
+ Trollop.options(args) do
25
28
  banner <<-EOF.undent
26
29
  Usage: rubocopfmt [options] [path ...]
27
30
 
@@ -30,8 +33,8 @@ module RuboCopFMT
30
33
  Options:
31
34
  EOF
32
35
 
33
- version "rubocopfmt #{RuboCopFMT::VERSION}" \
34
- " (rubocop #{RuboCop::Version::STRING})"
36
+ version "rubocopfmt #{RuboCopFMT::VERSION} " \
37
+ "(rubocop #{::RuboCop::Version::STRING})"
35
38
 
36
39
  opt :diff, 'Display diffs instead of rewriting files.'
37
40
  opt :list, 'List files whose formatting is incorrect.'
@@ -45,19 +48,22 @@ module RuboCopFMT
45
48
  conflicts :diff, :list, :write
46
49
  conflicts :diff_format, :list, :write
47
50
  end
51
+ end
48
52
 
49
- if opts[:diff_format] && !Diff.valid_format?(opts[:diff_format])
50
- Trollop.die :diff_format,
51
- "does not support \"#{opts[:diff_format]}\" format"
53
+ def validate_paths(opts)
54
+ [:list, :write].each do |opt|
55
+ if opts[opt] && opts[:paths].empty?
56
+ Trollop.die(opt, 'requires one or more paths to be specified')
57
+ end
52
58
  end
59
+ end
53
60
 
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}=")
58
- end
61
+ def validate_diff_format(opts)
62
+ return if opts[:diff_format].nil? \
63
+ || Diff.valid_format?(opts[:diff_format])
59
64
 
60
- options
65
+ Trollop.die(:diff_format,
66
+ "does not support \"#{opts[:diff_format]}\" format")
61
67
  end
62
68
  end
63
69
  end
@@ -27,11 +27,17 @@ module RuboCopFMT
27
27
  diff: diff_opts(format)
28
28
  )
29
29
 
30
- diff.to_s(:text)
30
+ out = diff.to_s(:text)
31
+ out << "\n" if rcs?(format)
32
+ out
31
33
  end
32
34
 
33
35
  private
34
36
 
37
+ def rcs?(format)
38
+ diff_opts(format) == FORMATS[:rcs]
39
+ end
40
+
35
41
  def diff_opts(format = nil)
36
42
  format ? FORMATS[format.downcase.to_sym] : DEFAULT_FORMAT
37
43
  end
@@ -1,6 +1,49 @@
1
- require 'rubocop'
1
+ require 'rubocopfmt/errors'
2
+ require 'rubocopfmt/options'
3
+ require 'rubocopfmt/source'
2
4
 
3
5
  module RuboCopFMT
4
- # Does nothing, as we want nothing output but the corrected source code.
5
- class Formatter < RuboCop::Formatter::BaseFormatter; end
6
+ class Formatter
7
+ attr_reader :sources
8
+
9
+ def initialize(opts = {})
10
+ opts.each do |k, v|
11
+ options.send("#{k}=", v) if options.respond_to?("#{k}=")
12
+ end
13
+ end
14
+
15
+ def run
16
+ sources = build_sources
17
+ sources.map(&:auto_correct)
18
+
19
+ @sources = sources
20
+ end
21
+
22
+ def options
23
+ @options ||= Options.new
24
+ end
25
+
26
+ private
27
+
28
+ def build_sources
29
+ if options.paths.empty?
30
+ [new_source_from_stdin(options.stdin_file)]
31
+ else
32
+ options.paths.map do |path|
33
+ new_source_from_file(path)
34
+ end
35
+ end
36
+ end
37
+
38
+ def new_source_from_stdin(path = nil)
39
+ Source.new($stdin.binmode.read, path)
40
+ end
41
+
42
+ def new_source_from_file(path)
43
+ raise FileNotFound, "File not found: #{path}" unless File.exist?(path)
44
+
45
+ source = File.read(path, mode: 'rb')
46
+ Source.new(source, path)
47
+ end
48
+ end
6
49
  end
@@ -1,5 +1,3 @@
1
- require 'rubocopfmt/options_parser'
2
-
3
1
  module RuboCopFMT
4
2
  class Options
5
3
  attr_accessor :stdin_file
@@ -0,0 +1,6 @@
1
+ require 'rubocop'
2
+
3
+ module RuboCopFMT
4
+ # Does nothing, as we want nothing output but the corrected source code.
5
+ class RubocopFormatter < ::RuboCop::Formatter::BaseFormatter; end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module RuboCopFMT
2
- VERSION = '0.1.0.beta4'.freeze
2
+ VERSION = '0.1.0.beta5'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocopfmt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.beta4
4
+ version: 0.1.0.beta5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Myhrberg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-09 00:00:00.000000000 Z
11
+ date: 2017-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -130,12 +130,13 @@ files:
130
130
  - lib/rubocopfmt.rb
131
131
  - lib/rubocopfmt/auto_corrector.rb
132
132
  - lib/rubocopfmt/cli.rb
133
+ - lib/rubocopfmt/cli_options.rb
133
134
  - lib/rubocopfmt/core_ext/string.rb
134
135
  - lib/rubocopfmt/diff.rb
135
136
  - lib/rubocopfmt/errors.rb
136
137
  - lib/rubocopfmt/formatter.rb
137
138
  - lib/rubocopfmt/options.rb
138
- - lib/rubocopfmt/options_parser.rb
139
+ - lib/rubocopfmt/rubocop_formatter.rb
139
140
  - lib/rubocopfmt/source.rb
140
141
  - lib/rubocopfmt/version.rb
141
142
  - rubocopfmt.gemspec