pod_lib_lint.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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=FirebaseInstanceID.podspec,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/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. with_removed_social_media_url(podspec_file) do
  67. system(*command)
  68. pod_lint_status = $?.exitstatus
  69. end
  70. end
  71. # Print every minute since linting can run for >10m without output.
  72. number_of_times_checked = 0
  73. while t.alive? do
  74. sleep 1.0
  75. number_of_times_checked += 1
  76. if (number_of_times_checked % 60) == 0 then
  77. puts "Still working, running for #{number_of_times_checked / 60}min."
  78. end
  79. end
  80. exit(pod_lint_status)
  81. end
  82. # Loads all the specs (inclusing subspecs) from the given podspec file.
  83. def load_specs(podspec_file)
  84. trace('loading', podspec_file)
  85. results = []
  86. spec = Pod::Spec.from_file(podspec_file)
  87. results.push(spec)
  88. results.push(*spec.subspecs)
  89. return results
  90. end
  91. # Finds all dependencies of the given list of specs
  92. def all_deps(specs)
  93. result = Set[]
  94. specs.each do |spec|
  95. spec.dependencies.each do |dep|
  96. name = dep.name.sub(/\/.*/, '')
  97. result.add(name)
  98. end
  99. end
  100. result = result.to_a
  101. trace(' deps', *result)
  102. return result
  103. end
  104. # Given a podspec file, finds all local dependencies that have a local podspec
  105. # in the same directory. Modifies seen to include all seen podspecs, which
  106. # guarantees that a given podspec will only be processed once.
  107. def find_local_deps(podspec_file, seen = Set[])
  108. # Mark the current podspec seen to prevent a pod from depending upon itself
  109. # (as might happen if a subspec of the pod depends upon another subpsec of
  110. # the pod.
  111. seen.add(File.basename(podspec_file))
  112. results = []
  113. spec_dir = File.dirname(podspec_file)
  114. specs = load_specs(podspec_file)
  115. deps = all_deps(specs)
  116. deps.each do |dep_name|
  117. dep_file = File.join(spec_dir, "#{dep_name}.podspec")
  118. if File.exist?(dep_file) then
  119. dep_podspec = File.basename(dep_file)
  120. if seen.add?(dep_podspec)
  121. # Depend on the podspec we found and any podspecs it depends upon.
  122. results.push(dep_podspec)
  123. results.push(*find_local_deps(dep_file, seen))
  124. end
  125. end
  126. end
  127. return results
  128. end
  129. # Returns an --include-podspecs argument that indicates the given deps are
  130. # locally available. Returns nil if deps is empty.
  131. def make_include_podspecs(deps)
  132. return nil if deps.empty?
  133. if deps.size == 1 then
  134. deps_joined = deps[0]
  135. else
  136. deps_joined = "{" + deps.join(',') + "}"
  137. end
  138. return "--include-podspecs=#{deps_joined}"
  139. end
  140. def trace(*args)
  141. return if not $DEBUG
  142. STDERR.puts(args.join(' '))
  143. end
  144. # Edits the given podspec file to remove the social_media_url, yields, then
  145. # restores the file to its original condition. Validating of the social URL
  146. # can be very flaky (see #3416). Remove it from spec to let the actual tests
  147. # pass.
  148. def with_removed_social_media_url(spec)
  149. podspec_content = File.read(spec)
  150. updated_podspec_content =
  151. podspec_content.gsub("s.social_media_url = ", "# s.social_media_url = ")
  152. write_file(spec, updated_podspec_content)
  153. yield
  154. ensure
  155. write_file(spec, podspec_content)
  156. end
  157. # Writes the text in +contents+ to the file named by +filename+.
  158. def write_file(filename, contents)
  159. File.open(filename, "w") do |file|
  160. file.write(contents)
  161. end
  162. end
  163. main(ARGV)