FirebaseBuilder.swift 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  18. /// a two-level
  19. /// zip with the option to install different Firebase library subsets.
  20. struct FirebaseBuilder {
  21. /// ZipBuilder instance.
  22. private let zipBuilder: ZipBuilder
  23. /// Default initializer.
  24. /// - Parameters:
  25. /// - zipBuilder: The zipBuilder object for this Firebase build.
  26. init(zipBuilder: ZipBuilder) {
  27. self.zipBuilder = zipBuilder
  28. }
  29. /// Wrapper around a generic zip builder that adds in Firebase specific steps including a
  30. /// multi-level zip file, a README, and optionally Carthage artifacts.
  31. func build(templateDir: URL,
  32. carthageBuildOptions: CarthageBuildOptions) {
  33. // Build the zip file and get the path.
  34. do {
  35. let artifacts = try zipBuilder.buildAndAssembleFirebaseRelease(templateDir: templateDir)
  36. let firebaseVersion = artifacts.firebaseVersion
  37. let location = artifacts.zipDir
  38. print("Firebase \(firebaseVersion) directory is ready to be packaged: \(location)")
  39. // Package carthage if it's enabled.
  40. let carthageRoot = CarthageUtils.packageCarthageRelease(
  41. templateDir: zipBuilder.paths.templateDir,
  42. artifacts: artifacts,
  43. options: carthageBuildOptions
  44. )
  45. print("Attempting to Zip the directory...")
  46. let candidateName = "Firebase-\(firebaseVersion)-latest.zip"
  47. let zipped = Zip.zipContents(ofDir: location, name: candidateName)
  48. // If an output directory was specified, copy the Zip file to that directory. Otherwise just
  49. // print
  50. // the location for further use.
  51. if let outputDir = zipBuilder.paths.outputDir {
  52. do {
  53. // We want the output to be in the X_Y_Z directory.
  54. let underscoredVersion = firebaseVersion.replacingOccurrences(of: ".", with: "_")
  55. let versionedOutputDir = outputDir.appendingPathComponent(underscoredVersion)
  56. try FileManager.default.createDirectory(at: versionedOutputDir,
  57. withIntermediateDirectories: true)
  58. let destination = versionedOutputDir.appendingPathComponent(zipped.lastPathComponent)
  59. try FileManager.default.copyItem(at: zipped, to: destination)
  60. } catch {
  61. fatalError("Could not copy Zip file to output directory: \(error)")
  62. }
  63. // Move the Carthage directory, if it exists.
  64. if let carthageOutput = carthageRoot {
  65. do {
  66. let carthageDir = outputDir.appendingPathComponent("carthage")
  67. try FileManager.default.copyItem(at: carthageOutput, to: carthageDir)
  68. } catch {
  69. fatalError("Could not copy Carthage output to directory: \(error)")
  70. }
  71. }
  72. } else {
  73. // Move zip to parent directory so it doesn't get removed with other artifacts.
  74. let parentLocation =
  75. zipped.deletingLastPathComponent().deletingLastPathComponent()
  76. .appendingPathComponent(zipped.lastPathComponent)
  77. // Clear out the output file if it exists.
  78. FileManager.default.removeIfExists(at: parentLocation)
  79. do {
  80. try FileManager.default.moveItem(at: zipped, to: parentLocation)
  81. } catch {
  82. fatalError("Could not move Zip file to output directory: \(error)")
  83. }
  84. print("Success! Zip file can be found at \(parentLocation.path)")
  85. }
  86. } catch {
  87. fatalError("Could not build the zip file: \(error)")
  88. }
  89. }
  90. }