pod_lib_lint.rb 5.1 KB

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