Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XcodePlugin support #1

Merged
merged 1 commit into from
Apr 12, 2023
Merged
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
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