Module: Opal::CliRunners

Defined in:
opal/lib/opal/cli_runners.rb,
opal/lib/opal/cli_runners/chrome.rb,
opal/lib/opal/cli_runners/nodejs.rb,
opal/lib/opal/cli_runners/server.rb,
opal/lib/opal/cli_runners/nashorn.rb,
opal/lib/opal/cli_runners/applescript.rb

Overview

Opal::CliRunners is the register in which JavaScript runners can be defined for use by Opal::CLI. Runners will be called via the #call method and passed a Hash containing the following keys:

  • options: a hash of options for the runner
  • output: an IO-like object responding to #write and #puts
  • argv: is the arguments vector coming from the CLI that is being forwarded to the program
  • builder: the current instance of Opal::Builder

Runners can be registered using #register_runner(name, runner).

Defined Under Namespace

Classes: Applescript, Chrome, Nashorn, Nodejs, RunnerError, Server

Constant Summary collapse

Compiler =

The compiler runner will just output the compiled JavaScript

->(data) {
  options  = data[:options] || {}
  builder  = data.fetch(:builder)
  map_file = options[:map_file]
  output   = data.fetch(:output)

  compiled_source = builder.to_s + "\n" + builder.source_map.to_data_uri_comment
  output.puts compiled_source
  File.write(map_file, builder.source_map.to_json) if map_file

  0
}

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



23
24
25
# File 'opal/lib/opal/cli_runners.rb', line 23

def self.[](name)
  @register[name.to_sym]
end

.[]=(name, runner) ⇒ Object



28
29
30
31
32
# File 'opal/lib/opal/cli_runners.rb', line 28

def self.[]=(name, runner)
  warn "Overwriting Opal CLI runner: #{name}" if @register.key? name.to_sym

  @register[name.to_sym] = runner
end

.alias_runner(new_name, old_name) ⇒ Object

Alias a runner name



57
58
59
60
61
# File 'opal/lib/opal/cli_runners.rb', line 57

def self.alias_runner(new_name, old_name)
  self[new_name.to_sym] = self[old_name.to_sym]

  nil
end

.register_runner(name, runner, path = nil) ⇒ Object

Parameters:

  • name (Symbol)

    the name at which the runner can be reached

  • runner (#call)

    a callable object that will act as the "runner"

  • runner (Symbol)

    a constant name that once autoloaded will point to a callable.

  • path (nil, String) (defaults to: nil)

    a path for setting up autoload on the constant



44
45
46
47
48
49
50
51
52
53
54
# File 'opal/lib/opal/cli_runners.rb', line 44

def self.register_runner(name, runner, path = nil)
  autoload runner, path if path

  if runner.respond_to?(:call)
    self[name] = runner
  else
    self[name] = ->(data) { const_get(runner).call(data) }
  end

  nil
end

.to_hObject



35
36
37
# File 'opal/lib/opal/cli_runners.rb', line 35

def self.to_h
  @register
end