Skip to content

Instantly share code, notes, and snippets.

@dkordik
Created November 21, 2012 15:25
Show Gist options
  • Save dkordik/4125419 to your computer and use it in GitHub Desktop.
Save dkordik/4125419 to your computer and use it in GitHub Desktop.
Check if each JS file is referenced in our Views, log unreferenced and referenced files - ACK required
#!/usr/bin/env ruby
class String
def -(other)
self.index(other) == 0 ? self[other.size..self.size] : nil
end
end
if ARGV.length < 2
puts
puts "Find JavaScript files that are no longer referenced in any Views."
puts
puts "USAGE: ackref <scripts root directory> <views root directory>"
puts
exit
end
BASE_LOG_PATH = "/home/dan.kordik/code/deadreferences" # change this, obviously
timestamp = Time.now.to_i
scripts_root_path = ARGV[0]
views_root_path = ARGV[1]
script_files = Dir[File.join(scripts_root_path, '**', '*.js')]
script_files.each do |script_file|
bare_script_file = script_file - scripts_root_path
ack_result = `ack -u "#{bare_script_file}" "#{views_root_path}"`
if ack_result.length > 0
puts "#{bare_script_file}"
File.open("#{BASE_LOG_PATH}/ackrefs-#{timestamp}.txt", 'a') do |f|
f.write "#{bare_script_file}\n\n"
f.write "#{ack_result}\n\n"
end
else
puts "DEAD: #{bare_script_file}"
File.open("#{BASE_LOG_PATH}/ackrefs-#{timestamp}-dead.txt", 'a') do |f|
f.write "#{script_file}\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment