localize_podfile.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "FirebaseCoreDiagnostics", "FirebaseRemoteConfig"]
  25. var didImplicits = false
  26. var fileContents = ""
  27. do {
  28. fileContents = try String(contentsOfFile: podfile, encoding: .utf8)
  29. } catch {
  30. fatalError("Could not read \(podfile). \(error)")
  31. }
  32. // Search the path upwards to find the root of the firebase-ios-sdk repo.
  33. var url = URL(fileURLWithPath: FileManager().currentDirectoryPath)
  34. while url.path != "/", url.lastPathComponent != "firebase-ios-sdk" {
  35. url = url.deletingLastPathComponent()
  36. }
  37. let repo = url
  38. let lines = fileContents.components(separatedBy: .newlines)
  39. var outBuffer = ""
  40. for line in lines {
  41. var newLine = line
  42. let tokens = line.components(separatedBy: [" ", ","] as CharacterSet)
  43. if tokens.first == "pod" {
  44. let podNameRaw = String(tokens[1]).replacingOccurrences(of: "'", with: "")
  45. var podName = podNameRaw
  46. // Firebase test Podfile's often use the Firebase subspec to access the
  47. // desired pod. We want to get the requested pod directly.
  48. if podNameRaw.starts(with: "Firebase/") {
  49. podName = podName.replacingOccurrences(of: "Firebase/", with: "Firebase")
  50. }
  51. let podspec = repo.appendingPathComponent(podName + ".podspec").path
  52. if FileManager().fileExists(atPath: podspec) {
  53. if didImplicits == false {
  54. didImplicits = true
  55. for implicit in implicitPods {
  56. let implicitPodspec = repo.appendingPathComponent(implicit + ".podspec").path
  57. outBuffer += "pod '\(implicit)', :path => '\(implicitPodspec)'\n"
  58. }
  59. }
  60. newLine = "pod '\(podName)', :path => '\(podspec)'"
  61. } else if podNameRaw.starts(with: "Firebase/") {
  62. // Update closed source pods referenced via a subspec from the Firebase pod.
  63. let firebasePodspec = repo.appendingPathComponent("Firebase.podspec").path
  64. newLine = "pod '\(podNameRaw)', :path => '\(firebasePodspec)'"
  65. }
  66. }
  67. outBuffer += newLine + "\n"
  68. }
  69. // Write out the changed file.
  70. do {
  71. try outBuffer.write(toFile: podfile, atomically: false, encoding: String.Encoding.utf8)
  72. } catch {
  73. fatalError("Failed to write \(podfile). \(error)")
  74. }