From 6ef683f43f732dbc59634f3468ee6521ab23db93 Mon Sep 17 00:00:00 2001 From: Gabor Lengyel Date: Mon, 15 Jul 2024 15:41:54 +0200 Subject: [PATCH] add sh versioning, add openapi server parameters (#4) --- .gitignore | 2 ++ .../RunOpenApiServerPlugin.swift | 2 +- Plugins/Shared/Extension + PluginContext.swift | 10 ++++++++-- Plugins/Shared/ScriptEnum.swift | 2 ++ .../scripts/CheckOpenApiValidationScript.swift | 2 +- .../Shared/scripts/RunOpenApiServerScript.swift | 16 +++++++++++++--- README.md | 17 +++++++++++------ 7 files changed, 38 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 979a055..4d1440d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ DerivedData/ .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc .swiftpm/xcode/xcshareddata/xcschemes/swift-plugins-Package.xcscheme +openapi/index.html +openapi/openapi.yaml diff --git a/Plugins/RunOpenApiServerPlugin/RunOpenApiServerPlugin.swift b/Plugins/RunOpenApiServerPlugin/RunOpenApiServerPlugin.swift index a28b3d4..0db6e7d 100644 --- a/Plugins/RunOpenApiServerPlugin/RunOpenApiServerPlugin.swift +++ b/Plugins/RunOpenApiServerPlugin/RunOpenApiServerPlugin.swift @@ -13,7 +13,7 @@ struct RunOpenApiServerPlugin: CommandPlugin { context: PackagePlugin.PluginContext, arguments: [String] ) async throws { - try context.runScript(RunOpenApiServerScript()) + try context.runScript(RunOpenApiServerScript(), arguments) } } diff --git a/Plugins/Shared/Extension + PluginContext.swift b/Plugins/Shared/Extension + PluginContext.swift index 8f86c68..07b55d1 100644 --- a/Plugins/Shared/Extension + PluginContext.swift +++ b/Plugins/Shared/Extension + PluginContext.swift @@ -13,8 +13,13 @@ import PackagePlugin //https://forums.swift.org/t/difficulty-sharing-code-between-swift-package-manager-plugins/61690/6 extension PackagePlugin.PluginContext { - func createFile(fileName: String, fileContent: String) throws -> String { - let filePath = self.pluginWorkDirectory.appending(fileName).string + func createFile( + fileVersion: String = "", + fileName: String, + fileContent: String + ) throws -> String { + let filePath = self.pluginWorkDirectory + .appending(fileVersion + fileName).string let fm = FileManager.default // check if file exist @@ -40,6 +45,7 @@ extension PackagePlugin.PluginContext { _ arguments: [String]? = nil ) throws { let scriptFilePath = try createFile( + fileVersion: ScriptEnum.version.rawValue + "_", fileName: script.shFile(), fileContent: script.scriptToRun() ) diff --git a/Plugins/Shared/ScriptEnum.swift b/Plugins/Shared/ScriptEnum.swift index 2fdda33..5026292 100644 --- a/Plugins/Shared/ScriptEnum.swift +++ b/Plugins/Shared/ScriptEnum.swift @@ -6,6 +6,8 @@ enum ScriptEnum: String { + case version = "4" + case head = """ #!/usr/bin/env bash set -euo pipefail diff --git a/Plugins/Shared/scripts/CheckOpenApiValidationScript.swift b/Plugins/Shared/scripts/CheckOpenApiValidationScript.swift index fbe4c64..95208f4 100644 --- a/Plugins/Shared/scripts/CheckOpenApiValidationScript.swift +++ b/Plugins/Shared/scripts/CheckOpenApiValidationScript.swift @@ -7,7 +7,7 @@ struct CheckOpenApiValidationScript: ScriptProtocol { func shFile() -> String { - return "check-openapi-validation" + return "check-openapi-validation.sh" } func scriptToRun() -> String { diff --git a/Plugins/Shared/scripts/RunOpenApiServerScript.swift b/Plugins/Shared/scripts/RunOpenApiServerScript.swift index ee9ddbd..412024e 100644 --- a/Plugins/Shared/scripts/RunOpenApiServerScript.swift +++ b/Plugins/Shared/scripts/RunOpenApiServerScript.swift @@ -7,7 +7,7 @@ struct RunOpenApiServerScript: ScriptProtocol { func shFile() -> String { - return "run-openapi-server" + return "run-openapi-server.sh" } func scriptToRun() -> String { @@ -23,10 +23,20 @@ struct RunOpenApiServerScript: ScriptProtocol { exit 0 fi + NAME="openapi-server" + PORT="8888:80" + while getopts ":n:p:": flag + do + case "${flag}" in + n) NAME=${OPTARG};; + p) PORT=${OPTARG};; + esac + done + # Run the Docker container to serve the OpenAPI files using Nginx - docker run --rm --name "run-openapi-server" \ + docker run --rm --name "${NAME}" \ -v "${OPENAPI_YAML_LOCATION}:/usr/share/nginx/html" \ - -p 8888:80 nginx + -p ${PORT} nginx """ } diff --git a/README.md b/README.md index 66748d0..8a83779 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This repository contains a set of Swift plugins designed to streamline various t Add the repository as a dependency: ```swift -.package(url: "https://github.com/BinaryBirds/swift-plugins", from: "0.0.2"), +.package(url: "https://github.com/BinaryBirds/swift-plugins", from: "0.0.4"), ``` Update the packages and you are ready. @@ -53,12 +53,12 @@ This plugin generates a list of contributors for the repository. It uses the git Usage: `swift package --disable-sandbox generate-contributors-list` ### InstallSwiftFormatPlugin -This plugin installs the swift-format tool, the version can be optionally defined using the `-v` parameter. +This plugin installs the swift-format tool, the version can be optionally defined using the `-v` parameter. The default version is `510.1.0`. Usage: `swift package --disable-sandbox install-swift-format` ### InstallSwiftOpenApiGeneratorPlugin -This plugin installs the Swift OpenAPI generator tool, the version can be optionally defined using the `-v` parameter. +This plugin installs the Swift OpenAPI generator tool, the version can be optionally defined using the `-v` parameter. The default version is `1.2.1`. Usage: `swift package --disable-sandbox install-swift-openapi-generator` @@ -73,14 +73,19 @@ This plugin cleans up build artifacts and other temporary files from the reposit Usage: `swift package --disable-sandbox run-clean` ### RunOpenApiServerPlugin -This plugin serves the OpenAPI documentation using an Nginx server. +This plugin serves the OpenAPI documentation using an Nginx server. This try to run inside a Docker container. + +Optional parameters: + +- `-n` : add a custom identifier for the container, the default is `openapi-server` +- `-p` : add a custom port to bind it to the container, the default is `8888:80` Usage: `swift package --disable-sandbox run-openapi-server` ### RunSwiftFormatPlugin -This plugin formats Swift code using the swift-format tool. It runs the tool on all Swift files in the repository, optionally fixing some of the issues if the `--fix` argument is provided. +This plugin checks/formats Swift code using the swift-format tool. It runs the tool on all Swift files in the repository, optionally fixing some of the issues if the `--fix` argument is provided. -Usage: `swift package --disable-sandbox run-swift-format` or `swift package--disable-sandbox run-swift-format --fix` for fixing +Usage: `swift package --disable-sandbox run-swift-format` for run lint or `swift package--disable-sandbox run-swift-format --fix` for fixing ## Makefile