Class: Solargraph::Shell

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ServerMethods

#available_port

Class Method Details

.exit_on_failure?Boolean

Tell Thor to ensure the process exits with status 1 if any error happens.

Returns:

  • (Boolean)


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

def self.exit_on_failure?
  true
end

Instance Method Details

#cache(gem, version = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • gem (String)
  • version (String, nil) (defaults to: nil)


101
102
103
104
105
# File 'lib/solargraph/shell.rb', line 101

def cache gem, version = nil
  spec = Gem::Specification.find_by_name(gem, version)
  pins = GemPins.build(spec)
  Cache.save('gems', "#{spec.name}-#{spec.version}.ser", pins)
end

#clearvoid

This method returns an undefined value.



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

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

#config(directory = '.') ⇒ void

This method returns an undefined value.

Parameters:

  • directory (String) (defaults to: '.')


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/solargraph/shell.rb', line 63

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

#gems(*names) ⇒ void

This method returns an undefined value.



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/solargraph/shell.rb', line 121

def gems *names
  if names.empty?
    Gem::Specification.to_a.each { |spec| do_cache spec }
  else
    names.each do |name|
      spec = Gem::Specification.find_by_name(*name.split('='))
      do_cache spec
    rescue Gem::MissingSpecError
      warn "Gem '#{name}' not found"
    end
  end
end

#listvoid

This method returns an undefined value.



210
211
212
213
214
# File 'lib/solargraph/shell.rb', line 210

def list
  workspace = Solargraph::Workspace.new(options[:directory])
  puts workspace.filenames unless options[:count]
  puts "#{workspace.filenames.length} files total."
end

#reportersvoid

This method returns an undefined value.



136
137
138
# File 'lib/solargraph/shell.rb', line 136

def reporters
  puts Solargraph::Diagnostics.reporters
end

#scanvoid

This method returns an undefined value.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/solargraph/shell.rb', line 184

def scan
  require 'benchmark'
  directory = File.realpath(options[:directory])
  api_map = nil
  time = Benchmark.measure {
    api_map = Solargraph::ApiMap.load_with_cache(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

#socketvoid

This method returns an undefined value.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/solargraph/shell.rb', line 27

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

#stdiovoid

This method returns an undefined value.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/solargraph/shell.rb', line 45

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) ⇒ void

This method returns an undefined value.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/solargraph/shell.rb', line 150

def typecheck *files
  directory = File.realpath(options[:directory])
  api_map = Solargraph::ApiMap.load_with_cache(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) ⇒ void

This method returns an undefined value.

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
116
# File 'lib/solargraph/shell.rb', line 109

def uncache *gems
  raise ArgumentError, 'No gems specified.' if gems.empty?
  gems.each do |gem|
    spec = Gem::Specification.find_by_name(gem)
    Cache.uncache('gems', "#{spec.name}-#{spec.version}.ser")
    Cache.uncache('gems', "#{spec.name}-#{spec.version}.yardoc")
  end
end

#versionvoid

This method returns an undefined value.



19
20
21
# File 'lib/solargraph/shell.rb', line 19

def version
  puts Solargraph::VERSION
end