pod_lib_lint.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env ruby
  2. # Copyright 2019 Google
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. require 'cocoapods'
  16. require 'set'
  17. # Enable ruby options after 'require' because cocoapods is noisy
  18. $VERBOSE = true # ruby -w
  19. #$DEBUG = true # ruby --debug
  20. def usage()
  21. script = File.basename($0)
  22. STDERR.puts <<~EOF
  23. USAGE: #{script} podspec [options]
  24. podspec is the podspec to lint
  25. options can be any options for pod spec lint
  26. EOF
  27. end
  28. def main(args)
  29. if args.size < 1 then
  30. usage()
  31. exit(1)
  32. end
  33. # Disabled CDN. See https://github.com/firebase/firebase-ios-sdk/issues/3165
  34. # command = %w(bundle exec pod lib lint --sources=https://cdn.jsdelivr.net/cocoa/)
  35. command = %w(bundle exec pod lib lint)
  36. # Figure out which dependencies are local
  37. podspec_file = args[0]
  38. deps = find_local_deps(podspec_file)
  39. arg = make_include_podspecs(deps)
  40. command.push(arg) if arg
  41. command.push(*args)
  42. puts command.join(' ')
  43. exec('bundle exec pod repo update')
  44. exec(*command)
  45. end
  46. # Loads all the specs (inclusing subspecs) from the given podspec file.
  47. def load_specs(podspec_file)
  48. trace('loading', podspec_file)
  49. results = []
  50. spec = Pod::Spec.from_file(podspec_file)
  51. results.push(spec)
  52. results.push(*spec.subspecs)
  53. return results
  54. end
  55. # Finds all dependencies of the given list of specs
  56. def all_deps(specs)
  57. result = Set[]
  58. specs.each do |spec|
  59. spec.dependencies.each do |dep|
  60. name = dep.name.sub(/\/.*/, '')
  61. result.add(name)
  62. end
  63. end
  64. result = result.to_a
  65. trace(' deps', *result)
  66. return result
  67. end
  68. # Given a podspec file, finds all local dependencies that have a local podspec
  69. # in the same directory. Modifies seen to include all seen podspecs, which
  70. # guarantees that a given podspec will only be processed once.
  71. def find_local_deps(podspec_file, seen = Set[])
  72. # Mark the current podspec seen to prevent a pod from depending upon itself
  73. # (as might happen if a subspec of the pod depends upon another subpsec of
  74. # the pod.
  75. seen.add(File.basename(podspec_file))
  76. results = []
  77. spec_dir = File.dirname(podspec_file)
  78. specs = load_specs(podspec_file)
  79. deps = all_deps(specs)
  80. deps.each do |dep_name|
  81. dep_file = File.join(spec_dir, "#{dep_name}.podspec")
  82. if File.exist?(dep_file) then
  83. dep_podspec = File.basename(dep_file)
  84. if seen.add?(dep_podspec)
  85. # Depend on the podspec we found and any podspecs it depends upon.
  86. results.push(dep_podspec)
  87. results.push(*find_local_deps(dep_file, seen))
  88. end
  89. end
  90. end
  91. return results
  92. end
  93. # Returns an --include-podspecs argument that indicates the given deps are
  94. # locally available. Returns nil if deps is empty.
  95. def make_include_podspecs(deps)
  96. return nil if deps.empty?
  97. if deps.size == 1 then
  98. deps_joined = deps[0]
  99. else
  100. deps_joined = "{" + deps.join(',') + "}"
  101. end
  102. return "--include-podspecs=#{deps_joined}"
  103. end
  104. def trace(*args)
  105. return if not $DEBUG
  106. STDERR.puts(args.join(' '))
  107. end
  108. main(ARGV)