Skip to content

Commit 716b045

Browse files
committed
Zip Carthage refactor (#5080)
1 parent f76ea00 commit 716b045

File tree

9 files changed

+305
-218
lines changed

9 files changed

+305
-218
lines changed

ZipBuilder/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ you can fix issues or dig in without having to dig too deep into the code.
88

99
## Zip Builder
1010

11-
This is a small Swift Package Manager project that allows users to package an iOS Zip file of binary
11+
This is a Swift Package Manager project that allows users to package an iOS Zip file of binary
1212
packages.
1313

1414
### Requirements
1515

1616
In order to build the Zip file, you will need:
1717

18-
- Xcode 10.1
18+
- Xcode 11.0
1919
- CocoaPods
2020
- An internet connection to fetch CocoaPods
2121

ZipBuilder/Sources/ZipBuilder/CarthageUtils.swift

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,56 @@ import Foundation
2222
enum CarthageUtils {}
2323

2424
extension CarthageUtils {
25+
/// Package all required files for a Carthage release.
26+
///
27+
/// - Parameters:
28+
/// - templateDir: The template project directory, contains the dummy Firebase library.
29+
/// - carthageJSONDir: Location of directory containing all JSON Carthage manifests.
30+
/// - artifacts: Release Artifacts from build.
31+
/// - rcNumber: The RC number.
32+
/// - Returns: The path to the root of the Carthage installation.
33+
static func packageCarthageRelease(templateDir: URL,
34+
carthageJSONDir: URL,
35+
artifacts: ZipBuilder.ReleaseArtifacts,
36+
rcNumber: Int?) -> URL? {
37+
guard let zipLocation = artifacts.carthageDir else { return nil }
38+
39+
do {
40+
print("Creating Carthage release...")
41+
let carthagePath =
42+
zipLocation.deletingLastPathComponent().appendingPathComponent("carthage_build")
43+
// Create a copy of the release directory since we'll be modifying it.
44+
let fileManager = FileManager.default
45+
fileManager.removeIfExists(at: carthagePath)
46+
try fileManager.copyItem(at: zipLocation, to: carthagePath)
47+
48+
// Package the Carthage distribution with the current directory structure.
49+
let carthageDir = zipLocation.deletingLastPathComponent().appendingPathComponent("carthage")
50+
fileManager.removeIfExists(at: carthageDir)
51+
var output = carthageDir.appendingPathComponent(artifacts.firebaseVersion)
52+
if let rcNumber = args.rcNumber {
53+
output.appendPathComponent("rc\(rcNumber)")
54+
} else {
55+
output.appendPathComponent("latest-non-rc")
56+
}
57+
try fileManager.createDirectory(at: output, withIntermediateDirectories: true)
58+
generateCarthageRelease(fromPackagedDir: carthagePath,
59+
templateDir: templateDir,
60+
jsonDir: carthageJSONDir,
61+
artifacts: artifacts,
62+
outputDir: output)
63+
64+
// Remove the duplicated Carthage build directory.
65+
fileManager.removeIfExists(at: carthagePath)
66+
print("Done creating Carthage release! Files written to \(output)")
67+
68+
// Save the directory for later copying.
69+
return carthageDir
70+
} catch {
71+
fatalError("Could not copy output directory for Carthage build: \(error)")
72+
}
73+
}
74+
2575
/// Generates all required files for a Carthage release.
2676
///
2777
/// - Parameters:
@@ -31,18 +81,19 @@ extension CarthageUtils {
3181
/// - firebaseVersion: The version of the Firebase pod.
3282
/// - coreDiagnosticsPath: The path to the Core Diagnostics framework built for Carthage.
3383
/// - outputDir: The directory where all artifacts should be created.
34-
static func generateCarthageRelease(fromPackagedDir packagedDir: URL,
35-
templateDir: URL,
36-
jsonDir: URL,
37-
firebaseVersion: String,
38-
coreDiagnosticsPath: URL,
39-
outputDir: URL) {
84+
85+
private static func generateCarthageRelease(fromPackagedDir packagedDir: URL,
86+
templateDir: URL,
87+
jsonDir: URL,
88+
artifacts: ZipBuilder.ReleaseArtifacts,
89+
outputDir: URL) {
4090
let directories: [String]
4191
do {
4292
directories = try FileManager.default.contentsOfDirectory(atPath: packagedDir.path)
4393
} catch {
4494
fatalError("Could not get contents of Firebase directory to package Carthage build. \(error)")
4595
}
96+
let firebaseVersion = artifacts.firebaseVersion
4697

4798
// Loop through each directory available and package it as a separate Zip file.
4899
for product in directories {
@@ -51,23 +102,20 @@ extension CarthageUtils {
51102

52103
// Parse the JSON file, ensure that we're not trying to overwrite a release.
53104
var jsonManifest = parseJSONFile(fromDir: jsonDir, product: product)
54-
guard jsonManifest[firebaseVersion] == nil else {
55-
print("Carthage release for \(product) \(firebaseVersion) already exists - skipping.")
56-
continue
105+
if !args.carthageSkipVersionCheck {
106+
guard jsonManifest[firebaseVersion] == nil else {
107+
print("Carthage release for \(product) \(firebaseVersion) already exists - skipping.")
108+
continue
109+
}
57110
}
58111

59-
// Find all the .frameworks in this directory.
60-
let allContents: [String]
61-
do {
62-
allContents = try FileManager.default.contentsOfDirectory(atPath: fullPath.path)
63-
} catch {
64-
fatalError("Could not get contents of \(product) for Carthage build in order to add " +
65-
"an Info.plist in each framework. \(error)")
112+
// Make updates to all frameworks to make Carthage happy. We don't worry about xcframeworks
113+
// here.
114+
let allFileObjects = FileManager.default.enumerator(atPath: fullPath.path)?.allObjects
115+
guard let allFiles = allFileObjects as? [String] else {
116+
fatalError("Failed to get file list for Carthage construction at \(fullPath.path)")
66117
}
67-
68-
// Carthage will fail to install a framework if it doesn't have an Info.plist, even though
69-
// they're not used for static frameworks. Generate one and write it to each framework.
70-
let frameworks = allContents.filter { $0.hasSuffix(".framework") }
118+
let frameworks = allFiles.filter { $0.hasSuffix(".framework") }
71119
for framework in frameworks {
72120
let plistPath = fullPath.appendingPathComponents([framework, "Info.plist"])
73121
// Drop the extension of the framework name.
@@ -93,17 +141,6 @@ extension CarthageUtils {
93141
} catch {
94142
fatalError("Could not copy \(noticesName) to FirebaseCore for Carthage build. \(error)")
95143
}
96-
97-
// Override the Core Diagnostics framework with one that includes the proper bit flipped.
98-
let coreDiagnosticsFramework = Constants.coreDiagnosticsName + ".framework"
99-
let destination = fullPath.appendingPathComponent(coreDiagnosticsFramework)
100-
do {
101-
// Remove the existing framework and replace it with the newly compiled one.
102-
try FileManager.default.removeItem(at: destination)
103-
try FileManager.default.copyItem(at: coreDiagnosticsPath, to: destination)
104-
} catch {
105-
fatalError("Could not replace \(coreDiagnosticsFramework) during Carthage build. \(error)")
106-
}
107144
}
108145

109146
// Hash the contents of the directory to get a unique name for Carthage.

ZipBuilder/Sources/ZipBuilder/CocoaPodUtils.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ enum CocoaPodUtils {
311311
return Array(returnDeps)
312312
}
313313

314+
/// Get all transitive pod dependencies for a pod with subspecs merged.
315+
/// - Returns: An array of Strings of pod names.
316+
static func transitiveMasterPodDependencies(for podName: String,
317+
in installedPods: [String: PodInfo]) -> [String] {
318+
return Array(Set(transitivePodDependencies(for: podName, in: installedPods).map {
319+
$0.components(separatedBy: "/")[0]
320+
}))
321+
}
322+
314323
/// Get all transitive pod dependencies for a pod.
315324
/// - Returns: An array of dependencies with versions for a given pod.
316325
static func transitiveVersionedPodDependencies(for podName: String,

ZipBuilder/Sources/ZipBuilder/FirebaseBuilder.swift

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,47 +35,16 @@ struct FirebaseBuilder {
3535
do {
3636
let artifacts = try builder.buildAndAssembleFirebaseRelease(inProjectDir: projectDir)
3737
let firebaseVersion = artifacts.firebaseVersion
38-
let location = artifacts.outputDir
38+
let location = artifacts.zipDir
3939
print("Firebase \(firebaseVersion) directory is ready to be packaged: \(location)")
4040

4141
// Package carthage if it's enabled.
4242
var carthageRoot: URL?
4343
if let carthageJSONDir = args.carthageDir {
44-
do {
45-
print("Creating Carthage release...")
46-
// Create a copy of the release directory since we'll be modifying it.
47-
let carthagePath =
48-
location.deletingLastPathComponent().appendingPathComponent("carthage_build")
49-
let fileManager = FileManager.default
50-
fileManager.removeIfExists(at: carthagePath)
51-
try fileManager.copyItem(at: location, to: carthagePath)
52-
53-
// Package the Carthage distribution with the current directory structure.
54-
let carthageDir = location.deletingLastPathComponent().appendingPathComponent("carthage")
55-
fileManager.removeIfExists(at: carthageDir)
56-
var output = carthageDir.appendingPathComponent(firebaseVersion)
57-
if let rcNumber = args.rcNumber {
58-
output.appendPathComponent("rc\(rcNumber)")
59-
} else {
60-
output.appendPathComponent("latest-non-rc")
61-
}
62-
try fileManager.createDirectory(at: output, withIntermediateDirectories: true)
63-
CarthageUtils.generateCarthageRelease(fromPackagedDir: carthagePath,
64-
templateDir: args.templateDir,
65-
jsonDir: carthageJSONDir,
66-
firebaseVersion: firebaseVersion,
67-
coreDiagnosticsPath: artifacts.carthageDiagnostics,
68-
outputDir: output)
69-
70-
// Remove the duplicated Carthage build directory.
71-
fileManager.removeIfExists(at: carthagePath)
72-
print("Done creating Carthage release! Files written to \(output)")
73-
74-
// Save the directory for later copying.
75-
carthageRoot = carthageDir
76-
} catch {
77-
fatalError("Could not copy output directory for Carthage build: \(error)")
78-
}
44+
carthageRoot = CarthageUtils.packageCarthageRelease(templateDir: args.templateDir,
45+
carthageJSONDir: carthageJSONDir,
46+
artifacts: artifacts,
47+
rcNumber: args.rcNumber)
7948
}
8049

8150
// Prepare the release directory for zip packaging.

0 commit comments

Comments
 (0)