Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cache mixed up with different swift versions #62

Merged
merged 9 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
### Removed
- None.
### Fixed
- None.
- Fix mixed caching of frameworks with different Swift versions.
Issue: [#61](https://github.com/JamitLabs/Accio/issues/61) | PR: [#62](https://github.com/JamitLabs/Accio/pull/62) | Author: [Frederick Pietschmann](https://github.com/fredpi)
### Security
- None.

Expand Down
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"repositoryURL": "https://github.com/tuist/xcodeproj.git",
"state": {
"branch": "master",
"revision": "b951777f42e9acbfb8f19da623b43aaa604422f9",
"revision": "739899ef4b007c8abac0f70609f00c4157434f40",
"version": null
}
}
Expand Down
17 changes: 15 additions & 2 deletions Sources/AccioKit/Commands/Protocols/DependencyInstaller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ extension DependencyInstaller {
try bash("mkdir -p '\(Constants.temporaryFrameworksUrl.path)'")
try bash("mkdir -p '\(Constants.temporaryUncachingUrl.path)'")

let swiftVersion = try SwiftVersionDetectorService.shared.getCurrentSwiftVersion()

typealias ParsingResult = (target: AppTarget, platform: Platform, frameworkProducts: [FrameworkProduct])

let appTargets: [AppTarget] = try manifest.appTargets()
Expand All @@ -77,7 +79,13 @@ extension DependencyInstaller {
let platform = try PlatformDetectorService.shared.detectPlatform(of: appTarget)
print("Resolving dependencies for target '\(appTarget.targetName)' on platform '\(platform.rawValue)' ...", level: .info)

let frameworkProducts = try CachedBuilderService(sharedCachePath: sharedCachePath).frameworkProducts(manifest: manifest, appTarget: appTarget, dependencyGraph: dependencyGraph, platform: platform)
let frameworkProducts = try CachedBuilderService(sharedCachePath: sharedCachePath).frameworkProducts(
manifest: manifest,
appTarget: appTarget,
dependencyGraph: dependencyGraph,
platform: platform,
swiftVersion: swiftVersion
)
return ParsingResult(target: appTarget, platform: platform, frameworkProducts: frameworkProducts)
}

Expand All @@ -94,7 +102,12 @@ extension DependencyInstaller {
at: URL(fileURLWithPath: workingDirectory).appendingPathComponent("Package.resolved"),
with: parsingResults.flatMap {
$0.frameworkProducts.map {
CachedFrameworkProduct(swiftVersion: Constants.swiftVersion, libraryName: $0.libraryName, commitHash: $0.commitHash, platform: $0.platformName)
CachedFrameworkProduct(
swiftVersion: swiftVersion,
libraryName: $0.libraryName,
commitHash: $0.commitHash,
platform: $0.platformName
)
}
}
)
Expand Down
14 changes: 0 additions & 14 deletions Sources/AccioKit/Globals/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,4 @@ enum Constants {

return FileManager.userCacheDirUrl.appendingPathComponent("Accio/Cache").path
}

static var swiftVersion: String {
#if swift(>=6.0)
return "Swift-6.0"
#elseif swift(>=5.2)
return "Swift-5.2"
#elseif swift(>=5.1)
return "Swift-5.1"
#elseif swift(>=5.0)
return "Swift-5.0"
#else
return "Swift-4.2"
#endif
}
}
64 changes: 56 additions & 8 deletions Sources/AccioKit/Services/CachedBuilderService.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import Foundation

enum CachedBuilderServiceError: Error {
case unableToRetrieveSwiftVersion
case swiftVersionChanged
}

extension CachedBuilderServiceError: LocalizedError {
var errorDescription: String? {
switch self {
case .unableToRetrieveSwiftVersion:
return "Unable to retrieve Swift version used for command line."

case .swiftVersionChanged:
return "Swift version used for command line apparently changed during runtime."
}
}
}

final class CachedBuilderService {
private let frameworkCachingService: FrameworkCachingService
private let carthageBuilderService: CarthageBuilderService

init(sharedCachePath: String?) {
self.frameworkCachingService = FrameworkCachingService(sharedCachePath: sharedCachePath)
self.carthageBuilderService = CarthageBuilderService(frameworkCachingService: frameworkCachingService)
frameworkCachingService = FrameworkCachingService(sharedCachePath: sharedCachePath)
carthageBuilderService = CarthageBuilderService(frameworkCachingService: frameworkCachingService)
}

func frameworkProducts(manifest: Manifest, appTarget: AppTarget, dependencyGraph: DependencyGraph, platform: Platform) throws -> [FrameworkProduct] {
func frameworkProducts(
manifest: Manifest,
appTarget: AppTarget,
dependencyGraph: DependencyGraph,
platform: Platform,
swiftVersion: String
) throws -> [FrameworkProduct] {
var frameworkProducts: [FrameworkProduct] = []

let frameworks = try appTarget.frameworkDependencies(manifest: manifest, dependencyGraph: dependencyGraph).flattenedDeepFirstOrder()
Expand All @@ -18,28 +41,53 @@ final class CachedBuilderService {
}

for framework in frameworksWithoutDuplicates {
if let cachedFrameworkProduct = try frameworkCachingService.cachedProduct(framework: framework, platform: platform) {
if
let cachedFrameworkProduct = try frameworkCachingService.cachedProduct(
framework: framework,
platform: platform,
swiftVersion: swiftVersion
)
{
frameworkProducts.append(cachedFrameworkProduct)
} else {
let frameworkProduct: FrameworkProduct
switch try InstallationTypeDetectorService.shared.detectInstallationType(for: framework) {
case .swiftPackageManager:
try XcodeProjectGeneratorService.shared.generateXcodeProject(framework: framework)
let frameworkProduct = try carthageBuilderService.build(
frameworkProduct = try carthageBuilderService.build(
framework: framework,
platform: platform,
swiftVersion: swiftVersion,
alreadyBuiltFrameworkProducts: frameworkProducts
)
frameworkProducts.append(frameworkProduct)

case .carthage:
try GitResetService.shared.resetGit(atPath: framework.projectDirectory, includeUntrackedFiles: false)
let frameworkProduct = try carthageBuilderService.build(
frameworkProduct = try carthageBuilderService.build(
framework: framework,
platform: platform,
swiftVersion: swiftVersion,
alreadyBuiltFrameworkProducts: frameworkProducts
)
frameworkProducts.append(frameworkProduct)
}

if
let frameworkSwiftVersion = (
try? SwiftVersionDetectorService.shared.detectSwiftVersion(ofFrameworkProduct: frameworkProduct)
) ?? (
try? SwiftVersionDetectorService.shared.getCurrentSwiftVersion()
)
{
// If detectSwiftVersion doesn't work (e. g. happening for RxAtomic because of missing Swift header file),
// fallback to just retrieving current swift version via bash command.
guard frameworkSwiftVersion == swiftVersion else {
throw CachedBuilderServiceError.swiftVersionChanged
}
} else {
throw CachedBuilderServiceError.unableToRetrieveSwiftVersion
}

frameworkProducts.append(frameworkProduct)
}
}

Expand Down
9 changes: 7 additions & 2 deletions Sources/AccioKit/Services/CarthageBuilderService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ final class CarthageBuilderService {
self.frameworkCachingService = frameworkCachingService
}

func build(framework: Framework, platform: Platform, alreadyBuiltFrameworkProducts: [FrameworkProduct]) throws -> FrameworkProduct {
func build(
framework: Framework,
platform: Platform,
swiftVersion: String,
alreadyBuiltFrameworkProducts: [FrameworkProduct]
) throws -> FrameworkProduct {
print("Building library \(framework.libraryName) with Carthage ...", level: .info)

// link already built subdependencies from previous calls of this method
Expand Down Expand Up @@ -58,7 +63,7 @@ final class CarthageBuilderService {
try frameworkProduct.cleanupRecursiveFrameworkIfNeeded()

print("Completed building scheme \(framework.libraryName) with Carthage.", level: .info)
try frameworkCachingService.cache(product: frameworkProduct, framework: framework, platform: platform)
try frameworkCachingService.cache(product: frameworkProduct, framework: framework, platform: platform, swiftVersion: swiftVersion)

return frameworkProduct
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/AccioKit/Services/FrameworkCachingService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ final class FrameworkCachingService {
self.sharedCachePath = sharedCachePath
}

func cachedProduct(framework: Framework, platform: Platform) throws -> FrameworkProduct? {
let subpath: String = cacheFileSubPath(framework: framework, platform: platform)
func cachedProduct(framework: Framework, platform: Platform, swiftVersion: String) throws -> FrameworkProduct? {
let subpath: String = cacheFileSubPath(framework: framework, platform: platform, swiftVersion: swiftVersion)
let localCachedFileUrl = URL(fileURLWithPath: Constants.localCachePath).appendingPathComponent(subpath)

if FileManager.default.fileExists(atPath: localCachedFileUrl.path) {
Expand All @@ -29,8 +29,8 @@ final class FrameworkCachingService {
return nil
}

func cache(product: FrameworkProduct, framework: Framework, platform: Platform) throws {
let subpath: String = cacheFileSubPath(framework: framework, platform: platform)
func cache(product: FrameworkProduct, framework: Framework, platform: Platform, swiftVersion: String) throws {
let subpath: String = cacheFileSubPath(framework: framework, platform: platform, swiftVersion: swiftVersion)

if
let sharedCachePath = sharedCachePath,
Expand Down Expand Up @@ -70,8 +70,8 @@ final class FrameworkCachingService {
return frameworkProduct
}

private func cacheFileSubPath(framework: Framework, platform: Platform) -> String {
return "\(Constants.swiftVersion)/\(framework.libraryName)/\(framework.commitHash)/\(platform.rawValue).zip"
private func cacheFileSubPath(framework: Framework, platform: Platform, swiftVersion: String) -> String {
return "\(swiftVersion)/\(framework.libraryName)/\(framework.commitHash)/\(platform.rawValue).zip"
}

private func cache(product: FrameworkProduct, to targetUrl: URL) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ final class ResolvedManifestCachingService {

if FileManager.default.fileExists(atPath: localCachedFileUrl.path) {
print("Found cached resolved manifest results in local cache - trying to reuse cached build products.", level: .info)
return try JSONDecoder().decode([CachedFrameworkProduct].self, from: Data(contentsOf: localCachedFileUrl))
return try? JSONDecoder().decode([CachedFrameworkProduct].self, from: Data(contentsOf: localCachedFileUrl))
}

if let sharedCachePath = sharedCachePath {
let sharedCacheFileUrl = URL(fileURLWithPath: sharedCachePath).appendingPathComponent(subpath)

if FileManager.default.fileExists(atPath: sharedCacheFileUrl.path) {
print("Found cached resolved manifest results in shared cache - trying to reuse cached build products.", level: .info)
return try JSONDecoder().decode([CachedFrameworkProduct].self, from: Data(contentsOf: sharedCacheFileUrl))
return try? JSONDecoder().decode([CachedFrameworkProduct].self, from: Data(contentsOf: sharedCacheFileUrl))
}
}

Expand Down
60 changes: 60 additions & 0 deletions Sources/AccioKit/Services/SwiftVersionDetectorService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Foundation
import HandySwift
import SwiftShell

enum SwiftVersionDetectorError: Error {
case swiftVersionCommandError
case parsingError
}

class SwiftVersionDetectorService {
static let shared = SwiftVersionDetectorService()

func getCurrentSwiftVersion() throws -> String {
fredpi marked this conversation as resolved.
Show resolved Hide resolved
let result = run(bash: "swift --version")
guard result.succeeded else {
throw SwiftVersionDetectorError.swiftVersionCommandError
}

return try convertToSwiftVersion(swiftVersionOutput: result.stdout)
}

func convertToSwiftVersion(swiftVersionOutput: String) throws -> String {
do {
let regex = try Regex(#"Apple Swift version ([\d.]*) \(swiftlang"#)
guard let versionNumber = regex.firstMatch(in: swiftVersionOutput)?.captures.first ?? nil else {
throw SwiftVersionDetectorError.parsingError
}

return "Swift-\(versionNumber)"
}
catch {
throw SwiftVersionDetectorError.parsingError
}
}

func detectSwiftVersion(ofFrameworkProduct frameworkProduct: FrameworkProduct) throws -> String {
let swiftHeaderFileUrl = frameworkProduct.frameworkDirUrl.appendingPathComponent(
"Headers/\(frameworkProduct.libraryName)-Swift.h"
)

if
FileManager.default.fileExists(atPath: swiftHeaderFileUrl.path),
let swiftHeaderFileContents = try? String(contentsOf: swiftHeaderFileUrl, encoding: .utf8)
{
do {
let regex = try Regex(#"// Generated by Apple Swift version ([\d.]*) "#)
guard let versionNumber = regex.firstMatch(in: swiftHeaderFileContents)?.captures.first ?? nil else {
throw SwiftVersionDetectorError.parsingError
}

return "Swift-\(versionNumber)"
}
catch {
throw SwiftVersionDetectorError.parsingError
}
}

throw SwiftVersionDetectorError.parsingError
}
}
24 changes: 14 additions & 10 deletions Tests/AccioKitTests/Services/FrameworkCachingServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ class FrameworkCachingServiceTests: XCTestCase {
TestHelper.shared.isStartedByUnitTests = true
let frameworkCachingService = FrameworkCachingService(sharedCachePath: nil)

let testFrameworkLocalCacheFilePath: String = "\(Constants.localCachePath)/\(Constants.swiftVersion)/\(testFramework.libraryName)/\(testFramework.commitHash)/\(Platform.iOS.rawValue).zip"
let testFrameworkSharedCacheFilePath: String = "\(sharedCachePath)/\(Constants.swiftVersion)/\(testFramework.libraryName)/\(testFramework.commitHash)/\(Platform.iOS.rawValue).zip"
let swiftVersion = try! SwiftVersionDetectorService.shared.getCurrentSwiftVersion()

var cachedProduct: FrameworkProduct? = try! frameworkCachingService.cachedProduct(framework: testFramework, platform: .iOS)
let testFrameworkLocalCacheFilePath: String = "\(Constants.localCachePath)/\(swiftVersion)/\(testFramework.libraryName)/\(testFramework.commitHash)/\(Platform.iOS.rawValue).zip"
let testFrameworkSharedCacheFilePath: String = "\(sharedCachePath)/\(swiftVersion)/\(testFramework.libraryName)/\(testFramework.commitHash)/\(Platform.iOS.rawValue).zip"

var cachedProduct: FrameworkProduct? = try! frameworkCachingService.cachedProduct(framework: testFramework, platform: .iOS, swiftVersion: swiftVersion)
XCTAssertNil(cachedProduct)

XCTAssert(!FileManager.default.fileExists(atPath: testFrameworkLocalCacheFilePath))
XCTAssert(!FileManager.default.fileExists(atPath: testFrameworkSharedCacheFilePath))

try! frameworkCachingService.cache(product: testFrameworkProduct, framework: testFramework, platform: .iOS)
try! frameworkCachingService.cache(product: testFrameworkProduct, framework: testFramework, platform: .iOS, swiftVersion: swiftVersion)

cachedProduct = try! frameworkCachingService.cachedProduct(framework: testFramework, platform: .iOS)
cachedProduct = try! frameworkCachingService.cachedProduct(framework: testFramework, platform: .iOS, swiftVersion: swiftVersion)
XCTAssertNotNil(cachedProduct)

XCTAssert(cachedProduct!.frameworkDirPath.hasPrefix(Constants.temporaryFrameworksUrl.path))
Expand All @@ -62,18 +64,20 @@ class FrameworkCachingServiceTests: XCTestCase {
TestHelper.shared.isStartedByUnitTests = true
let frameworkCachingService = FrameworkCachingService(sharedCachePath: sharedCachePath)

let testFrameworkLocalCacheFilePath: String = "\(Constants.localCachePath)/\(Constants.swiftVersion)/\(testFramework.libraryName)/\(testFramework.commitHash)/\(Platform.iOS.rawValue).zip"
let testFrameworkSharedCacheFilePath: String = "\(sharedCachePath)/\(Constants.swiftVersion)/\(testFramework.libraryName)/\(testFramework.commitHash)/\(Platform.iOS.rawValue).zip"
let swiftVersion = try! SwiftVersionDetectorService.shared.getCurrentSwiftVersion()

let testFrameworkLocalCacheFilePath: String = "\(Constants.localCachePath)/\(swiftVersion)/\(testFramework.libraryName)/\(testFramework.commitHash)/\(Platform.iOS.rawValue).zip"
let testFrameworkSharedCacheFilePath: String = "\(sharedCachePath)/\(swiftVersion)/\(testFramework.libraryName)/\(testFramework.commitHash)/\(Platform.iOS.rawValue).zip"

var cachedProduct: FrameworkProduct? = try! frameworkCachingService.cachedProduct(framework: testFramework, platform: .iOS)
var cachedProduct: FrameworkProduct? = try! frameworkCachingService.cachedProduct(framework: testFramework, platform: .iOS, swiftVersion: swiftVersion)
XCTAssertNil(cachedProduct)

XCTAssert(!FileManager.default.fileExists(atPath: testFrameworkLocalCacheFilePath))
XCTAssert(!FileManager.default.fileExists(atPath: testFrameworkSharedCacheFilePath))

try! frameworkCachingService.cache(product: testFrameworkProduct, framework: testFramework, platform: .iOS)
try! frameworkCachingService.cache(product: testFrameworkProduct, framework: testFramework, platform: .iOS, swiftVersion: swiftVersion)

cachedProduct = try! frameworkCachingService.cachedProduct(framework: testFramework, platform: .iOS)
cachedProduct = try! frameworkCachingService.cachedProduct(framework: testFramework, platform: .iOS, swiftVersion: swiftVersion)
XCTAssertNotNil(cachedProduct)

XCTAssert(cachedProduct!.frameworkDirPath.hasPrefix(Constants.temporaryFrameworksUrl.path))
Expand Down
Loading