@@ -33,61 +33,45 @@ public func removeUnusedAssets(libraryName: String, assetCatalogName: String, pa
33
33
var fileName = libraryName
34
34
_ = fileName. removeLast ( ) // Remove trailing "s"
35
35
36
- let result = try getAllIconNames ( pathToFluentIconSource: pathToFluentIconSource, libraryName: libraryName, fileName: fileName)
36
+ let ( allIconNames , weights ) = try getAllIconNames ( pathToFluentIconSource: pathToFluentIconSource, libraryName: libraryName, fileName: fileName)
37
37
38
38
print ( " Searching for possible swift icon references " )
39
39
let allPossibleSwiftIconReferences = searchForCodeReferences (
40
40
in: pathToSourceCode,
41
41
language: . swift,
42
- weights: result . weights,
42
+ weights: weights,
43
43
excludingFileName: fileName
44
44
)
45
45
46
46
print ( " Searching for possible objc icon references " )
47
47
let allPossibleObjcIconReferences = searchForCodeReferences (
48
48
in: pathToSourceCode,
49
49
language: . objc,
50
- weights: result . weights,
50
+ weights: weights,
51
51
excludingFileName: fileName
52
52
)
53
53
54
- var listOfIconsToKeep : [ String ] = [ ]
54
+ var listOfIconsToKeep = Set < String > ( )
55
55
if let pathToListOfIconsToKeep = pathToListOfIconsToKeep {
56
56
for path in pathToListOfIconsToKeep. split ( separator: " , " ) {
57
57
let fileContents = try String ( contentsOfFile: String ( path) , encoding: . utf8)
58
- listOfIconsToKeep. append ( contentsOf: fileContents. mapToLines ( ) )
58
+ for line in fileContents. mapToLines ( ) {
59
+ listOfIconsToKeep. insert ( line)
60
+ }
59
61
}
60
62
}
61
63
62
- let allPossibleIconReferences = allPossibleSwiftIconReferences + allPossibleObjcIconReferences + listOfIconsToKeep
63
-
64
- let allIconNames = result. names
65
-
66
64
// Validate custom list provided to script has all valid icons
67
- var invalidIcons : [ String ] = [ ]
68
- for icon in listOfIconsToKeep {
69
- if !allIconNames. contains ( icon) {
70
- invalidIcons. append ( icon)
71
- }
72
- }
65
+ let invalidIcons = listOfIconsToKeep. subtracting ( allIconNames)
73
66
if !invalidIcons. isEmpty {
74
67
print ( " List of icons provided contains invalid icons \( invalidIcons) " )
75
68
if exitOnIncorrectlyUsedIconName {
76
69
exit ( 1 )
77
70
}
78
71
}
79
72
80
- let allPossibleIconReferencesLower = allPossibleIconReferences. map { $0. lowercased ( ) }
81
- let allIconNamesLower = Set ( allIconNames. map { $0. lowercased ( ) } )
82
-
83
- var iconsUsed = Set < String > ( )
84
- for line in allPossibleIconReferencesLower {
85
- for iconName in allIconNamesLower {
86
- if line. contains ( iconName) {
87
- iconsUsed. insert ( iconName)
88
- }
89
- }
90
- }
73
+ print ( " Finding used icons " )
74
+ let allUsedIcons = allPossibleSwiftIconReferences. union ( allPossibleObjcIconReferences) . union ( listOfIconsToKeep)
91
75
92
76
let pathToFluentIconAssets = " \( pathToFluentIconSource) /ios/ \( libraryName) /Assets/ \( assetCatalogName) .xcassets "
93
77
@@ -100,13 +84,13 @@ public func removeUnusedAssets(libraryName: String, assetCatalogName: String, pa
100
84
}
101
85
102
86
print ( " Removing unused assets " )
103
- var count = 0
87
+ var removedCount = 0
104
88
for directory in directories {
105
- let iconName = getIconName ( from: directory) . lowercased ( )
106
- if !iconsUsed . contains ( iconName) {
89
+ let iconName = getIconName ( from: directory)
90
+ if !allUsedIcons . contains ( iconName) {
107
91
try FileManager . default. removeItem ( at: directory)
108
- count += 1
92
+ removedCount += 1
109
93
}
110
94
}
111
- print ( " Removed \( count ) unused assets. " )
95
+ print ( " Removed \( removedCount ) " )
112
96
}
0 commit comments