Skip to content

Command plugin should skip irrelevant targets when run without --target argument #798

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

Merged
Show file tree
Hide file tree
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
28 changes: 22 additions & 6 deletions Plugins/OpenAPIGeneratorCommand/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,39 @@ extension SwiftOpenAPIGeneratorPlugin: CommandPlugin {
var hadASuccessfulRun = false

for target in targets {
print("Considering target '\(target.name)':")
log("Considering target '\(target.name)':")
guard let swiftTarget = target as? SwiftSourceModuleTarget else {
print("- Not a swift source module. Can't generate OpenAPI code.")
log("- Not a swift source module. Can't generate OpenAPI code.")
continue
}
do {
print("- Trying OpenAPI code generation.")
log("- Trying OpenAPI code generation.")
try runCommand(
targetWorkingDirectory: target.directory,
tool: context.tool,
sourceFiles: swiftTarget.sourceFiles,
targetName: target.name
)
print("- ✅ OpenAPI code generation for target '\(target.name)' successfully completed.")
log("- ✅ OpenAPI code generation for target '\(target.name)' successfully completed.")
hadASuccessfulRun = true
} catch let error as PluginError {
if targetNameArguments.isEmpty, case .fileErrors(let errors) = error,
Set(errors.map(\.fileKind)) == Set(FileError.Kind.allCases),
errors.map(\.issue).allSatisfy({ $0 == FileError.Issue.noFilesFound })
{
// The command plugin was run with no --target argument so its looping over all targets.
// If a target does not have any of the required files, this should only be considered an error
// if the plugin is being explicitly run on a target, either using the build plugin, or using the
// command plugin with a --target argument.
log("- Skipping because target isn't configured for OpenAPI code generation.")
continue
}

if error.isMisconfigurationError {
print("- Stopping because target isn't configured for OpenAPI code generation.")
log("- Stopping because target is misconfigured for OpenAPI code generation.")
throw error
} else {
print("- OpenAPI code generation failed with error.")
log("- OpenAPI code generation failed with error.")
throw error
}
}
Expand All @@ -94,3 +106,7 @@ extension SwiftOpenAPIGeneratorPlugin: CommandPlugin {
guard hadASuccessfulRun else { throw PluginError.noTargetsWithExpectedFiles(targetNames: targets.map(\.name)) }
}
}

private func log(_ message: @autoclosure () -> String) {
FileHandle.standardError.write(Data(message().appending("\n").utf8))
}
8 changes: 4 additions & 4 deletions Plugins/PluginsShared/PluginError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import PackagePlugin
import Foundation

enum PluginError: Swift.Error, CustomStringConvertible, LocalizedError {
enum PluginError: Swift.Error, Equatable, CustomStringConvertible, LocalizedError {
case incompatibleTarget(name: String)
case generatorFailure(targetName: String)
case noTargetsWithExpectedFiles(targetNames: [String])
Expand Down Expand Up @@ -55,10 +55,10 @@ enum PluginError: Swift.Error, CustomStringConvertible, LocalizedError {
}
}

struct FileError: Swift.Error, CustomStringConvertible, LocalizedError {
struct FileError: Swift.Error, Equatable, CustomStringConvertible, LocalizedError {

/// The kind of the file.
enum Kind: CaseIterable {
enum Kind: Equatable, CaseIterable {
/// Config file.
case config
/// OpenAPI document file.
Expand All @@ -73,7 +73,7 @@ struct FileError: Swift.Error, CustomStringConvertible, LocalizedError {
}

/// Encountered issue.
enum Issue {
enum Issue: Equatable {
/// File wasn't found.
case noFilesFound
/// More than 1 file found.
Expand Down
4 changes: 4 additions & 0 deletions scripts/run-integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ swift package --package-path "${INTEGRATION_TEST_PACKAGE_PATH}" \
log "Building integration test package: ${INTEGRATION_TEST_PACKAGE_PATH}"
swift build --package-path "${INTEGRATION_TEST_PACKAGE_PATH}"

log "Running command plugin on integration test package: ${INTEGRATION_TEST_PACKAGE_PATH}"
swift package --package-path "${INTEGRATION_TEST_PACKAGE_PATH}" \
--allow-writing-to-package-directory generate-code-from-openapi

log "✅ Successfully built integration test package."