Class: Wasmify::Rails::Packer
- Inherits:
-
Object
- Object
- Wasmify::Rails::Packer
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/wasmify/rails/packer.rb
Overview
A wrapper for wasi-vfs to add application source code to the ruby.wasm module
Constant Summary collapse
- ROOT_FILES =
%w[Gemfile config.ru .ruby-version Rakefile]
Instance Attribute Summary collapse
-
#output_dir ⇒ Object
readonly
Returns the value of attribute output_dir.
Instance Method Summary collapse
-
#initialize(output_dir: Wasmify::Rails.config.output_dir) ⇒ Packer
constructor
A new instance of Packer.
- #run(ruby_wasm_path:, name:, directories: Wasmify::Rails.config.pack_directories, storage_dir: nil) ⇒ Object
Constructor Details
Instance Attribute Details
#output_dir ⇒ Object (readonly)
Returns the value of attribute output_dir.
13 14 15 |
# File 'lib/wasmify/rails/packer.rb', line 13 def output_dir @output_dir end |
Instance Method Details
#run(ruby_wasm_path:, name:, directories: Wasmify::Rails.config.pack_directories, storage_dir: nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wasmify/rails/packer.rb', line 38 def run(ruby_wasm_path:, name:, directories: Wasmify::Rails.config.pack_directories, storage_dir: nil) unless system("which wasi-vfs > /dev/null 2>&1") raise "wasi-vfs is required to pack the application.\n" + "Please see installations instructions at: https://github.com/kateinoigakukun/wasi-vfs" end pack_root = Wasmify::Rails.config.pack_root args = %W[ pack #{ruby_wasm_path} ] directories.each do |dir| args << "--dir #{Wasmify::Rails.config.root_dir.join(dir)}::#{pack_root}/#{dir}" end output_path = File.join(output_dir, name) FileUtils.mkdir_p(output_dir) # Generate a temporary directory with the require root files Dir.mktmpdir do |tdir| (ROOT_FILES + (Wasmify::Rails.config.additional_root_files || [])).each do |file| FileUtils.cp(Wasmify::Rails.config.root_dir.join(file), tdir) if File.exist?(Wasmify::Rails.config.root_dir.join(file)) end # Create a storage/ directory for Active Storage attachments FileUtils.mkdir_p(File.join(tdir, storage_dir)) if storage_dir args << "--dir #{tdir}::#{pack_root}" args << "-o #{output_path}" spawn("wasi-vfs #{args.join(" ")}").then { Process.wait(_1) } end $stdout.puts "Packed the application to #{output_path}\n" \ "Size: #{number_to_human_size(File.size(output_path))}" end |