FirebaseBuilder.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. /// Wrapper for the Firebase zip build. Unlike the generic zip builder, the Firebase build creates a two-level
  18. /// zip with the option to install different Firebase library subsets.
  19. struct FirebaseBuilder {
  20. /// ZipBuilder instance.
  21. private let zipBuilder: ZipBuilder
  22. /// Default initializer.
  23. /// - Parameters:
  24. /// - zipBuilder: The zipBuilder object for this Firebase build.
  25. init(zipBuilder: ZipBuilder) {
  26. self.zipBuilder = zipBuilder
  27. }
  28. /// Wrapper around a generic zip builder that adds in Firebase specific steps including a
  29. /// multi-level zip file, a README, and optionally Carthage artifacts.
  30. func build(templateDir: URL,
  31. carthageBuildOptions: CarthageBuildOptions?) {
  32. // Build the zip file and get the path.
  33. do {
  34. let artifacts = try zipBuilder.buildAndAssembleFirebaseRelease(templateDir: templateDir,
  35. includeCarthage: carthageBuildOptions !=
  36. nil)
  37. let firebaseVersion = artifacts.firebaseVersion
  38. let location = artifacts.zipDir
  39. print("Firebase \(firebaseVersion) directory is ready to be packaged: \(location)")
  40. // Package carthage if it's enabled.
  41. var carthageRoot: URL?
  42. if let carthageBuildOptions = carthageBuildOptions {
  43. carthageRoot = CarthageUtils.packageCarthageRelease(
  44. templateDir: zipBuilder.paths.templateDir,
  45. artifacts: artifacts,
  46. options: carthageBuildOptions
  47. )
  48. }
  49. // Prepare the release directory for zip packaging.
  50. do {
  51. // Move the Resources out of each directory in order to maintain the existing Zip structure.
  52. let fileManager = FileManager.default
  53. let contents = try fileManager.contentsOfDirectory(atPath: location.path)
  54. for fileOrFolder in contents {
  55. let fullPath = location.appendingPathComponent(fileOrFolder)
  56. // Ignore any files.
  57. guard fileManager.isDirectory(at: fullPath) else { continue }
  58. // Move all the bundles in the frameworks out to a common "Resources" directory to match the
  59. // existing Zip structure.
  60. let resourcesDir = fullPath.appendingPathComponent("Resources")
  61. _ = try ResourcesManager.moveAllBundles(inDirectory: fullPath, to: resourcesDir)
  62. }
  63. }
  64. print("Attempting to Zip the directory...")
  65. let candidateName = "Firebase-\(firebaseVersion)-latest.zip"
  66. let zipped = Zip.zipContents(ofDir: location, name: candidateName)
  67. // If an output directory was specified, copy the Zip file to that directory. Otherwise just print
  68. // the location for further use.
  69. if let outputDir = zipBuilder.paths.outputDir {
  70. do {
  71. // We want the output to be in the X_Y_Z directory.
  72. let underscoredVersion = firebaseVersion.replacingOccurrences(of: ".", with: "_")
  73. let versionedOutputDir = outputDir.appendingPathComponent(underscoredVersion)
  74. try FileManager.default.createDirectory(at: versionedOutputDir,
  75. withIntermediateDirectories: true)
  76. let destination = versionedOutputDir.appendingPathComponent(zipped.lastPathComponent)
  77. try FileManager.default.copyItem(at: zipped, to: destination)
  78. } catch {
  79. fatalError("Could not copy Zip file to output directory: \(error)")
  80. }
  81. // Move the Carthage directory, if it exists.
  82. if let carthageOutput = carthageRoot {
  83. do {
  84. let carthageDir = outputDir.appendingPathComponent("carthage")
  85. try FileManager.default.copyItem(at: carthageOutput, to: carthageDir)
  86. } catch {
  87. fatalError("Could not copy Carthage output to directory: \(error)")
  88. }
  89. }
  90. } else {
  91. // Move zip to parent directory so it doesn't get removed with other artifacts.
  92. let parentLocation =
  93. zipped.deletingLastPathComponent().deletingLastPathComponent()
  94. .appendingPathComponent(zipped.lastPathComponent)
  95. // Clear out the output file if it exists.
  96. FileManager.default.removeIfExists(at: parentLocation)
  97. do {
  98. try FileManager.default.moveItem(at: zipped, to: parentLocation)
  99. } catch {
  100. fatalError("Could not move Zip file to output directory: \(error)")
  101. }
  102. print("Success! Zip file can be found at \(parentLocation.path)")
  103. }
  104. } catch {
  105. fatalError("Could not build the zip file: \(error)")
  106. }
  107. }
  108. }