Skip to content

Commit

Permalink
Add XcodeProjectPlugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Dec 5, 2023
1 parent e1336e6 commit 76b51ed
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,35 @@ extension SafeDICollectInstantiables: XcodeBuildToolPlugin {
target: XcodeProjectPlugin.XcodeTarget)
throws -> [PackagePlugin.Command]
{
[] // showstopper TODO: Support Xcode project plugin!
let inputSwiftFiles = target
.inputFiles
.filter { $0.path.extension == "swift" }
.map(\.path)
guard !inputSwiftFiles.isEmpty else {
// There are no Swift files in this module!
return []
}
let outputSafeDIFile = context.pluginWorkDirectory.appending(subpath: "\(target.displayName).safedi")
let arguments = inputSwiftFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
+ [
"--instantiables-output",
outputSafeDIFile
.string
.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
].compactMap { $0 }

return [
.buildCommand(
displayName: "SafeDIPlugin",
executable: try context.tool(named: "SafeDIPlugin").path,
arguments: arguments,
environment: [:],
inputFiles: inputSwiftFiles,
outputFiles: [outputSafeDIFile])
]

}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ struct SafeDIGenerateDependencyTree: BuildToolPlugin {
}
}

extension Target {

var sourceModuleRecursiveDependencies: [SourceModuleTarget] {
recursiveTargetDependencies.compactMap {
$0 as? SourceModuleTarget
}
}

}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin

Expand All @@ -64,17 +74,73 @@ extension SafeDIGenerateDependencyTree: XcodeBuildToolPlugin {
target: XcodeProjectPlugin.XcodeTarget)
throws -> [PackagePlugin.Command]
{
[] // showstopper TODO: Support Xcode project plugin!
let inputSwiftFiles = target
.inputFiles
.filter { $0.path.extension == "swift" }
.map(\.path)
guard !inputSwiftFiles.isEmpty else {
// There are no Swift files in this module!
return []
}

let outputSwiftFile = context.pluginWorkDirectory.appending(subpath: "SafeDI.swift")
let targetDependencySafeDIOutputFiles = target
.sourceModuleRecursiveDependencies
.map {
context
.pluginWorkDirectory
.removingLastComponent() // Remove `SafeDIGenerateDependencyTree` from path.
.removingLastComponent() // Remove current module name from path.
.appending([
$0.displayName, // Dependency module name.
"SafeDICollectInstantiables", // SafeDICollectInstantiables working directory
"\($0.displayName).safedi" // SafeDICollectInstantiables output file.
])
}
.filter { FileManager.default.fileExists(atPath: $0.string) }

let arguments = inputSwiftFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
+ ["--instantiables-paths"]
+ targetDependencySafeDIOutputFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
+ [
"--dependency-tree-output",
outputSwiftFile
.string
.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
].compactMap { $0 }

return [
.buildCommand(
displayName: "SafeDIGenerateDependencyTree",
executable: try context.tool(named: "GenerateDependencyTree").path,
arguments: arguments,
environment: [:],
inputFiles: inputSwiftFiles + targetDependencySafeDIOutputFiles,
outputFiles: [outputSwiftFile])
]
}
}
#endif

extension Target {
extension XcodeProjectPlugin.XcodeTarget {

var sourceModuleRecursiveDependencies: [SourceModuleTarget] {
recursiveTargetDependencies.compactMap {
$0 as? SourceModuleTarget
}
var sourceModuleRecursiveDependencies: [XcodeProjectPlugin.XcodeTarget] {
dependencies
.compactMap { dependency in
switch dependency {
case let .target(xcodeTarget):
return xcodeTarget
case .product:
return nil
@unknown default:
return nil
}
}
.flatMap(\.sourceModuleRecursiveDependencies)
}

}
#endif

0 comments on commit 76b51ed

Please sign in to comment.