Skip to content

Commit

Permalink
Add XcodePlugin support (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Boogh <[email protected]>
  • Loading branch information
2 people authored and JoaoPinhoMinder committed Jan 28, 2025
1 parent 879b85a commit c18b566
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
]
)
63 changes: 63 additions & 0 deletions Plugins/SwiftGenPlugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit c18b566

Please sign in to comment.