Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Nov 7, 2023
1 parent 8dc0e85 commit dbf148d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 58 deletions.
25 changes: 21 additions & 4 deletions Plugins/SwiftFormatPlugin/SwiftFormatPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ import PackagePlugin
print("Command plugin execution with arguments \(arguments.description) for Swift package \(context.package.displayName). All target information: \(context.package.targets.description)")
}

var targetsToProcess: [Target] = context.package.targets

var argExtractor = ArgumentExtractor(arguments)

let selectedTargets = argExtractor.extractOption(named: "target")

if selectedTargets.isEmpty == false {
targetsToProcess = context.package.targets.filter { selectedTargets.contains($0.name) }.map { $0 }
let targetsToProcess: [Target]
if selectedTargets.isEmpty {
targetsToProcess = context.package.targets
} else {
targetsToProcess = try context.allLocalTargets(for: selectedTargets)
}

for target in targetsToProcess {
Expand All @@ -58,4 +59,20 @@ import PackagePlugin
}
}
}

extension PluginContext {
func allLocalTargets(for targetNames: [String]) throws -> [Target] {
let matchingTargets = try self.package.targets(named: targetNames)
let packageTargets = Set(self.package.targets.map(\.id))
let withLocalDependencies = matchingTargets.flatMap { [$0] + $0.recursiveTargetDependencies }
.filter { packageTargets.contains($0.id) }
let enumeratedKeyValues = withLocalDependencies.map(\.id).enumerated()
.map { (key: $0.element, value: $0.offset) }
let indexLookupTable = Dictionary(enumeratedKeyValues, uniquingKeysWith: { l, _ in l })
let groupedByID = Dictionary(grouping: withLocalDependencies, by: \.id)
let sortedUniqueTargets = groupedByID.map(\.value[0])
.sorted { indexLookupTable[$0.id, default: 0] < indexLookupTable[$1.id, default: 0] }
return sortedUniqueTargets
}
}
#endif
54 changes: 0 additions & 54 deletions Plugins/SwiftFormatPlugin/SwiftFormatPluginXcode.swift

This file was deleted.

0 comments on commit dbf148d

Please sign in to comment.