Module: Jazzy::SymbolGraph

Defined in:
lib/jazzy/symbol_graph.rb,
lib/jazzy/symbol_graph/graph.rb,
lib/jazzy/symbol_graph/symbol.rb,
lib/jazzy/symbol_graph/ext_node.rb,
lib/jazzy/symbol_graph/sym_node.rb,
lib/jazzy/symbol_graph/constraint.rb,
lib/jazzy/symbol_graph/relationship.rb

Defined Under Namespace

Classes: BaseNode, Constraint, ExtConstraints, ExtNode, Graph, Relationship, SymNode, Symbol

Class Method Summary collapse

Class Method Details

.arguments(config, output_path) ⇒ Object

Figure out the args to pass to symbolgraph-extract rubocop:disable Metrics/CyclomaticComplexity



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jazzy/symbol_graph.rb', line 41

def self.arguments(config, output_path)
  if config.module_name.empty?
    raise 'error: `--swift-build-tool symbolgraph` requires `--module`.'
  end

  user_args = config.build_tool_arguments.join

  if user_args =~ /--(?:module-name|minimum-access-level|output-dir)/
    raise 'error: `--build-tool-arguments` for '\
      "`--swift-build-tool symbolgraph` can't use `--module`, "\
      '`--minimum-access-level`, or `--output-dir`.'
  end

  # Default set
  args = [
    "--module-name=#{config.module_name}",
    '--minimum-access-level=private',
    "--output-dir=#{output_path}",
    '--skip-synthesized-members',
  ]

  # Things user can override
  args.append("--sdk=#{sdk(config)}") unless user_args =~ /--sdk/
  args.append("--target=#{target}") unless user_args =~ /--target/
  args.append("-F=#{config.source_directory}") unless user_args =~ /-F(?!s)/
  args.append("-I=#{config.source_directory}") unless user_args =~ /-I/

  args + config.build_tool_arguments
end

.build(config) ⇒ Object

Run ‘swift symbolgraph-extract` with configured args, parse the results, and return as JSON in SourceKit format.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jazzy/symbol_graph.rb', line 18

def self.build(config)
  Dir.mktmpdir do |tmp_dir|
    args = arguments(config, tmp_dir)

    Executable.execute_command('swift',
                               args.unshift('symbolgraph-extract'),
                               true) # raise on error

    Dir[tmp_dir + '/*.symbols.json'].map do |filename|
      # The @ part is for extensions in our module (before the @)
      # of types in another module (after the @).
      filename =~ /(.*?)(@(.*?))?\.symbols/
      module_name = Regexp.last_match[3] || Regexp.last_match[1]
      {
        filename =>
          Graph.new(File.read(filename), module_name).to_sourcekit,
      }
    end.to_json
  end
end

.demangle(usr) ⇒ Object

This is a last-ditch fallback for when symbolgraph doesn’t provide a name - at least conforming external types to local protocols.



87
88
89
90
91
92
93
# File 'lib/jazzy/symbol_graph.rb', line 87

def self.demangle(usr)
  args = %w[demangle -simplified -compact].append(usr.sub(/^s:/, 's'))
  output, = Executable.execute_command('swift', args, true)
  return output.chomp
rescue
  usr
end

.sdk(config) ⇒ Object

Get the SDK path. On !darwin this just isn’t needed.



73
74
75
# File 'lib/jazzy/symbol_graph.rb', line 73

def self.sdk(config)
  `xcrun --show-sdk-path --sdk #{config.sdk}`.chomp
end

.targetObject

Guess a default LLVM target. Feels like the tool should figure this out from sdk + the binary somehow?



79
80
81
82
# File 'lib/jazzy/symbol_graph.rb', line 79

def self.target
  `swift -version` =~ /Target: (.*?)$/
  Regexp.last_match[1] || 'x86_64-apple-macosx10.15'
end