Created
November 1, 2013 16:49
-
-
Save rdallasgray/7268276 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file_paths = Dir::glob("{app,lib}/**/*.rb") | |
tokens = {} | |
MODULE_DEC_RE = /^(?:(?:\s|\t)*(?:module|class))\s+([\w]+)/ | |
MODULE_METHODS_RE = /^(?:(?:\s|\t)+def\sself\.)([\w\?!]+)/ | |
INSTANCE_METHODS_RE = /^(?:(?:\s|\t)+def\s(?!self\.))([\w\?!]+)/ | |
CONSTANTS_RE = /^\s+([A-Z0-9_]+)(?:\s+=)/ | |
puts Time.now | |
def find_tokens(path, store) | |
File.open(path, "r:utf-8") do |f| | |
module_basename = "" | |
f.each_line do |line| | |
module_dec_match = MODULE_DEC_RE.match(line) | |
module_basename = module_dec_match[1] if module_dec_match | |
%w{module_methods instance_methods constants}.each do |token| | |
match = Kernel.const_get("#{token.upcase}_RE").match(line) | |
store[token.to_sym] << match[1] if match | |
end | |
end | |
store[:module_basename] = module_basename | |
end | |
end | |
def ack_token_with(basename, token, exp_receiver_token, path) | |
token.gsub!(/\?/, '\\\\\\?') | |
with_exp_receiver = "#{basename}#{exp_receiver_token}#{token}" | |
res = ack_token(with_exp_receiver) | |
res_in_path = ack_token_in_path(token, path) if res == 0 | |
puts "!!!! ----- UNUSED ----- !!!!" if res_in_path == 1 | |
end | |
def ack_token_in_path(token, path) | |
puts "Checking in #{path}:" | |
ack_result = `ack #{token}[^\\\\w\\!\\?] #{path}` | |
count = ack_result.lines.count | |
puts "#{count} results" | |
count | |
end | |
def ack_token(token, msg = nil) | |
puts "\n****** Ack #{token} #{msg}*******" | |
ack_result = `ack --type=ruby #{token}[^\\\\w\\!\\?] {app,lib}/**/*` | |
count = ack_result.lines.count | |
puts "#{count} results" | |
puts ack_result | |
count | |
end | |
file_paths.each do |path| | |
tokens[path] = { module_methods: [], instance_methods: [], constants: [] } | |
find_tokens(path, tokens[path]) | |
end | |
tokens.each do |path, values| | |
puts "\n\n-------------------------------------------------------------------------------------" | |
puts "Path: #{path}" | |
puts "Module basename: #{values[:module_basename]}" | |
unless values[:constants].empty? | |
puts "\nConstants" | |
puts "--------------------------------------------" | |
values[:constants].each do |token| | |
ack_token_with(values[:module_basename], token, "::", path) | |
end | |
end | |
unless values[:module_methods].empty? | |
puts "\nModule methods" | |
puts "--------------------------------------------" | |
values[:module_methods].each do |token| | |
ack_token_with(values[:module_basename], token, '\\\.', path) | |
end | |
end | |
unless values[:instance_methods].empty? | |
puts "\nInstance Methods" | |
puts "--------------------------------------------" | |
values[:instance_methods].each do |token| | |
unless token == "initialize" | |
ack_token_with("", token, '\\\.', path) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment