Module: Solargraph::YardMap::CoreDocs

Defined in:
lib/solargraph/yard_map/core_docs.rb

Overview

Tools for managing core documentation.

Constant Summary collapse

SOURCE =

The URL for downloading core documentation

'https://solargraph.org/download'
DEFAULT =

The default core documentation version

'2.2.2'

Class Method Summary collapse

Class Method Details

.availableArray<String>

Get a list of core documentation versions that are available for download.


78
79
80
81
82
83
84
# File 'lib/solargraph/yard_map/core_docs.rb', line 78

def available
  uri = URI.parse("#{SOURCE}/versions.json")
  response = Net::HTTP.get_response(uri)
  obj = JSON.parse(response.body)
  raise SourceNotAvailableError, "Error connecting to #{SOURCE}" unless obj['status'] == 'ok'
  obj['cores']
end

.best_download(current = RUBY_VERSION) ⇒ String

Get the version number of core documentation available for download that is the closest match for the current Ruby version.


91
92
93
94
95
96
97
98
# File 'lib/solargraph/yard_map/core_docs.rb', line 91

def best_download current = RUBY_VERSION
  rv = Gem::Version.new(current)
  found = available
  found.each do |ver|
    return ver if Gem::Version.new(ver) <= rv
  end
  found.last
end

.best_matchString

Get the version number of the installed core documentation that is the closest match for the current Ruby version.


65
66
67
68
69
70
71
72
# File 'lib/solargraph/yard_map/core_docs.rb', line 65

def best_match
  avail = versions
  cur = Gem::Version.new(RUBY_VERSION)
  avail.each do |v|
    return v if Gem::Version.new(v) <= cur
  end
  avail.last
end

.cache_dirString

The directory where core documentation is installed.


21
22
23
24
25
# File 'lib/solargraph/yard_map/core_docs.rb', line 21

def cache_dir
  # The directory is not stored in a variable so it can be overridden
  # in specs.
  ENV['SOLARGRAPH_CACHE'] || File.join(Dir.home, '.solargraph', 'cache')
end

.clearvoid

This method returns an undefined value.

Reset the core documentation cache to the minimum requirement.


139
140
141
142
# File 'lib/solargraph/yard_map/core_docs.rb', line 139

def clear
  FileUtils.rm_rf cache_dir, secure: true
  require_minimum
end

.download(version) ⇒ void

This method returns an undefined value.

Download the specified version of core documentation.


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/solargraph/yard_map/core_docs.rb', line 122

def download version
  FileUtils.mkdir_p cache_dir
  uri = URI.parse("#{SOURCE}/#{version}.tar.gz")
  # @type [Net::HTTPResponse]
  response = Net::HTTP.get_response(uri)
  if response.code == '404'
    raise ArgumentError, "Version #{version} is not available from #{SOURCE}"
  else
    zipfile = File.join(cache_dir, "#{version}.tar.gz")
    File.binwrite zipfile, response.body
    install_archive zipfile
  end
end

.require_minimumvoid

This method returns an undefined value.

Ensure installation of minimum documentation.


30
31
32
33
34
35
# File 'lib/solargraph/yard_map/core_docs.rb', line 30

def require_minimum
  return unless best_match.nil?
  FileUtils.mkdir_p cache_dir
  FileUtils.cp File.join(Solargraph::YARDOC_PATH, "#{DEFAULT}.tar.gz"), cache_dir
  install_archive File.join(cache_dir, "#{DEFAULT}.tar.gz")
end

.valid?(ver) ⇒ Boolean

True if core documentation is installed for the specified version number.


42
43
44
45
46
47
48
# File 'lib/solargraph/yard_map/core_docs.rb', line 42

def valid?(ver)
  dir = File.join(cache_dir, ver)
  return false unless File.directory?(dir)
  return false unless File.directory?(File.join(dir, 'yardoc'))
  return false unless File.directory?(File.join(dir, 'yardoc-stdlib'))
  true
end

.versionsArray<String>

Get a list of version numbers for currently installed core documentation.


54
55
56
57
58
59
# File 'lib/solargraph/yard_map/core_docs.rb', line 54

def versions
  dirs = Dir[File.join(cache_dir, '*')].map{|d| File.basename(d)}
  dirs.keep_if{|d| valid?(d)}
  dirs.sort!{|a, b| Gem::Version.new(b) <=> Gem::Version.new(a)}
  dirs
end

.yardoc_file(ver = best_match) ⇒ String

Get the path to a yardoc file for Ruby core documentation.

Raises:

  • (ArgumentError)

104
105
106
107
# File 'lib/solargraph/yard_map/core_docs.rb', line 104

def yardoc_file(ver = best_match)
  raise ArgumentError, "Invalid core yardoc version #{ver}" unless valid?(ver)
  File.join(cache_dir, ver, 'yardoc')
end

.yardoc_stdlib_file(ver = best_match) ⇒ String

Get the path to a yardoc file for Ruby stdlib documentation.

Raises:

  • (ArgumentError)

113
114
115
116
# File 'lib/solargraph/yard_map/core_docs.rb', line 113

def yardoc_stdlib_file(ver = best_match)
  raise ArgumentError, "Invalid core yardoc version #{ver}" unless valid?(ver)
  File.join(cache_dir, ver, 'yardoc-stdlib')
end