localize_podfile.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/swift
  2. /*
  3. * Copyright 2019 Google
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. // Modify a Podfile to get any podspecs defined in the firebase-ios-sdk repo locally.
  18. import Foundation
  19. let arg_cnts: Int = .init(CommandLine.argc)
  20. let podfile = CommandLine.arguments[1]
  21. var releaseTesting = false
  22. if arg_cnts > 2 {
  23. releaseTesting = CommandLine.arguments[1 ..< arg_cnts].contains("release_testing")
  24. }
  25. // Always add these, since they may not be in the Podfile, but we still want the local
  26. // versions when they're dependencies of other requested local pods.
  27. let implicitPods = [
  28. "FirebaseCore", "FirebaseInstallations", "Firebase",
  29. "FirebaseAuth", "FirebaseABTesting",
  30. "FirebaseRemoteConfig", "FirebaseCoreExtension",
  31. "FirebaseAppCheckInterop", "FirebaseAuthInterop",
  32. "FirebaseMessagingInterop", "FirebaseCoreInternal",
  33. "FirebaseSessions", "FirebaseSharedSwift",
  34. "FirebaseRemoteConfigInterop",
  35. ]
  36. let binaryPods = [
  37. "GoogleAppMeasurement",
  38. "GoogleAppMeasurementOnDeviceConversion",
  39. "FirebaseAnalytics",
  40. ]
  41. var didImplicits = false
  42. var fileContents = ""
  43. do {
  44. fileContents = try String(contentsOfFile: podfile, encoding: .utf8)
  45. } catch {
  46. fatalError("Could not read \(podfile). \(error)")
  47. }
  48. // Search the path upwards to find the root of the firebase-ios-sdk repo.
  49. var url = URL(fileURLWithPath: FileManager().currentDirectoryPath)
  50. while url.path != "/", url.lastPathComponent != "firebase-ios-sdk" {
  51. url = url.deletingLastPathComponent()
  52. }
  53. let repo = url
  54. let lines = fileContents.components(separatedBy: .newlines)
  55. var outBuffer =
  56. "source 'https://github.com/firebase/SpecsDev.git'\n" +
  57. "source 'https://github.com/firebase/SpecsStaging.git'\n" +
  58. "source 'https://cdn.cocoapods.org/'\n"
  59. for line in lines {
  60. var newLine = line.trimmingCharacters(in: .whitespacesAndNewlines)
  61. let tokens = newLine.components(separatedBy: [" ", ","] as CharacterSet)
  62. if tokens.first == "pod", !releaseTesting {
  63. let podNameRaw = String(tokens[1]).replacingOccurrences(of: "'", with: "")
  64. var podName = podNameRaw
  65. // Firebase test Podfile's often use the Firebase subspec to access the
  66. // desired pod. We want to get the requested pod directly.
  67. if podNameRaw.starts(with: "Firebase/") {
  68. podName = podName.replacingOccurrences(of: "Firebase/", with: "Firebase")
  69. }
  70. let podspec = repo.appendingPathComponent(podName + ".podspec").path
  71. if !binaryPods.contains(podName), FileManager().fileExists(atPath: podspec) {
  72. if didImplicits == false {
  73. didImplicits = true
  74. for implicit in implicitPods {
  75. let implicitPodspec = repo.appendingPathComponent(implicit + ".podspec").path
  76. outBuffer += "pod '\(implicit)', :path => '\(implicitPodspec)'\n"
  77. }
  78. }
  79. newLine = "pod '\(podName)', :path => '\(podspec)'"
  80. } else if podNameRaw.starts(with: "Firebase/") {
  81. // Update closed source pods referenced via a subspec from the Firebase pod.
  82. let firebasePodspec = repo.appendingPathComponent("Firebase.podspec").path
  83. newLine = "pod '\(podNameRaw)', :path => '\(firebasePodspec)'"
  84. }
  85. }
  86. outBuffer += newLine + "\n"
  87. }
  88. // Write out the changed file.
  89. do {
  90. try outBuffer.write(toFile: podfile, atomically: false, encoding: String.Encoding.utf8)
  91. } catch {
  92. fatalError("Failed to write \(podfile). \(error)")
  93. }