Class: Wasval::Install::RubyWasm

Inherits:
Object
  • Object
show all
Defined in:
lib/wasval/install/ruby_wasm.rb

Constant Summary collapse

GITHUB_RELEASES_URL =
"https://github.com/ruby/ruby.wasm/releases/latest/download"
TARGET =
"wasm32-unknown-wasip1"
BINARY_PATH_IN_TAR =
"usr/local/bin/ruby"
DEFAULT_INSTALL_DIR =
File.expand_path("~/.wasval")
DEFAULT_BINARY_NAME =
"ruby.wasm"
DEFAULT_SERIALIZED_BINARY_NAME =
"ruby.cwasm"
DEFAULT_PACKED_BINARY_NAME =
"ruby-packed.wasm"
DEFAULT_USR_DIR_NAME =
"usr"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dest: nil, serialized_dest: nil, ruby_version: nil, profile: :full, pack_dirs: nil, pack_output: nil, include_gems: nil) ⇒ RubyWasm

Returns a new instance of RubyWasm.



25
26
27
28
29
30
31
32
33
# File 'lib/wasval/install/ruby_wasm.rb', line 25

def initialize(dest: nil, serialized_dest: nil, ruby_version: nil, profile: :full, pack_dirs: nil, pack_output: nil, include_gems: nil)
  @ruby_version = ruby_version || ENV["WASVAL_RUBY_VERSION"] || default_ruby_version
  @profile = profile.to_s
  @dest = dest || ENV["WASVAL_RUBY_WASM_PATH"] || File.join(DEFAULT_INSTALL_DIR, DEFAULT_BINARY_NAME)
  @serialized_dest = serialized_dest || ENV["WASVAL_RUBY_CWASM_PATH"] || File.join(File.dirname(@dest), DEFAULT_SERIALIZED_BINARY_NAME)
  @pack_dirs = pack_dirs
  @pack_output = pack_output || File.join(File.dirname(@dest), DEFAULT_PACKED_BINARY_NAME)
  @include_gems = include_gems
end

Instance Attribute Details

#destObject (readonly)

Returns the value of attribute dest.



23
24
25
# File 'lib/wasval/install/ruby_wasm.rb', line 23

def dest
  @dest
end

#include_gemsObject (readonly)

Returns the value of attribute include_gems.



23
24
25
# File 'lib/wasval/install/ruby_wasm.rb', line 23

def include_gems
  @include_gems
end

#pack_dirsObject (readonly)

Returns the value of attribute pack_dirs.



23
24
25
# File 'lib/wasval/install/ruby_wasm.rb', line 23

def pack_dirs
  @pack_dirs
end

#pack_outputObject (readonly)

Returns the value of attribute pack_output.



23
24
25
# File 'lib/wasval/install/ruby_wasm.rb', line 23

def pack_output
  @pack_output
end

#profileObject (readonly)

Returns the value of attribute profile.



23
24
25
# File 'lib/wasval/install/ruby_wasm.rb', line 23

def profile
  @profile
end

#ruby_versionObject (readonly)

Returns the value of attribute ruby_version.



23
24
25
# File 'lib/wasval/install/ruby_wasm.rb', line 23

def ruby_version
  @ruby_version
end

#serialized_destObject (readonly)

Returns the value of attribute serialized_dest.



23
24
25
# File 'lib/wasval/install/ruby_wasm.rb', line 23

def serialized_dest
  @serialized_dest
end

Instance Method Details

#downloadObject



35
36
37
38
39
40
41
42
43
# File 'lib/wasval/install/ruby_wasm.rb', line 35

def download
  FileUtils.mkdir_p(File.dirname(dest))
  Dir.mktmpdir do |tmpdir|
    tarball_path = File.join(tmpdir, tarball_name)
    download_file(download_url, tarball_path)
    extract_binary(tarball_path)
  end
  dest
end

#download_urlObject



97
98
99
# File 'lib/wasval/install/ruby_wasm.rb', line 97

def download_url
  "#{GITHUB_RELEASES_URL}/#{tarball_name}"
end

#installObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wasval/install/ruby_wasm.rb', line 45

def install
  FileUtils.mkdir_p(File.dirname(dest))
  Dir.mktmpdir do |tmpdir|
    tarball_path = File.join(tmpdir, tarball_name)
    download_file(download_url, tarball_path)
    extract_binary(tarball_path)
    if pack_dirs || include_gems
      dirs = pack_dirs ? pack_dirs.dup : []
      if include_gems
        if include_gems == true
          actual_usr_dir = File.join(tmpdir, DEFAULT_USR_DIR_NAME)
          extract_usr_dir(tarball_path, actual_usr_dir)
        else
          actual_usr_dir = include_gems
        end
        dirs << actual_usr_dir
      end
      pack(*dirs, output: pack_output) if dirs.any?
    end
  end
  serialize
end

#installed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/wasval/install/ruby_wasm.rb', line 89

def installed?
  File.exist?(dest)
end

#pack(*dirs, output:) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wasval/install/ruby_wasm.rb', line 77

def pack(*dirs, output:)
  args = [dest]
  dirs.each do |dir|
    dir_arg = dir.include?("::") ? dir : "#{dir}::/#{File.basename(dir)}"
    args.push("--dir", dir_arg)
  end
  args.push("-o", output)

  system("rbwasm", "pack", *args, exception: true)
  output
end

#serializeObject



68
69
70
71
72
73
74
75
# File 'lib/wasval/install/ruby_wasm.rb', line 68

def serialize
  engine = Wasmtime::Engine.new(epoch_interruption: true)
  source = (pack_dirs || include_gems) ? pack_output : dest
  mod = Wasmtime::Module.from_file(engine, source)
  FileUtils.mkdir_p(File.dirname(serialized_dest))
  File.binwrite(serialized_dest, mod.serialize)
  serialized_dest
end

#tarball_nameObject



93
94
95
# File 'lib/wasval/install/ruby_wasm.rb', line 93

def tarball_name
  "ruby-#{ruby_version}-#{TARGET}-#{profile}.tar.gz"
end