From 4c3389be0d9fc171d89c72b45ae53dde0f227043 Mon Sep 17 00:00:00 2001 From: Petro Rovenskyy Date: Fri, 2 Oct 2020 00:22:08 +0300 Subject: [PATCH] Added SPM support (#283) * added SPM support * use SPM generated macro SWIFTPM_MODULE_BUNDLE * updated gitignore * Add GitHub to the SSH known hosts file * Use https insted of ssh for dependency URL * use make for CI. * use PINOperation from - 1.2.0. Co-authored-by: Garrett Moon --- .github/workflows/ci.yaml | 13 +++++++-- .gitignore | 5 ++++ Makefile | 10 +++++-- Package.swift | 47 ++++++++++++++++++++++++++++++ Source/PINCache.h | 8 ++--- Source/PINCache.m | 2 +- Source/PINDiskCache.h | 6 ++-- Source/PINDiskCache.m | 2 +- Source/PINMemoryCache.h | 6 ++-- Source/PINMemoryCache.m | 2 +- Tests/PINCacheTests.m | 13 +++++++-- Tests/PINDiskCache+PINCacheTests.h | 2 +- Tests/PINDiskCache+PINCacheTests.m | 2 +- 13 files changed, 96 insertions(+), 22 deletions(-) create mode 100644 Package.swift diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 040540e9..e1d46ba7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -32,13 +32,22 @@ jobs: name: CocoaPods runs-on: macOS-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Lint run: make cocoapods carthage: name: Carthage runs-on: macOS-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: carthage run: make carthage + swift-package-manager: + runs-on: macos-latest + env: + DEVELOPER_DIR: /Applications/Xcode_12.app/Contents/Developer + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Verify that PINCache can be build by SPM + run: make spm diff --git a/.gitignore b/.gitignore index 7f818743..0046cbf2 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,8 @@ xcuserdata /.buckd Carthage/Build + +# SPM +.swiftpm/ +.build/ +Package.resolved diff --git a/Makefile b/Makefile index de805f2b..bd4a4eda 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ PLATFORM="platform=iOS Simulator,name=iPhone 8" SDK="iphonesimulator" SHELL=/bin/bash -o pipefail -.PHONY: all cocoapods test carthage analyze +.PHONY: all cocoapods test carthage analyze spm cocoapods: pod lib lint @@ -25,4 +25,10 @@ carthage: carthage update --no-use-binaries --no-build carthage build --no-skip-current -all: carthage cocoapods test analyze +spm: +# For now just check whether we can assemble it +# TODO: replace it with "swift test --enable-test-discovery --sanitize=thread" when swiftPM resource-related bug would be fixed. +# https://bugs.swift.org/browse/SR-13560 + swift build + +all: carthage cocoapods test analyze spm diff --git a/Package.swift b/Package.swift new file mode 100644 index 00000000..38447e31 --- /dev/null +++ b/Package.swift @@ -0,0 +1,47 @@ +// swift-tools-version:5.3 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "PINCache", + platforms: [ + .macOS(.v10_10), + .iOS(.v9), + .watchOS(.v2), + .tvOS(.v9) + ], + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "PINCache", + targets: ["PINCache"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + .package(url: "https://github.com/pinterest/PINOperation.git", from: "1.2.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "PINCache", + dependencies: ["PINOperation"], + path: "Source", + exclude: ["Carthage", "docs", + "build_docs.sh", "Cartfile", + "Cartfile.resolved", "Makefile", + "PINCache.podspec", "Info.plist"], + publicHeadersPath: "."), + .testTarget( + name: "PINCacheTests", + dependencies: ["PINCache"], + path: "Tests", + exclude: ["Info.plist"], + resources: [.process("Default-568h@2x.png")], + cSettings: [ + .define("TEST_AS_SPM"), + ]), + ] +) diff --git a/Source/PINCache.h b/Source/PINCache.h index 7ddec1c6..bfc9d850 100644 --- a/Source/PINCache.h +++ b/Source/PINCache.h @@ -4,10 +4,10 @@ #import -#import -#import -#import -#import +#import "PINCacheMacros.h" +#import "PINCaching.h" +#import "PINDiskCache.h" +#import "PINMemoryCache.h" NS_ASSUME_NONNULL_BEGIN diff --git a/Source/PINCache.m b/Source/PINCache.m index f574e0c0..ef6d442c 100644 --- a/Source/PINCache.m +++ b/Source/PINCache.m @@ -4,7 +4,7 @@ #import "PINCache.h" -#import +@import PINOperation; static NSString * const PINCachePrefix = @"com.pinterest.PINCache"; static NSString * const PINCacheSharedName = @"PINCacheShared"; diff --git a/Source/PINDiskCache.h b/Source/PINDiskCache.h index 224f9ce1..81bb26a4 100644 --- a/Source/PINDiskCache.h +++ b/Source/PINDiskCache.h @@ -4,9 +4,9 @@ #import -#import -#import -#import +#import "PINCacheMacros.h" +#import "PINCaching.h" +#import "PINCacheObjectSubscripting.h" NS_ASSUME_NONNULL_BEGIN diff --git a/Source/PINDiskCache.m b/Source/PINDiskCache.m index 40e606d8..e7231e46 100644 --- a/Source/PINDiskCache.m +++ b/Source/PINDiskCache.m @@ -11,7 +11,7 @@ #import #import -#import +@import PINOperation; #define PINDiskCacheError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \ [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \ diff --git a/Source/PINMemoryCache.h b/Source/PINMemoryCache.h index ef413ce4..1cf9ff88 100644 --- a/Source/PINMemoryCache.h +++ b/Source/PINMemoryCache.h @@ -4,9 +4,9 @@ #import -#import -#import -#import +#import "PINCacheMacros.h" +#import "PINCaching.h" +#import "PINCacheObjectSubscripting.h" NS_ASSUME_NONNULL_BEGIN diff --git a/Source/PINMemoryCache.m b/Source/PINMemoryCache.m index 9f64c293..88fe537b 100644 --- a/Source/PINMemoryCache.m +++ b/Source/PINMemoryCache.m @@ -5,7 +5,7 @@ #import "PINMemoryCache.h" #import -#import +@import PINOperation; #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 #import diff --git a/Tests/PINCacheTests.m b/Tests/PINCacheTests.m index c0a5824b..e425c030 100644 --- a/Tests/PINCacheTests.m +++ b/Tests/PINCacheTests.m @@ -2,11 +2,12 @@ // Modifications by Garrett Moon // Copyright (c) 2015 Pinterest. All rights reserved. +@import PINCache; +@import PINOperation; + #import "PINCacheTests.h" #import "NSDate+PINCacheTests.h" #import "PINDiskCache+PINCacheTests.h" -#import -#import #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR @@ -79,8 +80,14 @@ - (PINImage *)image static PINImage *image = nil; if (!image) { +#ifdef TEST_AS_SPM + NSBundle *bun = SWIFTPM_MODULE_BUNDLE; +#else + NSBundle *bun = [NSBundle bundleForClass:self.class]; + +#endif + NSURL *imageURL = [bun URLForResource:@"Default-568h@2x" withExtension:@"png"]; NSError *error = nil; - NSURL *imageURL = [[NSBundle bundleForClass:self.class] URLForResource:@"Default-568h@2x" withExtension:@"png"]; NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL options:NSDataReadingUncached error:&error]; diff --git a/Tests/PINDiskCache+PINCacheTests.h b/Tests/PINDiskCache+PINCacheTests.h index ce487335..fc778cf4 100644 --- a/Tests/PINDiskCache+PINCacheTests.h +++ b/Tests/PINDiskCache+PINCacheTests.h @@ -1,4 +1,4 @@ -#import +#import "PINCache.h" @interface PINDiskCache (PINCacheTests) diff --git a/Tests/PINDiskCache+PINCacheTests.m b/Tests/PINDiskCache+PINCacheTests.m index 3535d43b..fd8a164f 100644 --- a/Tests/PINDiskCache+PINCacheTests.m +++ b/Tests/PINDiskCache+PINCacheTests.m @@ -15,7 +15,7 @@ @implementation PINDiskCache (PINCacheTests) - (void)setTtlCacheSync:(BOOL)ttlCache { [self lock]; - self->_ttlCache = ttlCache; + [self setValue:@(ttlCache) forKey:@"_ttlCache"]; [self unlock]; }