Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make build plugin communicate files via a file list rather than space-separated CLI arguments #10

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,20 @@ struct SafeDIGenerateDependencyTree: BuildToolPlugin {
.map(\.path)
}

let arguments = (targetSwiftFiles + dependenciesSourceFiles).map(\.string)
+ ["--dependency-tree-output", outputSwiftFile.string]
let inputSwiftFilesFilePath = context.pluginWorkDirectory.appending(subpath: "InputSwiftFiles.txt").string
try Data(
(targetSwiftFiles + dependenciesSourceFiles)
.map(\.string)
.joined(separator: "\n")
.utf8
)
.write(toPath: inputSwiftFilesFilePath)
let arguments = [
"--swift-file-paths-file-path",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this argument name. If you've got better ideas, I am all ears.

Copy link
Collaborator

@MrAdamBoyd MrAdamBoyd Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • --swift-sources-file-path
  • --swift-source-files-file-path
  • --swift-source-list-file-path

Best I got

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooooh swiftSources is good.

inputSwiftFilesFilePath,
"--dependency-tree-output",
outputSwiftFile.string
]

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

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

extension Data {
fileprivate func write(toPath filePath: String) throws {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extension also exists in another module, but this plug in module cannot depend on any other library we have in this package

#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
}
Comment on lines +137 to +146
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be simplified?

         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))
         }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nvm, it's #if something not if #something

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup. tried that 🙂. Took me awhile to get this right. Linux availability support is... bad (read: nonexistent).

}
15 changes: 13 additions & 2 deletions Sources/SafeDITool/SafeDITool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ struct SafeDITool: AsyncParsableCommand {

// MARK: Arguments

@Argument(help: "The swift files to parse")
var swiftFilePaths: [String]
@Argument(help: "The Swift files to parse.")
var swiftFilePaths: [String] = []

@Option(help: "A path to a file containing newline-separated Swift files to parse.")
var swiftFilePathsFilePath: 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 +126,14 @@ struct SafeDITool: AsyncParsableCommand {
of: String.self,
returning: [String].self
) { taskGroup in
let swiftFilePaths: [String]
if let swiftFilePathsFilePath {
swiftFilePaths = try String(contentsOfFile: swiftFilePathsFilePath)
.components(separatedBy: .newlines)
+ self.swiftFilePaths
} else {
swiftFilePaths = self.swiftFilePaths
}
for filePath in swiftFilePaths {
taskGroup.addTask {
let swiftFile = try String(contentsOfFile: filePath)
Expand Down