Skip to content

Commit

Permalink
Merge pull request PaulTaykalo#6 from PaulTaykalo/feature/ignore-file…
Browse files Browse the repository at this point in the history
…s-in-xibs-and-storyboards

Ignore files, declared in storboards and xibs
  • Loading branch information
PaulTaykalo committed Apr 26, 2018
2 parents 962fe5f + 8028c94 commit 3a68e4f
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions unused.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,26 @@ def find

# Usage within the file
if private_items.length > 0
find_usages_in_files([my_text_file], private_items)
find_usages_in_files([my_text_file], [], private_items)
end

}

puts "Total items to be checked #{items.length}"

items = items.uniq { |f| f.name }
puts "Total unique items to be checked #{items.length}"

puts "Starting searching globally it can take a while".green

find_usages_in_files(all_files, items)
xibs = Dir.glob("**/*.xib")
storyboards = Dir.glob("**/*.storyboard")

find_usages_in_files(all_files, xibs + storyboards, items)

end

def find_usages_in_files(files, items_in)
def find_usages_in_files(files, xibs, items_in)
items = items_in
usages = items.map { |f| 0 }
files.each { |file|
Expand All @@ -101,6 +105,25 @@ def find_usages_in_files(files, items_in)
indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) }
}

xibs.each { |xib|
lines = File.readlines(xib).map {|line| line.gsub(/^\s*\/\/.*/, "") }
full_xml = lines.join(" ")
classes = full_xml.scan(/(class|customClass)="([^"]+)"/).map { |cd| cd[1] }
classes_array = classes.group_by { |w| w }.map { |w, ws| [w, ws.length] }.flatten

wf = Hash[*classes_array]

items.each_with_index { |f, i|
usages[i] += (wf[f.name] || 0)
}
# Remove all items which has usage 2+
indexes = usages.each_with_index.select { |u, i| u >= 2 }.map { |f, i| i }

# reduce usage array if we found some functions already
indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) }

}


items = items.select { |f| !f.file.start_with?("Pods/") && !f.file.end_with?("Tests.swift") && !f.file.end_with?("Spec.swift") }
if items.length > 0
Expand All @@ -113,7 +136,6 @@ def find_usages_in_files(files, items_in)
end

def grab_items(file)
result = []
lines = File.readlines(file).map {|line| line.gsub(/^\s*\/\/.*/, "") }
items = lines.each_with_index.select { |line, i| line[/(func|let|var|class|enum|struct|protocol)\s+\w+/] }.map { |line, i| Item.new(file, line, i)}
end
Expand Down

0 comments on commit 3a68e4f

Please sign in to comment.