Debian Codegraph @

bin/rbeautify

raw symbols SCIP
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