Skip to content

Commit

Permalink
Make build plugin communicate files via a file list rather than space…
Browse files Browse the repository at this point in the history
…-separated CLI arguments (#10)
  • Loading branch information
dfed authored Jan 12, 2024
1 parent 4274e6f commit 484ee1e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import PackagePlugin

@main
Expand Down Expand Up @@ -26,8 +27,19 @@ struct SafeDIGenerateDependencyTree: BuildToolPlugin {
.map(\.path)
}

let arguments = (targetSwiftFiles + dependenciesSourceFiles).map(\.string)
+ ["--dependency-tree-output", outputSwiftFile.string]
let inputSourcesFilePath = context.pluginWorkDirectory.appending(subpath: "InputSwiftFiles.csv").string
try Data(
(targetSwiftFiles + dependenciesSourceFiles)
.map(\.string)
.joined(separator: ",")
.utf8
)
.write(toPath: inputSourcesFilePath)
let arguments = [
inputSourcesFilePath,
"--dependency-tree-output",
outputSwiftFile.string
]

return [
.buildCommand(
Expand Down Expand Up @@ -93,9 +105,16 @@ extension SafeDIGenerateDependencyTree: XcodeBuildToolPlugin {
}

let outputSwiftFile = context.pluginWorkDirectory.appending(subpath: "SafeDI.swift")
let arguments = inputSwiftFiles
.map(\.string)
+ [
let inputSourcesFilePath = context.pluginWorkDirectory.appending(subpath: "InputSwiftFiles.csv").string
try Data(
inputSwiftFiles
.map(\.string)
.joined(separator: ",")
.utf8
)
.write(toPath: inputSourcesFilePath)
let arguments = [
inputSourcesFilePath,
"--dependency-tree-output",
outputSwiftFile.string
]
Expand All @@ -112,3 +131,17 @@ extension SafeDIGenerateDependencyTree: XcodeBuildToolPlugin {
}
}
#endif

extension Data {
fileprivate func write(toPath filePath: String) throws {
#if os(Linux)
try write(to: URL(fileURLWithPath: filePath))
#else
if #available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) {
try write(to: URL(filePath: filePath))
} else {
try write(to: URL(fileURLWithPath: filePath))
}
#endif
}
}
6 changes: 4 additions & 2 deletions Sources/SafeDITool/SafeDITool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ struct SafeDITool: AsyncParsableCommand {

// MARK: Arguments

@Argument(help: "The swift files to parse")
var swiftFilePaths: [String]
@Argument(help: "A path to a CSV file containing paths of Swift files to parse.")
var swiftSourcesFilePath: String

@Option(parsing: .upToNextOption, help: "The names of modules to import in the generated dependency tree. This list is in addition to the import statements found in files that declare @Instantiable types.")
var additionalImportedModules: [String] = []
Expand Down Expand Up @@ -123,6 +123,8 @@ struct SafeDITool: AsyncParsableCommand {
of: String.self,
returning: [String].self
) { taskGroup in
let swiftFilePaths = try String(contentsOfFile: swiftSourcesFilePath)
.components(separatedBy: CharacterSet(arrayLiteral: ","))
for filePath in swiftFilePaths {
taskGroup.addTask {
let swiftFile = try String(contentsOfFile: filePath)
Expand Down

0 comments on commit 484ee1e

Please sign in to comment.