pod_lib_lint.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. script options:
  27. --no-analyze: don't run Xcode analyze on this podspec
  28. --ignore-local-podspecs: list of podspecs that should not be added to
  29. "--include-podspecs" list. If not specified, then all podspec
  30. dependencies will be passed to "--include-podspecs".
  31. Example: --ignore-local-podspecs=GoogleDataTransport.podspec
  32. EOF
  33. end
  34. def main(args)
  35. if args.size < 1 then
  36. usage()
  37. exit(1)
  38. end
  39. STDOUT.sync = true
  40. command = %w(bundle exec pod lib lint --sources=https://github.com/firebase/SpecsDev.git,https://github.com/firebase/SpecsStaging.git,https://cdn.cocoapods.org/)
  41. # Split arguments that need to be processed by the script itself and passed
  42. # to the pod command.
  43. pod_args = []
  44. ignore_local_podspecs = []
  45. analyze = true
  46. args.each do |arg|
  47. if arg =~ /--ignore-local-podspecs=(.*)/
  48. ignore_local_podspecs = $1.split(',')
  49. elsif arg =~ /--no-analyze/
  50. analyze = false
  51. else
  52. pod_args.push(arg)
  53. end
  54. end
  55. podspec_file = pod_args[0]
  56. # Figure out which dependencies are local
  57. deps = find_local_deps(podspec_file, ignore_local_podspecs.to_set)
  58. arg = make_include_podspecs(deps)
  59. command.push(arg) if arg
  60. command.push('--analyze') if analyze
  61. command.push(*pod_args)
  62. puts command.join(' ')
  63. # Run the lib lint command in a thread.
  64. pod_lint_status = 1
  65. t = Thread.new do
  66. system(*command)
  67. pod_lint_status = $?.exitstatus
  68. end
  69. # Print every minute since linting can run for >10m without output.
  70. number_of_times_checked = 0
  71. while t.alive? do
  72. sleep 1.0
  73. number_of_times_checked += 1
  74. if (number_of_times_checked % 60) == 0 then
  75. puts "Still working, running for #{number_of_times_checked / 60}min."
  76. end
  77. end
  78. exit(pod_lint_status)
  79. end
  80. # Loads all the specs (inclusing subspecs) from the given podspec file.
  81. def load_specs(podspec_file)
  82. trace('loading', podspec_file)
  83. results = []
  84. spec = Pod::Spec.from_file(podspec_file)
  85. results.push(spec)
  86. results.push(*spec.subspecs)
  87. return results
  88. end
  89. # Finds all dependencies of the given list of specs
  90. def all_deps(specs)
  91. result = Set[]
  92. specs.each do |spec|
  93. spec.dependencies.each do |dep|
  94. name = dep.name.sub(/\/.*/, '')
  95. result.add(name)
  96. end
  97. end
  98. result = result.to_a
  99. trace(' deps', *result)
  100. return result
  101. end
  102. # Given a podspec file, finds all local dependencies that have a local podspec
  103. # in the same directory. Modifies seen to include all seen podspecs, which
  104. # guarantees that a given podspec will only be processed once.
  105. def find_local_deps(podspec_file, seen = Set[])
  106. # Mark the current podspec seen to prevent a pod from depending upon itself
  107. # (as might happen if a subspec of the pod depends upon another subpsec of
  108. # the pod.
  109. seen.add(File.basename(podspec_file))
  110. results = []
  111. spec_dir = File.dirname(podspec_file)
  112. specs = load_specs(podspec_file)
  113. deps = all_deps(specs)
  114. deps.each do |dep_name|
  115. dep_file = File.join(spec_dir, "#{dep_name}.podspec")
  116. if File.exist?(dep_file) then
  117. dep_podspec = File.basename(dep_file)
  118. if seen.add?(dep_podspec)
  119. # Depend on the podspec we found and any podspecs it depends upon.
  120. results.push(dep_podspec)
  121. results.push(*find_local_deps(dep_file, seen))
  122. end
  123. end
  124. end
  125. return results
  126. end
  127. # Returns an --include-podspecs argument that indicates the given deps are
  128. # locally available. Returns nil if deps is empty.
  129. def make_include_podspecs(deps)
  130. return nil if deps.empty?
  131. if deps.size == 1 then
  132. deps_joined = deps[0]
  133. else
  134. deps_joined = "{" + deps.join(',') + "}"
  135. end
  136. return "--include-podspecs=#{deps_joined}"
  137. end
  138. def trace(*args)
  139. return if not $DEBUG
  140. STDERR.puts(args.join(' '))
  141. end
  142. # Writes the text in +contents+ to the file named by +filename+.
  143. def write_file(filename, contents)
  144. File.open(filename, "w") do |file|
  145. file.write(contents)
  146. end
  147. end
  148. main(ARGV)