From 51092960e171805831e2b463bf52618885c66362 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sat, 25 Nov 2017 00:14:12 +0800 Subject: [PATCH] Remove unnecessary code&&adjust indentation --- Source/CarthageKit/Algorithms.swift | 2 +- Source/CarthageKit/Constants.swift | 4 +- Source/CarthageKit/Dependency.swift | 2 +- Source/CarthageKit/Git.swift | 56 +++++++++-------- Source/CarthageKit/Project.swift | 93 ++++++++++++++--------------- Source/CarthageKit/Resolver.swift | 36 ++++++----- Source/CarthageKit/Xcode.swift | 2 +- 7 files changed, 95 insertions(+), 100 deletions(-) diff --git a/Source/CarthageKit/Algorithms.swift b/Source/CarthageKit/Algorithms.swift index 3f1c3732a0..2cff001f53 100644 --- a/Source/CarthageKit/Algorithms.swift +++ b/Source/CarthageKit/Algorithms.swift @@ -91,7 +91,7 @@ public func topologicalSort(_ graph: [Node: Set], nodes: /// Returns the set of nodes that the given node in the provided graph has as /// its incoming nodes, both directly and transitively. -private func transitiveIncomingNodes(_ graph: [Node: Set], node: Node) -> Set { +private func transitiveIncomingNodes(_ graph: [Node: Set], node: Node) -> Set { guard let nodes = graph[node] else { return Set() } diff --git a/Source/CarthageKit/Constants.swift b/Source/CarthageKit/Constants.swift index 839168d65b..e3148fac43 100644 --- a/Source/CarthageKit/Constants.swift +++ b/Source/CarthageKit/Constants.swift @@ -20,7 +20,7 @@ public struct Constants { } else { homePath = ("~/.carthage" as NSString).expandingTildeInPath } - return URL(fileURLWithPath: homePath, isDirectory:true) + return URL(fileURLWithPath: homePath, isDirectory: true) }() /// ~/Library/Caches/org.carthage.CarthageKit/ @@ -33,7 +33,7 @@ public struct Constants { let dependenciesURL = cachesURL.appendingPathComponent(Constants.bundleIdentifier, isDirectory: true) let dependenciesPath = dependenciesURL.absoluteString - if fileManager.fileExists(atPath: dependenciesPath, isDirectory:nil) { + if fileManager.fileExists(atPath: dependenciesPath, isDirectory: nil) { if fileManager.isWritableFile(atPath: dependenciesPath) { return Result(value: dependenciesURL) } else { diff --git a/Source/CarthageKit/Dependency.swift b/Source/CarthageKit/Dependency.swift index 24c084fbc1..a5ee33f129 100644 --- a/Source/CarthageKit/Dependency.swift +++ b/Source/CarthageKit/Dependency.swift @@ -42,7 +42,7 @@ extension Dependency { if urlString.contains(githubHostIdentifier) { let gitbubHostScanner = Scanner(string: urlString) - gitbubHostScanner.scanUpTo(githubHostIdentifier, into:nil) + gitbubHostScanner.scanUpTo(githubHostIdentifier, into: nil) gitbubHostScanner.scanString(githubHostIdentifier, into: nil) // find an SCP or URL path separator diff --git a/Source/CarthageKit/Git.swift b/Source/CarthageKit/Git.swift index 677d7d7fae..56655de78a 100644 --- a/Source/CarthageKit/Git.swift +++ b/Source/CarthageKit/Git.swift @@ -16,7 +16,7 @@ public struct FetchCache { /// Amount of time before a git repository is fetched again. Defaults to 1 minute public static var fetchCacheInterval: TimeInterval = 60.0 - private static var lastFetchTimes: [GitURL : TimeInterval] = [:] + private static var lastFetchTimes: [GitURL: TimeInterval] = [:] internal static func clearFetchTimes() { lastFetchTimes.removeAll() @@ -155,26 +155,25 @@ public func checkoutRepositoryToDirectory( _ workingDirectoryURL: URL, revision: String = "HEAD" ) -> SignalProducer<(), CarthageError> { - return SignalProducer - { () -> Result<[String: String], CarthageError> in - var environment = ProcessInfo.processInfo.environment - environment["GIT_WORK_TREE"] = workingDirectoryURL.path - return .success(environment) - } - .attempt { _ in - Result(attempt: { try FileManager.default.createDirectory(at: workingDirectoryURL, withIntermediateDirectories: true) }) - .mapError { - CarthageError.repositoryCheckoutFailed( - workingDirectoryURL: workingDirectoryURL, - reason: "Could not create working directory", - underlyingError: $0 - ) - } - } - .flatMap(.concat) { environment in - return launchGitTask([ "checkout", "--quiet", "--force", revision ], repositoryFileURL: repositoryFileURL, environment: environment) - } - .then(SignalProducer<(), CarthageError>.empty) + return SignalProducer { () -> Result<[String: String], CarthageError> in + var environment = ProcessInfo.processInfo.environment + environment["GIT_WORK_TREE"] = workingDirectoryURL.path + return .success(environment) + } + .attempt { _ in + Result(attempt: { try FileManager.default.createDirectory(at: workingDirectoryURL, withIntermediateDirectories: true) }) + .mapError { + CarthageError.repositoryCheckoutFailed( + workingDirectoryURL: workingDirectoryURL, + reason: "Could not create working directory", + underlyingError: $0 + ) + } + } + .flatMap(.concat) { environment in + return launchGitTask([ "checkout", "--quiet", "--force", revision ], repositoryFileURL: repositoryFileURL, environment: environment) + } + .then(SignalProducer<(), CarthageError>.empty) } /// Clones the given submodule into the working directory of its parent @@ -215,15 +214,14 @@ public func cloneSubmoduleInWorkingDirectory(_ submodule: Submodule, _ workingDi } } - return SignalProducer<(), CarthageError> - { () -> Result<(), CarthageError> in - repositoryCheck("remove submodule checkout") { - try FileManager.default.removeItem(at: submoduleDirectoryURL) - } + return SignalProducer<(), CarthageError> { () -> Result<(), CarthageError> in + repositoryCheck("remove submodule checkout") { + try FileManager.default.removeItem(at: submoduleDirectoryURL) } - .then(cloneRepository(submodule.url, workingDirectoryURL.appendingPathComponent(submodule.path), isBare: false)) - .then(checkoutSubmodule(submodule, submoduleDirectoryURL)) - .then(purgeGitDirectories) + } + .then(cloneRepository(submodule.url, workingDirectoryURL.appendingPathComponent(submodule.path), isBare: false)) + .then(checkoutSubmodule(submodule, submoduleDirectoryURL)) + .then(purgeGitDirectories) } /// Recursively checks out the given submodule's revision, in its working diff --git a/Source/CarthageKit/Project.swift b/Source/CarthageKit/Project.swift index b685cae246..878610d05f 100644 --- a/Source/CarthageKit/Project.swift +++ b/Source/CarthageKit/Project.swift @@ -643,7 +643,7 @@ public final class Project { // swiftlint:disable:this type_body_length /// Sends the URL of the dSYM after copying. public func copyDSYMToBuildFolderForFramework(_ frameworkURL: URL, fromDirectoryURL directoryURL: URL) -> SignalProducer { let destinationDirectoryURL = frameworkURL.deletingLastPathComponent() - return dSYMForFramework(frameworkURL, inDirectoryURL:directoryURL) + return dSYMForFramework(frameworkURL, inDirectoryURL: directoryURL) .copyFileURLsIntoDirectory(destinationDirectoryURL) } @@ -1213,57 +1213,56 @@ public func cloneOrFetch( let fileManager = FileManager.default let repositoryURL = repositoryFileURL(for: dependency, baseURL: destinationURL) - return SignalProducer - { - Result(at: destinationURL, attempt: { - try fileManager.createDirectory(at: $0, withIntermediateDirectories: true) - return dependency.gitURL(preferHTTPS: preferHTTPS)! - }) - } - .flatMap(.merge) { (remoteURL: GitURL) -> SignalProducer<(ProjectEvent?, URL), CarthageError> in - return isGitRepository(repositoryURL) - .flatMap(.merge) { isRepository -> SignalProducer<(ProjectEvent?, URL), CarthageError> in - if isRepository { - let fetchProducer: () -> SignalProducer<(ProjectEvent?, URL), CarthageError> = { - guard FetchCache.needsFetch(forURL: remoteURL) else { - return SignalProducer(value: (nil, repositoryURL)) - } - - return SignalProducer(value: (.fetching(dependency), repositoryURL)) - .concat( - fetchRepository(repositoryURL, remoteURL: remoteURL, refspec: "+refs/heads/*:refs/heads/*") - .then(SignalProducer<(ProjectEvent?, URL), CarthageError>.empty) - ) + return SignalProducer { + Result(at: destinationURL, attempt: { + try fileManager.createDirectory(at: $0, withIntermediateDirectories: true) + return dependency.gitURL(preferHTTPS: preferHTTPS)! + }) + } + .flatMap(.merge) { (remoteURL: GitURL) -> SignalProducer<(ProjectEvent?, URL), CarthageError> in + return isGitRepository(repositoryURL) + .flatMap(.merge) { isRepository -> SignalProducer<(ProjectEvent?, URL), CarthageError> in + if isRepository { + let fetchProducer: () -> SignalProducer<(ProjectEvent?, URL), CarthageError> = { + guard FetchCache.needsFetch(forURL: remoteURL) else { + return SignalProducer(value: (nil, repositoryURL)) } - // If we've already cloned the repo, check for the revision, possibly skipping an unnecessary fetch - if let commitish = commitish { - return SignalProducer.zip( - branchExistsInRepository(repositoryURL, pattern: commitish), - commitExistsInRepository(repositoryURL, revision: commitish) - ) - .flatMap(.concat) { branchExists, commitExists -> SignalProducer<(ProjectEvent?, URL), CarthageError> in - // If the given commitish is a branch, we should fetch. - if branchExists || !commitExists { - return fetchProducer() - } else { - return SignalProducer(value: (nil, repositoryURL)) - } - } - } else { - return fetchProducer() - } - } else { - // Either the directory didn't exist or it did but wasn't a git repository - // (Could happen if the process is killed during a previous directory creation) - // So we remove it, then clone - _ = try? fileManager.removeItem(at: repositoryURL) - return SignalProducer(value: (.cloning(dependency), repositoryURL)) + return SignalProducer(value: (.fetching(dependency), repositoryURL)) .concat( - cloneRepository(remoteURL, repositoryURL) + fetchRepository(repositoryURL, remoteURL: remoteURL, refspec: "+refs/heads/*:refs/heads/*") .then(SignalProducer<(ProjectEvent?, URL), CarthageError>.empty) ) } + + // If we've already cloned the repo, check for the revision, possibly skipping an unnecessary fetch + if let commitish = commitish { + return SignalProducer.zip( + branchExistsInRepository(repositoryURL, pattern: commitish), + commitExistsInRepository(repositoryURL, revision: commitish) + ) + .flatMap(.concat) { branchExists, commitExists -> SignalProducer<(ProjectEvent?, URL), CarthageError> in + // If the given commitish is a branch, we should fetch. + if branchExists || !commitExists { + return fetchProducer() + } else { + return SignalProducer(value: (nil, repositoryURL)) + } + } + } else { + return fetchProducer() + } + } else { + // Either the directory didn't exist or it did but wasn't a git repository + // (Could happen if the process is killed during a previous directory creation) + // So we remove it, then clone + _ = try? fileManager.removeItem(at: repositoryURL) + return SignalProducer(value: (.cloning(dependency), repositoryURL)) + .concat( + cloneRepository(remoteURL, repositoryURL) + .then(SignalProducer<(ProjectEvent?, URL), CarthageError>.empty) + ) } - } + } + } } diff --git a/Source/CarthageKit/Resolver.swift b/Source/CarthageKit/Resolver.swift index 4af18c301c..6fbe0ee4c6 100644 --- a/Source/CarthageKit/Resolver.swift +++ b/Source/CarthageKit/Resolver.swift @@ -188,32 +188,30 @@ public struct Resolver: ResolverProtocol { ) -> SignalProducer { let scheduler = QueueScheduler(qos: .default, name: "org.carthage.CarthageKit.Resolver.graphs") - return SignalProducer<(DependencyGraph, [DependencyNode]), CarthageError> - { () -> Result<(DependencyGraph, [DependencyNode]), CarthageError> in + return SignalProducer<(DependencyGraph, [DependencyNode]), CarthageError> { () -> Result<(DependencyGraph, [DependencyNode]), CarthageError> in var graph = inputGraph return graph .addNodes(nodes, dependenciesOf: dependencyOf) .map { newNodes in return (graph, newNodes) } - } - .flatMap(.concat) { graph, nodes -> SignalProducer in - return SignalProducer(nodes) - // Each producer represents all evaluations of one subtree. - .map { node in self.graphsForDependenciesOfNode(node, basedOnGraph: graph) } - .observe(on: scheduler) - .permute() - .flatMap(.concat) { graphs -> SignalProducer.Event, NoError> in - return SignalProducer - { - mergeGraphs([ inputGraph ] + graphs) - } - .materialize() + } + .flatMap(.concat) { graph, nodes -> SignalProducer in + return SignalProducer(nodes) + // Each producer represents all evaluations of one subtree. + .map { node in self.graphsForDependenciesOfNode(node, basedOnGraph: graph) } + .observe(on: scheduler) + .permute() + .flatMap(.concat) { graphs -> SignalProducer.Event, NoError> in + return SignalProducer { + mergeGraphs([ inputGraph ] + graphs) } - // Pass through resolution errors only if we never got - // a valid graph. - .dematerializeErrorsIfEmpty() - } + .materialize() + } + // Pass through resolution errors only if we never got + // a valid graph. + .dematerializeErrorsIfEmpty() + } } } diff --git a/Source/CarthageKit/Xcode.swift b/Source/CarthageKit/Xcode.swift index 76e6d01632..9d16a06383 100644 --- a/Source/CarthageKit/Xcode.swift +++ b/Source/CarthageKit/Xcode.swift @@ -373,7 +373,7 @@ private func mergeBuildProducts( let sourceModulesURL = SignalProducer(result: simulatorBuildSettings.relativeModulesPath.fanout(simulatorBuildSettings.builtProductsDirectoryURL)) .filter { $0.0 != nil } - .map { (modulesPath, productsURL) -> URL in + .map { modulesPath, productsURL in return productsURL.appendingPathComponent(modulesPath!) }