From c18b566db43f14b79c3f528e7d8fc5813c78c0e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pacheco=20Neves?= Date: Wed, 12 Apr 2023 10:17:46 +0100 Subject: [PATCH] Add XcodePlugin support (#1) Co-authored-by: Tobias Boogh --- CHANGELOG.md | 6 +++ Package.swift | 4 +- Plugins/SwiftGenPlugin/Plugin.swift | 63 +++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d19673..b2a66e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ --- +## 6.6.4 + +### Changes in core dependencies of SwiftGenPlugin + +* [SwiftGen 6.6.4-Mindera](https://github.com/Mindera/SwiftGen/blob/6.6.4-Mindera/CHANGELOG.md) + ## 6.6.2 ### Changes in core dependencies of SwiftGenPlugin diff --git a/Package.swift b/Package.swift index d39e5b1..ccc8ee2 100644 --- a/Package.swift +++ b/Package.swift @@ -29,8 +29,8 @@ let package = Package( ), .binaryTarget( name: "swiftgen", - url: "https://github.com/SwiftGen/SwiftGen/releases/download/6.6.2/swiftgen-6.6.2.artifactbundle.zip", - checksum: "7586363e24edcf18c2da3ef90f379e9559c1453f48ef5e8fbc0b818fbbc3a045" + url: "https://github.com/Mindera/SwiftGen/releases/download/6.6.4-Mindera/swiftgen-6.6.4.artifactbundle.zip", + checksum: "ee3ab9bf1bd1a816a173a51ce9afebef7463f64a02c9fbef9da9d033bcf2c3da" ) ] ) diff --git a/Plugins/SwiftGenPlugin/Plugin.swift b/Plugins/SwiftGenPlugin/Plugin.swift index 1aa9b45..db252a6 100644 --- a/Plugins/SwiftGenPlugin/Plugin.swift +++ b/Plugins/SwiftGenPlugin/Plugin.swift @@ -31,6 +31,33 @@ struct SwiftGenPlugin: BuildToolPlugin { } } +#if canImport(XcodeProjectPlugin) +import XcodeProjectPlugin + +extension SwiftGenPlugin: XcodeBuildToolPlugin { + func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] { + let fileManager = FileManager.default + + // Possible paths where there may be a config file (root of package, target dir.) + let configurations: [Path] = [context.xcodeProject.directory] + .map { $0.appending("swiftgen.yml") } + .filter { fileManager.fileExists(atPath: $0.string) } + + // Validate paths list + guard validate(configurations: configurations, target: target) else { + return [] + } + + // Clear the SwiftGen plugin's directory (in case of dangling files) + fileManager.forceClean(directory: context.pluginWorkDirectory) + + return try configurations.map { configuration in + try .swiftgen(using: configuration, context: context, target: target) + } + } +} +#endif + // MARK: - Helpers private extension SwiftGenPlugin { @@ -47,6 +74,21 @@ private extension SwiftGenPlugin { return true } + +#if canImport(XcodeProjectPlugin) + func validate(configurations: [Path], target: XcodeTarget) -> Bool { + guard !configurations.isEmpty else { + Diagnostics.error(""" + No SwiftGen configurations found for target \(target.displayName). If you would like to generate sources for this \ + target include a `swiftgen.yml` in the target's source directory, or include a shared `swiftgen.yml` at the \ + package's root. + """) + return false + } + + return true + } +#endif } private extension Command { @@ -69,6 +111,27 @@ private extension Command { outputFilesDirectory: context.pluginWorkDirectory ) } + +#if canImport(XcodeProjectPlugin) + static func swiftgen(using configuration: Path, context: XcodePluginContext, target: XcodeTarget) throws -> Command { + .prebuildCommand( + displayName: "SwiftGen BuildTool Plugin", + executable: try context.tool(named: "swiftgen").path, + arguments: [ + "config", + "run", + "--verbose", + "--config", "\(configuration)" + ], + environment: [ + "PROJECT_DIR": context.xcodeProject.directory, + "TARGET_NAME": target.displayName, + "DERIVED_SOURCES_DIR": context.pluginWorkDirectory + ], + outputFilesDirectory: context.pluginWorkDirectory + ) + } +#endif } private extension FileManager {