Skip to content

Commit

Permalink
Merge pull request #19 from dfed/dfed--support-older-os
Browse files Browse the repository at this point in the history
Support macOS 10.14, iOS 12, tvOS 12, and watchOS 5
  • Loading branch information
dfed authored Dec 24, 2019
2 parents cd8bca0 + 024ef4f commit b2565a0
Show file tree
Hide file tree
Showing 15 changed files with 546 additions and 94 deletions.
18 changes: 13 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ script:
matrix:
include:
- osx_image: xcode11.2
env: ACTION="swift-package";SDK="iphonesimulator";DESTINATION="platform=iOS Simulator,OS=13.2.2,name=iPad Pro (12.9-inch) (3rd generation)";SHOULD_TEST="true"
env: ACTION="swift-package";PLATFORMS="iOS_12,iOS_13";

- osx_image: xcode11.2
env: ACTION="swift-package";PLATFORMS="tvOS_12";
after_success:
bash <(curl -s https://codecov.io/bash) -J '^CacheAdvance$' -D .build/derivedData -t 8344b011-6b2a-4b3d-a573-eaf49684318e
- osx_image: xcode11.2
env: ACTION="swift-package";SDK="appletvsimulator13.2";DESTINATION="platform=tvOS Simulator,name=Apple TV";SHOULD_TEST="true";ENABLE_CODE_COVERAGE="true"
env: ACTION="swift-package";PLATFORMS="tvOS_13";
after_success:
bash <(curl -s https://codecov.io/bash) -J '^CacheAdvance$' -D .build/derivedData -t 8344b011-6b2a-4b3d-a573-eaf49684318e

- osx_image: xcode11.2
# We can't run tests on macOS because CacheAdvance supports a minimum of macOS 10.15, and Travis CI's machines run on macOS 10.14.
env: ACTION="swift-package";SDK="macosx10.15";DESTINATION="platform=OS X";SHOULD_TEST="false"
env: ACTION="swift-package";PLATFORMS="macOS_10_15";

- osx_image: xcode11.2
env: ACTION="swift-package";SDK="watchos6.1";DESTINATION="";SHOULD_TEST="false"
env: ACTION="swift-package";PLATFORMS="watchOS_5,watchOS_6";

- osx_image: xcode11.2
env: ACTION="pod-lint";SWIFT_VERSION="5.1"

- osx_image: xcode11.2
env: ACTION="carthage"

Expand Down
12 changes: 6 additions & 6 deletions CacheAdvance.podspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Pod::Spec.new do |s|
s.name = 'CacheAdvance'
s.version = '0.0.1'
s.version = '0.0.2'
s.license = 'Apache License, Version 2.0'
s.summary = 'A cache that enables the performant persistence of individual messages to disk'
s.homepage = 'https://github.com/dfed/CacheAdvance'
s.authors = 'Dan Federman'
s.source = { :git => 'https://github.com/dfed/CacheAdvance.git', :tag => s.version }
s.swift_version = '5.1'
s.source_files = 'Sources/CacheAdvance/**/*.{swift}'
s.ios.deployment_target = '13.0'
s.tvos.deployment_target = '13.0'
s.watchos.deployment_target = '6.0'
s.macos.deployment_target = '10.15'
s.source_files = 'Sources/CacheAdvance/**/*.{swift}', 'Sources/SwiftTryCatch/**/*.{h,m}'
s.ios.deployment_target = '12.0'
s.tvos.deployment_target = '12.0'
s.watchos.deployment_target = '5.0'
s.macos.deployment_target = '10.14'
end
27 changes: 20 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import PackageDescription
let package = Package(
name: "CacheAdvance",
platforms: [
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6),
.macOS(.v10_15),
.iOS(.v12),
.tvOS(.v12),
.watchOS(.v5),
.macOS(.v10_14),
],
products: [
.library(
Expand All @@ -19,11 +19,24 @@ let package = Package(
targets: [
.target(
name: "CacheAdvance",
dependencies: []),
dependencies: ["SwiftTryCatch"],
swiftSettings: [.define("SWIFT_PACKAGE_MANAGER")]
),
.testTarget(
name: "CacheAdvanceTests",
dependencies: ["CacheAdvance"])
dependencies: ["CacheAdvance"]),
.target(
name: "SwiftTryCatch",
dependencies: [],
publicHeadersPath: "./",
// Make Objective-C exceptions not leak, since we can now recover from them.
// For more info, see https://clang.llvm.org/docs/AutomaticReferenceCounting.html#exceptions
swiftSettings: [SwiftSetting.define("-fobjc-arc-exceptions")]
),
.testTarget(
name: "SwiftTryCatchTests",
dependencies: ["SwiftTryCatch"])
],
swiftLanguageVersions: [.v5]
)
let version = Version(0, 0, 1)
let version = Version(0, 0, 2)
155 changes: 125 additions & 30 deletions Scripts/build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import Foundation

// Usage: build.swift sdk destination should_test?
// Usage: build.swift platforms

func execute(commandPath: String, arguments: [String]) throws {
let task = Process()
Expand All @@ -20,38 +20,133 @@ enum TaskError: Error {
case code(Int32)
}

guard CommandLine.arguments.count > 3 else {
print("Usage: build.swift sdk destination should_test? enable_code_coverage?")
enum Platform: String, CaseIterable, CustomStringConvertible {
case iOS_12
case iOS_13
case tvOS_12
case tvOS_13
case macOS_10_15
case watchOS_5
case watchOS_6

var destination: String {
switch self {
case .iOS_12:
return "platform=iOS Simulator,OS=12.2,name=iPad Pro (12.9-inch) (3rd generation)"
case .iOS_13:
return "platform=iOS Simulator,OS=13.2.2,name=iPad Pro (12.9-inch) (3rd generation)"

case .tvOS_12:
return "platform=tvOS Simulator,OS=12.2,name=Apple TV"
case .tvOS_13:
return "platform=tvOS Simulator,OS=13.2,name=Apple TV"

case .macOS_10_15:
return "platform=OS X"

case .watchOS_5:
return "OS=5.2,name=Apple Watch Series 4 - 44mm"
case .watchOS_6:
return "OS=6.1,name=Apple Watch Series 4 - 44mm"
}
}

var sdk: String {
switch self {
case .iOS_12,
.iOS_13:
return "iphonesimulator"

case .tvOS_12,
.tvOS_13:
return "appletvsimulator"

case .macOS_10_15:
return "macosx10.15"

case .watchOS_5,
.watchOS_6:
return "watchsimulator"
}
}

var shouldTest: Bool {
switch self {
case .iOS_12,
.iOS_13,
.tvOS_12,
.tvOS_13:
return true

case .macOS_10_15:
// We can't run tests on macOS 10.15 because CacheAdvance assumes macOS 10.15 APIs are available, and Travis CI's machines run on macOS 10.14.
// For more information, see https://github.com/dfed/CacheAdvance/issues/2
return false

case .watchOS_5,
.watchOS_6:
// watchOS does not support unit testing (yet?).
return false
}
}

var enableCodeCoverage: Bool {
switch self {
case .tvOS_12,
.tvOS_13:
// We currently only have a after_success job to upload code coverage for our tvOS targets.
return true

case .iOS_12,
.iOS_13,
.macOS_10_15,
.watchOS_5,
.watchOS_6:
return false
}
}

var description: String {
rawValue
}
}

guard CommandLine.arguments.count > 1 else {
print("Usage: build.swift platforms")
throw TaskError.code(1)
}

try execute(commandPath: "/usr/bin/swift", arguments: ["package", "generate-xcodeproj", "--output=generated/"])

let sdk = CommandLine.arguments[1]
let destination = CommandLine.arguments[2]
let shouldTest = CommandLine.arguments.count > 3 ? Bool(CommandLine.arguments[3]) ?? false : false
let enableCodeCoverage = CommandLine.arguments.count > 4 ? Bool(CommandLine.arguments[4]) ?? false : false

var xcodeBuildArguments = [
"-project", "generated/CacheAdvance.xcodeproj",
"-scheme", "CacheAdvance-Package",
"-sdk", sdk,
"-configuration", "Release",
"-PBXBuildsContinueAfterErrors=0",
]
if !destination.isEmpty {
xcodeBuildArguments.append("-destination")
xcodeBuildArguments.append(destination)
}
if enableCodeCoverage {
xcodeBuildArguments.append("-enableCodeCoverage")
xcodeBuildArguments.append("YES")
xcodeBuildArguments.append("-derivedDataPath")
xcodeBuildArguments.append(".build/derivedData")
}
xcodeBuildArguments.append("build")
if shouldTest {
xcodeBuildArguments.append("test")
}
let rawPlatforms = CommandLine.arguments[1].components(separatedBy: ",")

try execute(commandPath: "/usr/bin/xcodebuild", arguments: xcodeBuildArguments)
for rawPlatform in rawPlatforms {
guard let platform = Platform(rawValue: rawPlatform) else {
print("Received unknown platform type \(rawPlatform)")
print("Possible platform types are: \(Platform.allCases)")
throw TaskError.code(1)
}
var xcodeBuildArguments = [
"-project", "generated/CacheAdvance.xcodeproj",
"-scheme", "CacheAdvance-Package",
"-sdk", platform.sdk,
"-configuration", "Release",
"-PBXBuildsContinueAfterErrors=0",
]
if !platform.destination.isEmpty {
xcodeBuildArguments.append("-destination")
xcodeBuildArguments.append(platform.destination)
}
if platform.enableCodeCoverage {
xcodeBuildArguments.append("-enableCodeCoverage")
xcodeBuildArguments.append("YES")
xcodeBuildArguments.append("-derivedDataPath")
xcodeBuildArguments.append(".build/derivedData")
}
xcodeBuildArguments.append("build")
if platform.shouldTest {
xcodeBuildArguments.append("test")
}

try execute(commandPath: "/usr/bin/xcodebuild", arguments: xcodeBuildArguments)
}
2 changes: 1 addition & 1 deletion Scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -ex
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if [ $ACTION == "swift-package" ]; then
$DIR/build.swift $SDK "$DESTINATION" $SHOULD_TEST $ENABLE_CODE_COVERAGE
$DIR/build.swift $PLATFORMS
fi

if [ $ACTION == "pod-lint" ]; then
Expand Down
16 changes: 8 additions & 8 deletions Sources/CacheAdvance/CacheAdvance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public final class CacheAdvance<T: Codable> {
}

deinit {
try? writer.close()
try? reader.close()
try? writer.closeHandle()
try? reader.closeHandle()
}

// MARK: Public
Expand Down Expand Up @@ -94,13 +94,13 @@ public final class CacheAdvance<T: Codable> {
// We'll need to start writing the file from the beginning of the file.

// Trim the file to the current position to remove soon-to-be-abandoned data from the file.
try writer.truncate(atOffset: writer.offsetInFile)
try writer.truncate(at: writer.offsetInFile)

// Set the offset back to the beginning of the file.
try writer.seek(toOffset: 0)
try writer.seek(to: 0)

// We know the oldest message is at the beginning of the file, since we just tossed out the rest of the file.
try reader.seek(toOffset: 0)
try reader.seek(to: 0)

// We know we're about to overwrite the oldest message, so advance the reader to the second oldest message.
try reader.seekToNextMessage(shouldSeekToOldestMessageIfFound: false, cacheOverwritesOldMessages: true)
Expand Down Expand Up @@ -149,7 +149,7 @@ public final class CacheAdvance<T: Codable> {
// This is our first cache action.
// We need to find out where we should write our next message.
try reader.seekToEndOfNewestMessage(cacheOverwritesOldMessages: shouldOverwriteOldMessages)
try writer.seek(toOffset: reader.offsetInFile)
try writer.seek(to: reader.offsetInFile)

// Now that we know where to write, we need to figure out where the oldest message is.
try reader.seekToBeginningOfOldestMessage(cacheOverwritesOldMessages: shouldOverwriteOldMessages)
Expand Down Expand Up @@ -196,11 +196,11 @@ public final class CacheAdvance<T: Codable> {
let endOfMessageOffset = writer.offsetInFile + Bytes(messageData.count)

// Write the complete messge data atomically.
try writer.__write(dataToWrite, error: ())
try writer.write(data: dataToWrite)

// Seek the file handle's offset back to the end of the message we just wrote.
// This way the next time we write a message, we'll overwrite the last message marker.
try writer.seek(toOffset: endOfMessageOffset)
try writer.seek(to: endOfMessageOffset)
}

private let writer: FileHandle
Expand Down
Loading

0 comments on commit b2565a0

Please sign in to comment.