require 'tmpdir' require 'json' module Jazzy class PodspecDocumenter attr_reader :podspec def initialize(podspec) @podspec = podspec end def sourcekitten_output installation_root = Pathname(Dir.mktmpdir(['jazzy', podspec.name])) installation_root.rmtree if installation_root.exist? Pod::Config.instance.with_changes(installation_root: installation_root) do sandbox = Pod::Sandbox.new(Pod::Config.instance.sandbox_root) installer = Pod::Installer.new(sandbox, podfile) installer.install! stdout = Dir.chdir(sandbox.root) do targets = installer.pod_targets .select { |pt| pt.pod_name == podspec.root.name } .map(&:label) targets.map do |t| SourceKitten.run_sourcekitten( %W(doc --module-name #{podspec.module_name} -- -target #{t}), ) end end stdout.reduce([]) { |a, s| a + JSON.load(s) }.to_json end end def self.create_podspec(podspec_path) case podspec_path when Pathname, String require 'cocoapods' Pod::Specification.from_file(podspec_path) end end # rubocop:disable Metrics/CyclomaticComplexity # rubocop:disable Metrics/PerceivedComplexity def self.apply_config_defaults(podspec, config) return unless podspec unless config.author_name_configured config.author_name = author_name(podspec) end unless config.module_name_configured config.module_name = podspec.module_name end unless config.author_url_configured config.author_url = podspec.homepage || github_file_prefix(podspec) end unless config.version_configured config.version = podspec.version.to_s end unless config.github_file_prefix_configured config.github_file_prefix = github_file_prefix(podspec) end end # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/PerceivedComplexity private # @!group Config helper methods def self.author_name(podspec) if podspec.authors.respond_to? :to_hash podspec.authors.keys.to_sentence || '' elsif podspec.authors.respond_to? :to_ary podspec.authors.to_sentence end || podspec.authors || '' end private_class_method :author_name def self.github_file_prefix(podspec) return unless podspec.source[:url] =~ %r{github.com[:/]+(.+)/(.+)} org, repo = Regexp.last_match return unless org && repo repo.sub!(/\.git$/, '') return unless rev = podspec.source[:tag] || podspec.source[:commit] "https://github.com/#{org}/#{repo}/blob/#{rev}" end private_class_method :github_file_prefix # @!group SourceKitten output helper methods def pod_path if podspec.defined_in_file podspec.defined_in_file.parent else config.source_directory end end def podfile podspec = @podspec path = pod_path @podfile ||= Pod::Podfile.new do install! 'cocoapods', integrate_targets: false, deterministic_uuids: false [podspec, *podspec.recursive_subspecs].each do |ss| ss.available_platforms.each do |p| # Travis builds take too long when building docs for all available # platforms for the Moya integration spec, so we just document OSX. # TODO: remove once jazzy is fast enough. next if ENV['JAZZY_INTEGRATION_SPECS'] && p.name != :osx target("Jazzy-#{ss.name.gsub('/', '__')}-#{p.name}") do use_frameworks! platform p.name, p.deployment_target pod ss.name, path: path.realpath.to_s end end end end end end end