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.
-
#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.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#preload ⇒ Object
readonly
Returns the value of attribute preload.
-
#requires ⇒ Object
readonly
Returns the value of attribute requires.
-
#stubs ⇒ Object
readonly
Returns the value of attribute stubs.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #build ⇒ Object
- #compile? ⇒ Boolean
- #compiled_source ⇒ Object
- #compiler_option_names ⇒ Object
-
#evals_or_file ⇒ Object
Internal: Yelds 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.
- #puts(*args) ⇒ Object
- #run ⇒ Object
- #run_code ⇒ Object
- #runner ⇒ Object
- #sexp? ⇒ Boolean
- #show_compiled_source ⇒ Object
- #show_sexp ⇒ Object
- #show_source_map ⇒ Object
- #skip_opal_require? ⇒ Boolean
Constructor Details
#initialize(options = nil) ⇒ CLI
Returns a new instance of CLI
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 59 60 61 62 63 64 65 66 |
# File 'opal/lib/opal/cli.rb', line 28 def initialize = nil ||= {} # Runner @runner_type = .delete(:runner) || :nodejs @port = .delete(:port) || 3000 @options = @compile = !!.delete(:compile) @sexp = .delete(:sexp) @file = .delete(:file) @map = .delete(:map) @no_exit = .delete(:no_exit) @lib_only = .delete(:lib_only) @argv = .delete(:argv) || [] @evals = .delete(:evals) || [] @requires = .delete(:requires) || [] @load_paths = .delete(:load_paths) || [] @gems = .delete(:gems) || [] @stubs = .delete(:stubs) || [] @preload = .delete(:preload) || [] @output = .delete(:output) || self.class.stdout || $stdout @verbose = .fetch(:verbose, false); .delete(:verbose) @debug = .fetch(:debug, false); .delete(:debug) @filename = .fetch(:filename) { @file && @file.path }; .delete(:filename) @skip_opal_require = .delete(:skip_opal_require) @compiler_options = Hash[ *compiler_option_names.map do |option| key = option.to_sym next unless .has_key? key value = .delete(key) [key, value] end.compact.flatten ] raise ArgumentError, "no runnable code provided (evals or file)" if @evals.empty? and @file.nil? and not(@lib_only) raise ArgumentError, "can't accept evals or file in `library only` mode" if (@evals.any? or @file) and @lib_only raise ArgumentError, "unknown options: #{.inspect}" unless @options.empty? end |
Class Attribute Details
.stdout ⇒ Object
Returns the value of attribute stdout
25 26 27 |
# File 'opal/lib/opal/cli.rb', line 25 def stdout @stdout end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def argv @argv end |
#compiler_options ⇒ Object (readonly)
Returns the value of attribute compiler_options
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def @compiler_options end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def debug @debug end |
#evals ⇒ Object (readonly)
Returns the value of attribute evals
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def evals @evals end |
#exit_status ⇒ Object (readonly)
Returns the value of attribute exit_status
91 92 93 |
# File 'opal/lib/opal/cli.rb', line 91 def exit_status @exit_status end |
#file ⇒ Object (readonly)
Returns the value of attribute file
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def file @file end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def filename @filename end |
#gems ⇒ Object (readonly)
Returns the value of attribute gems
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def gems @gems end |
#lib_only ⇒ Object (readonly)
Returns the value of attribute lib_only
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def lib_only @lib_only end |
#load_paths ⇒ Object (readonly)
Returns the value of attribute load_paths
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def load_paths @load_paths end |
#no_exit ⇒ Object (readonly)
Returns the value of attribute no_exit
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def no_exit @no_exit end |
#options ⇒ Object (readonly)
Returns the value of attribute options
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def @options end |
#output ⇒ Object (readonly)
Returns the value of attribute output
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def output @output end |
#port ⇒ Object (readonly)
Returns the value of attribute port
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def port @port end |
#preload ⇒ Object (readonly)
Returns the value of attribute preload
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def preload @preload end |
#requires ⇒ Object (readonly)
Returns the value of attribute requires
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def requires @requires end |
#stubs ⇒ Object (readonly)
Returns the value of attribute stubs
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def stubs @stubs end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose
8 9 10 |
# File 'opal/lib/opal/cli.rb', line 8 def verbose @verbose end |
Instance Method Details
#build ⇒ Object
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 |
# File 'opal/lib/opal/cli.rb', line 93 def build builder = Opal::Builder.new stubs: stubs, compiler_options: builder.append_paths(*load_paths) gems.each { |gem_name| builder.use_gem gem_name } builder.build 'opal' unless skip_opal_require? preload.each { |path| builder.build_require(path) } # FLAGS builder.build_str '$VERBOSE = true', '(flags)' if verbose builder.build_str '$DEBUG = true', '(flags)' if debug # REQUIRES: -r requires.each do |local_require| builder.build(local_require) end unless lib_only evals_or_file do |contents, filename| builder.build_str(contents, filename) end end builder.build_str 'Kernel.exit', '(exit)' unless no_exit builder end |
#compile? ⇒ Boolean
12 13 14 |
# File 'opal/lib/opal/cli.rb', line 12 def compile? @compile end |
#compiled_source ⇒ Object
122 123 124 |
# File 'opal/lib/opal/cli.rb', line 122 def compiled_source build.to_s end |
#compiler_option_names ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'opal/lib/opal/cli.rb', line 145 def compiler_option_names %w[ method_missing arity_check dynamic_require_severity source_map_enabled irb_enabled inline_operators ] end |
#evals_or_file ⇒ Object
Internal: Yelds a string of source code and the proper filename for either evals, stdin or a filepath.
158 159 160 161 162 163 164 165 166 |
# File 'opal/lib/opal/cli.rb', line 158 def evals_or_file if evals.any? yield evals.join("\n"), '-e' else if file and (filename != '-' or evals.empty?) yield file.read, filename end end end |
#puts(*args) ⇒ Object
168 169 170 |
# File 'opal/lib/opal/cli.rb', line 168 def puts(*args) output.puts(*args) end |
#run ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'opal/lib/opal/cli.rb', line 68 def run case when @map; show_source_map when sexp?; show_sexp when compile?; show_compiled_source else run_code end end |
#run_code ⇒ Object
86 87 88 89 |
# File 'opal/lib/opal/cli.rb', line 86 def run_code runner.run(compiled_source, argv) @exit_status = runner.exit_status end |
#runner ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'opal/lib/opal/cli.rb', line 77 def runner @runner ||= begin const_name = @runner_type.to_s.capitalize CliRunners.const_defined?(const_name) or raise ArgumentError, "unknown runner: #{@runner_type.inspect}" CliRunners.const_get(const_name).new(output: output, port: port) end end |
#sexp? ⇒ Boolean
16 17 18 |
# File 'opal/lib/opal/cli.rb', line 16 def sexp? @sexp end |
#show_compiled_source ⇒ Object
126 127 128 |
# File 'opal/lib/opal/cli.rb', line 126 def show_compiled_source puts compiled_source end |
#show_sexp ⇒ Object
130 131 132 133 134 135 |
# File 'opal/lib/opal/cli.rb', line 130 def show_sexp evals_or_file do |contents, filename| sexp = Opal::Parser.new.parse(contents, filename) puts sexp.inspect end end |
#show_source_map ⇒ Object
137 138 139 140 141 142 143 |
# File 'opal/lib/opal/cli.rb', line 137 def show_source_map evals_or_file do |contents, filename| compiler = Opal::Compiler.new(contents, .merge(file: filename)) compiler.compile puts compiler.source_map.to_json end end |
#skip_opal_require? ⇒ Boolean
20 21 22 |
# File 'opal/lib/opal/cli.rb', line 20 def skip_opal_require? @skip_opal_require end |