-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
441 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Plugins/SafeDICollectInstantiables/SafeDICollectInstantiables.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Foundation | ||
import PackagePlugin | ||
|
||
@main | ||
struct SafeDICollectInstantiables: BuildToolPlugin { | ||
func createBuildCommands( | ||
context: PluginContext, | ||
target: Target) | ||
async throws -> [Command] | ||
{ | ||
guard let sourceTarget = target as? SourceModuleTarget else { | ||
return [] | ||
} | ||
|
||
let outputSafeDIFile = context.pluginWorkDirectory.appending(subpath: "\(sourceTarget.moduleName).safedi") | ||
let inputSwiftFiles = sourceTarget.sourceFiles(withSuffix: ".swift").map(\.path) | ||
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]) | ||
] | ||
} | ||
} | ||
|
||
#if canImport(XcodeProjectPlugin) | ||
import XcodeProjectPlugin | ||
|
||
extension SafeDICollectInstantiables: XcodeBuildToolPlugin { | ||
func createBuildCommands( | ||
context: XcodeProjectPlugin.XcodePluginContext, | ||
target: XcodeProjectPlugin.XcodeTarget) | ||
throws -> [PackagePlugin.Command] | ||
{ | ||
[] // showstopper TODO: Support Xcode project plugin! | ||
} | ||
} | ||
#endif |
80 changes: 80 additions & 0 deletions
80
Plugins/SafeDIGenerateDependencyTree/SafeDIGenerateDependencyTree.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import Foundation | ||
import PackagePlugin | ||
|
||
@main | ||
struct SafeDIGenerateDependencyTree: BuildToolPlugin { | ||
func createBuildCommands( | ||
context: PluginContext, | ||
target: Target) | ||
async throws -> [Command] | ||
{ | ||
guard let sourceTarget = target as? SourceModuleTarget else { | ||
return [] | ||
} | ||
|
||
let inputSwiftFiles = sourceTarget.sourceFiles(withSuffix: ".swift").map(\.path) | ||
let outputSwiftFile = context.pluginWorkDirectory.appending(subpath: "SafeDI.swift") | ||
let targetDependencySafeDIOutputFiles = sourceTarget | ||
.sourceModuleRecursiveDependencies | ||
.map { | ||
context | ||
.pluginWorkDirectory | ||
.removingLastComponent() // Remove `SafeDIGenerateDependencyTree` 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. | ||
]) | ||
} | ||
.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]) | ||
] | ||
} | ||
} | ||
|
||
#if canImport(XcodeProjectPlugin) | ||
import XcodeProjectPlugin | ||
|
||
extension SafeDIGenerateDependencyTree: XcodeBuildToolPlugin { | ||
func createBuildCommands( | ||
context: XcodeProjectPlugin.XcodePluginContext, | ||
target: XcodeProjectPlugin.XcodeTarget) | ||
throws -> [PackagePlugin.Command] | ||
{ | ||
[] // showstopper TODO: Support Xcode project plugin! | ||
} | ||
} | ||
#endif | ||
|
||
extension Target { | ||
|
||
var sourceModuleRecursiveDependencies: [SourceModuleTarget] { | ||
recursiveTargetDependencies.compactMap { | ||
$0 as? SourceModuleTarget | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
Sources/SafeDICore/Generators/DependencyTreeGenerator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Distributed under the MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
public final class DependencyTreeGenerator { | ||
|
||
// MARK: Initialization | ||
|
||
public init(typeDescriptionToFulfillingInstantiable: [TypeDescription : Instantiable]) { | ||
self.typeDescriptionToFulfillingInstantiable = typeDescriptionToFulfillingInstantiable | ||
} | ||
|
||
// MARK: Public | ||
|
||
public func generate() async throws -> String { | ||
"" // TODO: actually generate the type | ||
} | ||
|
||
// MARK: Private | ||
|
||
private let typeDescriptionToFulfillingInstantiable: [TypeDescription : Instantiable] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.