Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 19 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ let package = Package(
name: "GraphQLGeneratorPlugin",
targets: ["GraphQLGeneratorPlugin"]
),
.plugin(
name: "GraphQLGeneratorCommand",
targets: ["GraphQLGeneratorCommand"]
),
.library(
name: "GraphQLGeneratorRuntime",
targets: ["GraphQLGeneratorRuntime"]
Expand All @@ -32,12 +36,26 @@ let package = Package(
.package(url: "https://github.com/jpsim/Yams.git", "4.0.0"..<"7.0.0"),
],
targets: [
// Build plugin
.plugin(
name: "GraphQLGeneratorPlugin",
capability: .buildTool(),
dependencies: ["GraphQLGenerator"]
),
.plugin(
name: "GraphQLGeneratorCommand",
capability: .command(
intent: .custom(
verb: "generate-code-from-graphql",
description: "Generate Swift code from a GraphQL Schema."
),
permissions: [
.writeToPackageDirectory(
reason: "To write the generated Swift files back into the source directory of the package."
)
]
),
dependencies: ["GraphQLGenerator"]
),
.executableTarget(
name: "GraphQLGenerator",
dependencies: [
Expand Down
1 change: 1 addition & 0 deletions Plugins/GraphQLGeneratorCommand/PluginsShared
83 changes: 83 additions & 0 deletions Plugins/GraphQLGeneratorCommand/plugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import Foundation
import PackagePlugin

@main
struct GraphQLGeneratorCommand {
func runCommand(
targetWorkingDirectory: URL,
tool: (String) throws -> PluginContext.Tool,
sourceFiles: FileList,
targetName: String
) throws {
let inputs = try PluginUtils.validateInputs(
pluginWorkDirectory: targetWorkingDirectory,
tool: tool,
sourceFiles: sourceFiles,
sourceDirectory: targetWorkingDirectory,
targetName: targetName,
pluginSource: .command
)

let process = Process()
process.executableURL = inputs.tool.url
process.arguments = inputs.arguments
process.environment = [:]
try process.run()
process.waitUntilExit()
guard process.terminationStatus == 0 else { throw PluginError.generatorFailure(targetName: targetName) }
}
}

extension GraphQLGeneratorCommand: CommandPlugin {
/// Entry point for creating command plugins for targets in Swift packages.
func performCommand(context: PluginContext, arguments: [String]) async throws {
let targetNameArguments = arguments.filter({ $0 != "--target" })
let targets: [Target]
if targetNameArguments.isEmpty {
targets = context.package.targets
} else {
let matchingTargets = try context.package.targets(named: targetNameArguments)
let packageTargets = Set(context.package.targets.map(\.id))
let withLocalDependencies = matchingTargets.flatMap { [$0] + $0.recursiveTargetDependencies }
.filter { packageTargets.contains($0.id) }
let enumeratedKeyValues = withLocalDependencies.map(\.id).enumerated()
.map { (key: $0.element, value: $0.offset) }
let indexLookupTable = Dictionary(enumeratedKeyValues, uniquingKeysWith: { l, _ in l })
let groupedByID = Dictionary(grouping: withLocalDependencies, by: \.id)
let sortedUniqueTargets = groupedByID.map(\.value[0])
.sorted { indexLookupTable[$0.id, default: 0] < indexLookupTable[$1.id, default: 0] }
targets = sortedUniqueTargets
}

guard !targets.isEmpty else { throw PluginError.noTargetsMatchingTargetNames(targetNameArguments) }

var hadASuccessfulRun = false

for target in targets {
print("Considering target '\(target.name)':")
guard let target = target as? SwiftSourceModuleTarget else {
print("- Not a swift source module. Can't generate GraphQL code.")
continue
}
do {
print("- Trying GraphQL code generation.")
try runCommand(
targetWorkingDirectory: target.directoryURL,
tool: context.tool,
sourceFiles: target.sourceFiles,
targetName: target.name
)
print("- ✅ GraphQL code generation for target '\(target.name)' successfully completed.")
hadASuccessfulRun = true
} catch PluginError.noSchemaFiles {
print("- No GraphQL schema files found. Skipping.")
continue
} catch {
print("- GraphQL code generation failed with error.")
throw error
}
}

guard hadASuccessfulRun else { throw PluginError.noTargetsGenerated(targetNames: targets.map(\.name)) }
}
}
1 change: 1 addition & 0 deletions Plugins/GraphQLGeneratorPlugin/PluginsShared
73 changes: 73 additions & 0 deletions Plugins/GraphQLGeneratorPlugin/plugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Foundation
import PackagePlugin

@main struct GraphQLGeneratorPlugin {
func createBuildCommands(
pluginWorkDirectory: URL,
tool: (String) throws -> PluginContext.Tool,
sourceFiles: FileList,
sourceDirectory: URL,
targetName: String
) throws -> [Command] {
let inputs = try PluginUtils.validateInputs(
pluginWorkDirectory: pluginWorkDirectory,
tool: tool,
sourceFiles: sourceFiles,
sourceDirectory: sourceDirectory,
targetName: targetName,
pluginSource: .build
)

let outputFiles = OutputFileName.allCases.map { inputs.outputDirectory.appending(component: $0.rawValue) }
var inputFiles = inputs.schemaFiles
if let config = inputs.config {
inputFiles.append(config)
}
return [
.buildCommand(
displayName: "Generating GraphQL Swift code",
executable: inputs.tool.url,
arguments: inputs.arguments,
environment: [:],
inputFiles: inputFiles,
outputFiles: outputFiles
)
]
}
}

extension GraphQLGeneratorPlugin: BuildToolPlugin {
/// Entry point for creating build commands for targets in Swift packages.
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
guard let swiftTarget = target as? SwiftSourceModuleTarget else {
throw PluginError.incompatibleTarget(name: target.name)
}
return try createBuildCommands(
pluginWorkDirectory: context.pluginWorkDirectoryURL,
tool: context.tool,
sourceFiles: swiftTarget.sourceFiles,
sourceDirectory: swiftTarget.directoryURL,
targetName: target.name
)
}
}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin

extension GraphQLGeneratorPlugin: XcodeBuildToolPlugin {
/// Entry point for creating build commands for targets in Xcode projects.
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws
-> [Command]
{
return try createBuildCommands(
pluginWorkDirectory: context.pluginWorkDirectoryURL,
tool: context.tool,
sourceFiles: target.inputFiles,
sourceDirectory: context.xcodeProject.directoryURL,
targetName: target.displayName
)
}
}

#endif
1 change: 1 addition & 0 deletions Plugins/PluginsShared/OutputFiles.swift
Loading
Loading