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 support for .yml extension (openapi.yml and openapi-generator-config.yml) #77

Merged
merged 3 commits into from
Jun 19, 2023
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
41 changes: 26 additions & 15 deletions Plugins/OpenAPIGenerator/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct SwiftOpenAPIGeneratorPlugin {
case incompatibleTarget(targetName: String)
case noConfigFound(targetName: String)
case noDocumentFound(targetName: String)
case multiConfigFound(targetName: String, files: [Path])
case multiDocumentFound(targetName: String, files: [Path])

var description: String {
switch self {
Expand All @@ -28,10 +30,16 @@ struct SwiftOpenAPIGeneratorPlugin {
"Incompatible target called '\(targetName)'. Only Swift source targets can be used with the Swift OpenAPI generator plugin."
case .noConfigFound(let targetName):
return
"No config found in the target named '\(targetName)'. Add a file called 'openapi-generator-config.yaml' to the target's source directory. See documentation for details."
"No config file found in the target named '\(targetName)'. Add a file called 'openapi-generator-config.yaml' or 'openapi-generator-config.yml' to the target's source directory. See documentation for details."
case .noDocumentFound(let targetName):
return
"No OpenAPI document found in the target named '\(targetName)'. Add a file called 'openapi.yaml' or 'openapi.json' (can also be a symlink) to the target's source directory. See documentation for details."
"No OpenAPI document found in the target named '\(targetName)'. Add a file called 'openapi.yaml', 'openapi.yml' or 'openapi.json' (can also be a symlink) to the target's source directory. See documentation for details."
Kyle-Ye marked this conversation as resolved.
Show resolved Hide resolved
case .multiConfigFound(let targetName, let files):
return
"Multiple config files found in the target named '\(targetName)', but exactly one is required. Found \(files.map(\.description).joined(separator: " "))."
case .multiDocumentFound(let targetName, let files):
return
"Multiple OpenAPI documents found in the target named '\(targetName)', but exactly one is required. Found \(files.map(\.description).joined(separator: " "))."
}
}

Expand All @@ -40,30 +48,33 @@ struct SwiftOpenAPIGeneratorPlugin {
}
}

private var supportedConfigFiles: Set<String> { Set(["yaml", "yml"].map { "openapi-generator-config." + $0 }) }
private var supportedDocFiles: Set<String> { Set(["yaml", "yml", "json"].map { "openapi." + $0 }) }
Kyle-Ye marked this conversation as resolved.
Show resolved Hide resolved

func createBuildCommands(
pluginWorkDirectory: PackagePlugin.Path,
tool: (String) throws -> PackagePlugin.PluginContext.Tool,
sourceFiles: FileList,
targetName: String
) throws -> [Command] {
let inputFiles = sourceFiles
guard let config = inputFiles.first(where: { $0.path.lastComponent == "openapi-generator-config.yaml" })?.path
else {
let matchedConfigs = inputFiles.filter { supportedConfigFiles.contains($0.path.lastComponent) }.map(\.path)
guard matchedConfigs.count > 0 else {
throw Error.noConfigFound(targetName: targetName)
}
guard
let doc = inputFiles.first(where: {
switch $0.path.lastComponent {
case "openapi.yaml", "openapi.json":
return true
default:
return false
}
})?
.path
else {
guard matchedConfigs.count == 1 else {
throw Error.multiConfigFound(targetName: targetName, files: matchedConfigs)
}
let config = matchedConfigs[0]

let matchedDocs = inputFiles.filter { supportedDocFiles.contains($0.path.lastComponent) }.map(\.path)
guard matchedDocs.count > 0 else {
throw Error.noDocumentFound(targetName: targetName)
}
guard matchedDocs.count == 1 else {
throw Error.multiDocumentFound(targetName: targetName, files: matchedDocs)
}
let doc = matchedDocs[0]
let genSourcesDir = pluginWorkDirectory.appending("GeneratedSources")
let outputFiles: [Path] = GeneratorMode.allCases.map { genSourcesDir.appending($0.outputFileName) }
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ The command-line tool also uses the same configuration file.

### Create a configuration file

The configuration file is named `openapi-generator-config.yaml` and must exist in the target source directory.
The configuration file is named `openapi-generator-config.yaml` or `openapi-generator-config.yml` and must exist in the target source directory.

> In the following tutorial, we will use `openapi-generator-config.yaml` as an example.

```
.
Expand Down
2 changes: 2 additions & 0 deletions scripts/check-license-headers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ read -ra PATHS_TO_CHECK_FOR_LICENSE <<< "$( \
":(exclude)**/Package.resolved" \
":(exclude)**/README.md" \
":(exclude)**/openapi.yaml" \
":(exclude)**/openapi.yml" \
":(exclude)**/petstore.yaml" \
":(exclude)**/openapi-generator-config.yaml" \
":(exclude)**/openapi-generator-config.yml" \
| xargs -0 \
)"

Expand Down
2 changes: 1 addition & 1 deletion scripts/run-swift-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if [ "${SWIFT_FORMAT_RC}" -ne 0 ]; then

To fix, run the following command:

% swift-format format --parallel --recursive --in-place Examples IntegrationTests Plugins Sources Tests
% swift-format format --parallel --recursive --in-place Examples IntegrationTest Plugins Sources Tests
Kyle-Ye marked this conversation as resolved.
Show resolved Hide resolved
"
exit "${SWIFT_FORMAT_RC}"
fi
Expand Down