ruby-beautify-0.97.4/0000755000004100000410000000000012665105535014442 5ustar www-datawww-dataruby-beautify-0.97.4/Rakefile0000644000004100000410000000067712665105535016121 0ustar www-datawww-data#!/usr/bin/env rake require "bundler/gem_tasks" desc "Generate a new usage scenario." task :generate_usage_scenario, [:scenario] do |task, args| unless args[:scenario] puts "must define a scenario" exit end files = ["#{args[:scenario]}.rb","#{args[:scenario]}_pretty.rb"] files.each do |f| f = "spec/usage_scenarios/#{f}" if File.exist? f puts " skipping #{f}" else puts " generating #{f}" FileUtils.touch f end end end ruby-beautify-0.97.4/bin/0000755000004100000410000000000012665105535015212 5ustar www-datawww-dataruby-beautify-0.97.4/bin/rbeautify0000755000004100000410000000302712665105535017134 0ustar www-datawww-data#!/usr/bin/env ruby require 'optparse' require 'ruby-beautify' include RubyBeautify my_argv = config_argv Options = OptionParser.new do |opts| opts.on("-V", "--version", "Print version") { |version| puts RubyBeautify::VERSION;exit 0} opts.on("-c", "--indent_count [COUNT]", Integer, "Count of characters to use for indenting. (default: 1)") { |count| @indent_count = count} opts.on("-t", "--tabs", "Use tabs for the indent character (default)") { @indent_token = "\t" } opts.on("-s", "--spaces", "Use spaces for the indent character") { @indent_token = " " } opts.on("--overwrite", "Overwrite files as you go (won't touch files that failed a syntax check).") { @overwrite = true } opts.banner = "Usage: print ruby into a pretty format, or break trying." end Options.parse!(my_argv) @indent_token = "\t" unless @indent_token @indent_count = 1 unless @indent_count def print_or_die(content) if content if syntax_ok? content puts pretty_string content, indent_token: @indent_token, indent_count: @indent_count else puts content exit 127 end end end if my_argv.empty? content = $stdin.read print_or_die content else my_argv.each do |file| if File.exist? file fh = open(file) content = fh.read fh.sync fh.close else puts "No such file: #{file}" exit end if @overwrite if syntax_ok? content fh = open(file, 'w') fh.write pretty_string content, indent_token: @indent_token, indent_count: @indent_count fh.sync fh.close else next end else print_or_die content end end end ruby-beautify-0.97.4/bin/ruby-beautify0000755000004100000410000000302712665105535017731 0ustar www-datawww-data#!/usr/bin/env ruby require 'optparse' require 'ruby-beautify' include RubyBeautify my_argv = config_argv Options = OptionParser.new do |opts| opts.on("-V", "--version", "Print version") { |version| puts RubyBeautify::VERSION;exit 0} opts.on("-c", "--indent_count [COUNT]", Integer, "Count of characters to use for indenting. (default: 1)") { |count| @indent_count = count} opts.on("-t", "--tabs", "Use tabs for the indent character (default)") { @indent_token = "\t" } opts.on("-s", "--spaces", "Use spaces for the indent character") { @indent_token = " " } opts.on("--overwrite", "Overwrite files as you go (won't touch files that failed a syntax check).") { @overwrite = true } opts.banner = "Usage: print ruby into a pretty format, or break trying." end Options.parse!(my_argv) @indent_token = "\t" unless @indent_token @indent_count = 1 unless @indent_count def print_or_die(content) if content if syntax_ok? content puts pretty_string content, indent_token: @indent_token, indent_count: @indent_count else puts content exit 127 end end end if my_argv.empty? content = $stdin.read print_or_die content else my_argv.each do |file| if File.exist? file fh = open(file) content = fh.read fh.sync fh.close else puts "No such file: #{file}" exit end if @overwrite if syntax_ok? content fh = open(file, 'w') fh.write pretty_string content, indent_token: @indent_token, indent_count: @indent_count fh.sync fh.close else next end else print_or_die content end end end ruby-beautify-0.97.4/Gemfile0000644000004100000410000000047312665105535015741 0ustar www-datawww-datasource 'https://rubygems.org' # Specify your gem's dependencies in ruby-beautify.gemspec gemspec group :development do gem 'guard', require:false gem 'guard-rspec', require:false gem 'guard-bundler', require:false gem 'pry', require:false gem "codeclimate-test-reporter", group: :test, require: nil end ruby-beautify-0.97.4/.rspec0000644000004100000410000000003712665105535015557 0ustar www-datawww-data--color --format documentation ruby-beautify-0.97.4/spec/0000755000004100000410000000000012665105535015374 5ustar www-datawww-dataruby-beautify-0.97.4/spec/bin/0000755000004100000410000000000012665105535016144 5ustar www-datawww-dataruby-beautify-0.97.4/spec/bin/ruby-beautify_spec.rb0000644000004100000410000000534112665105535022275 0ustar www-datawww-datarequire 'spec_helper.rb' describe "Ruby Beautify" do # This acts as a config block for files and anything else that you may set. before (:all) do # a very quick to parse file, since this isn't to test the function of the parser (but the function of the binary). @small_file = 'spec/binary_scenarios/small_example.rb' # should be the contents of the small file twice. @doubled_file = 'spec/binary_scenarios/doubled_example.rb' # Our file to overwrite (should be ugly to start). @overwrite_file = "spec/binary_scenarios/overwrite.rb" @overwrite_target_file = "tmp/copied.rb" @overwrite_pretty_file = "spec/binary_scenarios/overwrite_pretty.rb" # Our space test. @space_before = 'spec/binary_scenarios/spaces_before.rb' @space_after = 'spec/binary_scenarios/spaces_after.rb' @space_after_sum = Digest::MD5.hexdigest File.read @space_after # our count test. @count_before = 'spec/binary_scenarios/count_before.rb' @count_after = 'spec/binary_scenarios/count_after.rb' end it "will work" do small_md5_sum = Digest::MD5.hexdigest File.read(@small_file) md5_sum = Digest::MD5.hexdigest `bundle exec #{BEAUTIFY_BIN} #{@small_file}` expect(md5_sum).to eq small_md5_sum end it "will do multiple files" do md5_sum = Digest::MD5.hexdigest `bundle exec #{BEAUTIFY_BIN} #{@small_file} #{@small_file}` doubled_md5_sum = Digest::MD5.hexdigest File.read(@doubled_file) expect(md5_sum).to eq doubled_md5_sum end # I want to make sure the file actually changes, so I do this (I could make yet another file). it "will update files (overwrite) in place" do FileUtils.mkdir_p File.dirname @overwrite_target_file FileUtils.cp @overwrite_file, @overwrite_target_file `bundle exec #{BEAUTIFY_BIN} --overwrite #{@overwrite_target_file}` md5_sum = Digest::MD5.hexdigest File.read @overwrite_target_file overwrite_md5_sum = Digest::MD5.hexdigest File.read(@overwrite_pretty_file) expect(md5_sum).to eq overwrite_md5_sum FileUtils.rm @overwrite_target_file end it "will honor --spaces" do beautified_sum = Digest::MD5.hexdigest `bundle exec #{BEAUTIFY_BIN} --spaces #{@space_before}` expect(beautified_sum).to eq(@space_after_sum) end it "will honor the ident_count prefix" do beautified_sum = Digest::MD5.hexdigest `bundle exec #{BEAUTIFY_BIN} --indent_count=3 #{@count_before}` count_after_sum = Digest::MD5.hexdigest File.read @count_after expect(beautified_sum).to eq(count_after_sum) end it "will use a .ruby-beautify config file" do FileUtils.cp "spec/ruby-beautify.dotfile", "tmp/.ruby-beautify" Dir.chdir "tmp" beautified_sum = Digest::MD5.hexdigest `bundle exec #{BEAUTIFY_BIN} ../#{@space_before}` Dir.chdir ".." FileUtils.rm "tmp/.ruby-beautify" expect(beautified_sum).to eq(@space_after_sum) end end ruby-beautify-0.97.4/spec/binary_scenarios/0000755000004100000410000000000012665105535020726 5ustar www-datawww-dataruby-beautify-0.97.4/spec/binary_scenarios/overwrite_pretty.rb0000644000004100000410000000010012665105535024677 0ustar www-datawww-datadef ugly puts 'this' if true puts 'is ugly enough' end end ruby-beautify-0.97.4/spec/binary_scenarios/overwrite.rb0000644000004100000410000000007312665105535023301 0ustar www-datawww-datadef ugly puts 'this' if true puts 'is ugly enough' end end ruby-beautify-0.97.4/spec/binary_scenarios/count_after.rb0000644000004100000410000000003512665105535023562 0ustar www-datawww-datadef count puts "this" end ruby-beautify-0.97.4/spec/binary_scenarios/small_example.rb0000644000004100000410000000003612665105535024075 0ustar www-datawww-datadef small puts 'example' end ruby-beautify-0.97.4/spec/binary_scenarios/spaces_before.rb0000644000004100000410000000004612665105535024053 0ustar www-datawww-datadef spaces puts "are an option" end ruby-beautify-0.97.4/spec/binary_scenarios/doubled_example.rb0000644000004100000410000000007412665105535024405 0ustar www-datawww-datadef small puts 'example' end def small puts 'example' end ruby-beautify-0.97.4/spec/binary_scenarios/count_before.rb0000644000004100000410000000003312665105535023721 0ustar www-datawww-datadef count puts "this" end ruby-beautify-0.97.4/spec/binary_scenarios/spaces_after.rb0000644000004100000410000000004512665105535023711 0ustar www-datawww-datadef spaces puts "are an option" end ruby-beautify-0.97.4/spec/spec_helper.rb0000644000004100000410000000024412665105535020212 0ustar www-datawww-datarequire 'digest/md5' require "codeclimate-test-reporter" CodeClimate::TestReporter.start require 'ruby-beautify' BEAUTIFY_BIN = "#{Dir.pwd}/bin/ruby-beautify" ruby-beautify-0.97.4/spec/lib/0000755000004100000410000000000012665105535016142 5ustar www-datawww-dataruby-beautify-0.97.4/spec/lib/ruby-beautify_spec.rb0000644000004100000410000000173012665105535022271 0ustar www-datawww-datarequire 'spec_helper.rb' # unit tests suck. But I do want to make sure this one function behaves as expected. # it simplifies the rest of the testing process (by not requiring multiple passes of each scenario). describe "Ruby Beautify Library" do before :all do @ugly_string = File.open('spec/usage_scenarios/pre_indented.rb').read @pretty_string = File.open('spec/usage_scenarios/pre_indented_pretty.rb').read @pretty_string_tabs = @pretty_string.gsub("\t", "\t\t") @pretty_string_space = File.open('spec/usage_scenarios/pre_indented_pretty_space.rb').read end describe "#pretty_string" do it "will indent by tab ( by default )" do expect(RubyBeautify.pretty_string(@ugly_string)).to eq @pretty_string end it "will indent by space" do expect(RubyBeautify.pretty_string(@ugly_string, indent_token:' ')).to eq @pretty_string_space end it "will honor indent count" do expect(RubyBeautify.pretty_string(@ugly_string)).to eq @pretty_string end end end ruby-beautify-0.97.4/spec/usage_scenarios/0000755000004100000410000000000012665105535020546 5ustar www-datawww-dataruby-beautify-0.97.4/spec/usage_scenarios/lambda_literal.rb0000644000004100000410000000053212665105535024027 0ustar www-datawww-data# Test for lambda literal class ArrowOperator def one_liner x = -> (x) {x * x} end def one_liner2args x = -> (x, y) {x * y} end def multiline x = -> (x) { x * x } end def multiline2args x = -> (x, y) { x * y } end def omit_braces x = -> x { x * x } end def omit_braces2args x = -> x, y { x * y } end def argument_nothing x = -> { x * x } end end ruby-beautify-0.97.4/spec/usage_scenarios/mutli_level_pretty.rb0000644000004100000410000000074012665105535025024 0ustar www-datawww-datamodule Here class There def why?(argument = nil) @array = [ 1, 2, 3 ] hash = { one:1, two:2, three:3 } end # a comment def why_not?(argument: {}) @array = [4,5,6] hash = {four:4, five:5, six:6} s = "a #{"complex"} string." end def complex_method(one, two, three, four) regex = /regex/ end def with_block run = Proc.new { |argument| puts arugment } run do |arugment| puts argument end end end end ruby-beautify-0.97.4/spec/usage_scenarios/multiline_strings.rb0000644000004100000410000000034312665105535024646 0ustar www-datawww-data# Test for multiline string def m (x) puts "This is multi-line string. It's line1 \ It's line 2\ And this is\n line3 This line should not be mutated" puts "This is multi line interpolated string #{ x }" end ruby-beautify-0.97.4/spec/usage_scenarios/case_pretty.rb0000644000004100000410000000025412665105535023416 0ustar www-datawww-data# Test to validate case statements def case_test opts=nil case test_case when 'Passing' call(:pass) when 'Failing' call(:fail) when 'Error' call(:error) end end ruby-beautify-0.97.4/spec/usage_scenarios/from_control_pretty.rb0000644000004100000410000000043112665105535025203 0ustar www-datawww-data# Test for variable assignment from an if statement @products = if params[:category] Category.find(params[:category]).products else Product.all end # Test for variable assignment from a block response = begin if true? api_call(test) else rejected end rescue 'FALSE' end ruby-beautify-0.97.4/spec/usage_scenarios/or_equals_pretty.rb0000644000004100000410000000044412665105535024476 0ustar www-datawww-data# Test or-equals def expensive_value @expensive_value ||= begin if conn.active? expensive_call else :none end end end # Test multiple or-equals def expensive_value @expensive_value ||= @cached_value ||= begin if conn.active? expensive_call else :none end end end ruby-beautify-0.97.4/spec/usage_scenarios/lambda_literal_pretty.rb0000644000004100000410000000061712665105535025442 0ustar www-datawww-data# Test for lambda literal class ArrowOperator def one_liner x = -> (x) {x * x} end def one_liner2args x = -> (x, y) {x * y} end def multiline x = -> (x) { x * x } end def multiline2args x = -> (x, y) { x * y } end def omit_braces x = -> x { x * x } end def omit_braces2args x = -> x, y { x * y } end def argument_nothing x = -> { x * x } end end ruby-beautify-0.97.4/spec/usage_scenarios/case.rb0000644000004100000410000000024112665105535022003 0ustar www-datawww-data# Test to validate case statements def case_test opts=nil case test_case when 'Passing' call(:pass) when 'Failing' call(:fail) when 'Error' call(:error) end end ruby-beautify-0.97.4/spec/usage_scenarios/or_equals.rb0000644000004100000410000000041012665105535023060 0ustar www-datawww-data# Test or-equals def expensive_value @expensive_value ||= begin if conn.active? expensive_call else :none end end end # Test multiple or-equals def expensive_value @expensive_value ||= @cached_value ||= begin if conn.active? expensive_call else :none end end end ruby-beautify-0.97.4/spec/usage_scenarios/pre_indented.rb0000644000004100000410000000026312665105535023534 0ustar www-datawww-data# Test for already indented blocks class There2 < There def m1() puts "m1" end def m2() puts "m2" end def m3() puts "m3" end end ruby-beautify-0.97.4/spec/usage_scenarios/pre_indented_pretty_space.rb0000644000004100000410000000021512665105535026313 0ustar www-datawww-data# Test for already indented blocks class There2 < There def m1() puts "m1" end def m2() puts "m2" end def m3() puts "m3" end end ruby-beautify-0.97.4/spec/usage_scenarios/pre_indented_pretty.rb0000644000004100000410000000021512665105535025140 0ustar www-datawww-data# Test for already indented blocks class There2 < There def m1() puts "m1" end def m2() puts "m2" end def m3() puts "m3" end end ruby-beautify-0.97.4/spec/usage_scenarios/monolithic_example.rb0000644000004100000410000000113212665105535024750 0ustar www-datawww-datarequire 'ripper' h = Here::There.new h.why?({ here: 'there', there: 'here' }) h.with_block { puts 'yahooie' } h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf', 'alfkjsdlkfjsflgkjfglk', 'alfkjsdlkfjsflgkjfglk', 'alfkjsdlkfjsflgkjfglk' ) h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf', 'alfkjsdlkfjsflgkjfglk', 'alfkjsdlkfjsflgkjfglk', 'alfkjsdlkfjsflgkjfglk' ).map do |i| i end.map! { |i| i } if 1 > 0 puts 'something' elsif 1 < 0 puts 'never!' else puts 'not likely.' end # Flattened formatting if 1 > 0 then puts 'something' else puts 'nothing' end puts "This line should stay the same" ruby-beautify-0.97.4/spec/usage_scenarios/multiline_strings_pretty.rb0000644000004100000410000000035012665105535026253 0ustar www-datawww-data# Test for multiline string def m (x) puts "This is multi-line string. It's line1 \ It's line 2\ And this is\n line3 This line should not be mutated" puts "This is multi line interpolated string #{ x }" end ruby-beautify-0.97.4/spec/usage_scenarios/mutli_level.rb0000644000004100000410000000061712665105535023420 0ustar www-datawww-datamodule Here class There def why?(argument = nil) @array = [ 1, 2, 3 ] hash = { one:1, two:2, three:3 } end # a comment def why_not?(argument: {}) @array = [4,5,6] hash = {four:4, five:5, six:6} s = "a #{"complex"} string." end def complex_method(one, two, three, four) regex = /regex/ end def with_block run = Proc.new { |argument| puts arugment } run do |arugment| puts argument end end end end ruby-beautify-0.97.4/spec/usage_scenarios/monolithic_example_pretty.rb0000644000004100000410000000114712665105535026365 0ustar www-datawww-datarequire 'ripper' h = Here::There.new h.why?({ here: 'there', there: 'here' }) h.with_block { puts 'yahooie' } h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf', 'alfkjsdlkfjsflgkjfglk', 'alfkjsdlkfjsflgkjfglk', 'alfkjsdlkfjsflgkjfglk' ) h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf', 'alfkjsdlkfjsflgkjfglk', 'alfkjsdlkfjsflgkjfglk', 'alfkjsdlkfjsflgkjfglk' ).map do |i| i end.map! { |i| i } if 1 > 0 puts 'something' elsif 1 < 0 puts 'never!' else puts 'not likely.' end # Flattened formatting if 1 > 0 then puts 'something' else puts 'nothing' end puts "This line should stay the same" ruby-beautify-0.97.4/spec/usage_scenarios/from_control.rb0000644000004100000410000000041712665105535023600 0ustar www-datawww-data# Test for variable assignment from an if statement @products = if params[:category] Category.find(params[:category]).products else Product.all end # Test for variable assignment from a block response = begin if true? api_call(test) else rejected end rescue 'FALSE' end ruby-beautify-0.97.4/spec/usage_scenarios/README.md0000644000004100000410000000273612665105535022035 0ustar www-datawww-data## This Directory The examples in this directory are not direct examples but file pairs we want to consume/test to ensure that what we want to see is what we get. For example, the most basic test would be something like: ```ruby def this_is_ugly(nowhere) if true puts true else puts false end end ``` We would expect this to look like: ```ruby def this_is_ugly(nowhere) if true puts true else puts false end end ``` Notice we are testing that we get the *actual* output we want. Not the configuratbility of the binary or our output but rather the representation of ugly vs beauty. We can expect a high level of redundancy in these specific examples, and that is ok. We want to ensure that as we make changes to the binary, we don't break the possible ways people use Ruby. I don't care if one file tests something another file does, as long as our ideas of what we want prettied up are throughly tested. We should avoid testing the binary. Therefor, the tests will always be run with the binary in *default* mode. Testing the binary's options can be handled elsewhere. ## File names Each usage pair of files will need to be in a specific format of: ``` test_or_whatever.rb test_or_whatever_pretty.rb ``` With the pretty file being what we want it to look like by default. The spec framework will loop through, consume the pairs, execute the parsing method and print out the results. This will allow for dynamic testing and migrating from one framework to another if need be. ruby-beautify-0.97.4/spec/ruby-beautify.dotfile0000644000004100000410000000001112665105535021523 0ustar www-datawww-data--spaces ruby-beautify-0.97.4/spec/usage_scenarios_spec.rb0000644000004100000410000000122412665105535022104 0ustar www-datawww-datarequire 'spec_helper.rb' describe "Usage Scenarios" do files = Dir.glob("spec/usage_scenarios/*_pretty.rb") scenarios = files.map do |f| r = /.*\/(?.*)_pretty.rb/.match(f) r['scenario'] ||= nil end scenarios.each do |scenario| it "will test: #{scenario}" do scenario_file = "spec/usage_scenarios/#{scenario}.rb" scenario_md5_sum = Digest::MD5.hexdigest RubyBeautify.pretty_string File.read scenario_file scenario_pretty_file = "spec/usage_scenarios/#{scenario}_pretty.rb" scenario_pretty_md5_sum = Digest::MD5.hexdigest File.read(scenario_pretty_file) expect(scenario_md5_sum).to eq scenario_pretty_md5_sum end end end ruby-beautify-0.97.4/.travis.yml0000644000004100000410000000033512665105535016554 0ustar www-datawww-datalanguage: ruby rvm: - "2.0.0" - "2.1.0" branches: only: - master - dev script: bundle exec rspec spec addons: code_climate: repo_token: 596a4a033e7deae1bfd5e84da4c803ef81134158764a48b02279fe97eb83fcf4 ruby-beautify-0.97.4/lib/0000755000004100000410000000000012665105535015210 5ustar www-datawww-dataruby-beautify-0.97.4/lib/ruby-beautify.rb0000644000004100000410000001220212665105535020321 0ustar www-datawww-datarequire 'open3' require 'ripper' require 'ruby-beautify/version' module RubyBeautify extend self OPEN_BLOCK_START = ["module", "class", "begin", "def", 'if', 'while', 'unless', 'case'] BOTH_BLOCK = ["else", "elsif", 'rescue', 'when'] OPEN_BLOCK_DO = ['do', '{'] CLOSE_BLOCK = ['end', '}'] OPEN_BRACKETS = [:on_lparen, :on_lbracket, :on_lbrace, :on_embexpr_beg, :on_tlambeg] CLOSE_BRACKETS = [:on_rparen, :on_rbracket, :on_rbrace, :on_embexpr_end] NEW_LINES = [:on_nl, :on_ignored_nl, :on_comment, :on_embdoc_end] def pretty_string(content, indent_token: "\t", indent_count: 1) output_string = "" raise "Bad Syntax" unless syntax_ok? content lex = ::Ripper.lex(content) indent_level = 0 line_lex = [] # walk through line tokens lex.each do |token| line_lex << token if NEW_LINES.include? token[1] # if type of this token is a new line # did we just close something? if so, lets bring it down a level. if closing_block?(line_lex) || closing_assignment?(line_lex) indent_level -= 1 if indent_level > 0 end # print our line, in place. line_string = line_lex.map {|l| l.last}.join output_string += indented_line(indent_level, indent_token, indent_count, line_string) # oh, we opened something did we? lets indent for the next run. if opening_block?(line_lex) || opening_assignment?(line_lex) indent_level += 1 end line_lex.clear end end return output_string end # check the syntax of a string, will pipe it through the ruby bin to see if # it has a valid syntax. def syntax_ok?(string) out, err, status = Open3.capture3("ruby -c -", stdin_data:string ) return false unless err.empty? return true end # same trick as opening_block def opening_assignment?(line_lex) opens = opening_assignment_count line_lex closes = closing_assignment_count line_lex return false if opens == closes return true if opens > closes end # ... def closing_assignment?(line_lex) opens = opening_assignment_count line_lex closes = closing_assignment_count line_lex return false if opens == closes return true if closes > opens end # test for assignment from a block def contains_block_assignment?(line_lex) compacted_line = line_lex.reject{|x| x[1] == :on_sp} #remove spaces idx = compacted_line.rindex{|x| ['=', '||='].include? x[2]} #find last equal if idx return OPEN_BLOCK_START.include?(compacted_line[idx+1][2]) #check for if/begin block end return false end # is the first word a key word? def starts_block?(line_lex) return true if contains_block_assignment? line_lex line_lex.each do |x| # search for a first non-space token if not x[1] == :on_sp return x[1] == :on_kw && OPEN_BLOCK_START.include?(x[2]) end end end # is the first word one of our 'both' keywords? def both_block?(line_lex) line_lex.each do |x| # search for a first non-space token if not x[1] == :on_sp return x[1] == :on_kw && BOTH_BLOCK.include?(x[2]) end end end # kinda complex, we count open/close to determine if we ultimately have a # hanging line. Always true if it's a both_block. def opening_block?(line_lex) opens = (starts_block?(line_lex) || both_block?(line_lex)) ? 1 : 0 opens += opening_block_count line_lex closes = closing_block_count line_lex return false if opens == closes return true if opens > closes end # kinda complex, we count open/close to determine if we ultimately have close a # hanging line. Always true if it's a both_block. def closing_block?(line_lex) return true if both_block? line_lex opens = starts_block?(line_lex) ? 1 : 0 opens += opening_block_count line_lex closes = closing_block_count line_lex return false if opens == closes return true if opens < closes end private # how many times do we open in this line? def opening_block_count(line_lex) line_lex.select {|l| l[1] == :on_kw && OPEN_BLOCK_DO.include?(l[2])}.count end # how many times do we close? def closing_block_count(line_lex) line_lex.select {|l| l[1] == :on_kw && CLOSE_BLOCK.include?(l[2])}.count end # count the amount of opening assignments. def opening_assignment_count(line_lex) line_lex.select {|l| OPEN_BRACKETS.include? l[1]}.count end # count the amount of closing assignments. def closing_assignment_count(line_lex) line_lex.select {|l| CLOSE_BRACKETS.include? l[1]}.count end # prepare an indented line. Requires the level, token, count and string. def indented_line(level, token = "\t", count = 1, string) output_string = "" if string =~ /^\n$/ output_string += "\n" else indent = (token * count) * level output_string += "#{indent}#{string.lstrip}" end end # try to find a config and return a modified argv, walks all the way to root # looking for '.ruby-beautify' and then returns an argv with our new options. def config_argv target_dirs = Dir.pwd.split("/") # sloppy walk method, not well tested but shouldn't get in the way. while (target_dirs.any?) target = "#{target_dirs.join("/")}/.ruby-beautify" break if File.exist?(target) target_dirs.pop target = nil end return ARGV unless target lines = open(target).readlines return ARGV + lines.map {|l| l.chomp} end end ruby-beautify-0.97.4/lib/ruby-beautify/0000755000004100000410000000000012665105535017777 5ustar www-datawww-dataruby-beautify-0.97.4/lib/ruby-beautify/version.rb0000644000004100000410000000005412665105535022010 0ustar www-datawww-datamodule RubyBeautify VERSION = "0.97.4" end ruby-beautify-0.97.4/CONTRIB.md0000644000004100000410000000157712665105535016076 0ustar www-datawww-dataHow to contribute to this project. ## Thin rules (to be made into sections) * Work off the dev branch, not master. * Pull requests should be named something related to PR, `hotfix-XXX` or `feature-YYY`. * Do not edit the `version.rb` or the `gemspec`. * All pull requests have to pass tests on `travis-ci` before I'll consider merging them. * for tests of the actual parser, make sure you include a new `usage_scenario`, not update an existing one. I'll merge stuff as it gets cluttered. ## Formatting and style Run all your code through `ruby-beautify` with default settings before committing. This should work (and if it doesn't, please let me know) `ruby-beautify --overwrite lib/**/*.rb bin/ruby-beautify spec/**/*_spec.rb spec/**/*_helper.rb` Which should pretty up only the code files, and not our test files. This will help insure pretty commits and that they will be accepted. ruby-beautify-0.97.4/metadata.yml0000644000004100000410000001127012665105535016746 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: ruby-beautify version: !ruby/object:Gem::Version version: 0.97.4 platform: ruby authors: - Ernie Brodeur autorequire: bindir: bin cert_chain: [] date: 2016-01-14 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: rake requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: bundler requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: rspec requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' description: a cli tool (and module) to beautify ruby code. email: ebrodeur@ujami.net executables: - rbeautify - ruby-beautify extensions: [] extra_rdoc_files: [] files: - ".gitignore" - ".rspec" - ".travis.yml" - CONTRIB.md - Gemfile - Guardfile - LICENSE - README.md - Rakefile - WHATSNEW.md - bin/rbeautify - bin/ruby-beautify - lib/ruby-beautify.rb - lib/ruby-beautify/version.rb - ruby-beautify.gemspec - spec/bin/ruby-beautify_spec.rb - spec/binary_scenarios/count_after.rb - spec/binary_scenarios/count_before.rb - spec/binary_scenarios/doubled_example.rb - spec/binary_scenarios/overwrite.rb - spec/binary_scenarios/overwrite_pretty.rb - spec/binary_scenarios/small_example.rb - spec/binary_scenarios/spaces_after.rb - spec/binary_scenarios/spaces_before.rb - spec/lib/ruby-beautify_spec.rb - spec/ruby-beautify.dotfile - spec/spec_helper.rb - spec/usage_scenarios/README.md - spec/usage_scenarios/case.rb - spec/usage_scenarios/case_pretty.rb - spec/usage_scenarios/from_control.rb - spec/usage_scenarios/from_control_pretty.rb - spec/usage_scenarios/lambda_literal.rb - spec/usage_scenarios/lambda_literal_pretty.rb - spec/usage_scenarios/monolithic_example.rb - spec/usage_scenarios/monolithic_example_pretty.rb - spec/usage_scenarios/multiline_strings.rb - spec/usage_scenarios/multiline_strings_pretty.rb - spec/usage_scenarios/mutli_level.rb - spec/usage_scenarios/mutli_level_pretty.rb - spec/usage_scenarios/or_equals.rb - spec/usage_scenarios/or_equals_pretty.rb - spec/usage_scenarios/pre_indented.rb - spec/usage_scenarios/pre_indented_pretty.rb - spec/usage_scenarios/pre_indented_pretty_space.rb - spec/usage_scenarios_spec.rb homepage: https://github.com/erniebrodeur/ruby-beautify licenses: [] metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '2.0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.4.8 signing_key: specification_version: 4 summary: a cli tool (and module) to beautify ruby code. test_files: - spec/bin/ruby-beautify_spec.rb - spec/binary_scenarios/count_after.rb - spec/binary_scenarios/count_before.rb - spec/binary_scenarios/doubled_example.rb - spec/binary_scenarios/overwrite.rb - spec/binary_scenarios/overwrite_pretty.rb - spec/binary_scenarios/small_example.rb - spec/binary_scenarios/spaces_after.rb - spec/binary_scenarios/spaces_before.rb - spec/lib/ruby-beautify_spec.rb - spec/ruby-beautify.dotfile - spec/spec_helper.rb - spec/usage_scenarios/README.md - spec/usage_scenarios/case.rb - spec/usage_scenarios/case_pretty.rb - spec/usage_scenarios/from_control.rb - spec/usage_scenarios/from_control_pretty.rb - spec/usage_scenarios/lambda_literal.rb - spec/usage_scenarios/lambda_literal_pretty.rb - spec/usage_scenarios/monolithic_example.rb - spec/usage_scenarios/monolithic_example_pretty.rb - spec/usage_scenarios/multiline_strings.rb - spec/usage_scenarios/multiline_strings_pretty.rb - spec/usage_scenarios/mutli_level.rb - spec/usage_scenarios/mutli_level_pretty.rb - spec/usage_scenarios/or_equals.rb - spec/usage_scenarios/or_equals_pretty.rb - spec/usage_scenarios/pre_indented.rb - spec/usage_scenarios/pre_indented_pretty.rb - spec/usage_scenarios/pre_indented_pretty_space.rb - spec/usage_scenarios_spec.rb has_rdoc: ruby-beautify-0.97.4/WHATSNEW.md0000644000004100000410000000457412665105535016236 0ustar www-datawww-data## 0.97.4 * Feature: Correctly beautifies lambda expressions thanks to @tkosuga. ## 0.97.2 * Typo fix, thanks @andyw8. * More typos, cleaned up thanks to @veelenga. * @tnnn fixed a namespacing issue preventing version from working, thanks. ## 0.97.2 * Thanks goes to @bachue for discovering that `ruby-beautify` was not honoring spaces and indent count on file overwrites. ## 0.97.1 * Now with more config file flavor! (check the [README.md]). * All tests green. ## 0.97.0 * Split up the spec into usage scenarios, easier to contribut too. * Lots of refactoring. * Some documentation. * Added multiple file support back so that overwrite files is more interesting. * Added an overwrite files flag, useful for fixing up a directory of files at once. ## 0.96.0 * Rewrote the syntax checker so that it pipes a string through stdout, doesn't need a temp file. * Moved everything into the module to clean up the bin. * @Sir-Irk fixed a bug where else and end end up on the same line. ## 0.95.0 * Merged a quick fix from @pkuykendall to catch block assignments with ||= * Added a required version to the gemspec. ## 0.94.2 * Support for case statements thanks to @pkuykendall. * @pkuykendall also brings us proper syntax formatting for assignments from the return value of an if check (that's a mouth full). ## 0.94.1 * Multiline string and embedded doc support thanks to @veelenga. ## 0.94.0 * Added a very basic spec. * Added better support for piping and redirection (thanks @veelenga) * Renamed the bin to ruby-beautify and added a link for backwards compat. * Fixed extra spaces on blank lines (thanks @veelenga) ## 0.93.2 * Fixed a typo in the help usage (thanks @ronald) ## 0.93.0 * Complete rewrite of the lexing engine. ## 0.92.0 * Renamed a require in rspec so they would work again. * Dropped filemagic since this already has excellent file parsing and is platform agnostic. * Dropped the rest of app.rb, since it wasn't adding anything but line count at this point. * Added a RELEASE.md to track changes as I go. * Dropped the unneeded yajl and sys-proctree deps since it doesn't use them. ## 0.91.0 * Stripped out the few bits of my cli stuff that I used and dropped app.rb, cli.rb and filemagic.rb, since all the functionality was present elsewhere. * Fixed up the gem related files, generating a proper gemspec, fixed the deps. * Fixed up the README.md to give a few usage examples and brief explanation. ruby-beautify-0.97.4/ruby-beautify.gemspec0000644000004100000410000000150412665105535020576 0ustar www-datawww-datarequire File.expand_path('../lib/ruby-beautify/version', __FILE__) Gem::Specification.new do |gem| gem.required_ruby_version = '>= 2.0' gem.name = 'ruby-beautify' gem.summary = "a cli tool (and module) to beautify ruby code." gem.description = gem.summary gem.authors = ["Ernie Brodeur"] gem.email = 'ebrodeur@ujami.net' gem.homepage = "https://github.com/erniebrodeur/ruby-beautify" gem.files = `git ls-files`.split("\n") gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } gem.require_paths = ["lib"] gem.version = RubyBeautify::VERSION gem.add_development_dependency 'rake' gem.add_development_dependency 'bundler' gem.add_development_dependency 'rspec' end ruby-beautify-0.97.4/.gitignore0000644000004100000410000000003112665105535016424 0ustar www-datawww-dataGemfile.lock pkg/* tmp/* ruby-beautify-0.97.4/LICENSE0000644000004100000410000000205512665105535015451 0ustar www-datawww-dataCopyright (c) 2012 Ernie Brodeur MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.ruby-beautify-0.97.4/README.md0000644000004100000410000000567312665105535015734 0ustar www-datawww-data# Ruby Beautify [![Build Status](https://travis-ci.org/erniebrodeur/ruby-beautify.png?branch=master)](https://travis-ci.org/erniebrodeur/ruby-beautify) [![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/erniebrodeur/ruby-beautify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) This gem provides a cli binary named 'ruby-beautify' that will pretty up ruby code. Currenty, 'rbeautify' is included for backwards compatibility but will likely be phased out at some point. ## Installation `gem install ruby-beautify` ## Usage To Pretty up a file: `ruby-beautify filename` Without a filename it reads from STDIN, suitable for piping: `curl https://raw.githubusercontent.com/erniebrodeur/ruby-beautify/master/spec/monolithic_example.rb | ruby-beautify` It has help: `ruby-beautify --help` You can pick your indent character: `ruby-beautify --(t)abs` `ruby-beautify --(s)paces` You can also pick the count of characters: `ruby-beautify --indent_(c)ount 1` Examples: `ruby-beautify -c 2 -s filename` `ruby-beautify filename` `ruby-beautify -t -c 2 filename` ## Advanced Usage 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. `ruby-beautify --overwrite **/*.rb` ## Configuration file 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: ``` --spaces --indent_count=2 ``` Note, you'll have to add the equal sign between value and argument (tricky bit, that). 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. ## Bugs Please feel free to open issues, I am actively working on this project again, thanks entirely to the ripper gem. 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. ## Todo * Add vim style comment hinting. * add specs/pipe testing (epic). * remove the link to rbeautify (by 1.0). Longer term I'd like to do some more to assignment, line wrapping, and spacing in/around keywords. ## Contributing Please see the [Contribution Guide](CONTRIB.md) file for specifics on how to contribute to this project. # History The original analyzer is available at: http://www.arachnoid.com/ruby/rubyBeautifier.html. 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. 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. ruby-beautify-0.97.4/Guardfile0000644000004100000410000000043212665105535016266 0ustar www-datawww-dataguard :bundler do watch('Gemfile') watch(/^.+\.gemspec/) end guard :rspec, cmd: 'rspec' do watch('bin/ruby-beautify') { "spec" } watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } end