-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-comments.rb
More file actions
executable file
·178 lines (134 loc) · 4.37 KB
/
remove-comments.rb
File metadata and controls
executable file
·178 lines (134 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env ruby
# TechArch: www.techarch.com.sa
# Sabri : @KINGSABRI
#
# Source Code comments removal
#
=begin
Language to support
- Auto detect
- Java
- ASP.net
- JavaScript
- Ruby
- Python
- Go
=end
require 'optparse'
require 'pp'
class String
def red; colorize(self, "\e[1m\e[31m"); end
def green; colorize(self, "\e[1m\e[32m"); end
def dark_green; colorize(self, "\e[32m"); end
def bold; colorize(self, "\e[1m"); end
def underline; colorize(self, "\e[4m"); end
def colorize(text, color_code) "#{color_code}#{text}\e[0m" end
end
options = {}
option_parser = OptionParser.new
option_parser.release = '0.0.1'
option_parser.release
option_parser.banner = "#{'comments removal'.bold} - code comments removal."
option_parser.set_summary_indent ' '
option_parser.separator "\nHelp menu:".underline
option_parser.on('-d', '--dest <FILE | DIRECTORY>', 'Destination file or directory') {|o| options[:destination] = o}
option_parser.on('-l', '--language LANG', 'Force specific language', Array, '(supported languages: java,aspx,javascript,ruby,python,c)') {|o| options[:language] = o}
option_parser.on('-h', '--help', 'Show this help message')
option_parser.on_tail "\nUsage:\n".underline + "ruby #{__FILE__} -d <FILE|DIRECTORY> [OPTIONS]"
option_parser.parse ARGV
# comment regex store for all supported language
def regex
{
'java' => /(\/\*+([^\*]|\*(?!\/))*(\*\/))|(\/{2}.*)/, # (\/\*([\s\S]*?)\*\/)|(\/\/(.*)$)
'aspx' => //,
'javascript' => //,
'js' => //,
'ruby' => //,
'python' => //,
'c' => /((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/,
}
end
# clean, remove comments base on given regex then rewrite the file
def clean(file, regex)
full_code = File.read(file).scrub
code = full_code.gsub(regex, '')
File.open(file, 'w') {|file| file.print code}
return full_code.scan(regex).count
end
def remove_comments(destination, language=nil)
count = 0
if language
language.each do |lang|
if File.directory? destination
dst_dir = Dir.glob("#{destination}/**/*.#{lang}")
dst_dir.each do |file|
count += clean(file, regex[lang])
end
puts "[+] Total Removed comments: #{count}"
elsif File.file? destination
clean(destination, regex[lang])
count += clean(destination, regex[lang])
puts "[+] Total Removed comments: #{count}"
else
puts 'sorry......'
end
end
else # no specific language given
if File.directory? destination
dst_dir = Dir.glob("#{destination}/**/*.*")
dst_dir.each do |file|
extension = file.split('.').last.downcase # auto detect
puts "[+] Language detected by file extension: #{extension}"
count += clean(file, regex[extension])
end
puts "[+] Total Removed comments: #{count}"
elsif File.file? destination
extension = destination.split('.').last.downcase
puts "[+] Language detected by file extension: #{extension}"
count += clean(destination, regex[extension])
puts "[+] Removed comments: #{count}"
else
puts 'sorry......'
end
end
end
# def remove_comments(destination, language=nil?)
# begin
#
# count = 0
# if File.directory? destination
# dst_dir = Dir.glob("#{ARGV[0]}/**/*.java")
# dst_dir.each do |java_file|
# full_code = File.read(java_file).scrub
# code = full_code.gsub(Regex::Java, '')
# count += full_code.scan(Regex::Java).count
# puts "[->] #{java_file}"
# File.open(java_file, 'w') {|file| file.print code}
# end
# puts "[+] Total Removed comments: #{count}"
# elsif File.file? destination
# full_code = File.read(destination).scrub
# code = full_code.gsub(Regex::Java, '')
# count = full_code.scan(Regex::Java).count
# puts "[+] #{destination}"
# puts "[+] Removed comments: #{count}"
# File.open(destination, 'w') {|file| file.print code }
# else
# puts 'sorry......'
# end
#
# rescue Exception => e
# puts e
# puts e.backtrace
# puts e.backtrace_locations
# end
#
# end
case
when options[:destination] && options[:language]
remove_comments(options[:destination], options[:language].map(&:downcase))
when options[:destination]
remove_comments(options[:destination])
else
puts option_parser
end