localize_podfile.swift 3.4 KB

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