From 4ea76a8499a33af629130b4ced3b725ebc3f8e5b Mon Sep 17 00:00:00 2001 From: Ivan Smolin Date: Fri, 19 May 2023 16:41:49 +0300 Subject: [PATCH] feat: add SwiftLint --- CHANGELOG.md | 1 + Package.swift | 22 ++++++++++- Plugins/TISwiftLintPlugin/plugin.swift | 51 ++++++++++++++++++++++++++ build-scripts | 2 +- swiftlint_base.yml | 1 + 5 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Plugins/TISwiftLintPlugin/plugin.swift create mode 120000 swiftlint_base.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 39272316..ce7864ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 1.44.0 - **Added**: HTTP status codes to `EndpointErrorResult.apiError` responses +- **Added**: SwiftLint pre-build SPM step to TINetworking module ### 1.43.1 diff --git a/Package.swift b/Package.swift index ac8de7df..663448ce 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,7 @@ // swift-tools-version:5.7 + +#if canImport(PackageDescription) + import PackageDescription let package = Package( @@ -71,7 +74,12 @@ let package = Package( .target(name: "TIDeveloperUtils", dependencies: ["TISwiftUtils", "TIUIKitCore", "TIUIElements"], path: "TIDeveloperUtils/Sources"), // MARK: - Networking - .target(name: "TINetworking", dependencies: ["TIFoundationUtils", "Alamofire"], path: "TINetworking/Sources"), + + .target(name: "TINetworking", + dependencies: ["TIFoundationUtils", "Alamofire"], + path: "TINetworking/Sources", + plugins: [.plugin(name: "TISwiftLintPlugin")]), + .target(name: "TIMoyaNetworking", dependencies: ["TINetworking", "TIFoundationUtils", "Moya"], path: "TIMoyaNetworking"), .target(name: "TINetworkingCache", dependencies: ["TIFoundationUtils", "TINetworking", "Cache"], path: "TINetworkingCache/Sources"), @@ -89,7 +97,15 @@ let package = Package( dependencies: [.product(name: "Antlr4", package: "antlr4")], path: "TITextProcessing/Sources", exclude: ["TITextProcessing.app"]), - + + .binaryTarget(name: "SwiftLintBinary", + url: "https://github.com/realm/SwiftLint/releases/download/0.52.2/SwiftLintBinary-macos.artifactbundle.zip", + checksum: "89651e1c87fb62faf076ef785a5b1af7f43570b2b74c6773526e0d5114e0578e"), + + .plugin(name: "TISwiftLintPlugin", + capability: .buildTool(), + dependencies: ["SwiftLintBinary"]), + // MARK: - Tests .testTarget( @@ -102,3 +118,5 @@ let package = Package( path: "Tests/TITextProcessingTests") ] ) + +#endif diff --git a/Plugins/TISwiftLintPlugin/plugin.swift b/Plugins/TISwiftLintPlugin/plugin.swift new file mode 100644 index 00000000..65d8ad89 --- /dev/null +++ b/Plugins/TISwiftLintPlugin/plugin.swift @@ -0,0 +1,51 @@ +// +// Copyright (c) 2023 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the Software), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import PackagePlugin +import Foundation + +@main +struct SwiftLintPlugin: BuildToolPlugin { + func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { + let swiftlintScriptPath = context.package.directory.appending(["build-scripts", "xcode", "build_phases", "swiftlint.sh"]) + + let swiftlintExecutablePath = try context.tool(named: "swiftlint").path + + return [ + .prebuildCommand(displayName: "SwiftLint linting...", + executable: swiftlintScriptPath, + arguments: [ + swiftlintExecutablePath, + context.package.directory.appending(subpath: "swiftlint_base.yml") + ], + environment: [ + "SCRIPT_DIR": swiftlintScriptPath.removingLastComponent().string, + "SRCROOT": context.package.directory.string, + "SCRIPT_INPUT_FILE_COUNT": "1", + "SCRIPT_INPUT_FILE_0": target.directory.removingLastComponent().lastComponent, +// "FORCE_LINT": "1", // Lint all files in target (not only modified) + "AUTOCORRECT": "1" // + ], + outputFilesDirectory: context.package.directory) + ] + } +} diff --git a/build-scripts b/build-scripts index 1f83bf5d..b843196f 160000 --- a/build-scripts +++ b/build-scripts @@ -1 +1 @@ -Subproject commit 1f83bf5d08bbc2c2346141621a42b2d2e0dd6517 +Subproject commit b843196f3c9d9eda4ec8eeba29bf5cc859f55f18 diff --git a/swiftlint_base.yml b/swiftlint_base.yml new file mode 120000 index 00000000..569e1f72 --- /dev/null +++ b/swiftlint_base.yml @@ -0,0 +1 @@ +build-scripts/xcode/.swiftlint.yml \ No newline at end of file