pod_lib_lint.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. command = %w(bundle exec pod lib lint)
  34. # Figure out which dependencies are local
  35. podspec_file = args[0]
  36. deps = find_local_deps(podspec_file)
  37. arg = make_include_podspecs(deps)
  38. command.push(arg) if arg
  39. command.push(*args)
  40. puts command.join(' ')
  41. exec(*command)
  42. end
  43. # Loads all the specs (inclusing subspecs) from the given podspec file.
  44. def load_specs(podspec_file)
  45. trace('loading', podspec_file)
  46. results = []
  47. spec = Pod::Spec.from_file(podspec_file)
  48. results.push(spec)
  49. results.push(*spec.subspecs)
  50. return results
  51. end
  52. # Finds all dependencies of the given list of specs
  53. def all_deps(specs)
  54. result = Set[]
  55. specs.each do |spec|
  56. spec.dependencies.each do |dep|
  57. name = dep.name.sub(/\/.*/, '')
  58. result.add(name)
  59. end
  60. end
  61. result = result.to_a
  62. trace(' deps', *result)
  63. return result
  64. end
  65. # Given a podspec file, finds all local dependencies that have a local podspec
  66. # in the same directory. Modifies seen to include all seen podspecs, which
  67. # guarantees that a given podspec will only be processed once.
  68. def find_local_deps(podspec_file, seen = Set[])
  69. # Mark the current podspec seen to prevent a pod from depending upon itself
  70. # (as might happen if a subspec of the pod depends upon another subpsec of
  71. # the pod.
  72. seen.add(File.basename(podspec_file))
  73. results = []
  74. spec_dir = File.dirname(podspec_file)
  75. specs = load_specs(podspec_file)
  76. deps = all_deps(specs)
  77. deps.each do |dep_name|
  78. dep_file = File.join(spec_dir, "#{dep_name}.podspec")
  79. if File.exist?(dep_file) then
  80. dep_podspec = File.basename(dep_file)
  81. if seen.add?(dep_podspec)
  82. # Depend on the podspec we found and any podspecs it depends upon.
  83. results.push(dep_podspec)
  84. results.push(*find_local_deps(dep_file, seen))
  85. end
  86. end
  87. end
  88. return results
  89. end
  90. # Returns an --include-podspecs argument that indicates the given deps are
  91. # locally available. Returns nil if deps is empty.
  92. def make_include_podspecs(deps)
  93. return nil if deps.empty?
  94. if deps.size == 1 then
  95. deps_joined = deps[0]
  96. else
  97. deps_joined = "{" + deps.join(',') + "}"
  98. end
  99. return "--include-podspecs=#{deps_joined}"
  100. end
  101. def trace(*args)
  102. return if not $DEBUG
  103. STDERR.puts(args.join(' '))
  104. end
  105. main(ARGV)