Class: Wasmify::Rails::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/wasmify/rails/builder.rb

Overview

A wrapper for rbwasm build command

Constant Summary collapse

ORIGINAL_EXCLUDED_GEMS =
RubyWasm::Packager::EXCLUDED_GEMS.dup.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_dir: Wasmify::Rails.config.tmp_dir, target: Wasmify::Rails.config.wasm_target) ⇒ Builder

Returns a new instance of Builder.



26
27
28
29
# File 'lib/wasmify/rails/builder.rb', line 26

def initialize(output_dir: Wasmify::Rails.config.tmp_dir, target: Wasmify::Rails.config.wasm_target)
  @output_dir = output_dir
  @target = target
end

Instance Attribute Details

#output_dirObject (readonly)

Returns the value of attribute output_dir.



24
25
26
# File 'lib/wasmify/rails/builder.rb', line 24

def output_dir
  @output_dir
end

#targetObject (readonly)

Returns the value of attribute target.



24
25
26
# File 'lib/wasmify/rails/builder.rb', line 24

def target
  @target
end

Instance Method Details

#run(name:, exclude_gems: [], ignore_extensions: [], opts: "") ⇒ Object



31
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wasmify/rails/builder.rb', line 31

def run(name:, exclude_gems: [], ignore_extensions: [], opts: "")
  # Reset excluded gems
  RubyWasm::Packager::EXCLUDED_GEMS.replace(ORIGINAL_EXCLUDED_GEMS)
  $exclude_gems = []
  $exclude_exts = []

  # Add configured excluded gems
  Wasmify::Rails.config.exclude_gems.each do |gem_name|
    RubyWasm::Packager::EXCLUDED_GEMS << gem_name
  end

  # Add configured excluded extensions
  Wasmify::Rails.config.ignore_gem_extensions.each do |ext_name|
    $exclude_exts << ext_name
  end

  # Add additional excluded gems
  exclude_gems.each do |gem_name|
    RubyWasm::Packager::EXCLUDED_GEMS << gem_name
  end

  ignore_extensions.each do |gem_name|
    $exclude_exts << gem_name
  end

  RubyWasm::Packager::Core::BuildStrategy.prepend(Module.new do
    def specs_with_extensions
      super.reject do |spec|
        $exclude_exts.include?(spec.first.name)
      end
    end
  end)

  opts = opts&.split(" ") || []

  args = %W(
    build
    --ruby-version #{Wasmify::Rails.config.short_ruby_version}
    --target #{target}
    -o #{File.join(output_dir, name)}
  ) + opts

  patches_dir = Wasmify::Rails.config.root_dir.join("ruby_wasm_patches").to_s

  if File.directory?(patches_dir)
    Dir.children(patches_dir).each do |patch|
      args << "--patch=#{File.join(patches_dir, patch)}"
    end
  end

  FileUtils.mkdir_p(output_dir)
  RubyWasm::CLI.new(stdout: $stdout, stderr: $stderr).run(args)
end