Class: WGPU::Native::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/wgpu/native/installer.rb

Overview

Downloads, verifies, and installs the pinned wgpu-native artifact.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(platform: RUBY_PLATFORM, env: ENV, home: Dir.home, host_os: RbConfig::CONFIG["host_os"], output: $stdout, command_runner: nil, http_factory: nil, zip_file_loader: nil, distribution: Distribution) ⇒ Installer

Creates an installer for a target Ruby platform.

The final four dependencies are injectable to keep network, process, archive, and distribution branches testable without external effects.

Parameters:

  • platform (String) (defaults to: RUBY_PLATFORM)

    target Ruby platform

  • env (Hash) (defaults to: ENV)

    environment variables

  • home (String) (defaults to: Dir.home)

    home directory used for cache resolution

  • host_os (String) (defaults to: RbConfig::CONFIG["host_os"])

    host operating-system identifier

  • output (IO) (defaults to: $stdout)

    progress output

  • command_runner (#call, nil) (defaults to: nil)

    process runner

  • http_factory (#call, nil) (defaults to: nil)

    factory receiving a URI

  • zip_file_loader (#call, nil) (defaults to: nil)

    factory returning Zip::File

  • distribution (Module) (defaults to: Distribution)

    artifact metadata provider



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wgpu/native/installer.rb', line 32

def initialize(platform: RUBY_PLATFORM, env: ENV, home: Dir.home,
               host_os: RbConfig::CONFIG["host_os"], output: $stdout,
               command_runner: nil, http_factory: nil, zip_file_loader: nil,
               distribution: Distribution)
  @platform = platform
  @env = env
  @home = home
  @host_os = host_os
  @output = output
  @command_runner = command_runner || method(:system)
  @http_factory = http_factory || ->(uri) { Net::HTTP.new(uri.host, uri.port) }
  @zip_file_loader = zip_file_loader || lambda {
    require "zip"
    Zip::File
  }
  @distribution = distribution
end

Instance Attribute Details

#platformObject (readonly)

Returns the value of attribute platform.



16
17
18
# File 'lib/wgpu/native/installer.rb', line 16

def platform
  @platform
end

Instance Method Details

#clean_pathString

Returns the primary versioned cache directory.

Returns:

  • (String)


72
73
74
# File 'lib/wgpu/native/installer.rb', line 72

def clean_path
  @distribution.primary_cache_dir(env: @env, home: @home, host_os: @host_os)
end

#installString

Installs or reuses the native library for #platform.

Returns:

  • (String)

    absolute path to the native library

Raises:

  • (InstallError)

    if the artifact cannot be obtained or verified



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wgpu/native/installer.rb', line 54

def install
  return custom_library_path if custom_library_path

  artifact = @distribution.artifact_for(platform)
  cached_path = existing_library_path(artifact)
  if cached_path
    @output.puts "wgpu-native already cached at #{cached_path}"
    return cached_path
  end

  install_artifact(artifact)
rescue LoadError, InstallError => error
  raise InstallError, "#{error.message}\n\n#{recovery_instructions}"
end