-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage_data.rb
93 lines (75 loc) · 2.16 KB
/
usage_data.rb
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
require 'yaml'
module UsageData
DO_NOT_SEARCH_DIRS = %w[vendor Plugins]
GUID_REFERENCING_EXTENSIONS = %w[.unity .prefab .mat .asset .controller]
IDENTIFIER_REFERENCING_EXTENSIONS = %w[.cs]
IGNORE_UNUSED_EXTENSIONS = %w[.unity .preset .otf .ttf .asset .txt .asmdef]
IGNORE_UNUSED_DIRS = %w[Assets/test/]
class SourceFile
@@by_guid = {}
@@all = []
def self.all
@@all
end
attr_reader :path
def initialize(path)
@path = path
@@by_guid[guid] = self
@@all << self
end
def guid
@guid ||= YAML.load_file(@path + '.meta').fetch('guid')
end
def basename
File.basename(@path)
end
def class_name
basename.split('.').first
end
def contents
@contents ||= File.read(@path)
end
def referenced_guids
return [] unless path.end_with?(*GUID_REFERENCING_EXTENSIONS)
@referenced_guids ||= contents.scan(/guid: ([0-9a-f]+)/).flatten.uniq
end
def referenced_identifiers
return [] unless path.end_with?(*IDENTIFIER_REFERENCING_EXTENSIONS)
@referenced_identifiers ||= contents.scan(/\b[A-Z][A-Za-z0-9]+\b/).flatten.uniq
end
def uses?(file)
referenced_guids.include?(file.guid) || (
file.path.end_with?('.cs') &&
referenced_identifiers.include?(file.class_name)
)
end
def all_other_files
@all_other_files ||= @@all.reject { |f| f == self }
end
def used_by
all_other_files.filter { |f| f.uses?(self) }
end
def used?
path.end_with?(*IGNORE_UNUSED_EXTENSIONS) ||
IGNORE_UNUSED_DIRS.any? { path.include?(_1) } ||
used_by.any?
end
end
def self.search_dirs
@@search_dirs ||= Dir.glob(
File.expand_path('./Assets/*', __dir__)
).filter do |f|
File.directory?(f) && !DO_NOT_SEARCH_DIRS.any? { f.include?(_1) }
end
end
def self.find_matching_pattern(pattern)
search_dirs.flat_map do |search_dir|
Dir.glob(File.join(search_dir, '**', pattern))
end
end
def self.register_source_files
find_matching_pattern('*')
.filter { |f| File.file?(f) && !f.end_with?('.meta') }
.each { |f| SourceFile.new(f) }
end
end