pod_lib_lint.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. # Assert that the given podspec to lint does not have binary sources.
  57. podspec = Pod::Spec.from_file(podspec_file)
  58. if spec_has_binary_source(podspec) then
  59. STDERR.puts("""
  60. Error: `pod lib lint` does not work for specs with binary sources.
  61. The given podspec, #{podspec_file}, has a binary source.
  62. """)
  63. exit(1)
  64. end
  65. # Figure out which dependencies are local
  66. deps = find_local_deps(podspec_file, ignore_local_podspecs.to_set)
  67. arg = make_include_podspecs(deps)
  68. command.push(arg) if arg
  69. command.push('--analyze') if analyze
  70. command.push(*pod_args)
  71. puts command.join(' ')
  72. # Run the lib lint command in a thread.
  73. pod_lint_status = 1
  74. t = Thread.new do
  75. system(*command)
  76. pod_lint_status = $?.exitstatus
  77. end
  78. # Print every minute since linting can run for >10m without output.
  79. number_of_times_checked = 0
  80. while t.alive? do
  81. sleep 1.0
  82. number_of_times_checked += 1
  83. if (number_of_times_checked % 60) == 0 then
  84. puts "Still working, running for #{number_of_times_checked / 60}min."
  85. end
  86. end
  87. exit(pod_lint_status)
  88. end
  89. # Returns whether or not the given `Pod::Spec` has binary sources.
  90. def spec_has_binary_source(spec)
  91. # Specs with binary sources specify a URL for the `:http` key
  92. # in their `source`.
  93. return spec.source.has_key?(:http)
  94. end
  95. # Loads all the specs (including subspecs) from the given podspec file.
  96. def load_specs(podspec_file)
  97. trace('loading', podspec_file)
  98. results = []
  99. spec = Pod::Spec.from_file(podspec_file)
  100. results.push(spec)
  101. results.push(*spec.subspecs)
  102. return results
  103. end
  104. # Finds all dependencies of the given list of specs
  105. def all_deps(specs)
  106. result = Set[]
  107. specs.each do |spec|
  108. spec.dependencies.each do |dep|
  109. name = dep.name.sub(/\/.*/, '')
  110. result.add(name)
  111. end
  112. end
  113. result = result.to_a
  114. trace(' deps', *result)
  115. return result
  116. end
  117. # Given a podspec file, finds all local dependencies that have a local podspec
  118. # in the same directory. Modifies seen to include all seen podspecs, which
  119. # guarantees that a given podspec will only be processed once.
  120. def find_local_deps(podspec_file, seen = Set[])
  121. # Mark the current podspec seen to prevent a pod from depending upon itself
  122. # (as might happen if a subspec of the pod depends upon another subpsec of
  123. # the pod).
  124. seen.add(File.basename(podspec_file))
  125. results = []
  126. spec_dir = File.dirname(podspec_file)
  127. specs = load_specs(podspec_file)
  128. deps = all_deps(specs)
  129. deps.each do |dep_name|
  130. dep_file = File.join(spec_dir, "#{dep_name}.podspec")
  131. if File.exist?(dep_file) then
  132. local_dep_spec = Pod::Spec.from_file(dep_file)
  133. if !spec_has_binary_source(local_dep_spec) then
  134. dep_podspec = File.basename(dep_file)
  135. if seen.add?(dep_podspec)
  136. # Depend on the podspec we found and any podspecs it depends upon.
  137. results.push(dep_podspec)
  138. results.push(*find_local_deps(dep_file, seen))
  139. end
  140. end
  141. end
  142. end
  143. return results
  144. end
  145. # Returns an --include-podspecs argument that indicates the given deps are
  146. # locally available. Returns nil if deps is empty.
  147. def make_include_podspecs(deps)
  148. return nil if deps.empty?
  149. if deps.size == 1 then
  150. deps_joined = deps[0]
  151. else
  152. deps_joined = "{" + deps.join(',') + "}"
  153. end
  154. return "--include-podspecs=#{deps_joined}"
  155. end
  156. def trace(*args)
  157. return if not $DEBUG
  158. STDERR.puts(args.join(' '))
  159. end
  160. # Writes the text in +contents+ to the file named by +filename+.
  161. def write_file(filename, contents)
  162. File.open(filename, "w") do |file|
  163. file.write(contents)
  164. end
  165. end
  166. main(ARGV)