Skip to content

Commit 9c89147

Browse files
authored
Allow passing multiple index stores to unused imports tool (#50)
Useful for operating on per-module index stores. To wit, an unused `import B` in module A will only be marked as unused if the tool can collect the units from A and the USRs from B.
1 parent fd4ad35 commit 9c89147

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

Sources/unused-imports/main.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ private let ignoreRegex = try Regex(#"// *@ignore-import$"#)
88
private var cachedLines = [String: [String.SubSequence]]()
99

1010
private struct Configuration: Decodable {
11+
static func attemptingPath(_ path: String?) -> Configuration? {
12+
guard let path else { return nil }
13+
do {
14+
return try JSONDecoder().decode(
15+
Configuration.self,
16+
from: try Data(contentsOf: URL(fileURLWithPath: path))
17+
)
18+
} catch {
19+
return nil
20+
}
21+
}
22+
1123
let ignoredFileRegex: Regex<AnyRegexOutput>?
1224
let ignoredModuleRegex: Regex<AnyRegexOutput>?
1325
let alwaysKeepImports: Set<String>
@@ -144,7 +156,7 @@ private func collectUnitsAndRecords(indexStorePath: String) -> [(UnitReader, Rec
144156
}
145157

146158
private func main(
147-
indexStorePath: String,
159+
indexStorePaths: [String],
148160
configuration: Configuration)
149161
{
150162
if let directory = ProcessInfo.processInfo.environment["BUILD_WORKSPACE_DIRECTORY"] {
@@ -153,7 +165,7 @@ private func main(
153165

154166
let pwd = FileManager.default.currentDirectoryPath
155167
var filesToDefinitions: [String: References] = [:]
156-
let unitsAndRecords = collectUnitsAndRecords(indexStorePath: indexStorePath)
168+
let unitsAndRecords = indexStorePaths.flatMap(collectUnitsAndRecords(indexStorePath:))
157169
var modulesToUnits: [String: [UnitReader]] = [:]
158170
var allModuleNames = Set<String>()
159171

@@ -230,17 +242,15 @@ private func main(
230242
}
231243
}
232244

233-
if CommandLine.arguments.count == 3 {
234-
let configurationData = try! Data(contentsOf: URL(fileURLWithPath: CommandLine.arguments[1]))
235-
let configuration = try! JSONDecoder().decode(Configuration.self, from: configurationData)
236-
245+
let arguments = CommandLine.arguments.dropFirst()
246+
if let configuration = Configuration.attemptingPath(arguments.first) {
237247
main(
238-
indexStorePath: CommandLine.arguments[2],
248+
indexStorePaths: Array(arguments.dropFirst()),
239249
configuration: configuration
240250
)
241251
} else {
242252
main(
243-
indexStorePath: CommandLine.arguments[1],
253+
indexStorePaths: Array(arguments),
244254
configuration: Configuration()
245255
)
246256
}

0 commit comments

Comments
 (0)