ruby-beautify2 0.98.0

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
+ SHA256:
3
+ metadata.gz: 4f4b2b3c54b65eabf04399a42340e9638cc2786397cfbc9ac23e355690f6749d
4
+ data.tar.gz: 601028349dfbc1ae52c2183518a7ce552929e692fbbc4dc44de889860fa6a162
5
+ SHA512:
6
+ metadata.gz: 8c9139803964451848c0505332c0957007e7a02f6ffe4d28ba7c8ccc5eb8c5d410f08d4b9ed78cfb2bfc6cbd77049cedd7dd23be1a8b24e55e2ba4b5445d5f7e
7
+ data.tar.gz: 32c8b44c138b96ab5b4909d2dade0b2cd00aa1c735c8af593e9f2c41f81a63c4953a555b383286c111de38f8d99b8473df9f5fec4b1b6f9e029ebf469de9fc05
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2012-2016 Ernie Brodeur.
4
+ Copyright (c) 2017-2018 Jakub Jirutka <jakub@jirutka.cz>.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Ruby Beautify
2
+
3
+ [![Build Status](https://travis-ci.org/jirutka/ruby-beautify2.png?branch=master)](https://travis-ci.org/jirutka/ruby-beautify2)
4
+
5
+ This gem provides a cli binary named 'ruby-beautify' that will pretty up ruby code.
6
+
7
+ Currenty, 'rbeautify' is included for backwards compatibility but will likely be phased out at some point.
8
+
9
+ ## Installation
10
+
11
+ `gem install ruby-beautify2`
12
+
13
+ ## Usage
14
+
15
+ To Pretty up a file:
16
+
17
+ `ruby-beautify filename`
18
+
19
+ Without a filename it reads from STDIN, suitable for piping:
20
+
21
+ `curl https://raw.githubusercontent.com/jirutka/ruby-beautify2/master/spec/monolithic_example.rb | ruby-beautify`
22
+
23
+ It has help:
24
+
25
+ `ruby-beautify --help`
26
+
27
+ You can pick your indent character:
28
+
29
+ `ruby-beautify --(t)abs`
30
+
31
+ `ruby-beautify --(s)paces`
32
+
33
+ You can also pick the count of characters:
34
+
35
+ `ruby-beautify --indent_(c)ount 1`
36
+
37
+ Examples:
38
+
39
+ `ruby-beautify -c 2 -s filename`
40
+
41
+ `ruby-beautify filename`
42
+
43
+ `ruby-beautify -t -c 2 filename`
44
+
45
+ ## Advanced Usage
46
+
47
+ You can over write files in place, this is useful for doing an entire directory of files at once. This will not over write any files that fail syntax check.
48
+
49
+ `ruby-beautify --overwrite **/*.rb`
50
+
51
+ ## Configuration file
52
+
53
+ It can use a configuration file like some of the other ruby projects out there. The config file consists of each argument on a new line. Something like this:
54
+
55
+ ```
56
+ --spaces
57
+ --indent_count=2
58
+ ```
59
+
60
+ Note, you'll have to add the equal sign between value and argument (tricky bit, that).
61
+
62
+ Placing this into a `.ruby-beautify` anywhere in your tree (like git) will work. This allows you to put it at the root of a project and have it change the defaults anywhere in the project.
63
+
64
+ ## Bugs
65
+
66
+ Please feel free to open issues, I am actively working on this project again, thanks entirely to the ripper gem.
67
+
68
+ The gaps are getting smaller. I think we have most of the basic ruby use cases in place. I don't use rails/dsl's too often so I haven't tested those. I suspect it should 'just work' since the way we do syntax matching is really agnostic to what a DSL can change.
69
+
70
+ ## Todo
71
+
72
+ * Add vim style comment hinting.
73
+ * add specs/pipe testing (epic).
74
+ * remove the link to rbeautify (by 1.0).
75
+
76
+ Longer term I'd like to do some more to assignment, line wrapping, and spacing in/around keywords.
77
+
78
+ # History
79
+
80
+ ruby-beautify2 is a fork of [ruby-beautify](https://github.com/erniebrodeur/ruby-beautify) written by [Ernie Brodeur](https://github.com/erniebrodeur), which is currently unmaintained.
81
+
82
+ The original analyzer is available at: http://www.arachnoid.com/ruby/rubyBeautifier.html.
83
+
84
+ My work is based off of this sublime-text2 plugin: https://github.com/CraigWilliams/BeautifyRuby but cleaned up and made suitable for use directly in a shell.
85
+
86
+ I've recently re-written this to use the stdlib `ripper` gem to do the lexical analyzing. Consequently I've dropped all of the old legacy code that did this.
data/bin/rbeautify ADDED
@@ -0,0 +1 @@
1
+ bin/ruby-beautify
data/bin/ruby-beautify ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'ruby-beautify'
4
+
5
+ include RubyBeautify
6
+ my_argv = config_argv
7
+
8
+ Options = OptionParser.new do |opts|
9
+ opts.on("-V", "--version", "Print version") { |version| puts RubyBeautify::VERSION;exit 0}
10
+ opts.on("-c", "--indent_count [COUNT]", Integer, "Count of characters to use for indenting. (default: 1)") { |count| @indent_count = count}
11
+ opts.on("-t", "--tabs", "Use tabs for the indent character (default)") { @indent_token = "\t" }
12
+ opts.on("-s", "--spaces", "Use spaces for the indent character") { @indent_token = " " }
13
+ opts.on("--overwrite", "Overwrite files as you go (won't touch files that failed a syntax check).") { @overwrite = true }
14
+ opts.banner = "Usage: print ruby into a pretty format, or break trying."
15
+ end
16
+ Options.parse!(my_argv)
17
+
18
+ @indent_token = "\t" unless @indent_token
19
+ @indent_count = 1 unless @indent_count
20
+
21
+ def print_or_die(content)
22
+ if content
23
+ if syntax_ok? content
24
+ puts pretty_string content, indent_token: @indent_token, indent_count: @indent_count
25
+ else
26
+ puts content
27
+ exit 127
28
+ end
29
+ end
30
+ end
31
+
32
+ if my_argv.empty?
33
+ content = $stdin.read
34
+ print_or_die content
35
+ else
36
+ my_argv.each do |file|
37
+ if File.exist? file
38
+ fh = open(file)
39
+ content = fh.read
40
+ fh.sync
41
+ fh.close
42
+ else
43
+ puts "No such file: #{file}"
44
+ exit
45
+ end
46
+
47
+ if @overwrite
48
+ if syntax_ok? content
49
+ fh = open(file, 'w')
50
+ fh.write pretty_string content, indent_token: @indent_token, indent_count: @indent_count
51
+ fh.sync
52
+ fh.close
53
+ else
54
+ next
55
+ end
56
+ else
57
+ print_or_die content
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,184 @@
1
+ require 'ripper'
2
+ require 'ruby-beautify/version'
3
+
4
+ module RubyBeautify
5
+ extend self
6
+
7
+ OPEN_BLOCK_START = ["module", "class", "begin", "def", 'if', 'while', 'unless', 'case']
8
+ BOTH_BLOCK = ["else", "elsif", 'rescue', 'when']
9
+ OPEN_BLOCK_DO = ['do', '{']
10
+ CLOSE_BLOCK = ['end', '}']
11
+
12
+ OPEN_BRACKETS = [:on_lparen, :on_lbracket, :on_lbrace, :on_embexpr_beg, :on_tlambeg]
13
+ CLOSE_BRACKETS = [:on_rparen, :on_rbracket, :on_rbrace, :on_embexpr_end]
14
+ NEW_LINES = [:on_nl, :on_ignored_nl, :on_comment, :on_embdoc_end]
15
+
16
+
17
+ def pretty_string(content, indent_token: "\t", indent_count: 1)
18
+ output_string = ""
19
+ raise "Bad Syntax" unless syntax_ok? content
20
+ lex = ::Ripper.lex(content)
21
+
22
+ indent_level = 0
23
+ line_lex = []
24
+
25
+ # walk through line tokens
26
+ lex.each do |token|
27
+ line_lex << token
28
+ if NEW_LINES.include? token[1] # if type of this token is a new line
29
+
30
+ # did we just close something? if so, lets bring it down a level.
31
+ if closing_block?(line_lex) || closing_assignment?(line_lex)
32
+ indent_level -= 1 if indent_level > 0
33
+ end
34
+
35
+ # print our line, in place.
36
+ line_string = line_lex.map {|l| l[2]}.join
37
+ output_string += indented_line(indent_level, indent_token, indent_count, line_string)
38
+
39
+ # oh, we opened something did we? lets indent for the next run.
40
+ if opening_block?(line_lex) || opening_assignment?(line_lex)
41
+ indent_level += 1
42
+ end
43
+
44
+ line_lex.clear
45
+ end
46
+ end
47
+
48
+ return output_string
49
+ end
50
+
51
+ # check the syntax of a string
52
+ # see https://www.ruby-forum.com/topic/4419079#1130079
53
+ def syntax_ok?(string)
54
+ begin
55
+ # eval will parse the code, raising a SyntaxError if something is
56
+ # syntactically wrong, then start executing it. first thing is the
57
+ # throw, so it throws, skipping all the rest.
58
+ catch(:good) do
59
+ eval("BEGIN { throw :good }; #{string}")
60
+ end
61
+ return true
62
+ rescue SyntaxError
63
+ return false
64
+ end
65
+ end
66
+
67
+ # same trick as opening_block
68
+ def opening_assignment?(line_lex)
69
+ opens = opening_assignment_count line_lex
70
+ closes = closing_assignment_count line_lex
71
+ return false if opens == closes
72
+ return true if opens > closes
73
+ end
74
+
75
+ # ...
76
+ def closing_assignment?(line_lex)
77
+ opens = opening_assignment_count line_lex
78
+ closes = closing_assignment_count line_lex
79
+ return false if opens == closes
80
+ return true if closes > opens
81
+ end
82
+
83
+ # test for assignment from a block
84
+ def contains_block_assignment?(line_lex)
85
+ compacted_line = line_lex.reject{|x| x[1] == :on_sp} #remove spaces
86
+ idx = compacted_line.rindex{|x| ['=', '||='].include? x[2]} #find last equal
87
+ if idx
88
+ return OPEN_BLOCK_START.include?(compacted_line[idx+1][2]) #check for if/begin block
89
+ end
90
+ return false
91
+ end
92
+
93
+ # is the first word a key word?
94
+ def starts_block?(line_lex)
95
+ return true if contains_block_assignment? line_lex
96
+ line_lex.each do |x|
97
+ # search for a first non-space token
98
+ if not x[1] == :on_sp
99
+ return x[1] == :on_kw && OPEN_BLOCK_START.include?(x[2])
100
+ end
101
+ end
102
+ end
103
+
104
+ # is the first word one of our 'both' keywords?
105
+ def both_block?(line_lex)
106
+ line_lex.each do |x|
107
+ # search for a first non-space token
108
+ if not x[1] == :on_sp
109
+ return x[1] == :on_kw && BOTH_BLOCK.include?(x[2])
110
+ end
111
+ end
112
+ end
113
+
114
+ # kinda complex, we count open/close to determine if we ultimately have a
115
+ # hanging line. Always true if it's a both_block.
116
+ def opening_block?(line_lex)
117
+ opens = (starts_block?(line_lex) || both_block?(line_lex)) ? 1 : 0
118
+ opens += opening_block_count line_lex
119
+ closes = closing_block_count line_lex
120
+ return false if opens == closes
121
+ return true if opens > closes
122
+ end
123
+
124
+ # kinda complex, we count open/close to determine if we ultimately have close a
125
+ # hanging line. Always true if it's a both_block.
126
+ def closing_block?(line_lex)
127
+ return true if both_block? line_lex
128
+ opens = starts_block?(line_lex) ? 1 : 0
129
+ opens += opening_block_count line_lex
130
+ closes = closing_block_count line_lex
131
+ return false if opens == closes
132
+ return true if opens < closes
133
+ end
134
+
135
+ private
136
+ # how many times do we open in this line?
137
+ def opening_block_count(line_lex)
138
+ line_lex.select {|l| l[1] == :on_kw && OPEN_BLOCK_DO.include?(l[2])}.count
139
+ end
140
+
141
+ # how many times do we close?
142
+ def closing_block_count(line_lex)
143
+ line_lex.select {|l| l[1] == :on_kw && CLOSE_BLOCK.include?(l[2])}.count
144
+ end
145
+
146
+ # count the amount of opening assignments.
147
+ def opening_assignment_count(line_lex)
148
+ line_lex.select {|l| OPEN_BRACKETS.include? l[1]}.count
149
+ end
150
+
151
+ # count the amount of closing assignments.
152
+ def closing_assignment_count(line_lex)
153
+ line_lex.select {|l| CLOSE_BRACKETS.include? l[1]}.count
154
+ end
155
+
156
+ # prepare an indented line. Requires the level, token, count and string.
157
+ def indented_line(level, token = "\t", count = 1, string)
158
+ output_string = ""
159
+ if string =~ /^\n$/
160
+ output_string += "\n"
161
+ else
162
+ indent = (token * count) * level
163
+ output_string += "#{indent}#{string.lstrip}"
164
+ end
165
+ end
166
+
167
+ # try to find a config and return a modified argv, walks all the way to root
168
+ # looking for '.ruby-beautify' and then returns an argv with our new options.
169
+ def config_argv
170
+ target_dirs = Dir.pwd.split("/")
171
+
172
+ # sloppy walk method, not well tested but shouldn't get in the way.
173
+ while (target_dirs.any?)
174
+ target = "#{target_dirs.join("/")}/.ruby-beautify"
175
+ break if File.exist?(target)
176
+ target_dirs.pop
177
+ target = nil
178
+ end
179
+
180
+ return ARGV unless target
181
+ lines = open(target).readlines
182
+ return ARGV + lines.map {|l| l.chomp}
183
+ end
184
+ end
@@ -0,0 +1,3 @@
1
+ module RubyBeautify
2
+ VERSION = '0.98.0'
3
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../lib/ruby-beautify/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'ruby-beautify2'
5
+ s.version = RubyBeautify::VERSION
6
+ s.authors = ['Jakub Jirutka', 'Ernie Brodeur']
7
+ s.email = 'jakub@jirutka.cz'
8
+ s.homepage = 'https://github.com/jirutka/ruby-beautify2'
9
+ s.license = 'MIT'
10
+
11
+ s.summary = 'A CLI tool (and module) to beautify Ruby code'
12
+ s.description = "#{s.summary}. This is a maintained fork of the original ruby-beautify."
13
+
14
+ s.files = Dir['lib/**/*', '*.gemspec', 'LICENSE*', 'README*']
15
+ s.executables = Dir['bin/*'].map { |f| File.basename(f) }
16
+
17
+ s.required_ruby_version = '>= 2.0'
18
+
19
+ s.add_development_dependency 'bundler', '~> 1.16'
20
+ s.add_development_dependency 'rake', '~> 12.0'
21
+ s.add_development_dependency 'rspec', '~> 3.7'
22
+ s.add_development_dependency 'simplecov', '~> 0.15'
23
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-beautify2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.98.0
5
+ platform: ruby
6
+ authors:
7
+ - Jakub Jirutka
8
+ - Ernie Brodeur
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-06-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.16'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.16'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '12.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '12.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.7'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.7'
56
+ - !ruby/object:Gem::Dependency
57
+ name: simplecov
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.15'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.15'
70
+ description: A CLI tool (and module) to beautify Ruby code. This is a maintained fork
71
+ of the original ruby-beautify.
72
+ email: jakub@jirutka.cz
73
+ executables:
74
+ - rbeautify
75
+ - ruby-beautify
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - LICENSE
80
+ - README.md
81
+ - bin/rbeautify
82
+ - bin/ruby-beautify
83
+ - lib/ruby-beautify.rb
84
+ - lib/ruby-beautify/version.rb
85
+ - ruby-beautify2.gemspec
86
+ homepage: https://github.com/jirutka/ruby-beautify2
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '2.0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.7.3
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: A CLI tool (and module) to beautify Ruby code
110
+ test_files: []