Skip to content

Commit

Permalink
Allow passing multiple index stores to unused imports tool (#50)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pennig authored Jul 20, 2023
1 parent fd4ad35 commit 9c89147
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Sources/unused-imports/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ private let ignoreRegex = try Regex(#"// *@ignore-import$"#)
private var cachedLines = [String: [String.SubSequence]]()

private struct Configuration: Decodable {
static func attemptingPath(_ path: String?) -> Configuration? {
guard let path else { return nil }
do {
return try JSONDecoder().decode(
Configuration.self,
from: try Data(contentsOf: URL(fileURLWithPath: path))
)
} catch {
return nil
}
}

let ignoredFileRegex: Regex<AnyRegexOutput>?
let ignoredModuleRegex: Regex<AnyRegexOutput>?
let alwaysKeepImports: Set<String>
Expand Down Expand Up @@ -144,7 +156,7 @@ private func collectUnitsAndRecords(indexStorePath: String) -> [(UnitReader, Rec
}

private func main(
indexStorePath: String,
indexStorePaths: [String],
configuration: Configuration)
{
if let directory = ProcessInfo.processInfo.environment["BUILD_WORKSPACE_DIRECTORY"] {
Expand All @@ -153,7 +165,7 @@ private func main(

let pwd = FileManager.default.currentDirectoryPath
var filesToDefinitions: [String: References] = [:]
let unitsAndRecords = collectUnitsAndRecords(indexStorePath: indexStorePath)
let unitsAndRecords = indexStorePaths.flatMap(collectUnitsAndRecords(indexStorePath:))
var modulesToUnits: [String: [UnitReader]] = [:]
var allModuleNames = Set<String>()

Expand Down Expand Up @@ -230,17 +242,15 @@ private func main(
}
}

if CommandLine.arguments.count == 3 {
let configurationData = try! Data(contentsOf: URL(fileURLWithPath: CommandLine.arguments[1]))
let configuration = try! JSONDecoder().decode(Configuration.self, from: configurationData)

let arguments = CommandLine.arguments.dropFirst()
if let configuration = Configuration.attemptingPath(arguments.first) {
main(
indexStorePath: CommandLine.arguments[2],
indexStorePaths: Array(arguments.dropFirst()),
configuration: configuration
)
} else {
main(
indexStorePath: CommandLine.arguments[1],
indexStorePaths: Array(arguments),
configuration: Configuration()
)
}

0 comments on commit 9c89147

Please sign in to comment.