forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.swift
1592 lines (1421 loc) · 67.2 KB
/
Project.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// swiftlint:disable file_length
import Foundation
import Result
import ReactiveSwift
import Tentacle
import XCDBLD
import ReactiveTask
import struct SPMUtility.Version
/// Describes an event occurring to or with a project.
public enum ProjectEvent {
/// The project is beginning to clone.
case cloning(Dependency)
/// The project is beginning a fetch.
case fetching(Dependency)
/// The project is being checked out to the specified revision.
case checkingOut(Dependency, String)
/// The project is downloading a binary-only framework definition.
case downloadingBinaryFrameworkDefinition(Dependency, URL)
/// Any available binaries for the specified release of the project are
/// being downloaded. This may still be followed by `CheckingOut` event if
/// there weren't any viable binaries after all.
case downloadingBinaries(Dependency, String)
/// Downloading any available binaries of the project is being skipped,
/// because of a GitHub API request failure which is due to authentication
/// or rate-limiting.
case skippedDownloadingBinaries(Dependency, String)
/// Installing of a binary framework is being skipped because of an inability
/// to verify that it was built with a compatible Swift version.
case skippedInstallingBinaries(dependency: Dependency, error: Error)
/// Building the project is being skipped, since the project is not sharing
/// any framework schemes.
case skippedBuilding(Dependency, String)
/// Building the project is being skipped because it is cached.
case skippedBuildingCached(Dependency)
/// Rebuilding a cached project because of a version file/framework mismatch.
case rebuildingCached(Dependency)
/// Building an uncached project.
case buildingUncached(Dependency)
}
extension ProjectEvent: Equatable {
public static func == (lhs: ProjectEvent, rhs: ProjectEvent) -> Bool {
switch (lhs, rhs) {
case let (.cloning(left), .cloning(right)):
return left == right
case let (.fetching(left), .fetching(right)):
return left == right
case let (.checkingOut(leftIdentifier, leftRevision), .checkingOut(rightIdentifier, rightRevision)):
return leftIdentifier == rightIdentifier && leftRevision == rightRevision
case let (.downloadingBinaryFrameworkDefinition(leftIdentifier, leftURL), .downloadingBinaryFrameworkDefinition(rightIdentifier, rightURL)):
return leftIdentifier == rightIdentifier && leftURL == rightURL
case let (.downloadingBinaries(leftIdentifier, leftRevision), .downloadingBinaries(rightIdentifier, rightRevision)):
return leftIdentifier == rightIdentifier && leftRevision == rightRevision
case let (.skippedDownloadingBinaries(leftIdentifier, leftRevision), .skippedDownloadingBinaries(rightIdentifier, rightRevision)):
return leftIdentifier == rightIdentifier && leftRevision == rightRevision
case let (.skippedBuilding(leftIdentifier, leftRevision), .skippedBuilding(rightIdentifier, rightRevision)):
return leftIdentifier == rightIdentifier && leftRevision == rightRevision
default:
return false
}
}
}
/// Represents a project that is using Carthage.
public final class Project { // swiftlint:disable:this type_body_length
/// File URL to the root directory of the project.
public let directoryURL: URL
/// The file URL to the project's Cartfile.
public var cartfileURL: URL {
return directoryURL.appendingPathComponent(Constants.Project.cartfilePath, isDirectory: false)
}
/// The file URL to the project's Cartfile.resolved.
public var resolvedCartfileURL: URL {
return directoryURL.appendingPathComponent(Constants.Project.resolvedCartfilePath, isDirectory: false)
}
/// Whether to prefer HTTPS for cloning (vs. SSH).
public var preferHTTPS = true
/// Whether to use submodules for dependencies, or just check out their
/// working directories.
public var useSubmodules = false
/// Sends each event that occurs to a project underneath the receiver (or
/// the receiver itself).
public let projectEvents: Signal<ProjectEvent, NoError>
private let _projectEventsObserver: Signal<ProjectEvent, NoError>.Observer
public init(directoryURL: URL) {
precondition(directoryURL.isFileURL)
let (signal, observer) = Signal<ProjectEvent, NoError>.pipe()
projectEvents = signal
_projectEventsObserver = observer
self.directoryURL = directoryURL
}
private typealias CachedVersions = [Dependency: [PinnedVersion]]
private typealias CachedBinaryProjects = [URL: BinaryProject]
/// Caches versions to avoid expensive lookups, and unnecessary
/// fetching/cloning.
private var cachedVersions: CachedVersions = [:]
private let cachedVersionsQueue = SerialProducerQueue(name: "org.carthage.Constants.Project.cachedVersionsQueue")
// Cache the binary project definitions in memory to avoid redownloading during carthage operation
private var cachedBinaryProjects: CachedBinaryProjects = [:]
private let cachedBinaryProjectsQueue = SerialProducerQueue(name: "org.carthage.Constants.Project.cachedBinaryProjectsQueue")
private lazy var xcodeVersionDirectory: String = XcodeVersion.make()
.map { "\($0.version)_\($0.buildVersion)" } ?? "Unknown"
/// Attempts to load Cartfile or Cartfile.private from the given directory,
/// merging their dependencies.
public func loadCombinedCartfile() -> SignalProducer<Cartfile, CarthageError> {
let cartfileURL = directoryURL.appendingPathComponent(Constants.Project.cartfilePath, isDirectory: false)
let privateCartfileURL = directoryURL.appendingPathComponent(Constants.Project.privateCartfilePath, isDirectory: false)
func isNoSuchFileError(_ error: CarthageError) -> Bool {
switch error {
case let .readFailed(_, underlyingError):
if let underlyingError = underlyingError {
return underlyingError.domain == NSCocoaErrorDomain && underlyingError.code == NSFileReadNoSuchFileError
} else {
return false
}
default:
return false
}
}
let cartfile = SignalProducer { Cartfile.from(file: cartfileURL) }
.flatMapError { error -> SignalProducer<Cartfile, CarthageError> in
if isNoSuchFileError(error) && FileManager.default.fileExists(atPath: privateCartfileURL.path) {
return SignalProducer(value: Cartfile())
}
return SignalProducer(error: error)
}
let privateCartfile = SignalProducer { Cartfile.from(file: privateCartfileURL) }
.flatMapError { error -> SignalProducer<Cartfile, CarthageError> in
if isNoSuchFileError(error) {
return SignalProducer(value: Cartfile())
}
return SignalProducer(error: error)
}
return SignalProducer.zip(cartfile, privateCartfile)
.attemptMap { cartfile, privateCartfile -> Result<Cartfile, CarthageError> in
var cartfile = cartfile
let duplicateDeps = duplicateDependenciesIn(cartfile, privateCartfile).map { dependency in
return DuplicateDependency(
dependency: dependency,
locations: ["\(Constants.Project.cartfilePath)", "\(Constants.Project.privateCartfilePath)"]
)
}
if duplicateDeps.isEmpty {
cartfile.append(privateCartfile)
return .success(cartfile)
}
return .failure(.duplicateDependencies(duplicateDeps))
}
}
/// Reads the project's Cartfile.resolved.
public func loadResolvedCartfile() -> SignalProducer<ResolvedCartfile, CarthageError> {
return SignalProducer {
Result(catching: { try String(contentsOf: self.resolvedCartfileURL, encoding: .utf8) })
.mapError { .readFailed(self.resolvedCartfileURL, $0) }
.flatMap(ResolvedCartfile.from)
}
}
/// Writes the given Cartfile.resolved out to the project's directory.
public func writeResolvedCartfile(_ resolvedCartfile: ResolvedCartfile) -> Result<(), CarthageError> {
return Result(at: resolvedCartfileURL, attempt: {
try resolvedCartfile.description.write(to: $0, atomically: true, encoding: .utf8)
})
}
/// Limits the number of concurrent clones/fetches to the number of active
/// processors.
private let cloneOrFetchQueue = ConcurrentProducerQueue(name: "org.carthage.CarthageKit", limit: ProcessInfo.processInfo.activeProcessorCount)
/// Clones the given dependency to the global repositories folder, or fetches
/// inside it if it has already been cloned.
///
/// Returns a signal which will send the URL to the repository's folder on
/// disk once cloning or fetching has completed.
private func cloneOrFetchDependency(_ dependency: Dependency, commitish: String? = nil) -> SignalProducer<URL, CarthageError> {
return cloneOrFetch(dependency: dependency, preferHTTPS: self.preferHTTPS, commitish: commitish)
.on(value: { event, _ in
if let event = event {
self._projectEventsObserver.send(value: event)
}
})
.map { _, url in url }
.take(last: 1)
.startOnQueue(cloneOrFetchQueue)
}
func downloadBinaryFrameworkDefinition(binary: BinaryURL) -> SignalProducer<BinaryProject, CarthageError> {
return SignalProducer<Project.CachedBinaryProjects, CarthageError>(value: self.cachedBinaryProjects)
.flatMap(.merge) { binaryProjectsByURL -> SignalProducer<BinaryProject, CarthageError> in
if let binaryProject = binaryProjectsByURL[binary.url] {
return SignalProducer(value: binaryProject)
} else {
self._projectEventsObserver.send(value: .downloadingBinaryFrameworkDefinition(.binary(binary), binary.url))
return URLSession.shared.reactive.data(with: URLRequest(url: binary.url))
.mapError { CarthageError.readFailed(binary.url, $0 as NSError) }
.attemptMap { data, _ in
return BinaryProject.from(jsonData: data).mapError { error in
return CarthageError.invalidBinaryJSON(binary.url, error)
}
}
.on(value: { binaryProject in
self.cachedBinaryProjects[binary.url] = binaryProject
})
}
}
.startOnQueue(self.cachedBinaryProjectsQueue)
}
/// Sends all versions available for the given project.
///
/// This will automatically clone or fetch the project's repository as
/// necessary.
private func versions(for dependency: Dependency) -> SignalProducer<PinnedVersion, CarthageError> {
let fetchVersions: SignalProducer<PinnedVersion, CarthageError>
switch dependency {
case .git, .gitHub:
fetchVersions = cloneOrFetchDependency(dependency)
.flatMap(.merge) { repositoryURL in listTags(repositoryURL) }
.map { PinnedVersion($0) }
case let .binary(binary):
fetchVersions = downloadBinaryFrameworkDefinition(binary: binary)
.flatMap(.concat) { binaryProject -> SignalProducer<PinnedVersion, CarthageError> in
return SignalProducer(binaryProject.versions.keys)
}
}
return SignalProducer<Project.CachedVersions, CarthageError>(value: self.cachedVersions)
.flatMap(.merge) { versionsByDependency -> SignalProducer<PinnedVersion, CarthageError> in
if let versions = versionsByDependency[dependency] {
return SignalProducer(versions)
} else {
return fetchVersions
.collect()
.on(value: { newVersions in
self.cachedVersions[dependency] = newVersions
})
.flatMap(.concat) { versions in SignalProducer<PinnedVersion, CarthageError>(versions) }
}
}
.startOnQueue(cachedVersionsQueue)
.collect()
.flatMap(.concat) { versions -> SignalProducer<PinnedVersion, CarthageError> in
if versions.isEmpty {
return SignalProducer(error: .taggedVersionNotFound(dependency))
}
return SignalProducer(versions)
}
}
/// Produces the sub dependencies of the given dependency. Uses the checked out directory if able
private func dependencySet(for dependency: Dependency, version: PinnedVersion) -> SignalProducer<Set<Dependency>, CarthageError> {
return self.dependencies(for: dependency, version: version, tryCheckoutDirectory: true)
.map { $0.0 }
.collect()
.map { Set($0) }
.concat(value: Set())
.take(first: 1)
}
/// Loads the dependencies for the given dependency, at the given version.
private func dependencies(for dependency: Dependency, version: PinnedVersion) -> SignalProducer<(Dependency, VersionSpecifier), CarthageError> {
return self.dependencies(for: dependency, version: version, tryCheckoutDirectory: false)
}
/// Loads the dependencies for the given dependency, at the given version. Optionally can attempt to read from the Checkout directory
private func dependencies(
for dependency: Dependency,
version: PinnedVersion,
tryCheckoutDirectory: Bool
) -> SignalProducer<(Dependency, VersionSpecifier), CarthageError> {
switch dependency {
case .git, .gitHub:
let revision = version.commitish
let cartfileFetch: SignalProducer<Cartfile, CarthageError> = self.cloneOrFetchDependency(dependency, commitish: revision)
.flatMap(.concat) { repositoryURL in
return contentsOfFileInRepository(repositoryURL, Constants.Project.cartfilePath, revision: revision)
}
.flatMapError { _ in .empty }
.attemptMap(Cartfile.from(string:))
let cartfileSource: SignalProducer<Cartfile, CarthageError>
if tryCheckoutDirectory {
let dependencyURL = self.directoryURL.appendingPathComponent(dependency.relativePath)
cartfileSource = SignalProducer<Bool, NoError> { () -> Bool in
var isDirectory: ObjCBool = false
return FileManager.default.fileExists(atPath: dependencyURL.path, isDirectory: &isDirectory) && isDirectory.boolValue
}
.flatMap(.concat) { directoryExists -> SignalProducer<Cartfile, CarthageError> in
if directoryExists {
return SignalProducer(result: Cartfile.from(file: dependencyURL.appendingPathComponent(Constants.Project.cartfilePath)))
.flatMapError { _ in .empty }
} else {
return cartfileFetch
}
}
.flatMapError { _ in .empty }
} else {
cartfileSource = cartfileFetch
}
return cartfileSource
.flatMap(.concat) { cartfile -> SignalProducer<(Dependency, VersionSpecifier), CarthageError> in
return SignalProducer(cartfile.dependencies.map { ($0.0, $0.1) })
}
case .binary:
// Binary-only frameworks do not support dependencies
return .empty
}
}
/// Finds all the transitive dependencies for the dependencies to checkout.
func transitiveDependencies(
_ dependenciesToCheckout: [String]?,
resolvedCartfile: ResolvedCartfile
) -> SignalProducer<[String], CarthageError> {
return SignalProducer(value: resolvedCartfile)
.map { resolvedCartfile -> [(Dependency, PinnedVersion)] in
return resolvedCartfile.dependencies
.filter { dep, _ in dependenciesToCheckout?.contains(dep.name) ?? false }
}
.flatMap(.merge) { dependencies -> SignalProducer<[String], CarthageError> in
return SignalProducer<(Dependency, PinnedVersion), CarthageError>(dependencies)
.flatMap(.merge) { dependency, version -> SignalProducer<(Dependency, VersionSpecifier), CarthageError> in
return self.dependencies(for: dependency, version: version)
}
.map { $0.0.name }
.collect()
}
}
/// Finds the required dependencies and their corresponding version specifiers for each dependency in Cartfile.resolved.
func requirementsByDependency(
resolvedCartfile: ResolvedCartfile,
tryCheckoutDirectory: Bool
) -> SignalProducer<CompatibilityInfo.Requirements, CarthageError> {
return SignalProducer(resolvedCartfile.dependencies)
.flatMap(.concurrent(limit: 4)) { dependency, pinnedVersion -> SignalProducer<(Dependency, (Dependency, VersionSpecifier)), CarthageError> in
return self.dependencies(for: dependency, version: pinnedVersion, tryCheckoutDirectory: tryCheckoutDirectory)
.map { (dependency, $0) }
}
.collect()
.flatMap(.merge) { dependencyAndRequirements -> SignalProducer<CompatibilityInfo.Requirements, CarthageError> in
var dict: CompatibilityInfo.Requirements = [:]
for (dependency, requirement) in dependencyAndRequirements {
let (requiredDependency, requiredVersion) = requirement
var requirementsDict = dict[dependency] ?? [:]
if requirementsDict[requiredDependency] != nil {
return SignalProducer(error: .duplicateDependencies([DuplicateDependency(dependency: requiredDependency, locations: [])]))
}
requirementsDict[requiredDependency] = requiredVersion
dict[dependency] = requirementsDict
}
return SignalProducer(value: dict)
}
}
/// Attempts to resolve a Git reference to a version.
private func resolvedGitReference(_ dependency: Dependency, reference: String) -> SignalProducer<PinnedVersion, CarthageError> {
let repositoryURL = repositoryFileURL(for: dependency)
return cloneOrFetchDependency(dependency, commitish: reference)
.flatMap(.concat) { _ in
return resolveTagInRepository(repositoryURL, reference)
.map { _ in
// If the reference is an exact tag, resolves it to the tag.
return PinnedVersion(reference)
}
.flatMapError { _ in
return resolveReferenceInRepository(repositoryURL, reference)
.map(PinnedVersion.init)
}
}
}
/// Attempts to determine the latest satisfiable version of the project's
/// Carthage dependencies.
///
/// This will fetch dependency repositories as necessary, but will not check
/// them out into the project's working directory.
public func updatedResolvedCartfile(_ dependenciesToUpdate: [String]? = nil, resolver: ResolverProtocol) -> SignalProducer<ResolvedCartfile, CarthageError> {
let resolvedCartfile: SignalProducer<ResolvedCartfile?, CarthageError> = loadResolvedCartfile()
.map(Optional.init)
.flatMapError { _ in .init(value: nil) }
return SignalProducer
.zip(loadCombinedCartfile(), resolvedCartfile)
.flatMap(.merge) { cartfile, resolvedCartfile in
return resolver.resolve(
dependencies: cartfile.dependencies,
lastResolved: resolvedCartfile?.dependencies,
dependenciesToUpdate: dependenciesToUpdate
)
}
.map(ResolvedCartfile.init)
}
/// Attempts to determine the latest version (whether satisfiable or not)
/// of the project's Carthage dependencies.
///
/// This will fetch dependency repositories as necessary, but will not check
/// them out into the project's working directory.
private func latestDependencies(resolver: ResolverProtocol) -> SignalProducer<[Dependency: PinnedVersion], CarthageError> {
func resolve(prefersGitReference: Bool) -> SignalProducer<[Dependency: PinnedVersion], CarthageError> {
return SignalProducer
.combineLatest(loadCombinedCartfile(), loadResolvedCartfile())
.map { cartfile, resolvedCartfile in
resolvedCartfile
.dependencies
.reduce(into: [Dependency: VersionSpecifier]()) { result, group in
let dependency = group.key
let specifier: VersionSpecifier
if case let .gitReference(value)? = cartfile.dependencies[dependency], prefersGitReference {
specifier = .gitReference(value)
} else {
specifier = .any
}
result[dependency] = specifier
}
}
.flatMap(.merge) { resolver.resolve(dependencies: $0, lastResolved: nil, dependenciesToUpdate: nil) }
}
return resolve(prefersGitReference: false).flatMapError { error in
switch error {
case .taggedVersionNotFound:
return resolve(prefersGitReference: true)
default:
return SignalProducer(error: error)
}
}
}
public typealias OutdatedDependency = (Dependency, PinnedVersion, PinnedVersion, PinnedVersion)
/// Attempts to determine which of the project's Carthage
/// dependencies are out of date.
///
/// This will fetch dependency repositories as necessary, but will not check
/// them out into the project's working directory.
public func outdatedDependencies(_ includeNestedDependencies: Bool, useNewResolver: Bool = true, resolver: ResolverProtocol? = nil) -> SignalProducer<[OutdatedDependency], CarthageError> {
let resolverType: ResolverProtocol.Type
if useNewResolver {
resolverType = NewResolver.self
} else {
resolverType = Resolver.self
}
let dependencies: (Dependency, PinnedVersion) -> SignalProducer<(Dependency, VersionSpecifier), CarthageError>
if includeNestedDependencies {
dependencies = self.dependencies(for:version:)
} else {
dependencies = { _, _ in .empty }
}
let resolver = resolver ?? resolverType.init(
versionsForDependency: versions(for:),
dependenciesForDependency: dependencies,
resolvedGitReference: resolvedGitReference
)
let outdatedDependencies = SignalProducer
.combineLatest(
loadResolvedCartfile(),
updatedResolvedCartfile(resolver: resolver),
latestDependencies(resolver: resolver)
)
.map { ($0.dependencies, $1.dependencies, $2) }
.map { currentDependencies, updatedDependencies, latestDependencies -> [OutdatedDependency] in
return updatedDependencies.compactMap { project, version -> OutdatedDependency? in
if let resolved = currentDependencies[project], let latest = latestDependencies[project], resolved != version || resolved != latest {
if Version.from(resolved).value == nil, version == resolved {
// If resolved version is not a semantic version but a commit
// it is a false-positive if `version` and `resolved` are the same
return nil
}
return (project, resolved, version, latest)
} else {
return nil
}
}
}
if includeNestedDependencies {
return outdatedDependencies
}
return SignalProducer
.combineLatest(
outdatedDependencies,
loadCombinedCartfile()
)
.map { oudatedDependencies, combinedCartfile -> [OutdatedDependency] in
return oudatedDependencies.filter { project, _, _, _ in
return combinedCartfile.dependencies[project] != nil
}
}
}
/// Updates the dependencies of the project to the latest version. The
/// changes will be reflected in Cartfile.resolved, and also in the working
/// directory checkouts if the given parameter is true.
public func updateDependencies(
shouldCheckout: Bool = true,
useNewResolver: Bool = false,
buildOptions: BuildOptions,
dependenciesToUpdate: [String]? = nil
) -> SignalProducer<(), CarthageError> {
let resolverType: ResolverProtocol.Type
if useNewResolver {
resolverType = NewResolver.self
} else {
resolverType = Resolver.self
}
let resolver = resolverType.init(
versionsForDependency: versions(for:),
dependenciesForDependency: dependencies(for:version:),
resolvedGitReference: resolvedGitReference
)
return updatedResolvedCartfile(dependenciesToUpdate, resolver: resolver)
.attemptMap { resolvedCartfile -> Result<(), CarthageError> in
return self.writeResolvedCartfile(resolvedCartfile)
}
.then(shouldCheckout ? checkoutResolvedDependencies(dependenciesToUpdate, buildOptions: buildOptions) : .empty)
}
/// Constructs the file:// URL at which a given .framework
/// will be found. Depends on the location of the current project.
private func frameworkURLInCarthageBuildFolder(
forPlatform platform: Platform,
frameworkNameAndExtension: String
) -> Result<URL, CarthageError> {
guard let lastComponent = URL(string: frameworkNameAndExtension)?.pathExtension,
lastComponent == "framework" else {
return .failure(.internalError(description: "\(frameworkNameAndExtension) is not a valid framework identifier"))
}
guard let destinationURLInWorkingDir = platform
.relativeURL?
.appendingPathComponent(frameworkNameAndExtension, isDirectory: true) else {
return .failure(.internalError(description: "failed to construct framework destination url from \(platform) and \(frameworkNameAndExtension)"))
}
return .success(self
.directoryURL
.appendingPathComponent(destinationURLInWorkingDir.path, isDirectory: true)
.standardizedFileURL)
}
/// Unzips the file at the given URL and copies the frameworks, DSYM and
/// bcsymbolmap files into the corresponding folders for the project. This
/// step will also check framework compatibility and create a version file
/// for the given frameworks.
///
/// Sends the temporary URL of the unzipped directory
private func unarchiveAndCopyBinaryFrameworks(
zipFile: URL,
projectName: String,
pinnedVersion: PinnedVersion,
toolchain: String?
) -> SignalProducer<URL, CarthageError> {
// Helper type
typealias SourceURLAndDestinationURL = (frameworkSourceURL: URL, frameworkDestinationURL: URL)
// Returns the unique pairs in the input array
// or the duplicate keys by .frameworkDestinationURL
func uniqueSourceDestinationPairs(
_ sourceURLAndDestinationURLpairs: [SourceURLAndDestinationURL]
) -> Result<[SourceURLAndDestinationURL], CarthageError> {
let destinationMap = sourceURLAndDestinationURLpairs
.reduce(into: [URL: [URL]]()) { result, pair in
result[pair.frameworkDestinationURL] =
(result[pair.frameworkDestinationURL] ?? []) + [pair.frameworkSourceURL]
}
let dupes = destinationMap.filter { $0.value.count > 1 }
guard dupes.count == 0 else {
return .failure(CarthageError
.duplicatesInArchive(duplicates: CarthageError
.DuplicatesInArchive(dictionary: dupes)))
}
let uniquePairs = destinationMap
.filter { $0.value.count == 1}
.map { SourceURLAndDestinationURL(frameworkSourceURL: $0.value.first!,
frameworkDestinationURL: $0.key)}
return .success(uniquePairs)
}
return SignalProducer<URL, CarthageError>(value: zipFile)
.flatMap(.concat, unarchive(archive:))
.flatMap(.concat) { directoryURL -> SignalProducer<URL, CarthageError> in
// For all frameworks in the directory where the archive has been expanded
return frameworksInDirectory(directoryURL)
.collect()
// Check if multiple frameworks resolve to the same unique destination URL in the Carthage/Build/ folder.
// This is needed because frameworks might overwrite each others.
.flatMap(.merge) { frameworksUrls -> SignalProducer<SourceURLAndDestinationURL, CarthageError> in
return SignalProducer<URL, CarthageError>(frameworksUrls)
.flatMap(.merge) { url -> SignalProducer<URL, CarthageError> in
return platformForFramework(url)
.attemptMap { self.frameworkURLInCarthageBuildFolder(forPlatform: $0,
frameworkNameAndExtension: url.lastPathComponent) }
}
.collect()
.flatMap(.merge) { destinationUrls -> SignalProducer<SourceURLAndDestinationURL, CarthageError> in
let frameworkUrlAndDestinationUrlPairs = zip(frameworksUrls.map{$0.standardizedFileURL},
destinationUrls.map{$0.standardizedFileURL})
.map { SourceURLAndDestinationURL(frameworkSourceURL:$0,
frameworkDestinationURL: $1) }
return uniqueSourceDestinationPairs(frameworkUrlAndDestinationUrlPairs)
.producer
.flatMap(.merge) { SignalProducer($0) }
}
}
// Check if the framework are compatible with the current Swift version
.flatMap(.merge) { pair -> SignalProducer<SourceURLAndDestinationURL, CarthageError> in
return checkFrameworkCompatibility(pair.frameworkSourceURL, usingToolchain: toolchain)
.mapError { error in CarthageError.internalError(description: error.description) }
.then(SignalProducer<SourceURLAndDestinationURL, CarthageError>(value: pair))
}
// If the framework is compatible copy it over to the destination folder in Carthage/Build
.flatMap(.merge) { pair -> SignalProducer<URL, CarthageError> in
return SignalProducer<URL, CarthageError>(value: pair.frameworkSourceURL)
.copyFileURLsIntoDirectory(pair.frameworkDestinationURL.deletingLastPathComponent())
.then(SignalProducer<URL, CarthageError>(value: pair.frameworkDestinationURL))
}
// Copy .dSYM & .bcsymbolmap too
.flatMap(.merge) { frameworkDestinationURL -> SignalProducer<URL, CarthageError> in
return self.copyDSYMToBuildFolderForFramework(frameworkDestinationURL, fromDirectoryURL: directoryURL)
.then(self.copyBCSymbolMapsToBuildFolderForFramework(frameworkDestinationURL, fromDirectoryURL: directoryURL))
.then(SignalProducer(value: frameworkDestinationURL))
}
.collect()
// Write the .version file
.flatMap(.concat) { frameworkURLs -> SignalProducer<(), CarthageError> in
return self.createVersionFilesForFrameworks(
frameworkURLs,
fromDirectoryURL: directoryURL,
projectName: projectName,
commitish: pinnedVersion.commitish
)
}
.then(SignalProducer<URL, CarthageError>(value: directoryURL))
}
}
/// Removes the file located at the given URL
///
/// Sends empty value on successful removal
private func removeItem(at url: URL) -> SignalProducer<(), CarthageError> {
return SignalProducer {
Result(at: url, attempt: FileManager.default.removeItem(at:))
}
}
/// Installs binaries and debug symbols for the given project, if available.
///
/// Sends a boolean indicating whether binaries were installed.
private func installBinaries(for dependency: Dependency, pinnedVersion: PinnedVersion, toolchain: String?) -> SignalProducer<Bool, CarthageError> {
switch dependency {
case let .gitHub(server, repository):
let client = Client(server: server)
return self.downloadMatchingBinaries(for: dependency, pinnedVersion: pinnedVersion, fromRepository: repository, client: client)
.flatMapError { error -> SignalProducer<URL, CarthageError> in
if !client.isAuthenticated {
return SignalProducer(error: error)
}
return self.downloadMatchingBinaries(
for: dependency,
pinnedVersion: pinnedVersion,
fromRepository: repository,
client: Client(server: server, isAuthenticated: false)
)
}
.flatMap(.concat) {
return self.unarchiveAndCopyBinaryFrameworks(zipFile: $0, projectName: dependency.name, pinnedVersion: pinnedVersion, toolchain: toolchain)
}
.flatMap(.concat) { self.removeItem(at: $0) }
.map { true }
.flatMapError { error in
self._projectEventsObserver.send(value: .skippedInstallingBinaries(dependency: dependency, error: error))
return SignalProducer(value: false)
}
.concat(value: false)
.take(first: 1)
case .git, .binary:
return SignalProducer(value: false)
}
}
/// Downloads any binaries and debug symbols that may be able to be used
/// instead of a repository checkout.
///
/// Sends the URL to each downloaded zip, after it has been moved to a
/// less temporary location.
private func downloadMatchingBinaries(
for dependency: Dependency,
pinnedVersion: PinnedVersion,
fromRepository repository: Repository,
client: Client
) -> SignalProducer<URL, CarthageError> {
return client.execute(repository.release(forTag: pinnedVersion.commitish))
.map { _, release in release }
.filter { release in
return !release.isDraft && !release.assets.isEmpty
}
.flatMapError { error -> SignalProducer<Release, CarthageError> in
switch error {
case .doesNotExist:
return .empty
case let .apiError(_, _, error):
// Log the GitHub API request failure, not to error out,
// because that should not be fatal error.
self._projectEventsObserver.send(value: .skippedDownloadingBinaries(dependency, error.message))
return .empty
default:
return SignalProducer(error: .gitHubAPIRequestFailed(error))
}
}
.on(value: { release in
self._projectEventsObserver.send(value: .downloadingBinaries(dependency, release.nameWithFallback))
})
.flatMap(.concat) { release -> SignalProducer<URL, CarthageError> in
return SignalProducer<Release.Asset, CarthageError>(release.assets)
.filter { asset in
if asset.name.range(of: Constants.Project.binaryAssetPattern) == nil {
return false
}
return Constants.Project.binaryAssetContentTypes.contains(asset.contentType)
}
.flatMap(.concat) { asset -> SignalProducer<URL, CarthageError> in
let fileURL = fileURLToCachedBinary(dependency, release, asset)
if FileManager.default.fileExists(atPath: fileURL.path) {
return SignalProducer(value: fileURL)
} else {
return client.download(asset: asset)
.mapError(CarthageError.gitHubAPIRequestFailed)
.flatMap(.concat) { downloadURL in cacheDownloadedBinary(downloadURL, toURL: fileURL) }
}
}
}
}
/// Copies the DSYM matching the given framework and contained within the
/// given directory URL to the directory that the framework resides within.
///
/// If no dSYM is found for the given framework, completes with no values.
///
/// Sends the URL of the dSYM after copying.
public func copyDSYMToBuildFolderForFramework(_ frameworkURL: URL, fromDirectoryURL directoryURL: URL) -> SignalProducer<URL, CarthageError> {
let destinationDirectoryURL = frameworkURL.deletingLastPathComponent()
return dSYMForFramework(frameworkURL, inDirectoryURL: directoryURL)
.copyFileURLsIntoDirectory(destinationDirectoryURL)
}
/// Copies any *.bcsymbolmap files matching the given framework and contained
/// within the given directory URL to the directory that the framework
/// resides within.
///
/// If no bcsymbolmap files are found for the given framework, completes with
/// no values.
///
/// Sends the URLs of the bcsymbolmap files after copying.
public func copyBCSymbolMapsToBuildFolderForFramework(_ frameworkURL: URL, fromDirectoryURL directoryURL: URL) -> SignalProducer<URL, CarthageError> {
let destinationDirectoryURL = frameworkURL.deletingLastPathComponent()
return BCSymbolMapsForFramework(frameworkURL, inDirectoryURL: directoryURL)
.copyFileURLsIntoDirectory(destinationDirectoryURL)
}
/// Creates a .version file for all of the provided frameworks.
public func createVersionFilesForFrameworks(
_ frameworkURLs: [URL],
fromDirectoryURL directoryURL: URL,
projectName: String,
commitish: String
) -> SignalProducer<(), CarthageError> {
return createVersionFileForCommitish(commitish, dependencyName: projectName, buildProducts: frameworkURLs, rootDirectoryURL: self.directoryURL)
}
private let gitOperationQueue = SerialProducerQueue(name: "org.carthage.Constants.Project.gitOperationQueue")
/// Checks out the given dependency into its intended working directory,
/// cloning it first if need be.
private func checkoutOrCloneDependency(
_ dependency: Dependency,
version: PinnedVersion,
submodulesByPath: [String: Submodule]
) -> SignalProducer<(), CarthageError> {
let revision = version.commitish
return cloneOrFetchDependency(dependency, commitish: revision)
.flatMap(.merge) { repositoryURL -> SignalProducer<(), CarthageError> in
let workingDirectoryURL = self.directoryURL.appendingPathComponent(dependency.relativePath, isDirectory: true)
/// The submodule for an already existing submodule at dependency project’s path
/// or the submodule to be added at this path given the `--use-submodules` flag.
let submodule: Submodule?
if var foundSubmodule = submodulesByPath[dependency.relativePath] {
foundSubmodule.url = dependency.gitURL(preferHTTPS: self.preferHTTPS)!
foundSubmodule.sha = revision
submodule = foundSubmodule
} else if self.useSubmodules {
submodule = Submodule(name: dependency.relativePath, path: dependency.relativePath, url: dependency.gitURL(preferHTTPS: self.preferHTTPS)!, sha: revision)
} else {
submodule = nil
}
let symlinkCheckoutPaths = self.symlinkCheckoutPaths(for: dependency, version: version, withRepository: repositoryURL, atRootDirectory: self.directoryURL)
if let submodule = submodule {
// In the presence of `submodule` for `dependency` — before symlinking, (not after) — add submodule and its submodules:
// `dependency`, subdependencies that are submodules, and non-Carthage-housed submodules.
return addSubmoduleToRepository(self.directoryURL, submodule, GitURL(repositoryURL.path))
.startOnQueue(self.gitOperationQueue)
.then(symlinkCheckoutPaths)
} else {
return checkoutRepositoryToDirectory(repositoryURL, workingDirectoryURL, revision: revision)
// For checkouts of “ideally bare” repositories of `dependency`, we add its submodules by cloning ourselves, after symlinking.
.then(symlinkCheckoutPaths)
.then(
submodulesInRepository(repositoryURL, revision: revision)
.flatMap(.merge) {
cloneSubmoduleInWorkingDirectory($0, workingDirectoryURL)
}
)
}
}
.on(started: {
self._projectEventsObserver.send(value: .checkingOut(dependency, revision))
})
}
public func buildOrderForResolvedCartfile(
_ cartfile: ResolvedCartfile,
dependenciesToInclude: [String]? = nil
) -> SignalProducer<(Dependency, PinnedVersion), CarthageError> {
// swiftlint:disable:next nesting
typealias DependencyGraph = [Dependency: Set<Dependency>]
// A resolved cartfile already has all the recursive dependencies. All we need to do is sort
// out the relationships between them. Loading the cartfile will each will give us its
// dependencies. Building a recursive lookup table with this information will let us sort
// dependencies before the projects that depend on them.
return SignalProducer<(Dependency, PinnedVersion), CarthageError>(cartfile.dependencies.map { ($0, $1) })
.flatMap(.merge) { (dependency: Dependency, version: PinnedVersion) -> SignalProducer<DependencyGraph, CarthageError> in
return self.dependencySet(for: dependency, version: version)
.map { dependencies in
[dependency: dependencies]
}
}
.reduce(into: [:]) { (working: inout DependencyGraph, next: DependencyGraph) in
for (key, value) in next {
working.updateValue(value, forKey: key)
}
}
.flatMap(.latest) { (graph: DependencyGraph) -> SignalProducer<(Dependency, PinnedVersion), CarthageError> in
let dependenciesToInclude = Set(graph
.map { dependency, _ in dependency }
.filter { dependency in dependenciesToInclude?.contains(dependency.name) ?? false })
guard let sortedDependencies = topologicalSort(graph, nodes: dependenciesToInclude) else { // swiftlint:disable:this single_line_guard
return SignalProducer(error: .dependencyCycle(graph))
}
let sortedPinnedDependencies = cartfile.dependencies.keys
.filter { dependency in sortedDependencies.contains(dependency) }
.sorted { left, right in sortedDependencies.index(of: left)! < sortedDependencies.index(of: right)! }
.map { ($0, cartfile.dependencies[$0]!) }
return SignalProducer(sortedPinnedDependencies)
}
}
/// Checks out the dependencies listed in the project's Cartfile.resolved,
/// optionally they are limited by the given list of dependency names.
public func checkoutResolvedDependencies(_ dependenciesToCheckout: [String]? = nil, buildOptions: BuildOptions?) -> SignalProducer<(), CarthageError> {
/// Determine whether the repository currently holds any submodules (if
/// it even is a repository).
let submodulesSignal = submodulesInRepository(self.directoryURL)
.reduce(into: [:]) { (submodulesByPath: inout [String: Submodule], submodule) in
submodulesByPath[submodule.path] = submodule
}
return loadResolvedCartfile()
.flatMap(.latest) { resolvedCartfile -> SignalProducer<([String]?, ResolvedCartfile), CarthageError> in
guard let dependenciesToCheckout = dependenciesToCheckout else {
return SignalProducer(value: (nil, resolvedCartfile))
}
return self
.transitiveDependencies(dependenciesToCheckout, resolvedCartfile: resolvedCartfile)
.map { (dependenciesToCheckout + $0, resolvedCartfile) }
}
.map { dependenciesToCheckout, resolvedCartfile -> [(Dependency, PinnedVersion)] in
return resolvedCartfile.dependencies
.filter { dep, _ in dependenciesToCheckout?.contains(dep.name) ?? true }
}
.zip(with: submodulesSignal)
.flatMap(.merge) { dependencies, submodulesByPath -> SignalProducer<(), CarthageError> in
return SignalProducer<(Dependency, PinnedVersion), CarthageError>(dependencies)
.flatMap(.concurrent(limit: 4)) { dependency, version -> SignalProducer<(), CarthageError> in
switch dependency {
case .git, .gitHub:
return self.checkoutOrCloneDependency(dependency, version: version, submodulesByPath: submodulesByPath)
case .binary:
return .empty
}
}
}
.then(SignalProducer<(), CarthageError>.empty)
}
private func installBinariesForBinaryProject(
binary: BinaryURL,
pinnedVersion: PinnedVersion,
projectName: String,
toolchain: String?
) -> SignalProducer<(), CarthageError> {
return SignalProducer<Version, ScannableError>(result: Version.from(pinnedVersion))
.mapError { CarthageError(scannableError: $0) }
.combineLatest(with: self.downloadBinaryFrameworkDefinition(binary: binary))
.attemptMap { semanticVersion, binaryProject -> Result<(Version, URL), CarthageError> in
guard let frameworkURL = binaryProject.versions[pinnedVersion] else {
return .failure(CarthageError.requiredVersionNotFound(Dependency.binary(binary), VersionSpecifier.exactly(semanticVersion)))
}
return .success((semanticVersion, frameworkURL))
}
.flatMap(.concat) { semanticVersion, frameworkURL in
return self.downloadBinary(dependency: Dependency.binary(binary), version: semanticVersion, url: frameworkURL)
}
.flatMap(.concat) { self.unarchiveAndCopyBinaryFrameworks(zipFile: $0, projectName: projectName, pinnedVersion: pinnedVersion, toolchain: toolchain) }
.flatMap(.concat) { self.removeItem(at: $0) }
}
/// Downloads the binary only framework file. Sends the URL to each downloaded zip, after it has been moved to a
/// less temporary location.
private func downloadBinary(dependency: Dependency, version: Version, url: URL) -> SignalProducer<URL, CarthageError> {
let fileName = url.lastPathComponent
let fileURL = fileURLToCachedBinaryDependency(dependency, version, fileName)
if FileManager.default.fileExists(atPath: fileURL.path) {
return SignalProducer(value: fileURL)
} else {
return URLSession.shared.reactive.download(with: URLRequest(url: url))