Skip to content

Commit

Permalink
Add support for indexing plist files via the generic `--file-targets-…
Browse files Browse the repository at this point in the history
…path` option
  • Loading branch information
ileitch committed Dec 3, 2023
1 parent 8509117 commit bba4e3f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

##### Enhancements

- None.
- Add support for indexing plist files via the generic `--file-targets-path` option.

##### Bug Fixes

Expand Down
34 changes: 24 additions & 10 deletions Sources/PeripheryKit/Generic/GenericProjectDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,49 @@ import SystemPackage
import Shared

public final class GenericProjectDriver {
private enum FileKind: String {
case swift
case plist
}

public static func build() throws -> Self {
let configuration = Configuration.shared
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

let sourceFiles = try configuration.fileTargetsPath
.reduce(into: [FilePath: Set<IndexTarget>]()) { result, mapPath in
.reduce(into: [FileKind: [FilePath: Set<IndexTarget>]]()) { result, mapPath in
guard mapPath.exists else {
throw PeripheryError.pathDoesNotExist(path: mapPath.string)
}

let data = try Data(contentsOf: mapPath.url)
let map = try decoder
try decoder
.decode(FileTargetMapContainer.self, from: data)
.fileTargets
.reduce(into: [FilePath: Set<IndexTarget>](), { (result, tuple) in
let (key, value) = tuple
.forEach { key, value in
let path = FilePath.makeAbsolute(key)

if !path.exists {
throw PeripheryError.pathDoesNotExist(path: path.string)
}

guard let ext = path.extension, let fileKind = FileKind(rawValue: ext) else {
throw PeripheryError.unsupportedFileKind(path: path)
}

let indexTargets = value.mapSet { IndexTarget(name: $0) }
result[path] = indexTargets
})
result.merge(map) { $0.union($1) }
result[fileKind, default: [:]][path, default: []].formUnion(indexTargets)
}
}

return self.init(sourceFiles: sourceFiles, configuration: configuration)
}

private let sourceFiles: [FilePath: Set<IndexTarget>]
private let sourceFiles: [FileKind: [FilePath: Set<IndexTarget>]]
private let configuration: Configuration

init(sourceFiles: [FilePath: Set<IndexTarget>], configuration: Configuration) {
private init(sourceFiles: [FileKind: [FilePath: Set<IndexTarget>]], configuration: Configuration) {
self.sourceFiles = sourceFiles
self.configuration = configuration
}
Expand All @@ -48,7 +55,14 @@ extension GenericProjectDriver: ProjectDriver {
public func build() throws {}

public func index(graph: SourceGraph) throws {
try SwiftIndexer(sourceFiles: sourceFiles, graph: graph, indexStorePaths: configuration.indexStorePath).perform()
if let swiftFiles = sourceFiles[.swift] {
try SwiftIndexer(sourceFiles: swiftFiles, graph: graph, indexStorePaths: configuration.indexStorePath).perform()
}

if let plistFiles = sourceFiles[.plist] {
try InfoPlistIndexer(infoPlistFiles: Set(plistFiles.keys), graph: graph).perform()
}

graph.indexingComplete()
}
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/Shared/PeripheryError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum PeripheryError: Error, LocalizedError, CustomStringConvertible {
case unindexedTargetsError(targets: Set<String>, indexStorePaths: [FilePath])
case jsonDeserializationError(error: Error, json: String)
case indexStoreNotFound(derivedDataPath: String)
case unsupportedFileKind(path: FilePath)

public var errorDescription: String? {
switch self {
Expand Down Expand Up @@ -65,6 +66,8 @@ public enum PeripheryError: Error, LocalizedError, CustomStringConvertible {
return "JSON deserialization failed: \(describe(error))\nJSON:\n\(json)"
case let .indexStoreNotFound(derivedDataPath):
return "Failed to find index datastore at path: \(derivedDataPath)"
case let .unsupportedFileKind(path):
return "Unsupported file kind '\(path)'."
}
}

Expand Down

0 comments on commit bba4e3f

Please sign in to comment.