Skip to content

Commit

Permalink
Ensure we can build packages from command line via 'swift build'
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Dec 9, 2023
1 parent 98f74cb commit b3276b5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ struct SafeDICollectInstantiables: BuildToolPlugin {

let outputSafeDIFile = context.pluginWorkDirectory.appending(subpath: "\(sourceTarget.moduleName).safedi")
let inputSwiftFiles = sourceTarget.sourceFiles(withSuffix: ".swift").map(\.path)
guard !inputSwiftFiles.isEmpty else {
return []
}

let arguments = inputSwiftFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,50 @@ struct SafeDIGenerateDependencyTree: BuildToolPlugin {
}

let inputSwiftFiles = sourceTarget.sourceFiles(withSuffix: ".swift").map(\.path)
guard !inputSwiftFiles.isEmpty else {
return []
}

let outputSwiftFile = context.pluginWorkDirectory.appending(subpath: "SafeDI.swift")
let targetDependencySafeDIOutputFiles = sourceTarget
.sourceModuleRecursiveDependencies
.map {
.flatMap {[
// Find dependencies when building within a Package.swift file.
context
.pluginWorkDirectory
.removingLastComponent() // Remove `SafeDIGenerateDependencyTree` from path.
.removingLastComponent() // Remove `SafeDIGenerateDependencyTree` or `SafeDICollectInstantiables` from path.
.removingLastComponent() // Remove current module name from path.
.appending([
$0.name, // Dependency module name.
"SafeDICollectInstantiables", // SafeDICollectInstantiables working directory
"\($0.name).safedi" // SafeDICollectInstantiables output file.
]),
// Find dependencies when building within `swift build` CLI.
context
.pluginWorkDirectory
.removingLastComponent() // Remove `<Package>_<Target>.bundle` from path.
.appending([
"\(context.package.displayName)_\($0.name).bundle", // Dependency module bundle.
"\($0.name).safedi" // SafeDICollectInstantiables output file.
])
}
]}
.filter { FileManager.default.fileExists(atPath: $0.string) }

let arguments = inputSwiftFiles
let instantiablePaths = targetDependencySafeDIOutputFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
+ ["--instantiables-paths"]
+ targetDependencySafeDIOutputFiles
let instantiablePathsArguments: [String] = if !instantiablePaths.isEmpty {
["--instantiables-paths"]
+ targetDependencySafeDIOutputFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
} else {
[]
}
let arguments = inputSwiftFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
+ instantiablePathsArguments
+ [
"--dependency-tree-output",
outputSwiftFile
Expand Down Expand Up @@ -86,26 +107,43 @@ extension SafeDIGenerateDependencyTree: XcodeBuildToolPlugin {
let outputSwiftFile = context.pluginWorkDirectory.appending(subpath: "SafeDI.swift")
let targetDependencySafeDIOutputFiles = target
.sourceModuleRecursiveDependencies
.map {
.flatMap {[
// Find dependencies when building within a Package.swift file.
context
.pluginWorkDirectory
.removingLastComponent() // Remove `SafeDIGenerateDependencyTree` from path.
.removingLastComponent() // Remove `SafeDIGenerateDependencyTree` or `SafeDICollectInstantiables` 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.
])
}
]),
// Find dependencies when building within `swift build` CLI.
context
.pluginWorkDirectory
.removingLastComponent() // Remove `<Package>_<Target>.bundle` from path.
.appending([
"\(context.xcodeProject.displayName)_\($0.displayName).bundle", // Dependency module bundle.
"\($0.displayName).safedi" // SafeDICollectInstantiables output file.
]),
]}
.filter { FileManager.default.fileExists(atPath: $0.string) }

let arguments = inputSwiftFiles
let instantiablePaths = targetDependencySafeDIOutputFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
+ ["--instantiables-paths"]
+ targetDependencySafeDIOutputFiles
let instantiablePathsArguments: [String] = if !instantiablePaths.isEmpty {
["--instantiables-paths"]
+ targetDependencySafeDIOutputFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
} else {
[]
}
let arguments = inputSwiftFiles
.map(\.string)
.compactMap { $0.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
+ instantiablePathsArguments
+ [
"--dependency-tree-output",
outputSwiftFile
Expand Down

0 comments on commit b3276b5

Please sign in to comment.