main.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. import ManifestReader
  18. // Get the launch arguments, parsed by user defaults.
  19. let args = LaunchArgs.shared
  20. // Keep timing for how long it takes to change the Firebase pod versions.
  21. let buildStart = Date()
  22. var cocoaPodsUpdateMessage: String = ""
  23. var paths = FirebasePod.FilesystemPaths(currentReleasePath: args.currentReleasePath,
  24. gitRootPath: args.gitRootPath)
  25. /// Assembles the expected versions based on the release manifest passed in.
  26. /// Returns an array with the pod name as the key and version as the value,
  27. private func getExpectedVersions() -> [String: String] {
  28. // Merge the versions from the current release and the known public versions.
  29. var releasingVersions: [String: String] = [:]
  30. // Override any of the expected versions with the current release manifest, if it exists.
  31. let currentRelease = ManifestReader.loadCurrentRelease(fromTextproto: paths.currentReleasePath)
  32. print("Overriding the following Pod versions, taken from the current release manifest:")
  33. for pod in currentRelease.sdk {
  34. releasingVersions[pod.sdkName] = pod.sdkVersion
  35. print("\(pod.sdkName): \(pod.sdkVersion)")
  36. }
  37. if !releasingVersions.isEmpty {
  38. print("Updating Firebase Pod in git installation at \(paths.gitRootPath)) " +
  39. "with the following versions: \(releasingVersions)")
  40. }
  41. return releasingVersions
  42. }
  43. private func updateFirebasePod(newVersions: [String: String]) {
  44. let podspecFile = paths.gitRootPath + "/Firebase.podspec"
  45. var contents = ""
  46. do {
  47. contents = try String(contentsOfFile: podspecFile, encoding: .utf8)
  48. } catch {
  49. fatalError("Could not read Firebase podspec. \(error)")
  50. }
  51. for (pod, version) in newVersions {
  52. if pod == "Firebase" {
  53. // Replace version in string like s.version = '6.9.0'
  54. guard let range = contents.range(of: "s.version") else {
  55. fatalError("Could not find version of Firebase pod in podspec at \(podspecFile)")
  56. }
  57. var versionStartIndex = contents.index(range.upperBound, offsetBy: 1)
  58. while contents[versionStartIndex] != "'" {
  59. versionStartIndex = contents.index(versionStartIndex, offsetBy: 1)
  60. }
  61. var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1)
  62. while contents[versionEndIndex] != "'" {
  63. versionEndIndex = contents.index(versionEndIndex, offsetBy: 1)
  64. }
  65. contents.removeSubrange(versionStartIndex ... versionEndIndex)
  66. contents.insert(contentsOf: "'" + version + "'", at: versionStartIndex)
  67. } else {
  68. // Replace version in string like ss.dependency 'FirebaseCore', '6.3.0'
  69. guard let range = contents.range(of: pod) else {
  70. // This pod is not a top-level Firebase pod dependency.
  71. continue
  72. }
  73. var versionStartIndex = contents.index(range.upperBound, offsetBy: 2)
  74. while !contents[versionStartIndex].isWholeNumber {
  75. versionStartIndex = contents.index(versionStartIndex, offsetBy: 1)
  76. }
  77. var versionEndIndex = contents.index(versionStartIndex, offsetBy: 1)
  78. while contents[versionEndIndex] != "'" {
  79. versionEndIndex = contents.index(versionEndIndex, offsetBy: 1)
  80. }
  81. contents.removeSubrange(versionStartIndex ... versionEndIndex)
  82. contents.insert(contentsOf: version + "'", at: versionStartIndex)
  83. }
  84. }
  85. do {
  86. try contents.write(toFile: podspecFile, atomically: false, encoding: String.Encoding.utf8)
  87. } catch {
  88. fatalError("Failed to write \(podspecFile). \(error)")
  89. }
  90. }
  91. do {
  92. let newVersions = getExpectedVersions()
  93. updateFirebasePod(newVersions: newVersions)
  94. print("Updating Firebase pod for version \(String(describing: newVersions["Firebase"]!))")
  95. // Get the time since the tool start.
  96. let secondsSinceStart = -Int(buildStart.timeIntervalSinceNow)
  97. print("""
  98. Time profile:
  99. It took \(secondsSinceStart) seconds (~\(secondsSinceStart / 60)m) to update the Firebase pod.
  100. \(cocoaPodsUpdateMessage)
  101. """)
  102. }