rubocopfmt 0.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a012ee79b8442b495047743bacaa7638bd12f39e
4
+ data.tar.gz: 0f68b98d269a76ea2a4049c1dc8ffc66f56e6653
5
+ SHA512:
6
+ metadata.gz: 2db966f9d9b1e13cc952dfa6121cec9e08c63e7774ac0c5bd86d90175c43acb0a4611ebdc3b9ea57c42106d6df59a06edc6846e89bc312078017db352e9cbfb6
7
+ data.tar.gz: 4c957d666f21e30862be1b889659bcf36b6b9ab08cc2aa2307368e7673410dbb4b5eb8da074bbc5bcbf9a0526ba7963e08061baa8a1cce997cb9ad111426d6ab
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubocopfmt.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2017 Jim Myhrberg.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # rubocopfmt
2
+
3
+ Easy formatting of Ruby code
4
+ using [RuboCop](https://github.com/bbatsov/rubocop). Analogous
5
+ to [`gofmt`](https://golang.org/cmd/gofmt/).
6
+
7
+ ## Installation
8
+
9
+ ```
10
+ gem install rubocopfmt
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```
16
+ Usage: rubocopfmt [options] [path ...]
17
+
18
+ Reads from STDIN if no path is given.
19
+
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.
27
+ ```
28
+
29
+ ## Configure
30
+
31
+ Please refer to
32
+ RuboCop's [Documentation](http://rubocop.readthedocs.io/en/latest/).
33
+
34
+ ## Editor Integration
35
+
36
+ _Coming Soon!_
37
+
38
+ ## Todo
39
+
40
+ - Editor integration!
41
+ - "Stable" release
42
+ - Improve tests
43
+
44
+ ## License
45
+
46
+ (The MIT license)
47
+
48
+ Copyright (c) 2017 Jim Myhrberg.
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining a copy
51
+ of this software and associated documentation files (the "Software"), to deal
52
+ in the Software without restriction, including without limitation the rights
53
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
54
+ copies of the Software, and to permit persons to whom the Software is
55
+ furnished to do so, subject to the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be included in all
58
+ copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
62
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
63
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
64
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
65
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
66
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubocopfmt"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/rubocopfmt ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.expand_path('../../lib', File.realpath(__FILE__)))
3
+
4
+ require 'rubocopfmt'
5
+
6
+ result = RuboCopFMT::CLI.run(ARGV)
7
+
8
+ exit result
@@ -0,0 +1,50 @@
1
+ require 'rubocopfmt/options_parser'
2
+ require 'rubocopfmt/formatter'
3
+
4
+ module RuboCopFMT
5
+ class AutoCorrector
6
+ DISABLED_COPS = [
7
+ 'Lint/Debugger',
8
+ 'Lint/UnusedMethodArgument',
9
+ 'Lint/UnusedBlockArgument'
10
+ ].freeze
11
+
12
+ RUBOCOP_OPTS = [
13
+ '--auto-correct',
14
+ '--cache', 'false',
15
+ '--format', 'RuboCopFMT::Formatter',
16
+ '--except', DISABLED_COPS.join(','),
17
+ 'fake.rb'
18
+ ].freeze
19
+
20
+ attr_reader :source
21
+ attr_reader :runner
22
+
23
+ def initialize(source)
24
+ @source = source
25
+ end
26
+
27
+ def correct
28
+ options, paths = RuboCop::Options.new.parse(RUBOCOP_OPTS)
29
+ config_store = RuboCop::ConfigStore.new
30
+
31
+ options[:stdin] = source
32
+
33
+ Rainbow.enabled = false
34
+ config_store.options_config = options[:config] if options[:config]
35
+ config_store.force_default_config! if options[:force_default_config]
36
+
37
+ @runner = RuboCop::Runner.new(options, config_store)
38
+ @runner.run(paths)
39
+ options[:stdin]
40
+ end
41
+
42
+ def warnings
43
+ runner && runner.warnings
44
+ end
45
+
46
+ def errors
47
+ runner && runner.errors
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,111 @@
1
+ require 'diffy'
2
+ require 'rubocop'
3
+
4
+ require 'rubocopfmt/errors'
5
+ require 'rubocopfmt/source'
6
+
7
+ module RuboCopFMT
8
+ class CLI
9
+ def self.run(args = ARGV)
10
+ new(args).run
11
+ end
12
+
13
+ attr_reader :options
14
+
15
+ def initialize(args)
16
+ @options = OptionsParser.parse(args)
17
+ end
18
+
19
+ def run
20
+ if @options.list
21
+ print_corrected_list
22
+ elsif @options.diff
23
+ print_diff_of_corrections
24
+ elsif @options.write
25
+ write_corrected_source
26
+ else
27
+ print_corrected_source
28
+ end
29
+
30
+ 0
31
+ end
32
+
33
+ private
34
+
35
+ def auto_correct_sources
36
+ sources.map(&:auto_correct)
37
+ end
38
+
39
+ def require_real_files(flag)
40
+ return unless @options.files.empty?
41
+
42
+ $stderr.puts "ERROR: To use #{flag} you must specify one or more files"
43
+ exit 1
44
+ end
45
+
46
+ def print_corrected_list
47
+ require_real_files('--list')
48
+ auto_correct_sources
49
+
50
+ sources.each { |c| puts c.path if c.corrected? }
51
+ end
52
+
53
+ def print_diff_of_corrections
54
+ auto_correct_sources
55
+
56
+ sources.each do |source|
57
+ next unless source.corrected?
58
+ puts "diff #{source.path} rubocopfmt/#{source.path}" if source.path
59
+ puts diff_source(source)
60
+ end
61
+ end
62
+
63
+ def write_corrected_source
64
+ require_real_files('--write')
65
+ auto_correct_sources
66
+
67
+ sources.each do |source|
68
+ File.write(source.path, source.output) if source.corrected?
69
+ end
70
+ end
71
+
72
+ def print_corrected_source
73
+ auto_correct_sources
74
+
75
+ sources.each { |source| print source.output }
76
+ end
77
+
78
+ def sources
79
+ return @sources if @sources
80
+
81
+ if options.files.empty?
82
+ @sources = [new_source_from_stdin]
83
+ else
84
+ @sources = options.files.map do |path|
85
+ new_source_from_file(path)
86
+ end
87
+ end
88
+ end
89
+
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
+ )
96
+
97
+ diff.to_s(:text)
98
+ end
99
+
100
+ def new_source_from_stdin
101
+ Source.new($stdin.binmode.read)
102
+ end
103
+
104
+ def new_source_from_file(path)
105
+ raise FileNotFound, "File not found: #{path}" unless File.exist?(path)
106
+
107
+ source = File.read(path, mode: 'rb')
108
+ Source.new(source, path)
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,3 @@
1
+ module RuboCopFMT
2
+ class FileNotFound < StandardError; end
3
+ end
@@ -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 Formatter < RuboCop::Formatter::BaseFormatter; end
6
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubocopfmt/options_parser'
2
+
3
+ module RuboCopFMT
4
+ class Options
5
+ def files
6
+ @files.nil? ? [] : @files
7
+ end
8
+ attr_writer :files
9
+
10
+ def diff
11
+ @diff.nil? ? false : @diff
12
+ end
13
+ attr_writer :diff
14
+
15
+ def list
16
+ @list.nil? ? false : @list
17
+ end
18
+ attr_writer :list
19
+
20
+ def write
21
+ @write.nil? ? false : @write
22
+ end
23
+ attr_writer :write
24
+ end
25
+ end
@@ -0,0 +1,65 @@
1
+ require 'optparse'
2
+ require 'rubocop'
3
+
4
+ require 'rubocopfmt/options'
5
+ require 'rubocopfmt/version'
6
+
7
+ module RuboCopFMT
8
+ class OptionsParser
9
+ class << self
10
+ def parse(args, options = nil)
11
+ args = args.clone
12
+ options ||= Options.new
13
+
14
+ parse_flags(args, options)
15
+ options.files = args unless args.empty?
16
+
17
+ options
18
+ end
19
+
20
+ private
21
+
22
+ def parse_flags(args, options)
23
+ parser = OptionParser.new do |opts|
24
+ opts.program_name = 'rubocopfmt'
25
+ opts.version = RuboCopFMT::VERSION
26
+
27
+ opts.banner = 'Usage: rubocopfmt [options] [path ...]'
28
+ opts.separator ''
29
+ opts.separator 'Reads from STDIN if no path is given.'
30
+ opts.separator ''
31
+ opts.separator 'Options:'
32
+
33
+ opts.on(
34
+ '-d', '--diff',
35
+ 'Display diffs instead of rewriting files.'
36
+ ) { |v| options.diff = v }
37
+
38
+ opts.on(
39
+ '-l', '--list',
40
+ 'List files whose formatting is incorrect.'
41
+ ) { |v| options.list = v }
42
+
43
+ opts.on(
44
+ '-w', '--write',
45
+ 'Write result to (source) file instead of STDOUT.'
46
+ ) { |v| options.write = v }
47
+
48
+ opts.separator ''
49
+
50
+ opts.on('-v', '--version', 'Show version.') do
51
+ puts "#{opts.program_name} #{opts.version}" \
52
+ " (rubocop #{RuboCop::Version::STRING})"
53
+ exit
54
+ end
55
+
56
+ opts.on('-h', '--help', 'Show this message.') do
57
+ puts opts
58
+ exit
59
+ end
60
+ end
61
+ parser.parse!(args)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,28 @@
1
+ require 'rubocopfmt/auto_corrector'
2
+
3
+ module RuboCopFMT
4
+ class Source
5
+ attr_reader :path
6
+ attr_reader :input
7
+ attr_reader :output
8
+
9
+ def initialize(input, path = nil)
10
+ @input = input
11
+ @path = path
12
+ end
13
+
14
+ def auto_correct
15
+ return unless output.nil?
16
+
17
+ @corrector = AutoCorrector.new(input)
18
+ @output = @corrector.correct
19
+ end
20
+
21
+ def corrected?
22
+ return @corrected unless @corrected.nil?
23
+ return false if @output.nil?
24
+
25
+ @corrected = (@input != @output)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module RuboCopFMT
2
+ VERSION = '0.1.0.beta1'.freeze
3
+ end
data/lib/rubocopfmt.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'rubocopfmt/cli'
2
+ require 'rubocopfmt/version'
3
+
4
+ module RuboCopFMT
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubocopfmt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rubocopfmt'
8
+ spec.version = RuboCopFMT::VERSION
9
+ spec.authors = ['Jim Myhrberg']
10
+ spec.email = ['contact@jimeh.me']
11
+
12
+ spec.summary = 'Easy formatting of Ruby code using RuboCop'
13
+ spec.description = 'Easy formatting of Ruby code using RuboCop'
14
+ spec.homepage = 'https://github.com/jimeh/rubocopfmt'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.13'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ spec.add_development_dependency 'byebug'
27
+
28
+ spec.add_runtime_dependency 'rubocop', '~> 0.46'
29
+ spec.add_runtime_dependency 'diffy', '~> 3.1'
30
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocopfmt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.beta1
5
+ platform: ruby
6
+ authors:
7
+ - Jim Myhrberg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.46'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.46'
83
+ - !ruby/object:Gem::Dependency
84
+ name: diffy
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.1'
97
+ description: Easy formatting of Ruby code using RuboCop
98
+ email:
99
+ - contact@jimeh.me
100
+ executables:
101
+ - rubocopfmt
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/rubocopfmt
115
+ - lib/rubocopfmt.rb
116
+ - lib/rubocopfmt/auto_corrector.rb
117
+ - lib/rubocopfmt/cli.rb
118
+ - lib/rubocopfmt/errors.rb
119
+ - lib/rubocopfmt/formatter.rb
120
+ - lib/rubocopfmt/options.rb
121
+ - lib/rubocopfmt/options_parser.rb
122
+ - lib/rubocopfmt/source.rb
123
+ - lib/rubocopfmt/version.rb
124
+ - rubocopfmt.gemspec
125
+ homepage: https://github.com/jimeh/rubocopfmt
126
+ licenses: []
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">"
140
+ - !ruby/object:Gem::Version
141
+ version: 1.3.1
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.6.8
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Easy formatting of Ruby code using RuboCop
148
+ test_files: []