Class: Opal::CLI
- Inherits:
-
Object
- Object
- Opal::CLI
- Defined in:
- opal/lib/opal/cli.rb
Class Attribute Summary collapse
-
.stdout ⇒ Object
Returns the value of attribute stdout.
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#compiler_options ⇒ Object
readonly
Returns the value of attribute compiler_options.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#evals ⇒ Object
readonly
Returns the value of attribute evals.
-
#exit_status ⇒ Object
readonly
Returns the value of attribute exit_status.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#gems ⇒ Object
readonly
Returns the value of attribute gems.
-
#lib_only ⇒ Object
readonly
Returns the value of attribute lib_only.
-
#load_paths ⇒ Object
readonly
Returns the value of attribute load_paths.
-
#missing_require_severity ⇒ Object
readonly
Returns the value of attribute missing_require_severity.
-
#no_exit ⇒ Object
readonly
Returns the value of attribute no_exit.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#preload ⇒ Object
readonly
Returns the value of attribute preload.
-
#requires ⇒ Object
readonly
Returns the value of attribute requires.
-
#runner_options ⇒ Object
readonly
Returns the value of attribute runner_options.
-
#stubs ⇒ Object
readonly
Returns the value of attribute stubs.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #builder ⇒ Object
- #compiler_option_names ⇒ Object
- #create_builder ⇒ Object
-
#evals_or_file ⇒ Object
Internal: Yields a string of source code and the proper filename for either evals, stdin or a filepath.
-
#initialize(options = nil) ⇒ CLI
constructor
A new instance of CLI.
- #run ⇒ Object
- #runner ⇒ Object
- #show_sexp ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ CLI
Returns a new instance of CLI
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'opal/lib/opal/cli.rb', line 17 def initialize( = nil) ||= {} # Runner @runner_type = .delete(:runner) || :nodejs @runner_options = .delete(:runner_options) || {} @options = @sexp = .delete(:sexp) @file = .delete(:file) @no_exit = .delete(:no_exit) @lib_only = .delete(:lib_only) @argv = .delete(:argv) { [] } @evals = .delete(:evals) { [] } @load_paths = .delete(:load_paths) { [] } @gems = .delete(:gems) { [] } @stubs = .delete(:stubs) { [] } @preload = .delete(:preload) { [] } @output = .delete(:output) { self.class.stdout || $stdout } @verbose = .delete(:verbose) { false } @debug = .delete(:debug) { false } @filename = .delete(:filename) { @file && @file.path } @requires = .delete(:requires) { [] } @missing_require_severity = .delete(:missing_require_severity) { Opal::Config.missing_require_severity } @requires.unshift('opal') unless .delete(:skip_opal_require) @compiler_options = Hash[ *compiler_option_names.map do |option| key = option.to_sym next unless .key? key value = .delete(key) [key, value] end.compact.flatten ] raise ArgumentError, 'no libraries to compile' if @lib_only && @requires.empty? raise ArgumentError, 'no runnable code provided (evals or file)' if @evals.empty? && @file.nil? && !@lib_only raise ArgumentError, "can't accept evals or file in `library only` mode" if (@evals.any? || @file) && @lib_only raise ArgumentError, "unknown options: #{.inspect}" unless @options.empty? end |
Class Attribute Details
.stdout ⇒ Object
Returns the value of attribute stdout
14 15 16 |
# File 'opal/lib/opal/cli.rb', line 14 def stdout @stdout end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def argv @argv end |
#compiler_options ⇒ Object (readonly)
Returns the value of attribute compiler_options
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def @compiler_options end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def debug @debug end |
#evals ⇒ Object (readonly)
Returns the value of attribute evals
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def evals @evals end |
#exit_status ⇒ Object (readonly)
Returns the value of attribute exit_status
75 76 77 |
# File 'opal/lib/opal/cli.rb', line 75 def exit_status @exit_status end |
#file ⇒ Object (readonly)
Returns the value of attribute file
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def file @file end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def filename @filename end |
#gems ⇒ Object (readonly)
Returns the value of attribute gems
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def gems @gems end |
#lib_only ⇒ Object (readonly)
Returns the value of attribute lib_only
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def lib_only @lib_only end |
#load_paths ⇒ Object (readonly)
Returns the value of attribute load_paths
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def load_paths @load_paths end |
#missing_require_severity ⇒ Object (readonly)
Returns the value of attribute missing_require_severity
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def missing_require_severity @missing_require_severity end |
#no_exit ⇒ Object (readonly)
Returns the value of attribute no_exit
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def no_exit @no_exit end |
#options ⇒ Object (readonly)
Returns the value of attribute options
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def @options end |
#output ⇒ Object (readonly)
Returns the value of attribute output
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def output @output end |
#preload ⇒ Object (readonly)
Returns the value of attribute preload
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def preload @preload end |
#requires ⇒ Object (readonly)
Returns the value of attribute requires
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def requires @requires end |
#runner_options ⇒ Object (readonly)
Returns the value of attribute runner_options
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def @runner_options end |
#stubs ⇒ Object (readonly)
Returns the value of attribute stubs
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def stubs @stubs end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose
9 10 11 |
# File 'opal/lib/opal/cli.rb', line 9 def verbose @verbose end |
Instance Method Details
#builder ⇒ Object
77 78 79 |
# File 'opal/lib/opal/cli.rb', line 77 def builder @builder ||= create_builder end |
#compiler_option_names ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'opal/lib/opal/cli.rb', line 124 def compiler_option_names %w[ method_missing arity_check dynamic_require_severity source_map_enabled irb_enabled inline_operators enable_source_location parse_comments ] end |
#create_builder ⇒ Object
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 |
# File 'opal/lib/opal/cli.rb', line 81 def create_builder builder = Opal::Builder.new( stubs: stubs, compiler_options: , missing_require_severity: missing_require_severity, ) # --include builder.append_paths(*load_paths) # --gem gems.each { |gem_name| builder.use_gem gem_name } # --require requires.each { |required| builder.build(required) } # --preload preload.each { |path| builder.build_require(path) } # --verbose builder.build_str '$VERBOSE = true', '(flags)' if verbose # --debug builder.build_str '$DEBUG = true', '(flags)' if debug # --eval / stdin / file evals_or_file { |source, filename| builder.build_str(source, filename) } # --no-exit builder.build_str 'Kernel.exit', '(exit)' unless no_exit builder end |
#evals_or_file ⇒ Object
Internal: Yields a string of source code and the proper filename for either evals, stdin or a filepath.
139 140 141 142 143 144 145 146 147 148 |
# File 'opal/lib/opal/cli.rb', line 139 def evals_or_file # --library return if lib_only if evals.any? yield evals.join("\n"), '-e' elsif file && (filename != '-' || evals.empty?) yield file.read, filename end end |
#run ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'opal/lib/opal/cli.rb', line 60 def run return show_sexp if @sexp @exit_status = runner.call( options: , output: output, argv: argv, builder: builder, ) end |
#runner ⇒ Object
70 71 72 73 |
# File 'opal/lib/opal/cli.rb', line 70 def runner CliRunners[@runner_type] || raise(ArgumentError, "unknown runner: #{@runner_type.inspect}") end |
#show_sexp ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'opal/lib/opal/cli.rb', line 115 def show_sexp evals_or_file do |contents, filename| buffer = ::Opal::Parser::SourceBuffer.new(filename) buffer.source = contents sexp = Opal::Parser.default_parser.parse(buffer) output.puts sexp.inspect end end |