localize_podfile.swift 3.4 KB

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