Module: Jazzy::SourceKitten
- Defined in:
- lib/jazzy/sourcekitten.rb
Overview
This module interacts with the sourcekitten command-line executable
Class Method Summary collapse
-
.doc_coverage(sourcekitten_json) ⇒ Object
rubocop:enable Metrics/MethodLength.
-
.group_docs(docs, type) ⇒ Object
Group root-level docs by type and add as children to a group doc element.
- .make_default_doc_info(declaration) ⇒ Object
- .make_doc_info(doc, declaration) ⇒ Object
-
.make_doc_urls(docs, parents) ⇒ Hash
Generate doc URL by prepending its parents URLs.
-
.make_source_declarations(docs) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .make_substructure(doc, declaration) ⇒ Object
-
.parse(sourcekitten_output) ⇒ Hash
Parse sourcekitten STDOUT output as JSON.
-
.run_sourcekitten(arguments) ⇒ Object
Run sourcekitten with given arguments and return STDOUT.
Class Method Details
.doc_coverage(sourcekitten_json) ⇒ Object
rubocop:enable Metrics/MethodLength
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/jazzy/sourcekitten.rb', line 128 def self.doc_coverage(sourcekitten_json) documented = sourcekitten_json .map { |el| el['key.doc.documented'] } .inject(:+) undocumented = sourcekitten_json .map { |el| el['key.doc.undocumented'] } .inject(:+) return 0 if documented == 0 && undocumented == 0 (100 * documented) / (undocumented + documented) end |
.group_docs(docs, type) ⇒ Object
Group root-level docs by type and add as children to a group doc element
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jazzy/sourcekitten.rb', line 14 def self.group_docs(docs, type) group, docs = docs.partition { |doc| doc.type == type } docs << SourceDeclaration.new.tap do |sd| sd.type = SourceDeclaration::Type.overview sd.name = type.plural_name sd.abstract = "The following #{type.plural_name.downcase} are " \ 'available globally.' sd.children = group end if group.count > 0 docs end |
.make_default_doc_info(declaration) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/jazzy/sourcekitten.rb', line 50 def self.make_default_doc_info(declaration) # @todo: Fix these declaration.line = 0 declaration.column = 0 declaration.abstract = 'Undocumented' declaration.parameters = [] declaration.children = [] end |
.make_doc_info(doc, declaration) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jazzy/sourcekitten.rb', line 59 def self.make_doc_info(doc, declaration) xml_key = 'key.doc.full_as_xml' return make_default_doc_info(declaration) unless doc[xml_key] xml = Nokogiri::XML(doc[xml_key]).root declaration.line = XMLHelper.attribute(xml, 'line').to_i declaration.column = XMLHelper.attribute(xml, 'column').to_i decl = XMLHelper.xpath(xml, 'Declaration') declaration.declaration = Highlighter.highlight(decl, 'swift') declaration.abstract = XMLHelper.xpath(xml, 'Abstract') declaration.discussion = XMLHelper.xpath(xml, 'Discussion') declaration.return = XMLHelper.xpath(xml, 'ResultDiscussion') declaration.parameters = [] xml.xpath('Parameters/Parameter').each do |parameter_el| declaration.parameters << { name: XMLHelper.xpath(parameter_el, 'Name'), discussion: Jazzy.markdown.render( XMLHelper.xpath(parameter_el, 'Discussion'), ), } end end |
.make_doc_urls(docs, parents) ⇒ Hash
Generate doc URL by prepending its parents URLs
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jazzy/sourcekitten.rb', line 28 def self.make_doc_urls(docs, parents) docs.each do |doc| if doc.children.count > 0 # Create HTML page for this doc if it has children parents_slash = parents.count > 0 ? '/' : '' doc.url = parents.join('/') + parents_slash + doc.name + '.html' doc.children = make_doc_urls(doc.children, parents + [doc.name]) else # Don't create HTML page for this doc if it doesn't have children # Instead, make its link a hash-link on its parent's page doc.url = parents.join('/') + '.html#/' + doc.usr end end docs end |
.make_source_declarations(docs) ⇒ Object
rubocop:disable Metrics/MethodLength
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/jazzy/sourcekitten.rb', line 94 def self.make_source_declarations(docs) declarations = [] current_mark = SourceMark.new docs.each do |doc| if doc.key?('key.diagnostic_stage') declarations += make_source_declarations(doc['key.substructure']) next end declaration = SourceDeclaration.new declaration.type = SourceDeclaration::Type.new(doc['key.kind']) if declaration.type.mark? && doc['key.name'].start_with?('MARK: ') current_mark = SourceMark.new(doc['key.name']) end next unless declaration.type.declaration? unless declaration.type.name raise 'Please file an issue on ' \ 'https://github.com/realm/jazzy/issues about adding support for ' \ "`#{declaration.type.kind}`." end declaration.file = doc['key.filepath'] declaration.usr = doc['key.usr'] declaration.name = doc['key.name'] declaration.mark = current_mark make_doc_info(doc, declaration) make_substructure(doc, declaration) declarations << declaration end declarations end |
.make_substructure(doc, declaration) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/jazzy/sourcekitten.rb', line 83 def self.make_substructure(doc, declaration) if doc['key.substructure'] declaration.children = make_source_declarations( doc['key.substructure'], ) else declaration.children = [] end end |
.parse(sourcekitten_output) ⇒ Hash
Parse sourcekitten STDOUT output as JSON
141 142 143 144 145 146 147 148 |
# File 'lib/jazzy/sourcekitten.rb', line 141 def self.parse(sourcekitten_output) sourcekitten_json = JSON.parse(sourcekitten_output) docs = make_source_declarations(sourcekitten_json) SourceDeclaration::Type.all.each do |type| docs = group_docs(docs, type) end [make_doc_urls(docs, []), doc_coverage(sourcekitten_json)] end |
.run_sourcekitten(arguments) ⇒ Object
Run sourcekitten with given arguments and return STDOUT
45 46 47 48 |
# File 'lib/jazzy/sourcekitten.rb', line 45 def self.run_sourcekitten(arguments) bin_path = Pathname(__FILE__).parent + '../../bin' `#{bin_path}/sourcekitten #{(arguments).join(' ')}` end |