Class: Solargraph::Shell

Inherits:
Thor
  • Object
show all
Includes:
ServerMethods
Defined in:
lib/solargraph/shell.rb

Instance Method Summary collapse

Methods included from ServerMethods

#available_port

Instance Method Details

#available_coresObject



97
98
99
# File 'lib/solargraph/shell.rb', line 97

def available_cores
  puts Solargraph::YardMap::CoreDocs.available.join("\n")
end

#bundleObject



197
198
199
# File 'lib/solargraph/shell.rb', line 197

def bundle
  Documentor.new(options[:directory], rebuild: options[:rebuild], out: STDOUT).document
end

#clearObject



108
109
110
111
# File 'lib/solargraph/shell.rb', line 108

def clear
  puts "Deleting the cached documentation"
  Solargraph::YardMap::CoreDocs.clear
end

#config(directory = '.') ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/solargraph/shell.rb', line 52

def config(directory = '.')
  matches = []
  if options[:extensions]
    Gem::Specification.each do |g|
      if g.name.match(/^solargraph\-[A-Za-z0-9_\-]*?\-ext/)
        require g.name
        matches.push g.name
      end
    end
  end
  conf = Solargraph::Workspace::Config.new.raw_data
  unless matches.empty?
    matches.each do |m|
      conf['extensions'].push m
    end
  end
  File.open(File.join(directory, '.solargraph.yml'), 'w') do |file|
    file.puts conf.to_yaml
  end
  STDOUT.puts "Configuration file initialized."
end

#download_core(version = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/solargraph/shell.rb', line 75

def download_core version = nil
  ver = version || Solargraph::YardMap::CoreDocs.best_download
  if RUBY_VERSION != ver
    puts "Documentation for #{RUBY_VERSION} is not available. Reverting to closest match..."
  else
    puts "Downloading docs for #{ver}..."
  end
  Solargraph::YardMap::CoreDocs.download ver
  # Clear cached documentation if it exists
  FileUtils.rm_rf Dir.glob(File.join(Solargraph::YardMap::CoreDocs.cache_dir, ver, '*.ser'))
rescue ArgumentError => e
  STDERR.puts "ERROR: #{e.message}"
  STDERR.puts "Run `solargraph available-cores` for a list."
  exit 1
end

#list_coresObject



92
93
94
# File 'lib/solargraph/shell.rb', line 92

def list_cores
  puts Solargraph::YardMap::CoreDocs.versions.join("\n")
end

#rdoc(gem, version = '>= 0') ⇒ Object



202
203
204
205
206
# File 'lib/solargraph/shell.rb', line 202

def rdoc gem, version = '>= 0'
  spec = Gem::Specification.find_by_name(gem, version)
  puts "Caching #{spec.name} #{spec.version} from RDoc"
  Solargraph::YardMap::RdocToYard.run(spec)
end

#reportersObject



127
128
129
# File 'lib/solargraph/shell.rb', line 127

def reporters
  puts Solargraph::Diagnostics.reporters
end

#scanObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/solargraph/shell.rb', line 172

def scan
  require 'benchmark'
  directory = File.realpath(options[:directory])
  api_map = nil
  time = Benchmark.measure {
    api_map = Solargraph::ApiMap.load(directory)
    api_map.pins.each do |pin|
      begin
        puts pin_description(pin) if options[:verbose]
        pin.typify api_map
        pin.probe api_map
      rescue StandardError => e
        STDERR.puts "Error testing #{pin_description(pin)} #{pin.location ? "at #{pin.location.filename}:#{pin.location.range.start.line + 1}" : ''}"
        STDERR.puts "[#{e.class}]: #{e.message}"
        STDERR.puts e.backtrace.join("\n")
        exit 1
      end
    end
  }
  puts "Scanned #{directory} (#{api_map.pins.length} pins) in #{time.real} seconds."
end

#socketObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/solargraph/shell.rb', line 19

def socket
  require 'backport'
  port = options[:port]
  port = available_port if port.zero?
  Backport.run do
    Signal.trap("INT") do
      Backport.stop
    end
    Signal.trap("TERM") do
      Backport.stop
    end
    Backport.prepare_tcp_server host: options[:host], port: port, adapter: Solargraph::LanguageServer::Transport::Adapter
    STDERR.puts "Solargraph is listening PORT=#{port} PID=#{Process.pid}"
  end
end

#stdioObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/solargraph/shell.rb', line 36

def stdio
  require 'backport'
  Backport.run do
    Signal.trap("INT") do
      Backport.stop
    end
    Signal.trap("TERM") do
      Backport.stop
    end
    Backport.prepare_stdio_server adapter: Solargraph::LanguageServer::Transport::Adapter
    STDERR.puts "Solargraph is listening on stdio PID=#{Process.pid}"
  end
end

#typecheck(*files) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/solargraph/shell.rb', line 140

def typecheck *files
  directory = File.realpath(options[:directory])
  api_map = Solargraph::ApiMap.load(directory)
  if files.empty?
    files = api_map.source_maps.map(&:filename)
  else
    files.map! { |file| File.realpath(file) }
  end
  probcount = 0
  filecount = 0
  files.each do |file|
    checker = TypeChecker.new(file, api_map: api_map, level: options[:level].to_sym)
    problems = checker.problems
    next if problems.empty?
    problems.sort! { |a, b| a.location.range.start.line <=> b.location.range.start.line }
    puts problems.map { |prob| "#{prob.location.filename}:#{prob.location.range.start.line + 1} - #{prob.message}" }.join("\n")
    filecount += 1
    probcount += problems.length
  end
  puts "#{probcount} problem#{probcount != 1 ? 's' : ''} found#{files.length != 1 ? " in #{filecount} of #{files.length} files" : ''}."
  exit 1 if probcount > 0
end

#uncache(*gems) ⇒ Object

Raises:

  • (ArgumentError)


116
117
118
119
120
121
122
123
124
# File 'lib/solargraph/shell.rb', line 116

def uncache *gems
  raise ArgumentError, 'No gems specified.' if gems.empty?
  gems.each do |gem|
    Dir[File.join(Solargraph::YardMap::CoreDocs.cache_dir, 'gems', "#{gem}-*")].each do |dir|
      puts "Deleting cache: #{dir}"
      FileUtils.remove_entry_secure dir
    end
  end
end

#versionObject



12
13
14
# File 'lib/solargraph/shell.rb', line 12

def version
  puts Solargraph::VERSION
end