Skip to content

Commit

Permalink
arrange removeUnneededItems
Browse files Browse the repository at this point in the history
  • Loading branch information
sidepelican committed Sep 22, 2019
1 parent 51ee5c0 commit aca72ab
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 112 deletions.
9 changes: 0 additions & 9 deletions Source/CarthageKit/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ public enum CarthageError: Error {
/// An archive (.zip, .gz, .bz2 ...) contains binaries that would
/// be copied to the same destination path
case duplicatesInArchive(duplicates: DuplicatesInArchive)

/// An expected VersionFile wasn't found
case versionFileNotFound(Dependency, URL)
}

extension CarthageError {
Expand Down Expand Up @@ -200,9 +197,6 @@ extension CarthageError: Equatable {
case let (.duplicatesInArchive(left), .duplicatesInArchive(right)):
return left == right

case let (.versionFileNotFound(left), .versionFileNotFound(right)):
return left == right

default:
return false
}
Expand Down Expand Up @@ -387,9 +381,6 @@ extension CarthageError: CustomStringConvertible {
.map { "* \t\($0.value.map{ url in return url.absoluteString }.joined(separator: "\n\t")) \n\t\tto:\n\t\($0.key)" }
.joined(separator: "\n")
return "Invalid archive - Found multiple frameworks with the same unarchiving destination:\n\(prettyDupeList)"

case let .versionFileNotFound(dependency, url):
return "VersionFile for \(dependency.name) does not exit at \(url.path)"
}
}
}
177 changes: 87 additions & 90 deletions Source/CarthageKit/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -933,102 +933,99 @@ public final class Project { // swiftlint:disable:this type_body_length
}
}


public func removeUnneededItems() -> SignalProducer<(), CarthageError> {
return loadResolvedCartfile()
.flatMap(.merge) { resolved -> SignalProducer<URL, CarthageError> in
let fileManager = FileManager.default

var checkoutURLs = Set<URL>()
var versionFileURLs = Set<URL>()
var binaryURLs = Set<URL>()

for (dependency, _) in resolved.dependencies {
checkoutURLs.insert(
self.directoryURL
.appendingPathComponent(dependency.relativePath, isDirectory: true)
.resolvingSymlinksInPath()
)

let versionFileURL = VersionFile
.url(for: dependency, rootDirectoryURL: self.directoryURL)
.resolvingSymlinksInPath()

guard let versionFile = VersionFile(url: versionFileURL) else {
return SignalProducer(error: CarthageError.versionFileNotFound(dependency, versionFileURL))
}

versionFileURLs.insert(versionFileURL)

let binariesDirectoryURL = self.directoryURL
.appendingPathComponent(Constants.binariesFolderPath, isDirectory: true)
.resolvingSymlinksInPath()
let binariesDirectoryURL = self.directoryURL
.appendingPathComponent(Constants.binariesFolderPath, isDirectory: true)
.resolvingSymlinksInPath()

binaryURLs.formUnion(Platform.supportedPlatforms.flatMap { platform -> [URL] in
let cachedFrameworks: [CachedFramework]?
switch platform {
case .macOS: cachedFrameworks = versionFile.macOS
case .iOS: cachedFrameworks = versionFile.iOS
case .watchOS: cachedFrameworks = versionFile.watchOS
case .tvOS: cachedFrameworks = versionFile.tvOS
}
return (cachedFrameworks ?? []).flatMap { cachedFramework -> [URL] in
let frameworkURL = versionFile
.frameworkURL(
for: cachedFramework, platform: platform,
binariesDirectoryURL: binariesDirectoryURL
)
.resolvingSymlinksInPath()
return [
frameworkURL,
URL(fileURLWithPath: frameworkURL.relativePath)
.appendingPathExtension("dSYM")
.resolvingSymlinksInPath(),
]
}
})
return loadResolvedCartfile()
.flatMap(.merge) { resolved -> SignalProducer<Dependency, CarthageError> in
return SignalProducer(resolved.dependencies.keys)
}
.flatMap(.merge) { dependency -> SignalProducer<(checkoutURL: URL, versionFileURL: URL, binaryURLs: [URL]), CarthageError> in
let checkoutURL = self.directoryURL
.appendingPathComponent(dependency.relativePath, isDirectory: true)
.resolvingSymlinksInPath()

let versionFileURL = VersionFile
.url(for: dependency, rootDirectoryURL: self.directoryURL)
.resolvingSymlinksInPath()

let binaryURLs = buildableSchemesInDirectory(checkoutURL, withConfiguration: "Release")
.flatMap(.concat) { scheme, project -> SignalProducer<BuildSettings, CarthageError> in
let buildArguments = BuildArguments(project: project, scheme: scheme, configuration: "Release")
return BuildSettings.load(with: buildArguments)
}
.flatMap(.concat) { settings -> SignalProducer<(BuildSettings, String), CarthageError> in
return SignalProducer(settings.wrapperName).map { (settings, $0) }
}
.flatMap(.concat) { settings, wrapperName -> SignalProducer<URL, CarthageError> in
settings.buildSDKs.map { sdk -> URL in
settings.productDestinationPath(in: binariesDirectoryURL.appendingPathComponent(sdk.platform.rawValue, isDirectory: true))
.appendingPathComponent(wrapperName)
}
}
.collect()

var urls: [URL] = []
urls += (try? fileManager
.contentsOfDirectory(
at: self.directoryURL.appendingPathComponent(
Constants.checkoutsFolderPath, isDirectory: true
),
includingPropertiesForKeys: nil
)
.map { $0.resolvingSymlinksInPath() }
.filter { !checkoutURLs.contains($0) }) ?? []

urls += (try? fileManager
.contentsOfDirectory(
at: self.directoryURL.appendingPathComponent(
Constants.binariesFolderPath, isDirectory: true
),
includingPropertiesForKeys: nil
)
.map { $0.resolvingSymlinksInPath() }
.filter { $0.pathExtension == VersionFile.pathExtension &&
!versionFileURLs.contains($0) }) ?? []

urls += Platform.supportedPlatforms
.flatMap { platform -> [URL] in
(try? fileManager
.contentsOfDirectory(
at: self.directoryURL.appendingPathComponent(
platform.relativePath, isDirectory: true
),
includingPropertiesForKeys: nil
)
.map { $0.resolvingSymlinksInPath() }
.filter { !binaryURLs.contains($0) }) ?? []
}
return binaryURLs.map { (checkoutURL, versionFileURL, $0) }
}
.collect()
.map { urls -> (checkoutURLs: Set<URL>, versionFileURLs: Set<URL>, binaryURLs: Set<URL>) in
var checkoutURLSet: Set<URL> = []
var versionFileURLSet: Set<URL> = []
var binaryURLSet: Set<URL> = []

for (checkoutURL, versionFileURL, binaryURLs) in urls {
checkoutURLSet.insert(checkoutURL)
versionFileURLSet.insert(versionFileURL)
binaryURLSet.formUnion(binaryURLs)
}

return SignalProducer(Set(urls))
return (checkoutURLSet, versionFileURLSet, binaryURLSet)
}
.flatMap(.merge) { (checkoutURLs, versionFileURLs, binaryURLs) -> SignalProducer<URL, CarthageError> in
let fileManager = FileManager.default

var urls: [URL] = []
urls += (try? fileManager
.contentsOfDirectory(
at: self.directoryURL.appendingPathComponent(
Constants.checkoutsFolderPath, isDirectory: true
),
includingPropertiesForKeys: nil
)
.map { $0.resolvingSymlinksInPath() }
.filter { !checkoutURLs.contains($0) }) ?? []

urls += (try? fileManager
.contentsOfDirectory(
at: self.directoryURL.appendingPathComponent(
Constants.binariesFolderPath, isDirectory: true
),
includingPropertiesForKeys: nil
)
.map { $0.resolvingSymlinksInPath() }
.filter { $0.pathExtension == VersionFile.pathExtension &&
!versionFileURLs.contains($0) }) ?? []

urls += Platform.supportedPlatforms
.flatMap { platform -> [URL] in
(try? fileManager
.contentsOfDirectory(
at: self.directoryURL.appendingPathComponent(
platform.relativePath, isDirectory: true
),
includingPropertiesForKeys: nil
)
.map { $0.resolvingSymlinksInPath() }
.filter { !binaryURLs.contains($0) }) ?? []
}
.on { self._projectEventsObserver.send(value: ProjectEvent.removingUnneededItem($0)) }
.flatMap(.merge, self.removeItem(at:))
.then(SignalProducer<(), CarthageError>.empty)

return SignalProducer(Set(urls))
}
.on { self._projectEventsObserver.send(value: ProjectEvent.removingUnneededItem($0)) }
.flatMap(.merge, self.removeItem(at:))
.then(SignalProducer<(), CarthageError>.empty)
}

/// Checks out the dependencies listed in the project's Cartfile.resolved,
Expand Down
4 changes: 4 additions & 0 deletions Source/CarthageKit/VersionFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ public func versionFileMatches(

let platformsToCheck = platforms.isEmpty ? Set<Platform>(Platform.supportedPlatforms) : platforms

let rootBinariesURL = rootDirectoryURL
.appendingPathComponent(Constants.binariesFolderPath, isDirectory: true)
.resolvingSymlinksInPath()

return swiftVersion(usingToolchain: toolchain)
.mapError { error in CarthageError.internalError(description: error.description) }
.flatMap(.concat) { localSwiftVersion in
Expand Down
13 changes: 0 additions & 13 deletions Tests/CarthageKitTests/ProjectSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -644,19 +644,6 @@ class ProjectSpec: QuickSpec {

expect(Set(removedItems)) == expectedItems
}

it("should fail if version files are missing") {
let directoryURL = baseDirectoryURL.appendingPathComponent("VersionFileMissing", isDirectory: true)
let project = Project(directoryURL: directoryURL)
var events = [ProjectEvent]()
project.projectEvents.observeValues { events.append($0) }

let dependencyName = "TestFramework3"
let error = project.removeUnneededItems().wait().error
let url = directoryURL.appendingPathComponent("Carthage/Build/.\(dependencyName).version", isDirectory: false)
expect(error) == CarthageError.versionFileNotFound(.git(GitURL(dependencyName)), url)
expect(events).to(beEmpty())
}
}

describe("transitiveDependencies") {
Expand Down

0 comments on commit aca72ab

Please sign in to comment.