From 25bc36cadda60c5c890c357304b0d43ef719f50d Mon Sep 17 00:00:00 2001 From: Tony Li Date: Thu, 30 May 2024 13:37:45 +1200 Subject: [PATCH 1/6] Enable SwiftLint plugin for local development --- .swiftlint.yml | 2 +- Makefile | 2 +- Package.resolved | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ Package.swift | 75 +++++++++++++++++++++++++++++++---------- 4 files changed, 146 insertions(+), 19 deletions(-) create mode 100644 Package.resolved diff --git a/.swiftlint.yml b/.swiftlint.yml index a03c9e1e0..8f4983eda 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -1,4 +1,4 @@ -swiftlint_version: 0.53.0 +swiftlint_version: 0.55.1 strict: true included: - native/swift diff --git a/Makefile b/Makefile index d8e06c87d..4b7afc199 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ docker_container_repo_dir=/app # Common docker options rust_docker_container := public.ecr.aws/docker/library/rust:1.76 -swiftlint_container := ghcr.io/realm/swiftlint:0.53.0 +swiftlint_container := ghcr.io/realm/swiftlint:0.55.1 docker_opts_shared := --rm -v "$(PWD)":$(docker_container_repo_dir) -w $(docker_container_repo_dir) rust_docker_run := docker run -v $(PWD):/$(docker_container_repo_dir) -w $(docker_container_repo_dir) -it -e CARGO_HOME=/app/.cargo $(rust_docker_container) diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 000000000..62c52f596 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,86 @@ +{ + "pins" : [ + { + "identity" : "collectionconcurrencykit", + "kind" : "remoteSourceControl", + "location" : "https://github.com/JohnSundell/CollectionConcurrencyKit.git", + "state" : { + "revision" : "b4f23e24b5a1bff301efc5e70871083ca029ff95", + "version" : "0.2.0" + } + }, + { + "identity" : "cryptoswift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/krzyzanowskim/CryptoSwift.git", + "state" : { + "revision" : "c9c3df6ab812de32bae61fc0cd1bf6d45170ebf0", + "version" : "1.8.2" + } + }, + { + "identity" : "sourcekitten", + "kind" : "remoteSourceControl", + "location" : "https://github.com/jpsim/SourceKitten.git", + "state" : { + "revision" : "fd4df99170f5e9d7cf9aa8312aa8506e0e7a44e7", + "version" : "0.35.0" + } + }, + { + "identity" : "swift-argument-parser", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-argument-parser.git", + "state" : { + "revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b", + "version" : "1.4.0" + } + }, + { + "identity" : "swift-syntax", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-syntax.git", + "state" : { + "revision" : "303e5c5c36d6a558407d364878df131c3546fad8", + "version" : "510.0.2" + } + }, + { + "identity" : "swiftlint", + "kind" : "remoteSourceControl", + "location" : "https://github.com/realm/SwiftLint", + "state" : { + "revision" : "b515723b16eba33f15c4677ee65f3fef2ce8c255", + "version" : "0.55.1" + } + }, + { + "identity" : "swiftytexttable", + "kind" : "remoteSourceControl", + "location" : "https://github.com/scottrhoyt/SwiftyTextTable.git", + "state" : { + "revision" : "c6df6cf533d120716bff38f8ff9885e1ce2a4ac3", + "version" : "0.9.0" + } + }, + { + "identity" : "swxmlhash", + "kind" : "remoteSourceControl", + "location" : "https://github.com/drmohundro/SWXMLHash.git", + "state" : { + "revision" : "a853604c9e9a83ad9954c7e3d2a565273982471f", + "version" : "7.0.2" + } + }, + { + "identity" : "yams", + "kind" : "remoteSourceControl", + "location" : "https://github.com/jpsim/Yams.git", + "state" : { + "revision" : "9234124cff5e22e178988c18d8b95a8ae8007f76", + "version" : "5.1.2" + } + } + ], + "version" : 2 +} diff --git a/Package.swift b/Package.swift index 85247ec48..f8f829022 100644 --- a/Package.swift +++ b/Package.swift @@ -2,13 +2,9 @@ // The swift-tools-version declares the minimum version of Swift required to build this package. // Swift Package: WordpressApi +import Foundation import PackageDescription -enum WordPressRSVersion { - case local - case release(version: String, checksum: String) -} - let libwordpressFFIVersion: WordPressRSVersion = .local #if os(Linux) @@ -17,20 +13,10 @@ let libwordpressFFI: Target = .systemLibrary( path: "target/swift-bindings/libwordpressFFI-linux/" ) #elseif os(macOS) -let libwordpressFFI: Target -switch libwordpressFFIVersion { - case .local: - libwordpressFFI = .binaryTarget(name: "libwordpressFFI", path: "target/libwordpressFFI.xcframework") - case let .release(version, checksum): - libwordpressFFI = .binaryTarget( - name: "libwordpressFFI", - url: "https://github.com/Automattic/wordpress-rs/releases/download/\(version)/libwordpressFFI.xcframework.zip", - checksum: checksum - ) -} +let libwordpressFFI: Target = libwordpressFFIVersion.target #endif -let package = Package( +var package = Package( name: "WordPress", platforms: [ .iOS(.v13), @@ -74,3 +60,58 @@ let package = Package( ) ] ) + +let isCI = ProcessInfo.processInfo.environment["CI"] == "true" + +// MARK: - Enable local development toolings + +let localDevelopment = !isCI && libwordpressFFIVersion.isLocal + +if localDevelopment { + try enableSwiftLint() +} + +// MARK: - Helpers + +enum WordPressRSVersion { + case local + case release(version: String, checksum: String) + + var isLocal: Bool { + if case .local = self { + return true + } + return false + } + + var target: Target { + switch libwordpressFFIVersion { + case .local: + return .binaryTarget(name: "libwordpressFFI", path: "target/libwordpressFFI.xcframework") + case let .release(version, checksum): + return .binaryTarget( + name: "libwordpressFFI", + url: "https://github.com/Automattic/wordpress-rs/releases/download/\(version)/libwordpressFFI.xcframework.zip", + checksum: checksum + ) + } + } +} + +func enableSwiftLint() throws { + let version = try String(contentsOf: URL(string:"./.swiftlint.yml", relativeTo: URL(filePath: #filePath))!) + .split(separator: "\n") + .first(where: { $0.starts(with: "swiftlint_version") })? + .split(separator: ":") + .last? + .trimmingCharacters(in: .whitespaces) + guard let version else { + fatalError("Can't find swiftlint_version in .swiftlint.yml") + } + + package.dependencies.append(.package(url: "https://github.com/realm/SwiftLint", exact: .init(version)!)) + + if let target = package.targets.first(where: { $0.name == "WordPressAPI" }) { + target.plugins = (target.plugins ?? []) + [.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLint")] + } +} From e545609b73e1a0e9f9c204738cae1810ed8bafca Mon Sep 17 00:00:00 2001 From: Tony Li Date: Fri, 31 May 2024 09:31:06 +1200 Subject: [PATCH 2/6] Only add SwiftLint on macOS --- Package.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Package.swift b/Package.swift index f8f829022..be321629c 100644 --- a/Package.swift +++ b/Package.swift @@ -98,7 +98,9 @@ enum WordPressRSVersion { } } +// Add SwiftLint to the package so that we can see linting issues directly from Xcode. func enableSwiftLint() throws { +#if os(macOS) let version = try String(contentsOf: URL(string:"./.swiftlint.yml", relativeTo: URL(filePath: #filePath))!) .split(separator: "\n") .first(where: { $0.starts(with: "swiftlint_version") })? @@ -114,4 +116,5 @@ func enableSwiftLint() throws { if let target = package.targets.first(where: { $0.name == "WordPressAPI" }) { target.plugins = (target.plugins ?? []) + [.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLint")] } +#endif } From 168801c6fcc2d3b1be440fc672cfc77f71a8bf1c Mon Sep 17 00:00:00 2001 From: Tony Li Date: Fri, 7 Jun 2024 11:38:36 +1200 Subject: [PATCH 3/6] Use SwiftLint plugin on CI --- .buildkite/pipeline.yml | 4 ++++ Makefile | 5 ++--- Package.swift | 4 +--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index 6af53a1c2..acbe847db 100644 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -87,6 +87,10 @@ steps: command: | echo "--- :swift: Swiftlint" make lint-swift + env: + IMAGE_ID: xcode-15.3 + agents: + queue: mac # # Kotlin Group - group: ":kotlin: Kotlin Wrapper" diff --git a/Makefile b/Makefile index 4b7afc199..ca6f5616e 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,6 @@ docker_container_repo_dir=/app # Common docker options rust_docker_container := public.ecr.aws/docker/library/rust:1.76 -swiftlint_container := ghcr.io/realm/swiftlint:0.55.1 docker_opts_shared := --rm -v "$(PWD)":$(docker_container_repo_dir) -w $(docker_container_repo_dir) rust_docker_run := docker run -v $(PWD):/$(docker_container_repo_dir) -w $(docker_container_repo_dir) -it -e CARGO_HOME=/app/.cargo $(rust_docker_container) @@ -181,10 +180,10 @@ lint-rust: $(rust_docker_run) /bin/bash -c "rustup component add clippy && cargo clippy --all -- -D warnings && cargo clippy --tests --all -- -D warnings" lint-swift: - docker run -v $(PWD):$(docker_container_repo_dir) -w $(docker_container_repo_dir) -it $(swiftlint_container) swiftlint + swift package plugin swiftlint lintfix-swift: - docker run -v $(PWD):$(docker_container_repo_dir) -w $(docker_container_repo_dir) -it $(swiftlint_container) swiftlint --autocorrect + swift package plugin swiftlint --autocorrect fmt-rust: $(rust_docker_run) /bin/bash -c "rustup component add rustfmt && cargo fmt" diff --git a/Package.swift b/Package.swift index be321629c..93f7bb666 100644 --- a/Package.swift +++ b/Package.swift @@ -61,11 +61,9 @@ var package = Package( ] ) -let isCI = ProcessInfo.processInfo.environment["CI"] == "true" - // MARK: - Enable local development toolings -let localDevelopment = !isCI && libwordpressFFIVersion.isLocal +let localDevelopment = libwordpressFFIVersion.isLocal if localDevelopment { try enableSwiftLint() From 9837b2cf4248979ccd916d2f252b397128eaa647 Mon Sep 17 00:00:00 2001 From: Tony Li Date: Fri, 7 Jun 2024 11:50:47 +1200 Subject: [PATCH 4/6] [Swift] Skip Swift Package plugin validation --- .buildkite/swift-test.sh | 1 + scripts/xcodebuild-test.sh | 1 + 2 files changed, 2 insertions(+) diff --git a/.buildkite/swift-test.sh b/.buildkite/swift-test.sh index 455da7712..b8da80d89 100755 --- a/.buildkite/swift-test.sh +++ b/.buildkite/swift-test.sh @@ -23,6 +23,7 @@ function build_for_real_device() { xcodebuild -destination "generic/platform=$platform" \ -scheme WordPress \ -derivedDataPath DerivedData \ + -skipPackagePluginValidation \ build | xcbeautify } diff --git a/scripts/xcodebuild-test.sh b/scripts/xcodebuild-test.sh index 46c2ccdde..89f47149c 100755 --- a/scripts/xcodebuild-test.sh +++ b/scripts/xcodebuild-test.sh @@ -14,5 +14,6 @@ xcodebuild \ -scheme WordPress \ -derivedDataPath DerivedData \ -destination "id=${device_id}" \ + -skipPackagePluginValidation \ test \ | xcbeautify From 0c552d61fe675cd1ac51b8cbc54f414b6f8727c6 Mon Sep 17 00:00:00 2001 From: Tony Li Date: Fri, 7 Jun 2024 11:53:59 +1200 Subject: [PATCH 5/6] Download xcframework for the lint-swift step --- .buildkite/download-xcframework.sh | 9 +++++++++ .buildkite/pipeline.yml | 2 ++ .buildkite/swift-test.sh | 7 ++----- .buildkite/validate-cocoapods.sh | 7 ++----- 4 files changed, 15 insertions(+), 10 deletions(-) create mode 100755 .buildkite/download-xcframework.sh diff --git a/.buildkite/download-xcframework.sh b/.buildkite/download-xcframework.sh new file mode 100755 index 000000000..612f70c10 --- /dev/null +++ b/.buildkite/download-xcframework.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -euo pipefail + +echo "--- :arrow_down: Downloading xcframework" +buildkite-agent artifact download target/libwordpressFFI.xcframework.zip . --step "xcframework" +buildkite-agent artifact download native/swift/Sources/wordpress-api-wrapper/wp_api.swift . --step "xcframework" +unzip target/libwordpressFFI.xcframework.zip -d . +rm target/libwordpressFFI.xcframework.zip diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index acbe847db..96232f625 100644 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -85,8 +85,10 @@ steps: make test-swift-linux - label: ":swift: Lint" command: | + .buildkite/download-xcframework.sh echo "--- :swift: Swiftlint" make lint-swift + depends_on: xcframework env: IMAGE_ID: xcode-15.3 agents: diff --git a/.buildkite/swift-test.sh b/.buildkite/swift-test.sh index b8da80d89..a4f64fe09 100755 --- a/.buildkite/swift-test.sh +++ b/.buildkite/swift-test.sh @@ -2,11 +2,8 @@ set -euo pipefail -echo "--- :arrow_down: Downloading xcframework" -buildkite-agent artifact download target/libwordpressFFI.xcframework.zip . --step "xcframework" -buildkite-agent artifact download native/swift/Sources/wordpress-api-wrapper/wp_api.swift . --step "xcframework" -unzip target/libwordpressFFI.xcframework.zip -d . -rm target/libwordpressFFI.xcframework.zip +.buildkite/download-xcframework.sh + export SKIP_PACKAGE_WP_API=true function run_tests() { diff --git a/.buildkite/validate-cocoapods.sh b/.buildkite/validate-cocoapods.sh index fe24ad8dc..faa5bd57a 100755 --- a/.buildkite/validate-cocoapods.sh +++ b/.buildkite/validate-cocoapods.sh @@ -41,11 +41,8 @@ Pod::Spec.new do |spec| end EOT -echo "--- :arrow_down: Downloading xcframework" -buildkite-agent artifact download target/libwordpressFFI.xcframework.zip . --step "xcframework" -buildkite-agent artifact download native/swift/Sources/wordpress-api-wrapper/wp_api.swift . --step "xcframework" -unzip target/libwordpressFFI.xcframework.zip -d . -rm target/libwordpressFFI.xcframework.zip +.buildkite/download-xcframework.sh + export SKIP_PACKAGE_WP_API=true echo "--- :rubygems: Setting up Gems" From 03b20d73f2b85b249bde920253b1078dfda188c5 Mon Sep 17 00:00:00 2001 From: Tony Li Date: Fri, 7 Jun 2024 12:08:49 +1200 Subject: [PATCH 6/6] Workaround incompatble macOS version with SwiftLint plugin --- Package.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Package.swift b/Package.swift index 93f7bb666..25ca1fdf7 100644 --- a/Package.swift +++ b/Package.swift @@ -111,6 +111,13 @@ func enableSwiftLint() throws { package.dependencies.append(.package(url: "https://github.com/realm/SwiftLint", exact: .init(version)!)) + var platforms = package.platforms ?? [] + if let mac = platforms.firstIndex(where: { $0 == .macOS(.v11) }) { + platforms.remove(at: mac) + platforms.append(.macOS(.v12)) + } + package.platforms = platforms + if let target = package.targets.first(where: { $0.name == "WordPressAPI" }) { target.plugins = (target.plugins ?? []) + [.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLint")] }