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 #4

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
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) }
Comment on lines +42 to +44
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current implementation, it cares only about a top-level configuration.
We have to care about a target-specified configuration.

Unfortunately, we can't fetch the top-level directory of each target from XcodeBuildToolPlugin, because a top-level directory of targets doesn't always exist in xcodeproj.

But most likely it will be $SRCROOT/TARGET_NAME.

So it's better to add target.displayName as a search path.

Suggested change
let configurations: [Path] = [context.xcodeProject.directory]
.map { $0.appending("swiftgen.yml") }
.filter { fileManager.fileExists(atPath: $0.string) }
let configurations: [Path] = [
context.xcodeProject.directory,
context.xcodeProject.directory.appending(subpath: target.displayName),
]
.map { $0.appending("swiftgen.yml") }
.filter { fileManager.fileExists(atPath: $0.string) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works well for my multi-target situation, thanks!


// 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