Class: YARD::LinkStdlib::RubySource

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/yard/link_stdlib/ruby_source.rb

Overview

Light utility object around a Ruby Gem::Version used to download and extract it’s source code to the tmp_dir.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ RubySource

Make a new instance for a version.

Parameters:

  • version (Gem::Version || #to_s)

    The Ruby version to work with.



97
98
99
# File 'lib/yard/link_stdlib/ruby_source.rb', line 97

def initialize version
  @version = Gem::Version.new version
end

Instance Attribute Details

#versionGem::Version (readonly)

Ruby version.

Returns:

  • (Gem::Version)


86
87
88
# File 'lib/yard/link_stdlib/ruby_source.rb', line 86

def version
  @version
end

Class Method Details

.ensure(version) ⇒ RubySource

Ensure the version’s source is downloaded and extracted.

Examples:

YARD::LinkStdlib::RubySource.ensure '2.5.1'

Parameters:

  • version (Gem::Version || #to_s)

    The Ruby version you need present.

Returns:



65
66
67
# File 'lib/yard/link_stdlib/ruby_source.rb', line 65

def self.ensure version
  new( version ).ensure
end

.listObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/yard/link_stdlib/ruby_source.rb', line 70

def self.list
  LinkStdlib.tmp_dir.entries.
    select { |filename|
      filename.to_s =~ /\Aruby\-\d+\_\d+\_\d+\z/
    }.
    map { |filename|
      new filename.to_s.sub( /\Aruby\-/, '' ).gsub( '_', '.' )
    }.
    sort
end

.make_missing=(value) ⇒ Object



49
50
51
# File 'lib/yard/link_stdlib/ruby_source.rb', line 49

def self.make_missing= value
  @make_missing = !!value
end

.make_missing?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/yard/link_stdlib/ruby_source.rb', line 40

def self.make_missing?
  unless instance_variable_defined? :@make_missing
    @make_missing = true
  end
  
  @make_missing
end

Instance Method Details

#<=>(other) ⇒ Object



199
200
201
# File 'lib/yard/link_stdlib/ruby_source.rb', line 199

def <=> other
  version <=> other.version
end

#download(force: false) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/yard/link_stdlib/ruby_source.rb', line 131

def download force: false
  if force
    log.info "FORCING download of Ruby #{ version } tarball..."
  elsif tar_path.exist?
    log.info "Ruby #{ version } tarball present."
    return self
  else
    log.info "Downloading Ruby #{ version } tarball..."
  end

  response = LinkStdlib.http_get url
  tar_path.open( "wb" ) { |file| file.write response.body }

  self # For chaining
end

#ensureObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/yard/link_stdlib/ruby_source.rb', line 169

def ensure
  if src_path.exist?
    # Nothing to do, source is already in place
    log.info "Source for Ruby #{ version } is present."
    return
  end
  
  unless self.class.make_missing?
    raise RuntimeError,
      "Object map for Ruby version #{ version } missing; " +
      "not configured to auto-make"
  end

  # Download unless the tar's already there
  download

  # And we must need to extract it since the src path wasn't there
  extract

  self # For chaining
end

#extract(force: false) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/yard/link_stdlib/ruby_source.rb', line 148

def extract force: false
  if force
    log.info "FORCING extraction of Ruby #{ version } source tarball..."
  elsif src_path.exist?
    log.info "Ruby #{ version } source present."
    return self
  else
    log.info "Extracting #{ tar_path } -> #{ src_path }..."
  end

  LinkStdlib.system! \
    'tar',  '-x',                           # extract
            '-f', tar_path.to_s,            # file
            '-C', LinkStdlib.tmp_dir.to_s   # directory (chdir)
  
  log.info "Source for Ruby #{ version } extracted to #{ src_path }."

  self # For chaining
end

#inspectObject



196
# File 'lib/yard/link_stdlib/ruby_source.rb', line 196

def inspect; to_s; end

#ruby_style_versionObject

Instance Methods



105
106
107
# File 'lib/yard/link_stdlib/ruby_source.rb', line 105

def ruby_style_version
  @ruby_style_version ||= version.to_s.gsub '.', '_'
end

#src_pathObject



126
127
128
# File 'lib/yard/link_stdlib/ruby_source.rb', line 126

def src_path
  @src_path ||= LinkStdlib.tmp_dir.join "ruby-#{ ruby_style_version }"
end

#tar_filenameObject



116
117
118
# File 'lib/yard/link_stdlib/ruby_source.rb', line 116

def tar_filename
  @tar_filename ||= "ruby-#{ ruby_style_version }.tar.gz"
end

#tar_pathObject



121
122
123
# File 'lib/yard/link_stdlib/ruby_source.rb', line 121

def tar_path
  @tar_path ||= LinkStdlib.tmp_dir.join tar_filename
end

#to_sObject



192
193
194
# File 'lib/yard/link_stdlib/ruby_source.rb', line 192

def to_s
  %{#<YARD::LinkStdlib::RubySource "#{ version }">}
end

#urlObject



110
111
112
113
# File 'lib/yard/link_stdlib/ruby_source.rb', line 110

def url
  @url ||=
    "https://github.com/ruby/ruby/archive/v#{ ruby_style_version }.tar.gz"
end