localize_podfile.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "FirebaseSharedSwift",
  34. ]
  35. let binaryPods = [
  36. "GoogleAppMeasurement",
  37. "GoogleAppMeasurementOnDeviceConversion",
  38. "FirebaseAnalytics",
  39. ]
  40. var didImplicits = false
  41. var fileContents = ""
  42. do {
  43. fileContents = try String(contentsOfFile: podfile, encoding: .utf8)
  44. } catch {
  45. fatalError("Could not read \(podfile). \(error)")
  46. }
  47. // Search the path upwards to find the root of the firebase-ios-sdk repo.
  48. var url = URL(fileURLWithPath: FileManager().currentDirectoryPath)
  49. while url.path != "/", url.lastPathComponent != "firebase-ios-sdk" {
  50. url = url.deletingLastPathComponent()
  51. }
  52. let repo = url
  53. let lines = fileContents.components(separatedBy: .newlines)
  54. var outBuffer =
  55. "source 'https://github.com/firebase/SpecsDev.git'\n" +
  56. "source 'https://github.com/firebase/SpecsStaging.git'\n" +
  57. "source 'https://cdn.cocoapods.org/'\n"
  58. for line in lines {
  59. var newLine = line.trimmingCharacters(in: .whitespacesAndNewlines)
  60. let tokens = newLine.components(separatedBy: [" ", ","] as CharacterSet)
  61. if tokens.first == "pod", !releaseTesting {
  62. let podNameRaw = String(tokens[1]).replacingOccurrences(of: "'", with: "")
  63. var podName = podNameRaw
  64. // Firebase test Podfile's often use the Firebase subspec to access the
  65. // desired pod. We want to get the requested pod directly.
  66. if podNameRaw.starts(with: "Firebase/") {
  67. podName = podName.replacingOccurrences(of: "Firebase/", with: "Firebase")
  68. }
  69. let podspec = repo.appendingPathComponent(podName + ".podspec").path
  70. if !binaryPods.contains(podName), FileManager().fileExists(atPath: podspec) {
  71. if didImplicits == false {
  72. didImplicits = true
  73. for implicit in implicitPods {
  74. let implicitPodspec = repo.appendingPathComponent(implicit + ".podspec").path
  75. outBuffer += "pod '\(implicit)', :path => '\(implicitPodspec)'\n"
  76. }
  77. }
  78. newLine = "pod '\(podName)', :path => '\(podspec)'"
  79. } else if podNameRaw.starts(with: "Firebase/") {
  80. // Update closed source pods referenced via a subspec from the Firebase pod.
  81. let firebasePodspec = repo.appendingPathComponent("Firebase.podspec").path
  82. newLine = "pod '\(podNameRaw)', :path => '\(firebasePodspec)'"
  83. }
  84. }
  85. outBuffer += newLine + "\n"
  86. }
  87. // Write out the changed file.
  88. do {
  89. try outBuffer.write(toFile: podfile, atomically: false, encoding: String.Encoding.utf8)
  90. } catch {
  91. fatalError("Failed to write \(podfile). \(error)")
  92. }