Skip to content

Commit 484ee1e

Browse files
authored
Make build plugin communicate files via a file list rather than space-separated CLI arguments (#10)
1 parent 4274e6f commit 484ee1e

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

Plugins/SafeDIGenerateDependencyTree/SafeDIGenerateDependencyTree.swift

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Foundation
12
import PackagePlugin
23

34
@main
@@ -26,8 +27,19 @@ struct SafeDIGenerateDependencyTree: BuildToolPlugin {
2627
.map(\.path)
2728
}
2829

29-
let arguments = (targetSwiftFiles + dependenciesSourceFiles).map(\.string)
30-
+ ["--dependency-tree-output", outputSwiftFile.string]
30+
let inputSourcesFilePath = context.pluginWorkDirectory.appending(subpath: "InputSwiftFiles.csv").string
31+
try Data(
32+
(targetSwiftFiles + dependenciesSourceFiles)
33+
.map(\.string)
34+
.joined(separator: ",")
35+
.utf8
36+
)
37+
.write(toPath: inputSourcesFilePath)
38+
let arguments = [
39+
inputSourcesFilePath,
40+
"--dependency-tree-output",
41+
outputSwiftFile.string
42+
]
3143

3244
return [
3345
.buildCommand(
@@ -93,9 +105,16 @@ extension SafeDIGenerateDependencyTree: XcodeBuildToolPlugin {
93105
}
94106

95107
let outputSwiftFile = context.pluginWorkDirectory.appending(subpath: "SafeDI.swift")
96-
let arguments = inputSwiftFiles
97-
.map(\.string)
98-
+ [
108+
let inputSourcesFilePath = context.pluginWorkDirectory.appending(subpath: "InputSwiftFiles.csv").string
109+
try Data(
110+
inputSwiftFiles
111+
.map(\.string)
112+
.joined(separator: ",")
113+
.utf8
114+
)
115+
.write(toPath: inputSourcesFilePath)
116+
let arguments = [
117+
inputSourcesFilePath,
99118
"--dependency-tree-output",
100119
outputSwiftFile.string
101120
]
@@ -112,3 +131,17 @@ extension SafeDIGenerateDependencyTree: XcodeBuildToolPlugin {
112131
}
113132
}
114133
#endif
134+
135+
extension Data {
136+
fileprivate func write(toPath filePath: String) throws {
137+
#if os(Linux)
138+
try write(to: URL(fileURLWithPath: filePath))
139+
#else
140+
if #available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) {
141+
try write(to: URL(filePath: filePath))
142+
} else {
143+
try write(to: URL(fileURLWithPath: filePath))
144+
}
145+
#endif
146+
}
147+
}

Sources/SafeDITool/SafeDITool.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ struct SafeDITool: AsyncParsableCommand {
2929

3030
// MARK: Arguments
3131

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

3535
@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.")
3636
var additionalImportedModules: [String] = []
@@ -123,6 +123,8 @@ struct SafeDITool: AsyncParsableCommand {
123123
of: String.self,
124124
returning: [String].self
125125
) { taskGroup in
126+
let swiftFilePaths = try String(contentsOfFile: swiftSourcesFilePath)
127+
.components(separatedBy: CharacterSet(arrayLiteral: ","))
126128
for filePath in swiftFilePaths {
127129
taskGroup.addTask {
128130
let swiftFile = try String(contentsOfFile: filePath)

0 commit comments

Comments
 (0)