Class: RubyWasm::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stderr:) ⇒ CLI

Returns a new instance of CLI.



6
7
8
9
# File 'lib/ruby_wasm/cli.rb', line 6

def initialize(stdout:, stderr:)
  @stdout = stdout
  @stderr = stderr
end

Instance Method Details

#build(args) ⇒ Object



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ruby_wasm/cli.rb', line 44

def build(args)
  # @type var options: cli_options
  options = {
    save_temps: false,
    optimize: false,
    remake: false,
    reconfigure: false,
    clean: false,
    ruby_version: "3.3",
    target_triplet: "wasm32-unknown-wasip1",
    profile: "full",
    stdlib: true,
    without_stdlib_components: [],
    dest_dir: nil,
    disable_gems: false,
    gemfile: nil,
    patches: [],
  }
  OptionParser
    .new do |opts|
      opts.banner = "Usage: rbwasm build [options]"
      opts.on("-h", "--help", "Prints this help") do
        @stdout.puts opts
        exit
      end

      opts.on("--save-temps", "Save temporary files") do
        options[:save_temps] = true
      end

      opts.on("--ruby-version VERSION", "Ruby version") do |version|
        options[:ruby_version] = version
      end

      opts.on("--target TRIPLET", "Target triplet") do |triplet|
        options[:target_triplet] = triplet
      end

      opts.on(
        "--build-profile PROFILE",
        "Build profile. full or minimal"
      ) { |profile| options[:profile] = profile }

      opts.on("--optimize", "Optimize the output") do
        options[:optimize] = true
      end

      opts.on("--remake", "Re-execute make for Ruby") do
        options[:remake] = true
      end

      opts.on("--reconfigure", "Re-execute configure for Ruby") do
        options[:reconfigure] = true
      end

      opts.on("--clean", "Clean build artifacts") { options[:clean] = true }

      opts.on("-o", "--output FILE", "Output file") do |file|
        options[:output] = file
      end

      opts.on("--[no-]stdlib", "Include stdlib") do |stdlib|
        options[:stdlib] = stdlib
      end

      opts.on("--without-stdlib COMPONENT", "Exclude stdlib component") do |component|
        options[:without_stdlib_components] << component
      end

      opts.on("--disable-gems", "Disable gems") do
        options[:disable_gems] = true
      end

      opts.on("--dest-dir PATH", "(Experimental) Destination directory") do |path|
        options[:dest_dir] = path
      end

      opts.on("-p", "--patch PATCH", "Apply a patch") do |patch|
        options[:patches] << patch
      end

      opts.on("--format FORMAT", "Output format") do |format|
        options[:format] = format
      end

      opts.on("--print-ruby-cache-key", "Print Ruby cache key") do
        options[:print_ruby_cache_key] = true
      end
    end
    .parse!(args)

  __skip__ = if defined?(Bundler)
    Bundler.settings.temporary(force_ruby_platform: true) do
      do_build_with_force_ruby_platform(options)
    end
  else
    do_build_with_force_ruby_platform(options)
  end
end

#do_build_with_force_ruby_platform(options) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ruby_wasm/cli.rb', line 144

def do_build_with_force_ruby_platform(options)
  verbose = RubyWasm.logger.level == :debug
  executor = RubyWasm::BuildExecutor.new(verbose: verbose)

  packager = self.derive_packager(options)

  if options[:print_ruby_cache_key]
    self.do_print_ruby_cache_key(packager)
    exit
  end

  unless options[:output]
    @stderr.puts "Output file is not specified"
    exit 1
  end

  require "tmpdir"

  if dest_dir = options[:dest_dir]
    self.do_build(executor, dest_dir, packager, options)
  elsif options[:save_temps]
    tmpdir = Dir.mktmpdir
    self.do_build(executor, tmpdir, packager, options)
    @stderr.puts "Temporary files are saved to #{tmpdir}"
    exit
  else
    Dir.mktmpdir do |tmpdir|
      self.do_build(executor, tmpdir, packager, options)
    end
  end
end

#pack(args) ⇒ Object



176
177
178
179
# File 'lib/ruby_wasm/cli.rb', line 176

def pack(args)
  self.require_extension
  RubyWasmExt::WasiVfs.run_cli([$0, "pack", *args])
end

#run(args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_wasm/cli.rb', line 11

def run(args)
  available_commands = %w[build pack]
  parser =
    OptionParser.new do |opts|
      opts.banner = <<~USAGE
      Usage: rbwasm [options...] [command]

      Available commands: #{available_commands.join(", ")}
    USAGE
      opts.version = RubyWasm::VERSION
      opts.on("-h", "--help", "Prints this help") do
        @stderr.puts opts
        exit
      end
      opts.on("--log-level LEVEL", "Log level") do |level|
        RubyWasm.log_level = level.to_sym
      end
    end
  parser.order!(args)

  command = args.shift
  case command
  when "build"
    build(args)
  when "pack"
    pack(args)
  else
    @stderr.puts "Unknown command: #{command}"
    @stderr.puts parser
    exit
  end
end