Class: Babydebugger
- Inherits:
-
Object
- Object
- Babydebugger
- Defined in:
- lib/babydebugger.rb
Class Attribute Summary collapse
-
.files_modified ⇒ Object
Returns the value of attribute files_modified.
-
.lines_modified ⇒ Object
Returns the value of attribute lines_modified.
-
.mode ⇒ Object
Returns the value of attribute mode.
-
.word ⇒ Object
Returns the value of attribute word.
Class Method Summary collapse
- .comment_out_breakpoints(file) ⇒ Object
- .go(args) ⇒ Object
- .load_files ⇒ Object
- .print_help ⇒ Object
- .print_stats ⇒ Object
- .print_usage ⇒ Object
- .process_args(args) ⇒ Object
- .remove_breakpoints(file) ⇒ Object
- .remove_comments_from_breakpoints(file) ⇒ Object
Class Attribute Details
.files_modified ⇒ Object
Returns the value of attribute files_modified.
4 5 6 |
# File 'lib/babydebugger.rb', line 4 def files_modified @files_modified end |
.lines_modified ⇒ Object
Returns the value of attribute lines_modified.
4 5 6 |
# File 'lib/babydebugger.rb', line 4 def lines_modified @lines_modified end |
.mode ⇒ Object
Returns the value of attribute mode.
4 5 6 |
# File 'lib/babydebugger.rb', line 4 def mode @mode end |
.word ⇒ Object
Returns the value of attribute word.
4 5 6 |
# File 'lib/babydebugger.rb', line 4 def word @word end |
Class Method Details
.comment_out_breakpoints(file) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/babydebugger.rb', line 77 def self.comment_out_breakpoints(file) self.word = 'commented out' file_changed = false temp_file = Tempfile.new('tempfile') open(file, 'r').each do |l| if l.strip[0] == '//' temp_file << l elsif l.include?('debugger') self.lines_modified += 1 file_changed = true temp_file << "#{' ' * l.index('debugger')}\// #{l.strip}\n" else temp_file << l end end if file_changed temp_file.close FileUtils.mv(temp_file.path, file) self.files_modified += 1 end end |
.go(args) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/babydebugger.rb', line 7 def self.go(args) Babydebugger.mode = '' Babydebugger.lines_modified = 0 Babydebugger.files_modified = 0 process_args(args) load_files print_stats end |
.load_files ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/babydebugger.rb', line 42 def self.load_files() if self.mode == 'r' Dir.glob(File.join(".", "**", "*.js")).each { |file| remove_breakpoints(file) } elsif self.mode == 'c' Dir.glob(File.join(".", "**", "*.js")).each { |file| comment_out_breakpoints(file) } elsif self.mode == 'u' Dir.glob(File.join(".", "**", "*.js")).each { |file| remove_comments_from_breakpoints(file) } elsif self.mode == 'e' print_usage elsif self.mode == 'h' print_help end end |
.print_help ⇒ Object
21 22 23 24 25 26 |
# File 'lib/babydebugger.rb', line 21 def self.print_help print_usage puts "-r: Remove breakpoints from files." puts "-c: Comment out lines with debugger breakpoints." puts "-u: Uncomment lines with debugger breakpoints." end |
.print_stats ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/babydebugger.rb', line 125 def self.print_stats if self.lines_modified == 0 && self.files_modified == 0 puts 'Babydebugger: No changes were made.' else puts "Babydebugger: #{self.lines_modified} lines were #{self.word} in #{self.files_modified} files." end end |
.print_usage ⇒ Object
17 18 19 |
# File 'lib/babydebugger.rb', line 17 def self.print_usage puts "USAGE: babydebugger [option]" end |
.process_args(args) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/babydebugger.rb', line 28 def self.process_args(args) if args == nil self.mode = 'c' else args = args.gsub('-', '') self.mode = 'h' if args == '-help' if args.length > 1 || args.scan(/u|r|c|h/).empty? self.mode = 'e' elsif self.mode = args end end end |
.remove_breakpoints(file) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/babydebugger.rb', line 56 def self.remove_breakpoints(file) self.word = 'removed' file_changed = false temp_file = Tempfile.new('tempfile') open(file, 'r').each do |l| if l.include?('debugger') self.lines_modified += 1 file_changed = true else temp_file << l end end if file_changed temp_file.close FileUtils.mv(temp_file.path, file) self.files_modified += 1 end end |
.remove_comments_from_breakpoints(file) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/babydebugger.rb', line 102 def self.remove_comments_from_breakpoints(file) self.word = 'uncommented' file_changed = false temp_file = Tempfile.new('tempfile') open(file, 'r').each do |l| if l.include?('debugger') && l.include?('//') spaces = l.index('//') temp_file << "#{' ' * spaces}#{l[spaces + 2..l.length]}" self.lines_modified += 1 file_changed = true else temp_file << l end end if file_changed temp_file.close FileUtils.mv(temp_file.path, file) self.files_modified += 1 end end |