localize_podfile.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 podfile = CommandLine.arguments[1]
  20. // Always add these, since they may not be in the Podfile, but we still want the local
  21. // versions when they're dependencies of other requested local pods.
  22. let implicitPods = ["FirebaseCore", "FirebaseInstanceID", "FirebaseInstallations", "Firebase",
  23. "GoogleDataTransport", "GoogleDataTransportCCTSupport", "GoogleUtilities",
  24. "FirebaseAuth",
  25. "FirebaseAnalyticsInterop", "FirebaseAuthInterop", "FirebaseCoreDiagnostics",
  26. "FirebaseCoreDiagnosticsInterop", "FirebaseRemoteConfig", "FirebaseAuthInterop"]
  27. var didImplicits = false
  28. var fileContents = ""
  29. do {
  30. fileContents = try String(contentsOfFile: podfile, encoding: .utf8)
  31. } catch {
  32. fatalError("Could not read \(podfile). \(error)")
  33. }
  34. // Search the path upwards to find the root of the firebase-ios-sdk repo.
  35. var url = URL(fileURLWithPath: FileManager().currentDirectoryPath)
  36. while url.path != "/", url.lastPathComponent != "firebase-ios-sdk" {
  37. url = url.deletingLastPathComponent()
  38. }
  39. let repo = url
  40. let lines = fileContents.components(separatedBy: .newlines)
  41. var outBuffer = "source 'https://github.com/firebase/SpecsStaging.git'\n" +
  42. "source 'https://cdn.cocoapods.org/'\n"
  43. for line in lines {
  44. var newLine = line
  45. let tokens = line.components(separatedBy: [" ", ","] as CharacterSet)
  46. if tokens.first == "pod" {
  47. let podNameRaw = String(tokens[1]).replacingOccurrences(of: "'", with: "")
  48. var podName = podNameRaw
  49. // Firebase test Podfile's often use the Firebase subspec to access the
  50. // desired pod. We want to get the requested pod directly.
  51. if podNameRaw.starts(with: "Firebase/") {
  52. podName = podName.replacingOccurrences(of: "Firebase/", with: "Firebase")
  53. }
  54. let podspec = repo.appendingPathComponent(podName + ".podspec").path
  55. if FileManager().fileExists(atPath: podspec) {
  56. if didImplicits == false {
  57. didImplicits = true
  58. for implicit in implicitPods {
  59. let implicitPodspec = repo.appendingPathComponent(implicit + ".podspec").path
  60. outBuffer += "pod '\(implicit)', :path => '\(implicitPodspec)'\n"
  61. }
  62. }
  63. newLine = "pod '\(podName)', :path => '\(podspec)'"
  64. } else if podNameRaw.starts(with: "Firebase/") {
  65. // Update closed source pods referenced via a subspec from the Firebase pod.
  66. let firebasePodspec = repo.appendingPathComponent("Firebase.podspec").path
  67. newLine = "pod '\(podNameRaw)', :path => '\(firebasePodspec)'"
  68. }
  69. }
  70. outBuffer += newLine + "\n"
  71. }
  72. // Write out the changed file.
  73. do {
  74. try outBuffer.write(toFile: podfile, atomically: false, encoding: String.Encoding.utf8)
  75. } catch {
  76. fatalError("Failed to write \(podfile). \(error)")
  77. }