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

Fix contextual menu command and command-line #9

Open
wants to merge 2 commits 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
43 changes: 26 additions & 17 deletions Plugins/SwiftGen-Generate/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,21 @@ struct SwiftGenPlugin: CommandPlugin {
func performCommand(context: PluginContext, arguments: [String]) async throws {
let swiftgen = try context.tool(named: "swiftgen")
let fileManager = FileManager.default

let configuration = context.package.directory.appending("swiftgen.yml")
if fileManager.fileExists(atPath: configuration.string) {
try swiftgen.run(configuration, environment: env(context: context))
}

// if user provided arguments, use those
if !arguments.isEmpty {
try swiftgen.run(arguments: arguments, environment: env(context: context))
} else {
// otherwise scan for configs
let configuration = context.package.directory.appending("swiftgen.yml")
// check each target
let targetsFromArgs = parseTargets(arguments)
let targets = context.package.targets
.compactMap { $0 as? SourceModuleTarget }
.filter { targetsFromArgs.isEmpty || targetsFromArgs.contains($0.name) }
for target in targets {
let configuration = target.directory.appending("swiftgen.yml")
if fileManager.fileExists(atPath: configuration.string) {
try swiftgen.run(configuration, environment: env(context: context))
}

// check each target
let targets = context.package.targets.compactMap { $0 as? SourceModuleTarget }
for target in targets {
let configuration = target.directory.appending("swiftgen.yml")
if fileManager.fileExists(atPath: configuration.string) {
try swiftgen.run(configuration, environment: env(context: context, target: target))
}
try swiftgen.run(configuration, environment: env(context: context, target: target))
}
}
}
Expand All @@ -41,9 +38,21 @@ private extension SwiftGenPlugin {
[
"PROJECT_DIR": context.package.directory.string,
"TARGET_NAME": target?.name ?? "",
"PRODUCT_MODULE_NAME": target?.moduleName ?? ""
"PRODUCT_MODULE_NAME": target?.moduleName ?? "",
"DERIVED_SOURCES_DIR": context.pluginWorkDirectory.string
]
}

func parseTargets(_ arguments: [String]) -> [String] {
var result = [String]()
for (i, arg) in arguments.enumerated() where arg == "--target" {
let valueIdx = i + 1
if valueIdx < arguments.count {
result.append(arguments[valueIdx])
}
}
return result
}
}

private extension PluginContext.Tool {
Expand Down