Skip to content

Commit

Permalink
Move old cache to new
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfedr committed Dec 24, 2017
1 parent 212cd91 commit 5b8995a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Source/CarthageKit/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,26 @@ private func BCSymbolMapsForFramework(_ frameworkURL: URL, inDirectoryURL direct

/// Returns the file URL at which the given project's repository will be
/// located.
private func repositoryFileURL(for dependency: Dependency, baseURL: URL = Constants.Dependency.repositoriesURL) -> URL {
return baseURL.appendingPathComponent(dependency.cacheName, isDirectory: true)
internal func repositoryFileURL(for dependency: Dependency, baseURL: URL = Constants.Dependency.repositoriesURL) -> URL {
let url = baseURL.appendingPathComponent(dependency.cacheName, isDirectory: true)
let fileManager = FileManager.default
// If the new dir exists, use it
if fileManager.fileExists(atPath: url.path) {
return url
}
// The old cache was based on dependency name, and we want to migrate to the new cache name
let oldUrl = baseURL.appendingPathComponent(dependency.name, isDirectory: true)
if !fileManager.fileExists(atPath: oldUrl.path) {
return url
}

do {
try fileManager.moveItem(at: oldUrl, to: url)
} catch {
NSLog("Warning: Failed to move old cache dir: \(error.localizedDescription). (\(error))")
}

return url
}

/// Returns the string representing a relative path from a dependency back to the root
Expand Down
33 changes: 33 additions & 0 deletions Tests/CarthageKitTests/ProjectSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,39 @@ class ProjectSpec: QuickSpec {
expect(actualPlatform) == .iOS
}
}

describe("repositoryFileURL") {
let dependency = Dependency.gitHub(.dotCom, Repository(owner: "owner", name: "name"))
let temporaryPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString)
let temporaryURL = URL(fileURLWithPath: temporaryPath, isDirectory: true)

beforeEach {
expect { try FileManager.default.createDirectory(atPath: temporaryURL.path, withIntermediateDirectories: true) }.notTo(throwError())
}

afterEach {
_ = try? FileManager.default.removeItem(at: temporaryURL)
}

it("should append cacheName to baseURL") {
let url = CarthageKit.repositoryFileURL(for: dependency, baseURL: temporaryURL)

expect(url) == temporaryURL.appendingPathComponent(dependency.cacheName, isDirectory: true)
}

it("should move old cache to new cache") {
let fileManager = FileManager.default

let oldCacheUrl = temporaryURL.appendingPathComponent(dependency.name, isDirectory: true)
expect { try FileManager.default.createDirectory(atPath: oldCacheUrl.path, withIntermediateDirectories: true) }.notTo(throwError())

let url = CarthageKit.repositoryFileURL(for: dependency, baseURL: temporaryURL)

expect(url) == temporaryURL.appendingPathComponent(dependency.cacheName, isDirectory: true)
expect(fileManager.fileExists(atPath: oldCacheUrl.path)) == false
expect(fileManager.fileExists(atPath: url.path)) == true
}
}
}
}

Expand Down

0 comments on commit 5b8995a

Please sign in to comment.