Class: RBS::Collection::Sources::Local

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/rbs/collection/sources/local.rb,
sig/collection/sources.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#dependencies_of

Constructor Details

#initialize(path:, base_directory:) ⇒ Local

Returns a new instance of Local.

Parameters:

  • path: (String)
  • base_directory: (Pathname)


11
12
13
14
15
# File 'lib/rbs/collection/sources/local.rb', line 11

def initialize(path:, base_directory:)
  # TODO: resolve relative path from dir of rbs_collection.yaml
  @path = Pathname(path)
  @full_path = base_directory / path
end

Instance Attribute Details

#full_pathPathname (readonly)

Returns the value of attribute full_path.

Returns:

  • (Pathname)


9
10
11
# File 'lib/rbs/collection/sources/local.rb', line 9

def full_path
  @full_path
end

#pathPathname (readonly)

Returns the value of attribute path.

Returns:

  • (Pathname)


9
10
11
# File 'lib/rbs/collection/sources/local.rb', line 9

def path
  @path
end

Instance Method Details

#_install(src, dst) ⇒ void

This method returns an undefined value.

Parameters:

  • src (Pathname)
  • dest (Pathname)


59
60
61
62
# File 'lib/rbs/collection/sources/local.rb', line 59

private def _install(src, dst)
  dst.dirname.mkpath
  File.symlink(src, dst)
end

#has?(name, version) ⇒ Boolean

Parameters:

  • name (String)
  • version (String, nil)

Returns:

  • (Boolean)


17
18
19
20
21
22
23
# File 'lib/rbs/collection/sources/local.rb', line 17

def has?(name, version)
  if version
    @full_path.join(name, version).directory?
  else
    not versions(name).empty?
  end
end

#install(dest:, name:, version:, stdout:) ⇒ void

This method returns an undefined value.

Create a symlink instead of copying file to refer files in @path. By avoiding copying RBS files, the users do not need re-run rbs collection install when the RBS files are updated.

Parameters:

  • dest: (Pathname)
  • name: (String)
  • version: (String)
  • stdout: (CLI::_IO)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rbs/collection/sources/local.rb', line 32

def install(dest:, name:, version:, stdout:)
  from = @full_path.join(name, version)
  gem_dir = dest.join(name, version)

  colored_io = CLI::ColoredIO.new(stdout: stdout)

  case
  when gem_dir.symlink? && gem_dir.readlink == from
    colored_io.puts "Using #{name}:#{version} (#{from})"
  when gem_dir.symlink?
    prev = gem_dir.readlink
    gem_dir.unlink
    _install(from, dest.join(name, version))
    colored_io.puts_green("Updating #{name}:#{version} to #{from} from #{prev}")
  when gem_dir.directory?
    # TODO: Show version of git source
    FileUtils.remove_entry_secure(gem_dir.to_s)
    _install(from, dest.join(name, version))
    colored_io.puts_green("Updating #{name}:#{version} from git source")
  when !gem_dir.exist?
    _install(from, dest.join(name, version))
    colored_io.puts_green("Installing #{name}:#{version} (#{from})")
  else
    raise
  end
end

#manifest_of(name, version) ⇒ manifest_entry?

Parameters:

  • name (String)
  • version (String)

Returns:

  • (manifest_entry, nil)


64
65
66
67
68
69
70
# File 'lib/rbs/collection/sources/local.rb', line 64

def manifest_of(name, version)
  gem_dir = @full_path.join(name, version)
  raise unless gem_dir.exist?

  manifest_path = gem_dir.join('manifest.yaml')
  YAML.safe_load(manifest_path.read) if manifest_path.exist?
end

#to_lockfilesource_entry

Returns:

  • (source_entry)


72
73
74
75
76
77
# File 'lib/rbs/collection/sources/local.rb', line 72

def to_lockfile
  {
    'type' => 'local',
    'path' => @path.to_s,
  }
end

#versions(name) ⇒ Array[String]

Parameters:

  • name (String)

Returns:

  • (Array[String])


25
26
27
# File 'lib/rbs/collection/sources/local.rb', line 25

def versions(name)
  @full_path.join(name).glob('*/').map { |path| path.basename.to_s }
end