FirebaseBuilder.swift 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. let firebaseVersion = artifacts.firebaseVersion
  36. let location = artifacts.zipDir
  37. print("Firebase \(firebaseVersion) directory is ready to be packaged: \(location)")
  38. // Package carthage if it's enabled.
  39. let carthageRoot = CarthageUtils.packageCarthageRelease(
  40. templateDir: zipBuilder.paths.templateDir,
  41. artifacts: artifacts,
  42. options: carthageBuildOptions
  43. )
  44. print("Attempting to Zip the directory...")
  45. let candidateName = "Firebase-\(firebaseVersion)-latest.zip"
  46. let zipped = Zip.zipContents(ofDir: location, name: candidateName)
  47. // If an output directory was specified, copy the Zip file to that directory. Otherwise just print
  48. // the location for further use.
  49. if let outputDir = zipBuilder.paths.outputDir {
  50. do {
  51. // We want the output to be in the X_Y_Z directory.
  52. let underscoredVersion = firebaseVersion.replacingOccurrences(of: ".", with: "_")
  53. let versionedOutputDir = outputDir.appendingPathComponent(underscoredVersion)
  54. try FileManager.default.createDirectory(at: versionedOutputDir,
  55. withIntermediateDirectories: true)
  56. let destination = versionedOutputDir.appendingPathComponent(zipped.lastPathComponent)
  57. try FileManager.default.copyItem(at: zipped, to: destination)
  58. } catch {
  59. fatalError("Could not copy Zip file to output directory: \(error)")
  60. }
  61. // Move the Carthage directory, if it exists.
  62. if let carthageOutput = carthageRoot {
  63. do {
  64. let carthageDir = outputDir.appendingPathComponent("carthage")
  65. try FileManager.default.copyItem(at: carthageOutput, to: carthageDir)
  66. } catch {
  67. fatalError("Could not copy Carthage output to directory: \(error)")
  68. }
  69. }
  70. } else {
  71. // Move zip to parent directory so it doesn't get removed with other artifacts.
  72. let parentLocation =
  73. zipped.deletingLastPathComponent().deletingLastPathComponent()
  74. .appendingPathComponent(zipped.lastPathComponent)
  75. // Clear out the output file if it exists.
  76. FileManager.default.removeIfExists(at: parentLocation)
  77. do {
  78. try FileManager.default.moveItem(at: zipped, to: parentLocation)
  79. } catch {
  80. fatalError("Could not move Zip file to output directory: \(error)")
  81. }
  82. print("Success! Zip file can be found at \(parentLocation.path)")
  83. }
  84. } catch {
  85. fatalError("Could not build the zip file: \(error)")
  86. }
  87. }
  88. }