Last active
December 3, 2018 22:01
-
-
Save eanakashima/9459773 to your computer and use it in GitHub Desktop.
Remove unused classes and IDs
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'tempfile' | |
require 'fileutils' | |
require 'set' | |
require 'pry' | |
# run with: | |
# `ruby un-id.rb` | |
# --------------------------------------------------------------------------- | |
# Configure these variables to match your file structure | |
# --------------------------------------------------------------------------- | |
@css_file_path = '/Users/ea/go/src/github.com/honeycombio/hound/cmd/poodle/static/dist/main.d0f9d93462974fcd988c.css' | |
@html_dir_paths = ['/Users/ea/go/src/github.com/honeycombio/hound/cmd/poodle/templates', '/Users/ea/go/src/github.com/honeycombio/hound/cmd/poodle/javascript'] | |
@result_file_name = 'result_' + Time.now.strftime("%Y%m%d-%H%M") + '.txt' | |
# --------------------------------------------------------------------------- | |
# Nothing to configure below this line | |
# --------------------------------------------------------------------------- | |
@selectors = Set.new | |
@template_lines = Set.new | |
def find_selectors | |
source = File.expand_path(@css_file_path, __FILE__) | |
File.foreach(source) { |line| find_selectors_by_line(line) } | |
end | |
def find_selectors_by_line(line) | |
# check for multiple matches per line | |
while m = line.match(/(\.|\#)[_a-zA-Z]+[_a-zA-Z0-9-]*/) | |
@selectors << m.to_s | |
log("found selector", m.to_s) | |
line.gsub!(m.to_s, '') | |
end | |
end | |
def check_selectors | |
find_template_lines_with_classes_or_ids | |
@selectors.each do |selector| | |
unless selector_found_in_any_file?(selector) | |
log("potentially unused selector:", selector) | |
append_result(selector) | |
end | |
end | |
end | |
def find_template_lines_with_classes_or_ids | |
@html_dir_paths.each do |dir| | |
html_dir = File.expand_path(dir, __FILE__) | |
Dir.glob(html_dir + "/**/*").each do |file| | |
if !File.directory?(file) && %w{.js .jsx .tmpl}.include?(File.extname(file)) | |
File.foreach(file) do |line| | |
if line.match(/(class=|id=|className=)/) | |
log("line:", line) | |
log("filename:", file) | |
@template_lines << line | |
end | |
end | |
end | |
end | |
end | |
end | |
def selector_found_in_any_file?(selector) | |
s = strip_selector_chars(selector) | |
@template_lines.each do |line| | |
if line.match(/(class=|id=|className=).*("|\s)#{s}("|\s)/).to_s != "" | |
log("found:", line, s) | |
return true | |
end | |
end | |
end | |
def strip_selector_chars(selector) | |
selector.gsub('.','').gsub('#','') | |
end | |
def append_result(selector) | |
print '.' | |
File.open(@result_file_name, 'a+') { |f| f.puts(selector) } | |
end | |
def log(*args) | |
puts(args) | |
end | |
# --------------------------------------------------------------------------- | |
# run script | |
# --------------------------------------------------------------------------- | |
log "finding selectors" | |
find_selectors | |
log "checking selectors" | |
check_selectors | |
# --------------------------------------------------------------------------- | |
# post-processing to do in the shell... | |
# --------------------------------------------------------------------------- | |
`sort #{@result_file_name} | uniq > latest_results.txt` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment