localize_podfile.swift 3.3 KB

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